Skip to content

Commit dd05d12

Browse files
bors[bot]The0x539
andauthored
Merge #11412
11412: fix: Include `fn`/`type`/`const` keyword in trait impl completion item source ranges r=Veykril a=The0x539 Fixes #11301 If the user has typed, say, `fn de` while implementing `Default`, or `type Ta` when implementing `Deref`, then the resulting completion suggestion will replace the entire "line", which, on its own, is fine. However, the use of `ctx.source_range()` in this code was meant that `source_range` field of the `CompletionItem` covers only the identifier and not the preceding keyword. Over in `rust_analyzer::to_proto::completion_item`, this caused the LSP completion response to be broken up into a text edit that replaces `de` with `fn default() -> Self {` and then an entry in `additional_text_edits` to remove the extra `fn`. I'm pretty sure that using the field like that is (slightly) out of [spec](https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#completionItem): > Edits must not overlap [...] with the main edit > Additional text edits should be used to change text **unrelated to the current cursor position** VS Code supports `additionalTextEdits` in such a way that this doesn't seem like a problem, so has gone largely unnoticed. The various LSP clients I've tried, however, do not, and as a result this bug has been haunting me for ages. Co-authored-by: The0x539 <[email protected]>
2 parents 0feafe8 + 1536fc0 commit dd05d12

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

crates/ide_completion/src/completions/trait_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ fn add_function_impl(
145145
} else {
146146
CompletionItemKind::SymbolKind(SymbolKind::Function)
147147
};
148-
let mut item = CompletionItem::new(completion_kind, ctx.source_range(), label);
149-
item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
150148

151149
let range = replacement_range(ctx, fn_def_node);
150+
let mut item = CompletionItem::new(completion_kind, range, label);
151+
item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
152152

153153
if let Some(source) = ctx.sema.source(func) {
154154
let assoc_item = ast::AssocItem::Fn(source.value);
@@ -209,7 +209,7 @@ fn add_type_alias_impl(
209209
let snippet = format!("type {} = ", alias_name);
210210

211211
let range = replacement_range(ctx, type_def_node);
212-
let mut item = CompletionItem::new(SymbolKind::TypeAlias, ctx.source_range(), &snippet);
212+
let mut item = CompletionItem::new(SymbolKind::TypeAlias, range, &snippet);
213213
item.text_edit(TextEdit::replace(range, snippet))
214214
.lookup_by(alias_name)
215215
.set_documentation(type_alias.docs(ctx.db));
@@ -237,7 +237,7 @@ fn add_const_impl(
237237
let snippet = make_const_compl_syntax(&transformed_const);
238238

239239
let range = replacement_range(ctx, const_def_node);
240-
let mut item = CompletionItem::new(SymbolKind::Const, ctx.source_range(), &snippet);
240+
let mut item = CompletionItem::new(SymbolKind::Const, range, &snippet);
241241
item.text_edit(TextEdit::replace(range, snippet))
242242
.lookup_by(const_name)
243243
.set_documentation(const_.docs(ctx.db));

0 commit comments

Comments
 (0)