Repository navigation
Description
What
Since LSP 3.16, the textEdit of a completion item can be an InsertReplaceEdit. It has two ranges, and the client picks one:
insert: from the start of the word to the caret;replace: from the start of the word to its end, past the caret.
A server sends it only when the client sets textDocument.completion.completionItem.insertReplaceSupport (completion client capabilities). Since 3.17, CompletionList.itemDefaults.editRange can also be { insert, replace }.
Why
Servers that send InsertReplaceEdit only when the client asks for it:
- rust-analyzer:
insert_replace_supportincrates/rust-analyzer/src/lsp/to_proto.rs. - typescript-language-server:
completionInsertReplaceSupportinsrc/completion.ts. - jdtls:
isCompletionInsertReplaceSupport()inCompletionProposalRequestor.java.
What users miss: completion inside a word. With the caret in foo|Bar, accepting fooBaz with Enter should give fooBazBar, and with Tab fooBaz. In IntelliJ, Tab means "replace" (Lookup.REPLACE_SELECT_CHAR), and the server knows where its word ends. In VS Code the user picks the default with editor.suggest.insertMode.
Today
insertReplaceSupportis not announced (ClientCapabilitiesFactory.java#L169-L179). So these servers send a plainTextEdit, usually with the insert range.- When an
InsertReplaceEditdoes come, LSP4IJ always applies itsinsertrange ("trick to partially support the new InsertReplaceEdit", LSPCompletionProposal.java#L321-L333). AnInsertReplaceRangedefault is turned into anInsertReplaceEdit(LSPCompletionContributor.java#L244-L247), and then the same applies. handleInsertnever looks atcontext.getCompletionChar().- On Tab, IntelliJ itself deletes the text from the caret to
CompletionInitializationContext.IDENTIFIER_END_OFFSETbefore it callshandleInsert(CodeCompletionHandlerBase.insertItem, checked in the 2026.2 bytecode). LSP4IJ doesn't set that offset (nobeforeCompletion), so it is IntelliJ's default identifier end, made of Java identifier characters. This doesn't match a server's word:-in CSS, Clojure or Lisp names,.or:in some languages, and so on.
How it could be done
-
Announce
insertReplaceSupport: true. -
In
LSPCompletionProposal.handleInsert, usereplacewhencontext.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR, andinsertotherwise. Both classes exist in 2024.2 and 2026.x. -
Keep IntelliJ's tail deletion from fighting the server's range. There are two options:
- (a) In
LSPCompletionContributor.beforeCompletion, set the identifier end to the caret (context.setReplacementOffset(...)) for files that an LSP completion serves. LSP4IJ then deletes the tail itself: the server'sreplacerange when there is one, or today's default otherwise. - (b) Keep the platform's deletion and shorten
replaceby what was already deleted.
(a) is cleaner. Whether it interacts with prefix matching in the lookup needs a test.
- (a) In
-
The same applies to snippets (
InsertTextFormat.Snippet) with anInsertReplaceEdit.
Test
Completion fixture tests: Enter and Tab inside foo|Bar, with an InsertReplaceEdit, with an InsertReplaceRange item default, and with a plain TextEdit (unchanged behaviour), plus a word with -.
Activity
cordeirops commentedon Nov 26, 2026
Hi! I'd like to work on this issue. I already submitted a fix in #2297 : the client now declares
insertReplaceSupport, and Tab applies thereplacerange, while any other key appliesinsert. Could you pleaseassign it to me?
Thank you!
/assign
fix: Tab replaces the whole word and Enter inserts at the caret, for …