Author: hokein Date: Wed Jul 10 06:44:22 2019 New Revision: 365631 URL: http://llvm.org/viewvc/llvm-project?rev=365631&view=rev Log: [clangd] Add a flag to clangdServer rename function to control whether we want format the replacements.
Summary: This would allow clangd embedders to use the ClangdServer::rename for other purposes (highlighting all the occurrences of the symbol in prepare stage). Reviewers: sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64481 Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.cpp clang-tools-extra/trunk/clangd/ClangdServer.h clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=365631&r1=365630&r2=365631&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Wed Jul 10 06:44:22 2019 @@ -577,7 +577,7 @@ void ClangdLSPServer::onRename(const Ren "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<std::vector<TextEdit>> Edits) { Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=365631&r1=365630&r2=365631&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Wed Jul 10 06:44:22 2019 @@ -283,23 +283,25 @@ ClangdServer::formatOnType(llvm::StringR } void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName, - Callback<std::vector<TextEdit>> CB) { - auto Action = [Pos, this](Path File, std::string NewName, - Callback<std::vector<TextEdit>> CB, - llvm::Expected<InputsAndAST> InpAST) { + bool WantFormat, Callback<std::vector<TextEdit>> CB) { + auto Action = [Pos, WantFormat, this](Path File, std::string NewName, + Callback<std::vector<TextEdit>> CB, + llvm::Expected<InputsAndAST> 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<TextEdit> Edits; for (const auto &Rep : *Changes) Modified: clang-tools-extra/trunk/clangd/ClangdServer.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=365631&r1=365630&r2=365631&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/ClangdServer.h (original) +++ clang-tools-extra/trunk/clangd/ClangdServer.h Wed Jul 10 06:44:22 2019 @@ -240,8 +240,11 @@ public: /// 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<std::vector<TextEdit>> CB); + bool WantFormat, Callback<std::vector<TextEdit>> CB); struct TweakRef { std::string ID; /// ID to pass for applyTweak. Modified: clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp?rev=365631&r1=365630&r2=365631&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp Wed Jul 10 06:44:22 2019 @@ -102,7 +102,7 @@ llvm::Expected<std::vector<TextEdit>> ru PathRef File, Position Pos, llvm::StringRef NewName) { llvm::Optional<llvm::Expected<std::vector<TextEdit>>> Result; - Server.rename(File, Pos, NewName, capture(Result)); + Server.rename(File, Pos, NewName, /*WantFormat=*/true, capture(Result)); return std::move(*Result); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits