Selecting &mut foo completion now actually inserts &mut

This commit is contained in:
Aleksey Kladov 2021-03-09 19:04:27 +03:00
parent 12fe301a0c
commit 73b9937e4e

View File

@ -175,12 +175,6 @@ pub(crate) fn completion_item(
line_index: &LineIndex, line_index: &LineIndex,
item: CompletionItem, item: CompletionItem,
) -> Vec<lsp_types::CompletionItem> { ) -> Vec<lsp_types::CompletionItem> {
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 additional_text_edits = Vec::new();
let mut text_edit = None; let mut text_edit = None;
// LSP does not allow arbitrary edits in completion, so we have to do a // 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() { 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() { if item.deprecated() {
@ -233,11 +229,16 @@ pub(crate) fn completion_item(
let mut res = match item.ref_match() { let mut res = match item.ref_match() {
Some(mutability) => { Some(mutability) => {
let mut refed = lsp_item.clone(); let mut lsp_item_with_ref = lsp_item.clone();
let label = format!("&{}{}", mutability.as_keyword_for_ref(), refed.label); lsp_item.preselect = Some(true);
set_score(&mut refed, &label); lsp_item.sort_text = Some(format!(" {}", item.label()));
refed.label = label; lsp_item_with_ref.label =
vec![lsp_item, refed] 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], None => vec![lsp_item],
}; };
@ -1104,13 +1105,13 @@ mod tests {
expect_test::expect![[r#" expect_test::expect![[r#"
[ [
( (
"arg", "&arg",
None, None,
), ),
( (
"&arg", "arg",
Some( Some(
" &arg", " arg",
), ),
), ),
] ]