diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -423,10 +423,24 @@ if (!Changed) return CB(Changed.takeError()); - CB(IncludeReplaces.merge(format::reformat( + auto Replacements = IncludeReplaces.merge(format::reformat( Style, *Changed, tooling::calculateRangesAfterReplacements(IncludeReplaces, Ranges), - File))); + File)); + clang::tooling::Replacements ReplacementsInRange; + for (const auto &Repl : Replacements) { + tooling::Range ReplRange{ + Repl.getOffset(), + std::max(Repl.getLength(), + Repl.getReplacementText().size()), + }; + if (ReplRange.overlapsWith(Ranges.front())) { + if (auto Err = ReplacementsInRange.add(Repl)) { + CB(std::move(Err)); + } + } + } + CB(ReplacementsInRange); }; WorkScheduler->runQuick("Format", File, std::move(Action)); } diff --git a/clang-tools-extra/clangd/test/formatting.test b/clang-tools-extra/clangd/test/formatting.test --- a/clang-tools-extra/clangd/test/formatting.test +++ b/clang-tools-extra/clangd/test/formatting.test @@ -8,19 +8,6 @@ # CHECK-NEXT: "jsonrpc": "2.0", # CHECK-NEXT: "result": [ # CHECK-NEXT: { -# CHECK-NEXT: "newText": "\n ", -# CHECK-NEXT: "range": { -# CHECK-NEXT: "end": { -# CHECK-NEXT: "character": 4, -# CHECK-NEXT: "line": 1 -# CHECK-NEXT: }, -# CHECK-NEXT: "start": { -# CHECK-NEXT: "character": 19, -# CHECK-NEXT: "line": 0 -# CHECK-NEXT: } -# CHECK-NEXT: } -# CHECK-NEXT: }, -# CHECK-NEXT: { # CHECK-NEXT: "newText": " ", # CHECK-NEXT: "range": { # CHECK-NEXT: "end": { @@ -45,19 +32,6 @@ # CHECK-NEXT: "line": 1 # CHECK-NEXT: } # CHECK-NEXT: } -# CHECK-NEXT: }, -# CHECK-NEXT: { -# CHECK-NEXT: "newText": "\n ", -# CHECK-NEXT: "range": { -# CHECK-NEXT: "end": { -# CHECK-NEXT: "character": 4, -# CHECK-NEXT: "line": 2 -# CHECK-NEXT: }, -# CHECK-NEXT: "start": { -# CHECK-NEXT: "character": 12, -# CHECK-NEXT: "line": 1 -# CHECK-NEXT: } -# CHECK-NEXT: } # CHECK-NEXT: } # CHECK-NEXT: ] ---