From 73b9937e4eea2633005e7d2814cb7990e5f20e8f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Mar 2021 19:04:27 +0300 Subject: Selecting `&mut foo` completion now actually inserts `&mut` --- crates/rust-analyzer/src/to_proto.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index a730fb448..2380e021a 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -175,12 +175,6 @@ pub(crate) fn completion_item( line_index: &LineIndex, item: CompletionItem, ) -> Vec { - fn set_score(lsp_item: &mut lsp_types::CompletionItem, label: &str) { - lsp_item.preselect = Some(true); - // HACK: sort preselect items first - lsp_item.sort_text = Some(format!(" {}", label)); - } - let mut additional_text_edits = Vec::new(); let mut text_edit = None; // LSP does not allow arbitrary edits in completion, so we have to do a @@ -220,7 +214,9 @@ pub(crate) fn completion_item( }; if item.score().is_some() { - set_score(&mut lsp_item, item.label()); + lsp_item.preselect = Some(true); + // HACK: sort preselect items first + lsp_item.sort_text = Some(format!(" {}", item.label())); } if item.deprecated() { @@ -233,11 +229,16 @@ pub(crate) fn completion_item( let mut res = match item.ref_match() { Some(mutability) => { - let mut refed = lsp_item.clone(); - let label = format!("&{}{}", mutability.as_keyword_for_ref(), refed.label); - set_score(&mut refed, &label); - refed.label = label; - vec![lsp_item, refed] + let mut lsp_item_with_ref = lsp_item.clone(); + lsp_item.preselect = Some(true); + lsp_item.sort_text = Some(format!(" {}", item.label())); + lsp_item_with_ref.label = + format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label); + if let Some(lsp_types::CompletionTextEdit::Edit(it)) = &mut lsp_item_with_ref.text_edit + { + it.new_text = format!("&{}{}", mutability.as_keyword_for_ref(), it.new_text); + } + vec![lsp_item_with_ref, lsp_item] } None => vec![lsp_item], }; @@ -1104,13 +1105,13 @@ mod tests { expect_test::expect![[r#" [ ( - "arg", + "&arg", None, ), ( - "&arg", + "arg", Some( - " &arg", + " arg", ), ), ] -- cgit v1.2.3