lsp: Add partial support for insert/replace completions (#9634)

Most notably, this should do away with completions overriding the whole
word around completion trigger text. Fixes: #4816



Release Notes:

- Fixed code completions overriding text around the cursor.
This commit is contained in:
Piotr Osiewicz 2024-03-21 16:19:21 +01:00 committed by GitHub
parent 6184278faf
commit e20508f66c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 3 deletions

View File

@ -590,6 +590,7 @@ impl LanguageServer {
"additionalTextEdits".to_string(),
],
}),
insert_replace_support: Some(true),
..Default::default()
}),
completion_list: Some(CompletionListCapability {

View File

@ -1557,9 +1557,19 @@ impl LspCommand for GetCompletions {
(range, text)
}
Some(lsp::CompletionTextEdit::InsertAndReplace(_)) => {
log::info!("unsupported insert/replace completion");
return None;
Some(lsp::CompletionTextEdit::InsertAndReplace(edit)) => {
let range = range_from_lsp(edit.insert);
let start = snapshot.clip_point_utf16(range.start, Bias::Left);
let end = snapshot.clip_point_utf16(range.end, Bias::Left);
if start != range.start.0 || end != range.end.0 {
log::info!("completion out of expected range");
return None;
}
(
snapshot.anchor_before(start)..snapshot.anchor_after(end),
edit.new_text.clone(),
)
}
};