summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2019-07-04 12:27:21 +0000
committerHaojian Wu <hokein@google.com>2019-07-04 12:27:21 +0000
commit1ca2ee4dc11437b1b2f183190b1bf52d9f0e1151 (patch)
treeed9bc1fe7cd9ac4c26b0ea01049f69d4bd5a7197
parent5bec85a34c27c8b49ca780b8cdb9b2140a96f8cc (diff)
downloadbcm5719-llvm-1ca2ee4dc11437b1b2f183190b1bf52d9f0e1151.tar.gz
bcm5719-llvm-1ca2ee4dc11437b1b2f183190b1bf52d9f0e1151.zip
[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 llvm-svn: 365135
-rw-r--r--clang-tools-extra/clangd/ClangdLSPServer.cpp13
-rw-r--r--clang-tools-extra/clangd/SemanticHighlighting.cpp19
-rw-r--r--clang-tools-extra/clangd/SemanticHighlighting.h10
-rw-r--r--clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp9
4 files changed, 28 insertions, 23 deletions
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp
index df2e3c5da69..f37aa1d2b58 100644
--- a/clang-tools-extra/clangd/ClangdLSPServer.cpp
+++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp
@@ -82,6 +82,17 @@ CompletionItemKindBitset defaultCompletionItemKinds() {
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 InitializeParams &Params,
Result.getObject("capabilities")
->insert(
{"semanticHighlighting",
- llvm::json::Object{{"scopes", getTextMateScopeLookupTable()}}});
+ llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}});
Reply(std::move(Result));
}
diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp
index a4517af9fb6..e3c2c7fa8bb 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -149,16 +149,17 @@ toSemanticHighlightingInformation(llvm::ArrayRef<HighlightingToken> Tokens) {
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
diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h b/clang-tools-extra/clangd/SemanticHighlighting.h
index 345c29ecd5c..385db8d2268 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.h
+++ b/clang-tools-extra/clangd/SemanticHighlighting.h
@@ -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 &Lhs, const HighlightingToken &Rhs);
// 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>
diff --git a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
index 90119b697c5..1a3f022c1d9 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AnnotateHighlightings.cpp
@@ -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:
OpenPOWER on IntegriCloud