[clang-tools-extra] r365130 - [clangd] Add a hidden tweak to annotate all highlighting tokens of the file.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 4 03:49:32 PDT 2019
Author: hokein
Date: Thu Jul 4 03:49:32 2019
New Revision: 365130
URL: http://llvm.org/viewvc/llvm-project?rev=365130&view=rev
Log:
[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
Added:
clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp
Modified:
clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
Added: clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp?rev=365130&view=auto
==============================================================================
--- clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp (added)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp Thu Jul 4 03:49:32 2019
@@ -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
Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt?rev=365130&r1=365129&r2=365130&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt Thu Jul 4 03:49:32 2019
@@ -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
Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=365130&r1=365129&r2=365130&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Thu Jul 4 03:49:32 2019
@@ -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
More information about the cfe-commits
mailing list