[clang] [clang-tools-extra] [clangd] Add InsertReplaceEdit for code completion (PR #187623)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 19 19:49:31 PDT 2026


================
@@ -1874,6 +1896,26 @@ class CodeCompleteFlow {
     return toCodeCompleteResult(Top);
   }
 
+  // Returns the LSP position at the end of the identifier suffix after the
+  // code completion cursor.
+  Position getEndOfCodeCompletionReplace(const SourceManager &SM) {
+    const Preprocessor &PP = Recorder->CCSema->getPreprocessor();
+    const LangOptions &LangOpts = Recorder->CCSema->getLangOpts();
+
+    // Skip past the code completion NUL byte and scan forward through
+    // identifier continuation characters (letters, digits, _, $, UCN,
+    // unicode). This handles all cases uniformly: with prefix ("vac^1abc"),
+    // without prefix ("vec.^asdf"), and digit-starting ("vec.^1abc").
+    const SourceLocation SuffixBegin =
+        PP.getCodeCompletionLoc().getLocWithOffset(1);
+    Position End = sourceLocToPosition(
+        SM, Lexer::findEndOfIdentifierContinuation(SuffixBegin, SM, LangOpts));
+    // Adjust for the NUL byte inserted at the cursor by code completion,
+    // which inflates the column by 1.
+    End.character--;
----------------
argothiel wrote:

Feels a bit hacky. If anyone has an idea how not to go off by one here, I'm open for suggestions.

https://github.com/llvm/llvm-project/pull/187623


More information about the cfe-commits mailing list