hokein created this revision.
hokein added reviewers: sammccall, jvikstrom.
Herald added subscribers: kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64202

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

Index: clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
@@ -12,15 +12,6 @@
 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:
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -25,7 +25,9 @@
 
 enum class HighlightingKind {
   Variable = 0,
-  Function = 1,
+  Function,
+
+  Last, // placeholder for last value.
 };
 
 // Contains all information needed for the highlighting a token.
@@ -40,9 +42,9 @@
 // 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>
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -149,16 +149,19 @@
   return Lines;
 }
 
-std::vector<std::vector<std::string>> getTextMateScopeLookupTable() {
+llvm::StringRef toTextMateScope(HighlightingKind Kind) {
+  assert(Kind != HighlightingKind::Last &&
+         "must not pass Last to the function");
   // 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";
+  default:
+    break;
+  }
+  llvm_unreachable("unhandled HighlightingKind");
 }
 
 } // namespace clangd
Index: clang-tools-extra/clangd/ClangdLSPServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -82,6 +82,16 @@
   return Defaults;
 }
 
+// Build a lookup table (HighlightingKind => {TextMate Scopes}), which is sent
+// to the LSP client.
+std::vector<std::vector<std::string>> buildTextMateScopeLookupTable() {
+  std::vector<std::vector<std::string>> LookupTable;
+  // HighlightingKind is using as the index.
+  for (int KindValue = 0; KindValue < (int)HighlightingKind::Last; ++KindValue)
+    LookupTable.push_back({toTextMateScope((HighlightingKind)(KindValue))});
+  return LookupTable;
+}
+
 } // namespace
 
 // MessageHandler dispatches incoming LSP messages.
@@ -414,7 +424,7 @@
     Result.getObject("capabilities")
         ->insert(
             {"semanticHighlighting",
-             llvm::json::Object{{"scopes", getTextMateScopeLookupTable()}}});
+             llvm::json::Object{{"scopes", buildTextMateScopeLookupTable()}}});
   Reply(std::move(Result));
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to