[clangd] Return clangd::TextEdit in ClangdServer::rename. NFC
authorIlya Biryukov <ibiryukov@google.com>
Wed, 3 Apr 2019 07:18:43 +0000 (07:18 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Wed, 3 Apr 2019 07:18:43 +0000 (07:18 +0000)
Summary:
Instead of tooling::Replacement. To avoid the need to have contents of
the file at the caller site. This also aligns better with other methods
in ClangdServer, majority of those already return LSP-specific data
types.

Reviewers: hokein, ioeric, sammccall

Reviewed By: sammccall

Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D60179

llvm-svn: 357561

clang-tools-extra/clangd/ClangdLSPServer.cpp
clang-tools-extra/clangd/ClangdServer.cpp
clang-tools-extra/clangd/ClangdServer.h
clang-tools-extra/unittests/clangd/SyncAPI.cpp
clang-tools-extra/unittests/clangd/SyncAPI.h

index 78466f4..7d6f54e 100644 (file)
@@ -541,19 +541,13 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
   Server->rename(
       File, Params.position, Params.newName,
       Bind(
-          [File, Code, Params](
-              decltype(Reply) Reply,
-              llvm::Expected<std::vector<tooling::Replacement>> Replacements) {
-            if (!Replacements)
-              return Reply(Replacements.takeError());
-
-            // Turn the replacements into the format specified by the Language
-            // Server Protocol. Fuse them into one big JSON array.
-            std::vector<TextEdit> Edits;
-            for (const auto &R : *Replacements)
-              Edits.push_back(replacementToEdit(*Code, R));
+          [File, Code, Params](decltype(Reply) Reply,
+                               llvm::Expected<std::vector<TextEdit>> Edits) {
+            if (!Edits)
+              return Reply(Edits.takeError());
+
             WorkspaceEdit WE;
-            WE.changes = {{Params.textDocument.uri.uri(), Edits}};
+            WE.changes = {{Params.textDocument.uri.uri(), *Edits}};
             Reply(WE);
           },
           std::move(Reply)));
index a265f37..907eb30 100644 (file)
@@ -280,9 +280,9 @@ ClangdServer::formatOnType(llvm::StringRef Code, PathRef File, Position Pos) {
 }
 
 void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
-                          Callback<std::vector<tooling::Replacement>> CB) {
+                          Callback<std::vector<TextEdit>> CB) {
   auto Action = [Pos](Path File, std::string NewName,
-                      Callback<std::vector<tooling::Replacement>> CB,
+                      Callback<std::vector<TextEdit>> CB,
                       llvm::Expected<InputsAndAST> InpAST) {
     if (!InpAST)
       return CB(InpAST.takeError());
@@ -306,7 +306,7 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
     if (!ResultCollector.Result.getValue())
       return CB(ResultCollector.Result->takeError());
 
-    std::vector<tooling::Replacement> Replacements;
+    std::vector<TextEdit> Replacements;
     for (const tooling::AtomicChange &Change : ResultCollector.Result->get()) {
       tooling::Replacements ChangeReps = Change.getReplacements();
       for (const auto &Rep : ChangeReps) {
@@ -320,7 +320,8 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
         //   * rename globally in project
         //   * rename in open files
         if (Rep.getFilePath() == File)
-          Replacements.push_back(Rep);
+          Replacements.push_back(
+              replacementToEdit(InpAST->Inputs.Contents, Rep));
       }
     }
     return CB(std::move(Replacements));
index e4e517c..228897f 100644 (file)
@@ -217,7 +217,7 @@ public:
   /// Rename all occurrences of the symbol at the \p Pos in \p File to
   /// \p NewName.
   void rename(PathRef File, Position Pos, llvm::StringRef NewName,
-              Callback<std::vector<tooling::Replacement>> CB);
+              Callback<std::vector<TextEdit>> CB);
 
   struct TweakRef {
     std::string ID;    /// ID to pass for applyTweak.
index 91e7a91..102cecb 100644 (file)
@@ -98,10 +98,10 @@ runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos) {
   return std::move(*Result);
 }
 
-llvm::Expected<std::vector<tooling::Replacement>>
-runRename(ClangdServer &Server, PathRef File, Position Pos,
-          llvm::StringRef NewName) {
-  llvm::Optional<llvm::Expected<std::vector<tooling::Replacement>>> Result;
+llvm::Expected<std::vector<TextEdit>> runRename(ClangdServer &Server,
+                                                PathRef File, Position Pos,
+                                                llvm::StringRef NewName) {
+  llvm::Optional<llvm::Expected<std::vector<TextEdit>>> Result;
   Server.rename(File, Pos, NewName, capture(Result));
   return std::move(*Result);
 }
index 8de9c05..c141652 100644 (file)
@@ -38,7 +38,7 @@ runLocateSymbolAt(ClangdServer &Server, PathRef File, Position Pos);
 llvm::Expected<std::vector<DocumentHighlight>>
 runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos);
 
-llvm::Expected<std::vector<tooling::Replacement>>
+llvm::Expected<std::vector<TextEdit>>
 runRename(ClangdServer &Server, PathRef File, Position Pos, StringRef NewName);
 
 std::string runDumpAST(ClangdServer &Server, PathRef File);