Skip to content

LSP: replace the rest of the word on Tab in completion (InsertReplaceEdit, insertReplaceSupport) #2234

Done

Description

@lagergren

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_support in crates/rust-analyzer/src/lsp/to_proto.rs.
  • typescript-language-server: completionInsertReplaceSupport in src/completion.ts.
  • jdtls: isCompletionInsertReplaceSupport() in CompletionProposalRequestor.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

  • insertReplaceSupport is not announced (ClientCapabilitiesFactory.java#L169-L179). So these servers send a plain TextEdit, usually with the insert range.
  • When an InsertReplaceEdit does come, LSP4IJ always applies its insert range ("trick to partially support the new InsertReplaceEdit", LSPCompletionProposal.java#L321-L333). An InsertReplaceRange default is turned into an InsertReplaceEdit (LSPCompletionContributor.java#L244-L247), and then the same applies.
  • handleInsert never looks at context.getCompletionChar().
  • On Tab, IntelliJ itself deletes the text from the caret to CompletionInitializationContext.IDENTIFIER_END_OFFSET before it calls handleInsert (CodeCompletionHandlerBase.insertItem, checked in the 2026.2 bytecode). LSP4IJ doesn't set that offset (no beforeCompletion), 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

  1. Announce insertReplaceSupport: true.

  2. In LSPCompletionProposal.handleInsert, use replace when context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR, and insert otherwise. Both classes exist in 2024.2 and 2026.x.

  3. 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's replace range when there is one, or today's default otherwise.
    • (b) Keep the platform's deletion and shorten replace by what was already deleted.

    (a) is cleaner. Whether it interacts with prefix matching in the lookup needs a test.

  4. The same applies to snippets (InsertTextFormat.Snippet) with an InsertReplaceEdit.

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

  1. added
    enhancement
    on Nov 25, 2026
  2. cordeirops commented on Nov 26, 2026

    @cordeirops
    Contributor

    Hi! I'd like to work on this issue. I already submitted a fix in #2297 : the client now declares
    insertReplaceSupport, and Tab applies the replace range, while any other key applies insert. Could you please
    assign it to me?

    Thank you!

    /assign

  3. modified the milestones: 0.30.3, 0.30.2 on Nov 26, 2026
  4. added a commit that references this issue on Nov 27, 2026
    ff1902e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    completionenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions