[PATCH] D64481: [clangd] Add a flag to clangdServer rename function to control whether we want format the replacements.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 10 06:44:32 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL365631: [clangd] Add a flag to clangdServer rename function to control whether we want… (authored by hokein, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D64481?vs=208940&id=208943#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D64481/new/

https://reviews.llvm.org/D64481

Files:
  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


Index: clang-tools-extra/trunk/clangd/ClangdServer.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp
@@ -283,23 +283,25 @@
 }
 
 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)
Index: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
+++ clang-tools-extra/trunk/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<std::vector<TextEdit>> Edits) {
Index: clang-tools-extra/trunk/clangd/ClangdServer.h
===================================================================
--- clang-tools-extra/trunk/clangd/ClangdServer.h
+++ clang-tools-extra/trunk/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<std::vector<TextEdit>> CB);
+              bool WantFormat, Callback<std::vector<TextEdit>> CB);
 
   struct TweakRef {
     std::string ID;    /// ID to pass for applyTweak.
Index: clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp
+++ clang-tools-extra/trunk/clangd/unittests/SyncAPI.cpp
@@ -102,7 +102,7 @@
                                                 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);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64481.208943.patch
Type: text/x-patch
Size: 3998 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190710/93cd6d12/attachment-0001.bin>


More information about the cfe-commits mailing list