diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -577,7 +577,7 @@ "onRename called for non-added file", ErrorCode::InvalidParams)); Server->rename( - File, Params.position, Params.newName, + File, Params.position, Params.newName, /*WantFormat=*/true, Bind( [File, Code, Params](decltype(Reply) Reply, llvm::Expected> Edits) { diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -240,8 +240,11 @@ /// Rename all occurrences of the symbol at the \p Pos in \p File to /// \p NewName. + /// If WantFormat is false, the final TextEdit will be not formatted, + /// embedders could use this method to get all occurrences of the symbol (e.g. + /// highlighting them in prepare stage). void rename(PathRef File, Position Pos, llvm::StringRef NewName, - Callback> CB); + bool WantFormat, Callback> CB); struct TweakRef { std::string ID; /// ID to pass for applyTweak. 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 @@ -283,23 +283,25 @@ } void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName, - Callback> CB) { - auto Action = [Pos, this](Path File, std::string NewName, - Callback> CB, - llvm::Expected InpAST) { + bool WantFormat, Callback> CB) { + auto Action = [Pos, WantFormat, this](Path File, std::string NewName, + Callback> CB, + llvm::Expected InpAST) { if (!InpAST) return CB(InpAST.takeError()); auto Changes = renameWithinFile(InpAST->AST, File, Pos, NewName, Index); if (!Changes) return CB(Changes.takeError()); - auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents, - InpAST->Inputs.FS.get()); - if (auto Formatted = - cleanupAndFormat(InpAST->Inputs.Contents, *Changes, Style)) - *Changes = std::move(*Formatted); - else - elog("Failed to format replacements: {0}", Formatted.takeError()); + if (WantFormat) { + auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents, + InpAST->Inputs.FS.get()); + if (auto Formatted = + cleanupAndFormat(InpAST->Inputs.Contents, *Changes, Style)) + *Changes = std::move(*Formatted); + else + elog("Failed to format replacements: {0}", Formatted.takeError()); + } std::vector Edits; for (const auto &Rep : *Changes) diff --git a/clang-tools-extra/clangd/unittests/SyncAPI.cpp b/clang-tools-extra/clangd/unittests/SyncAPI.cpp --- a/clang-tools-extra/clangd/unittests/SyncAPI.cpp +++ b/clang-tools-extra/clangd/unittests/SyncAPI.cpp @@ -102,7 +102,7 @@ PathRef File, Position Pos, llvm::StringRef NewName) { llvm::Optional>> Result; - Server.rename(File, Pos, NewName, capture(Result)); + Server.rename(File, Pos, NewName, /*WantFormat=*/true, capture(Result)); return std::move(*Result); }