Skip to content

Commit

Permalink
Update tools/lsp/preview/drop_location.rs
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Goffart <[email protected]>
  • Loading branch information
hunger and ogoffart committed Jun 19, 2024
1 parent 15ac565 commit 1f0262c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion tools/lsp/common/component_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ pub fn file_local_components(
url: &lsp_types::Url,
result: &mut Vec<ComponentInformation>,
) {
let Some(doc) = document_cache.get_document(url) else { return; };
let Some(doc) = document_cache.get_document(url) else {
return;
};
let exported_components =
doc.exports.iter().filter_map(|(_, e)| e.as_ref().left()).cloned().collect::<Vec<_>>();
for component in &*doc.inner_components {
Expand Down
2 changes: 1 addition & 1 deletion tools/lsp/common/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn test_file_name(name: &str) -> PathBuf {
pub fn compile_test_with_sources(
style: &str,
code: HashMap<lsp_types::Url, String>,
allow_warnings: bool
allow_warnings: bool,
) -> common::DocumentCache {
i_slint_backend_testing::init_no_event_loop();
recompile_test_with_sources(style, code, allow_warnings)
Expand Down
18 changes: 11 additions & 7 deletions tools/lsp/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ fn add_new_component() {
})
}

let Some(document_cache) = document_cache() else { return; };
let Some(document_cache) = document_cache() else {
return;
};

let preview_component = {
let cache = CONTENT_CACHE.get_or_init(Default::default).lock().unwrap();
Expand All @@ -175,7 +177,9 @@ fn add_new_component() {
return;
};

let Some(document) = &document.node else { return; };
let Some(document) = &document.node else {
return;
};

if let Some((edit, drop_data)) = drop_location::add_new_component(&component_name, document) {
element_selection::select_element_at_source_code_position(
Expand Down Expand Up @@ -479,11 +483,11 @@ fn finish_parsing(ok: bool) {
&mut components,
);
if let Some(previewed_url) = previewed_url {
component_catalog::file_local_components(
&document_cache,
&previewed_url,
&mut components,
);
component_catalog::file_local_components(
&document_cache,
&previewed_url,
&mut components,
);
}

components.sort_by(|a, b| a.name.cmp(&b.name));
Expand Down
8 changes: 6 additions & 2 deletions tools/lsp/preview/drop_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,12 @@ pub fn add_new_component(
);

let selection_offset = insert_position.insertion_position.offset()
+ new_text.chars().take_while(|c| c.is_whitespace()).map(|c| c.len_utf8() as u32).sum::<u32>()
+ 10 /* component<SPACE> */;
+ new_text
.chars()
.take_while(|c| c.is_whitespace())
.map(|c| c.len_utf8() as u32)
.sum::<u32>()
+ "component ".len() as u32;

let source_file = document.source_file.clone();
let path = source_file.path().to_path_buf();
Expand Down

0 comments on commit 1f0262c

Please sign in to comment.