aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-03-09 16:04:27 +0000
committerAleksey Kladov <[email protected]>2021-03-09 16:04:27 +0000
commit73b9937e4eea2633005e7d2814cb7990e5f20e8f (patch)
tree408925e5d9e67745b60fc6e6cf95a31411c3e4ed
parent12fe301a0cbe4ffecdabae1c9b827e740e3ce027 (diff)
Selecting `&mut foo` completion now actually inserts `&mut`
-rw-r--r--crates/rust-analyzer/src/to_proto.rs31
1 files 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(
175 line_index: &LineIndex, 175 line_index: &LineIndex,
176 item: CompletionItem, 176 item: CompletionItem,
177) -> Vec<lsp_types::CompletionItem> { 177) -> Vec<lsp_types::CompletionItem> {
178 fn set_score(lsp_item: &mut lsp_types::CompletionItem, label: &str) {
179 lsp_item.preselect = Some(true);
180 // HACK: sort preselect items first
181 lsp_item.sort_text = Some(format!(" {}", label));
182 }
183
184 let mut additional_text_edits = Vec::new(); 178 let mut additional_text_edits = Vec::new();
185 let mut text_edit = None; 179 let mut text_edit = None;
186 // LSP does not allow arbitrary edits in completion, so we have to do a 180 // LSP does not allow arbitrary edits in completion, so we have to do a
@@ -220,7 +214,9 @@ pub(crate) fn completion_item(
220 }; 214 };
221 215
222 if item.score().is_some() { 216 if item.score().is_some() {
223 set_score(&mut lsp_item, item.label()); 217 lsp_item.preselect = Some(true);
218 // HACK: sort preselect items first
219 lsp_item.sort_text = Some(format!(" {}", item.label()));
224 } 220 }
225 221
226 if item.deprecated() { 222 if item.deprecated() {
@@ -233,11 +229,16 @@ pub(crate) fn completion_item(
233 229
234 let mut res = match item.ref_match() { 230 let mut res = match item.ref_match() {
235 Some(mutability) => { 231 Some(mutability) => {
236 let mut refed = lsp_item.clone(); 232 let mut lsp_item_with_ref = lsp_item.clone();
237 let label = format!("&{}{}", mutability.as_keyword_for_ref(), refed.label); 233 lsp_item.preselect = Some(true);
238 set_score(&mut refed, &label); 234 lsp_item.sort_text = Some(format!(" {}", item.label()));
239 refed.label = label; 235 lsp_item_with_ref.label =
240 vec![lsp_item, refed] 236 format!("&{}{}", mutability.as_keyword_for_ref(), lsp_item_with_ref.label);
237 if let Some(lsp_types::CompletionTextEdit::Edit(it)) = &mut lsp_item_with_ref.text_edit
238 {
239 it.new_text = format!("&{}{}", mutability.as_keyword_for_ref(), it.new_text);
240 }
241 vec![lsp_item_with_ref, lsp_item]
241 } 242 }
242 None => vec![lsp_item], 243 None => vec![lsp_item],
243 }; 244 };
@@ -1104,13 +1105,13 @@ mod tests {
1104 expect_test::expect![[r#" 1105 expect_test::expect![[r#"
1105 [ 1106 [
1106 ( 1107 (
1107 "arg", 1108 "&arg",
1108 None, 1109 None,
1109 ), 1110 ),
1110 ( 1111 (
1111 "&arg", 1112 "arg",
1112 Some( 1113 Some(
1113 " &arg", 1114 " arg",
1114 ), 1115 ),
1115 ), 1116 ),
1116 ] 1117 ]