Skip to content

Commit 077c1c3

Browse files
Less panic, more tests
1 parent 045d7f0 commit 077c1c3

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

crates/completion/src/completions/unqualified_path.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ mod tests {
122122
use test_utils::mark;
123123

124124
use crate::{
125-
test_utils::{check_edit, completion_list},
126-
CompletionKind,
125+
test_utils::{check_edit, check_edit_with_config, completion_list},
126+
CompletionConfig, CompletionKind,
127127
};
128128

129129
fn check(ra_fixture: &str, expect: Expect) {
@@ -807,6 +807,43 @@ use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}};
807807
fn main() {
808808
ThirdStruct
809809
}
810+
"#,
811+
);
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+
}
810847
"#,
811848
);
812849
}

crates/completion/src/item.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,17 @@ impl Builder {
346346
}
347347
};
348348

349-
if !self.resolve_import_lazily {
350-
if let Some(import_edit) =
351-
self.import_to_add.as_ref().and_then(|import_edit| import_edit.to_text_edit())
352-
{
353-
text_edit.union(import_edit).expect("Failed to unite import and completion edits");
349+
let import_to_add = if self.resolve_import_lazily {
350+
self.import_to_add
351+
} else {
352+
match apply_import_eagerly(self.import_to_add.as_ref(), &mut text_edit) {
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+
}
354358
}
355-
}
359+
};
356360

357361
CompletionItem {
358362
source_range: self.source_range,
@@ -368,7 +372,7 @@ impl Builder {
368372
trigger_call_info: self.trigger_call_info.unwrap_or(false),
369373
score: self.score,
370374
ref_match: self.ref_match,
371-
import_to_add: self.import_to_add,
375+
import_to_add,
372376
}
373377
}
374378
pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
@@ -449,6 +453,16 @@ impl Builder {
449453
}
450454
}
451455

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+
452466
impl<'a> Into<CompletionItem> for Builder {
453467
fn into(self) -> CompletionItem {
454468
self.build()

crates/rust-analyzer/src/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,12 +1626,12 @@ fn fill_resolve_data(
16261626
let imported_name = import_edit.import_path.segments.clone().pop()?.to_string();
16271627

16281628
*resolve_data = Some(
1629-
serde_json::to_value(CompletionResolveData {
1629+
to_value(CompletionResolveData {
16301630
position: position.to_owned(),
16311631
full_import_path,
16321632
imported_name,
16331633
})
1634-
.expect("Failed to serialize a regular struct with derives"),
1634+
.unwrap(),
16351635
)
16361636
}
16371637
Some(())

0 commit comments

Comments
 (0)