diff options
-rw-r--r-- | crates/completion/src/completions/unqualified_path.rs | 41 | ||||
-rw-r--r-- | crates/completion/src/item.rs | 28 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 4 |
3 files changed, 62 insertions, 11 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index 26a2b7a1b..fe6507c55 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs | |||
@@ -122,8 +122,8 @@ mod tests { | |||
122 | use test_utils::mark; | 122 | use test_utils::mark; |
123 | 123 | ||
124 | use crate::{ | 124 | use crate::{ |
125 | test_utils::{check_edit, completion_list}, | 125 | test_utils::{check_edit, check_edit_with_config, completion_list}, |
126 | CompletionKind, | 126 | CompletionConfig, CompletionKind, |
127 | }; | 127 | }; |
128 | 128 | ||
129 | fn check(ra_fixture: &str, expect: Expect) { | 129 | fn check(ra_fixture: &str, expect: Expect) { |
@@ -810,4 +810,41 @@ fn main() { | |||
810 | "#, | 810 | "#, |
811 | ); | 811 | ); |
812 | } | 812 | } |
813 | |||
814 | /// LSP protocol supports separate completion resolve requests to do the heavy computations there. | ||
815 | /// This test checks that for a certain resolve capatilities no such operations (autoimport) are done. | ||
816 | #[test] | ||
817 | fn no_fuzzy_completions_applied_for_certain_resolve_capability() { | ||
818 | let mut completion_config = CompletionConfig::default(); | ||
819 | completion_config | ||
820 | .active_resolve_capabilities | ||
821 | .insert(crate::CompletionResolveCapability::AdditionalTextEdits); | ||
822 | |||
823 | check_edit_with_config( | ||
824 | completion_config, | ||
825 | "ThirdStruct", | ||
826 | r#" | ||
827 | //- /lib.rs crate:dep | ||
828 | pub struct FirstStruct; | ||
829 | pub mod some_module { | ||
830 | pub struct SecondStruct; | ||
831 | pub struct ThirdStruct; | ||
832 | } | ||
833 | |||
834 | //- /main.rs crate:main deps:dep | ||
835 | use dep::{FirstStruct, some_module::SecondStruct}; | ||
836 | |||
837 | fn main() { | ||
838 | this<|> | ||
839 | } | ||
840 | "#, | ||
841 | r#" | ||
842 | use dep::{FirstStruct, some_module::SecondStruct}; | ||
843 | |||
844 | fn main() { | ||
845 | ThirdStruct | ||
846 | } | ||
847 | "#, | ||
848 | ); | ||
849 | } | ||
813 | } | 850 | } |
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index 519204bfa..978ea76f9 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs | |||
@@ -346,13 +346,17 @@ impl Builder { | |||
346 | } | 346 | } |
347 | }; | 347 | }; |
348 | 348 | ||
349 | if !self.resolve_import_lazily { | 349 | let import_to_add = if self.resolve_import_lazily { |
350 | if let Some(import_edit) = | 350 | self.import_to_add |
351 | self.import_to_add.as_ref().and_then(|import_edit| import_edit.to_text_edit()) | 351 | } else { |
352 | { | 352 | match apply_import_eagerly(self.import_to_add.as_ref(), &mut text_edit) { |
353 | text_edit.union(import_edit).expect("Failed to unite import and completion edits"); | 353 | Ok(()) => self.import_to_add, |
354 | Err(()) => { | ||
355 | log::error!("Failed to apply eager import edit: original edit and import edit intersect"); | ||
356 | None | ||
357 | } | ||
354 | } | 358 | } |
355 | } | 359 | }; |
356 | 360 | ||
357 | CompletionItem { | 361 | CompletionItem { |
358 | source_range: self.source_range, | 362 | source_range: self.source_range, |
@@ -368,7 +372,7 @@ impl Builder { | |||
368 | trigger_call_info: self.trigger_call_info.unwrap_or(false), | 372 | trigger_call_info: self.trigger_call_info.unwrap_or(false), |
369 | score: self.score, | 373 | score: self.score, |
370 | ref_match: self.ref_match, | 374 | ref_match: self.ref_match, |
371 | import_to_add: self.import_to_add, | 375 | import_to_add, |
372 | } | 376 | } |
373 | } | 377 | } |
374 | pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { | 378 | pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { |
@@ -449,6 +453,16 @@ impl Builder { | |||
449 | } | 453 | } |
450 | } | 454 | } |
451 | 455 | ||
456 | fn apply_import_eagerly( | ||
457 | import_to_add: Option<&ImportEdit>, | ||
458 | original_edit: &mut TextEdit, | ||
459 | ) -> Result<(), ()> { | ||
460 | match import_to_add.and_then(|import_edit| import_edit.to_text_edit()) { | ||
461 | Some(import_edit) => original_edit.union(import_edit).map_err(|_| ()), | ||
462 | None => Ok(()), | ||
463 | } | ||
464 | } | ||
465 | |||
452 | impl<'a> Into<CompletionItem> for Builder { | 466 | impl<'a> Into<CompletionItem> for Builder { |
453 | fn into(self) -> CompletionItem { | 467 | fn into(self) -> CompletionItem { |
454 | self.build() | 468 | self.build() |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index f92280524..f80c55df7 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -1626,12 +1626,12 @@ fn fill_resolve_data( | |||
1626 | let imported_name = import_edit.import_path.segments.clone().pop()?.to_string(); | 1626 | let imported_name = import_edit.import_path.segments.clone().pop()?.to_string(); |
1627 | 1627 | ||
1628 | *resolve_data = Some( | 1628 | *resolve_data = Some( |
1629 | serde_json::to_value(CompletionResolveData { | 1629 | to_value(CompletionResolveData { |
1630 | position: position.to_owned(), | 1630 | position: position.to_owned(), |
1631 | full_import_path, | 1631 | full_import_path, |
1632 | imported_name, | 1632 | imported_name, |
1633 | }) | 1633 | }) |
1634 | .expect("Failed to serialize a regular struct with derives"), | 1634 | .unwrap(), |
1635 | ) | 1635 | ) |
1636 | } | 1636 | } |
1637 | Some(()) | 1637 | Some(()) |