[clang-tools-extra] r365135 - [clangd] Some tweaks on semantic highlighting lookuptable.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 4 05:27:22 PDT 2019


Author: hokein
Date: Thu Jul  4 05:27:21 2019
New Revision: 365135

URL: http://llvm.org/viewvc/llvm-project?rev=365135&view=rev
Log:
[clangd] Some tweaks on semantic highlighting lookuptable.

Summary:
- move toTextMateScope to SemanticHighlighting.h;
- move the buildLookupTable to LSP layer (as LSP requires such form);

Reviewers: sammccall, jvikstrom

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

Tags: #clang

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

Modified:
    clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
    clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
    clang-tools-extra/trunk/clangd/SemanticHighlighting.h
    clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp

Modified: clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp?rev=365135&r1=365134&r2=365135&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdLSPServer.cpp Thu Jul  4 05:27:21 2019
@@ -82,6 +82,17 @@ CompletionItemKindBitset defaultCompleti
   return Defaults;
 }
 
+// Build a lookup table (HighlightingKind => {TextMate Scopes}), which is sent
+// to the LSP client.
+std::vector<std::vector<std::string>> buildHighlightScopeLookupTable() {
+  std::vector<std::vector<std::string>> LookupTable;
+  // HighlightingKind is using as the index.
+  for (int KindValue = 0; KindValue < (int)HighlightingKind::NumKinds;
+       ++KindValue)
+    LookupTable.push_back({toTextMateScope((HighlightingKind)(KindValue))});
+  return LookupTable;
+}
+
 } // namespace
 
 // MessageHandler dispatches incoming LSP messages.
@@ -414,7 +425,7 @@ void ClangdLSPServer::onInitialize(const
     Result.getObject("capabilities")
         ->insert(
             {"semanticHighlighting",
-             llvm::json::Object{{"scopes", getTextMateScopeLookupTable()}}});
+             llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}});
   Reply(std::move(Result));
 }
 

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp?rev=365135&r1=365134&r2=365135&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp Thu Jul  4 05:27:21 2019
@@ -149,16 +149,17 @@ toSemanticHighlightingInformation(llvm::
   return Lines;
 }
 
-std::vector<std::vector<std::string>> getTextMateScopeLookupTable() {
+llvm::StringRef toTextMateScope(HighlightingKind Kind) {
   // FIXME: Add scopes for C and Objective C.
-  std::map<HighlightingKind, std::vector<std::string>> Scopes = {
-      {HighlightingKind::Variable, {"variable.cpp"}},
-      {HighlightingKind::Function, {"entity.name.function.cpp"}}};
-  std::vector<std::vector<std::string>> NestedScopes(Scopes.size());
-  for (const auto &Scope : Scopes)
-    NestedScopes[static_cast<int>(Scope.first)] = Scope.second;
-
-  return NestedScopes;
+  switch (Kind) {
+  case HighlightingKind::Function:
+    return "entity.name.function.cpp";
+  case HighlightingKind::Variable:
+    return "variable.cpp";
+  case HighlightingKind::NumKinds:
+    llvm_unreachable("must not pass NumKinds to the function");
+  }
+  llvm_unreachable("unhandled HighlightingKind");
 }
 
 } // namespace clangd

Modified: clang-tools-extra/trunk/clangd/SemanticHighlighting.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SemanticHighlighting.h?rev=365135&r1=365134&r2=365135&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/SemanticHighlighting.h (original)
+++ clang-tools-extra/trunk/clangd/SemanticHighlighting.h Thu Jul  4 05:27:21 2019
@@ -25,7 +25,9 @@ namespace clangd {
 
 enum class HighlightingKind {
   Variable = 0,
-  Function = 1,
+  Function,
+
+  NumKinds,
 };
 
 // Contains all information needed for the highlighting a token.
@@ -40,9 +42,9 @@ bool operator==(const HighlightingToken
 // main AST.
 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST);
 
-// Gets the TextMate scopes as a double nested array where the
-// SemanticHighlightKind indexes correctly into this vector.
-std::vector<std::vector<std::string>> getTextMateScopeLookupTable();
+/// Converts a HighlightingKind to a corresponding TextMate scope
+/// (https://manual.macromates.com/en/language_grammars).
+llvm::StringRef toTextMateScope(HighlightingKind Kind);
 
 // Convert to LSP's semantic highlighting information.
 std::vector<SemanticHighlightingInformation>

Modified: 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=365135&r1=365134&r2=365135&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp (original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/AnnotateHighlightings.cpp Thu Jul  4 05:27:21 2019
@@ -12,15 +12,6 @@ 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:




More information about the cfe-commits mailing list