Skip to content

Commit 73b9937

Browse files
committed
Selecting &mut foo completion now actually inserts &mut
1 parent 12fe301 commit 73b9937

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

crates/rust-analyzer/src/to_proto.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,6 @@ pub(crate) fn completion_item(
175175
line_index: &LineIndex,
176176
item: CompletionItem,
177177
) -> 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-
184178
let mut additional_text_edits = Vec::new();
185179
let mut text_edit = None;
186180
// LSP does not allow arbitrary edits in completion, so we have to do a
@@ -220,7 +214,9 @@ pub(crate) fn completion_item(
220214
};
221215

222216
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()));
224220
}
225221

226222
if item.deprecated() {
@@ -233,11 +229,16 @@ pub(crate) fn completion_item(
233229

234230
let mut res = match item.ref_match() {
235231
Some(mutability) => {
236-
let mut refed = lsp_item.clone();
237-
let label = format!("&{}{}", mutability.as_keyword_for_ref(), refed.label);
238-
set_score(&mut refed, &label);
239-
refed.label = label;
240-
vec![lsp_item, refed]
232+
let mut lsp_item_with_ref = lsp_item.clone();
233+
lsp_item.preselect = Some(true);
234+
lsp_item.sort_text = Some(format!(" {}", item.label()));
235+
lsp_item_with_ref.label =
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]
241242
}
242243
None => vec![lsp_item],
243244
};
@@ -1104,13 +1105,13 @@ mod tests {
11041105
expect_test::expect![[r#"
11051106
[
11061107
(
1107-
"arg",
1108+
"&arg",
11081109
None,
11091110
),
11101111
(
1111-
"&arg",
1112+
"arg",
11121113
Some(
1113-
" &arg",
1114+
" arg",
11141115
),
11151116
),
11161117
]

0 commit comments

Comments
 (0)