Skip to content

Commit 61dba02

Browse files
authored
Merge pull request #18503 from SomeoneToIgnore/kb/better-resolve-indexing
Use completion item indices instead of property matching when searching for the completion item to resolve
2 parents c2ffafd + e646263 commit 61dba02

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

src/tools/rust-analyzer/crates/rust-analyzer/src/handlers/request.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ pub(crate) fn handle_completion_resolve(
11311131
else {
11321132
return Ok(original_completion);
11331133
};
1134-
let resolved_completions = to_proto::completion_items(
1134+
let mut resolved_completions = to_proto::completion_items(
11351135
&snap.config,
11361136
&forced_resolve_completions_config.fields_to_resolve,
11371137
&line_index,
@@ -1140,15 +1140,13 @@ pub(crate) fn handle_completion_resolve(
11401140
resolve_data.trigger_character,
11411141
resolved_completions,
11421142
);
1143-
let Some(mut resolved_completion) = resolved_completions.into_iter().find(|completion| {
1144-
completion.label == original_completion.label
1145-
&& completion.kind == original_completion.kind
1146-
&& completion.deprecated == original_completion.deprecated
1147-
&& completion.preselect == original_completion.preselect
1148-
&& completion.sort_text == original_completion.sort_text
1149-
}) else {
1150-
return Ok(original_completion);
1151-
};
1143+
1144+
let mut resolved_completion =
1145+
if resolved_completions.get(resolve_data.completion_item_index).is_some() {
1146+
resolved_completions.swap_remove(resolve_data.completion_item_index)
1147+
} else {
1148+
return Ok(original_completion);
1149+
};
11521150

11531151
if !resolve_data.imports.is_empty() {
11541152
let additional_edits = snap

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/ext.rs

+1
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ pub struct CompletionResolveData {
826826
pub imports: Vec<CompletionImport>,
827827
pub version: Option<i32>,
828828
pub trigger_character: Option<char>,
829+
pub completion_item_index: usize,
829830
}
830831

831832
#[derive(Debug, Serialize, Deserialize)]

src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/to_proto.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -392,18 +392,36 @@ fn completion_item(
392392
} else {
393393
Vec::new()
394394
};
395-
if something_to_resolve || !imports.is_empty() {
396-
let data = lsp_ext::CompletionResolveData {
395+
let (ref_resolve_data, resolve_data) = if something_to_resolve || !imports.is_empty() {
396+
let mut item_index = acc.len();
397+
let ref_resolve_data = if ref_match.is_some() {
398+
let ref_resolve_data = lsp_ext::CompletionResolveData {
399+
position: tdpp.clone(),
400+
imports: Vec::new(),
401+
version,
402+
trigger_character: completion_trigger_character,
403+
completion_item_index: item_index,
404+
};
405+
item_index += 1;
406+
Some(to_value(ref_resolve_data).unwrap())
407+
} else {
408+
None
409+
};
410+
let resolve_data = lsp_ext::CompletionResolveData {
397411
position: tdpp.clone(),
398412
imports,
399413
version,
400414
trigger_character: completion_trigger_character,
415+
completion_item_index: item_index,
401416
};
402-
lsp_item.data = Some(to_value(data).unwrap());
403-
}
417+
(ref_resolve_data, Some(to_value(resolve_data).unwrap()))
418+
} else {
419+
(None, None)
420+
};
404421

405422
if let Some((label, indel, relevance)) = ref_match {
406-
let mut lsp_item_with_ref = lsp_types::CompletionItem { label, ..lsp_item.clone() };
423+
let mut lsp_item_with_ref =
424+
lsp_types::CompletionItem { label, data: ref_resolve_data, ..lsp_item.clone() };
407425
lsp_item_with_ref
408426
.additional_text_edits
409427
.get_or_insert_with(Default::default)
@@ -412,6 +430,7 @@ fn completion_item(
412430
acc.push(lsp_item_with_ref);
413431
};
414432

433+
lsp_item.data = resolve_data;
415434
acc.push(lsp_item);
416435

417436
fn set_score(

src/tools/rust-analyzer/docs/dev/lsp-extensions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp/ext.rs hash: 90cf7718d54fe3c2
2+
lsp/ext.rs hash: 96f88b7a5d0080c6
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

0 commit comments

Comments
 (0)