[clang-tools-extra] 614b46e - [clangd] Add a flag to disable formatting of tweak edits

Kadir Cetinkaya via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 28 11:53:15 PDT 2021


Author: Kadir Cetinkaya
Date: 2021-06-28T20:52:47+02:00
New Revision: 614b46e4dcab0d095e05f8b4da45ef935b7b86b4

URL: https://github.com/llvm/llvm-project/commit/614b46e4dcab0d095e05f8b4da45ef935b7b86b4
DIFF: https://github.com/llvm/llvm-project/commit/614b46e4dcab0d095e05f8b4da45ef935b7b86b4.diff

LOG: [clangd] Add a flag to disable formatting of tweak edits

Some tweaks might edit file types not supported by clang-format. This
patch gives them a way to signal that they do not require formatting.

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/ClangdServer.cpp
    clang-tools-extra/clangd/refactor/Tweak.h
    clang-tools-extra/clangd/unittests/ClangdTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp
index 0f525f3b9a0a4..1e722086e2e04 100644
--- a/clang-tools-extra/clangd/ClangdServer.cpp
+++ b/clang-tools-extra/clangd/ClangdServer.cpp
@@ -637,8 +637,8 @@ void ClangdServer::applyTweak(PathRef File, Range Sel, StringRef TweakID,
       Effect = T.takeError();
     }
     assert(Effect.hasValue() && "Expected at least one selection");
-    if (*Effect) {
-      // Tweaks don't apply clang-format, do that centrally here.
+    if (*Effect && (*Effect)->FormatEdits) {
+      // Format tweaks that require it centrally here.
       for (auto &It : (*Effect)->ApplyEdits) {
         Edit &E = It.second;
         format::FormatStyle Style =

diff  --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h
index 60ee34d138d6b..5b2d9cc80d9fd 100644
--- a/clang-tools-extra/clangd/refactor/Tweak.h
+++ b/clang-tools-extra/clangd/refactor/Tweak.h
@@ -78,6 +78,9 @@ class Tweak {
     /// A message to be displayed to the user.
     llvm::Optional<std::string> ShowMessage;
     FileEdits ApplyEdits;
+    /// Whether the edits should be formatted before presenting to the client.
+    /// Note that it applies to all files.
+    bool FormatEdits = true;
 
     static Effect showMessage(StringRef S) {
       Effect E;

diff  --git a/clang-tools-extra/clangd/unittests/ClangdTests.cpp b/clang-tools-extra/clangd/unittests/ClangdTests.cpp
index 49e1f7aa93b67..07f5da1fbc52f 100644
--- a/clang-tools-extra/clangd/unittests/ClangdTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ClangdTests.cpp
@@ -18,12 +18,14 @@
 #include "TestTU.h"
 #include "TidyProvider.h"
 #include "URI.h"
+#include "refactor/Tweak.h"
 #include "support/MemoryTree.h"
 #include "support/Path.h"
 #include "support/Threading.h"
 #include "clang/Config/config.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "clang/Tooling/ArgumentsAdjusters.h"
+#include "clang/Tooling/Core/Replacement.h"
 #include "llvm/ADT/None.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
@@ -31,6 +33,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Errc.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/Support/VirtualFileSystem.h"
@@ -1259,6 +1262,60 @@ TEST(ClangdServer, MemoryUsageTest) {
   ASSERT_TRUE(MT.children().count("tuscheduler"));
   EXPECT_TRUE(MT.child("tuscheduler").children().count(FooCpp));
 }
+
+TEST(ClangdServer, RespectsTweakFormatting) {
+  static constexpr const char *TweakID = "ModuleTweak";
+  static constexpr const char *NewContents = "{not;\nformatted;}";
+
+  // Contributes a tweak that generates a non-formatted insertion and disables
+  // formatting.
+  struct TweakContributingModule final : public FeatureModule {
+    struct ModuleTweak final : public Tweak {
+      const char *id() const override { return TweakID; }
+      bool prepare(const Selection &Sel) override { return true; }
+      Expected<Effect> apply(const Selection &Sel) override {
+        auto &SM = Sel.AST->getSourceManager();
+        llvm::StringRef FilePath = SM.getFilename(Sel.Cursor);
+        tooling::Replacements Reps;
+        llvm::cantFail(
+            Reps.add(tooling::Replacement(FilePath, 0, 0, NewContents)));
+        auto E = llvm::cantFail(Effect::mainFileEdit(SM, std::move(Reps)));
+        E.FormatEdits = false;
+        return E;
+      }
+      std::string title() const override { return id(); }
+      llvm::StringLiteral kind() const override {
+        return llvm::StringLiteral("");
+      };
+    };
+
+    void contributeTweaks(std::vector<std::unique_ptr<Tweak>> &Out) override {
+      Out.emplace_back(new ModuleTweak);
+    }
+  };
+
+  MockFS FS;
+  MockCompilationDatabase CDB;
+  auto Opts = ClangdServer::optsForTest();
+  FeatureModuleSet Set;
+  Set.add(std::make_unique<TweakContributingModule>());
+  Opts.FeatureModules = &Set;
+  ClangdServer Server(CDB, FS, Opts);
+
+  auto FooCpp = testPath("foo.cpp");
+  Server.addDocument(FooCpp, "");
+  ASSERT_TRUE(Server.blockUntilIdleForTest());
+
+  // Ensure that disabled formatting is respected.
+  Notification N;
+  Server.applyTweak(FooCpp, {}, TweakID, [&](llvm::Expected<Tweak::Effect> E) {
+    ASSERT_TRUE(static_cast<bool>(E));
+    EXPECT_THAT(llvm::cantFail(E->ApplyEdits.lookup(FooCpp).apply()),
+                NewContents);
+    N.notify();
+  });
+  N.wait();
+}
 } // namespace
 } // namespace clangd
 } // namespace clang


        


More information about the cfe-commits mailing list