summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2019-07-04 10:49:32 +0000
committerHaojian Wu <hokein@google.com>2019-07-04 10:49:32 +0000
commitb7ec41556b9c1ad9670b003b63b35f67683b84b4 (patch)
treebf1d33bcd900553131e569c513d5a4321b859135
parentf347541fbcfcdeeeed185c1d0437e5533df381c9 (diff)
downloadbcm5719-llvm-b7ec41556b9c1ad9670b003b63b35f67683b84b4.tar.gz
bcm5719-llvm-b7ec41556b9c1ad9670b003b63b35f67683b84b4.zip
[clangd] Add a hidden tweak to annotate all highlighting tokens of the file.
Reviewers: sammccall, jvikstrom Subscribers: mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64137 llvm-svn: 365130
-rw-r--r--clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp82
-rw-r--r--clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt1
-rw-r--r--clang-tools-extra/clangd/unittests/TweakTests.cpp8
3 files changed, 91 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
new file mode 100644
index 00000000000..90119b697c5
--- /dev/null
+++ b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
@@ -0,0 +1,82 @@
+//===--- AnnotateHighlightings.cpp -------------------------------*- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#include "SemanticHighlighting.h"
+#include "refactor/Tweak.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+// FIXME: move it to SemanticHighlighting.h.
+llvm::StringRef toTextMateScope(HighlightingKind Kind) {
+ static const auto &TextMateLookupTable = getTextMateScopeLookupTable();
+ auto LookupIndex = static_cast<size_t>(Kind);
+ assert(LookupIndex < TextMateLookupTable.size() &&
+ !TextMateLookupTable[LookupIndex].empty());
+ return TextMateLookupTable[LookupIndex].front();
+}
+
+/// Annotate all highlighting tokens in the current file. This is a hidden tweak
+/// which is used to debug semantic highlightings.
+/// Before:
+/// void f() { int abc; }
+/// ^^^^^^^^^^^^^^^^^^^^^
+/// After:
+/// void /* entity.name.function.cpp */ f() { int /* variable.cpp */ abc; }
+class AnnotateHighlightings : public Tweak {
+public:
+ const char *id() const override final;
+
+ bool prepare(const Selection &Inputs) override {
+ for (auto N = Inputs.ASTSelection.commonAncestor(); N && !InterestedDecl;
+ N = N->Parent)
+ InterestedDecl = N->ASTNode.get<Decl>();
+ return InterestedDecl;
+ }
+ Expected<Effect> apply(const Selection &Inputs) override;
+
+ std::string title() const override { return "Annotate highlighting tokens"; }
+ Intent intent() const override { return Refactor; }
+ bool hidden() const override { return true; }
+
+private:
+ const Decl *InterestedDecl = nullptr;
+};
+REGISTER_TWEAK(AnnotateHighlightings)
+
+Expected<Tweak::Effect> AnnotateHighlightings::apply(const Selection &Inputs) {
+ // Store the existing scopes.
+ const auto &BackupScopes = Inputs.AST.getASTContext().getTraversalScope();
+ // Narrow the traversal scope to the selected node.
+ Inputs.AST.getASTContext().setTraversalScope(
+ {const_cast<Decl *>(InterestedDecl)});
+ auto HighlightingTokens = getSemanticHighlightings(Inputs.AST);
+ // Restore the traversal scope.
+ Inputs.AST.getASTContext().setTraversalScope(BackupScopes);
+
+ auto &SM = Inputs.AST.getSourceManager();
+ tooling::Replacements Result;
+ for (const auto &Token : HighlightingTokens) {
+ assert(Token.R.start.line == Token.R.end.line &&
+ "Token must be at the same line");
+ auto InsertOffset = positionToOffset(Inputs.Code, Token.R.start);
+ if (!InsertOffset)
+ return InsertOffset.takeError();
+
+ auto InsertReplacement = tooling::Replacement(
+ SM.getFileEntryForID(SM.getMainFileID())->getName(), *InsertOffset, 0,
+ ("/* " + toTextMateScope(Token.Kind) + " */").str());
+ if (auto Err = Result.add(InsertReplacement))
+ return std::move(Err);
+ }
+ return Effect::applyEdit(Result);
+}
+
+} // namespace
+} // namespace clangd
+} // namespace clang
diff --git a/clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt b/clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
index fb6f6281351..a646436fd04 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
+++ b/clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
@@ -12,6 +12,7 @@ set(LLVM_LINK_COMPONENTS
# $<TARGET_OBJECTS:obj.clangDaemonTweaks> to a list of sources, see
# clangd/tool/CMakeLists.txt for an example.
add_clang_library(clangDaemonTweaks OBJECT
+ AnnotateHighlightings.cpp
DumpAST.cpp
RawStringLiteral.cpp
SwapIfBranches.cpp
diff --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index e0122903c35..6bbec5ce1df 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -278,6 +278,14 @@ TEST(TweakTest, DumpRecordLayout) {
EXPECT_THAT(getMessage(ID, Input), ::testing::HasSubstr("0 | int x"));
}
+TEST(TweakTest, AnnotateHighlightings) {
+ llvm::StringLiteral ID = "AnnotateHighlightings";
+ checkAvailable(ID, "^vo^id^ ^f(^) {^}^"); // available everywhere.
+ const char *Input = "void ^f() {}";
+ const char *Output = "void /* entity.name.function.cpp */f() {}";
+ checkTransform(ID, Input, Output);
+}
+
} // namespace
} // namespace clangd
} // namespace clang
OpenPOWER on IntegriCloud