From 077c1c3c1f16b5387c9e20cfa087c517dac3f4c8 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Dec 2020 21:23:30 +0200 Subject: Less panic, more tests --- .../completion/src/completions/unqualified_path.rs | 41 ++++++++++++++++++++-- crates/completion/src/item.rs | 28 +++++++++++---- crates/rust-analyzer/src/handlers.rs | 4 +-- 3 files changed, 62 insertions(+), 11 deletions(-) (limited to 'crates') 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 { use test_utils::mark; use crate::{ - test_utils::{check_edit, completion_list}, - CompletionKind, + test_utils::{check_edit, check_edit_with_config, completion_list}, + CompletionConfig, CompletionKind, }; fn check(ra_fixture: &str, expect: Expect) { @@ -807,6 +807,43 @@ use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}}; fn main() { ThirdStruct } +"#, + ); + } + + /// LSP protocol supports separate completion resolve requests to do the heavy computations there. + /// This test checks that for a certain resolve capatilities no such operations (autoimport) are done. + #[test] + fn no_fuzzy_completions_applied_for_certain_resolve_capability() { + let mut completion_config = CompletionConfig::default(); + completion_config + .active_resolve_capabilities + .insert(crate::CompletionResolveCapability::AdditionalTextEdits); + + check_edit_with_config( + completion_config, + "ThirdStruct", + r#" +//- /lib.rs crate:dep +pub struct FirstStruct; +pub mod some_module { +pub struct SecondStruct; +pub struct ThirdStruct; +} + +//- /main.rs crate:main deps:dep +use dep::{FirstStruct, some_module::SecondStruct}; + +fn main() { +this<|> +} +"#, + r#" +use dep::{FirstStruct, some_module::SecondStruct}; + +fn main() { +ThirdStruct +} "#, ); } 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 { } }; - if !self.resolve_import_lazily { - if let Some(import_edit) = - self.import_to_add.as_ref().and_then(|import_edit| import_edit.to_text_edit()) - { - text_edit.union(import_edit).expect("Failed to unite import and completion edits"); + let import_to_add = if self.resolve_import_lazily { + self.import_to_add + } else { + match apply_import_eagerly(self.import_to_add.as_ref(), &mut text_edit) { + Ok(()) => self.import_to_add, + Err(()) => { + log::error!("Failed to apply eager import edit: original edit and import edit intersect"); + None + } } - } + }; CompletionItem { source_range: self.source_range, @@ -368,7 +372,7 @@ impl Builder { trigger_call_info: self.trigger_call_info.unwrap_or(false), score: self.score, ref_match: self.ref_match, - import_to_add: self.import_to_add, + import_to_add, } } pub(crate) fn lookup_by(mut self, lookup: impl Into) -> Builder { @@ -449,6 +453,16 @@ impl Builder { } } +fn apply_import_eagerly( + import_to_add: Option<&ImportEdit>, + original_edit: &mut TextEdit, +) -> Result<(), ()> { + match import_to_add.and_then(|import_edit| import_edit.to_text_edit()) { + Some(import_edit) => original_edit.union(import_edit).map_err(|_| ()), + None => Ok(()), + } +} + impl<'a> Into for Builder { fn into(self) -> CompletionItem { 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( let imported_name = import_edit.import_path.segments.clone().pop()?.to_string(); *resolve_data = Some( - serde_json::to_value(CompletionResolveData { + to_value(CompletionResolveData { position: position.to_owned(), full_import_path, imported_name, }) - .expect("Failed to serialize a regular struct with derives"), + .unwrap(), ) } Some(()) -- cgit v1.2.3