diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index 748269d5aef4b..d0e8d139a40e8 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -485,8 +485,8 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, } } - ClangdServerOpts.SemanticHighlighting = - Params.capabilities.SemanticHighlighting; + ClangdServerOpts.TheiaSemanticHighlighting = + Params.capabilities.TheiaSemanticHighlighting; if (Params.rootUri && *Params.rootUri) ClangdServerOpts.WorkspaceRoot = std::string(Params.rootUri->file()); else if (Params.rootPath && !Params.rootPath->empty()) @@ -611,7 +611,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params, }}}}; if (NegotiatedOffsetEncoding) Result["offsetEncoding"] = *NegotiatedOffsetEncoding; - if (Params.capabilities.SemanticHighlighting) + if (Params.capabilities.TheiaSemanticHighlighting) Result.getObject("capabilities") ->insert( {"semanticHighlighting", @@ -1169,8 +1169,8 @@ void ClangdLSPServer::applyConfiguration( reparseOpenedFiles(ModifiedFiles); } -void ClangdLSPServer::publishSemanticHighlighting( - const SemanticHighlightingParams &Params) { +void ClangdLSPServer::publishTheiaSemanticHighlighting( + const TheiaSemanticHighlightingParams &Params) { notify("textDocument/semanticHighlighting", Params); } @@ -1376,12 +1376,12 @@ void ClangdLSPServer::onHighlightingsReady( // LSP allows us to send incremental edits of highlightings. Also need to diff // to remove highlightings from tokens that should no longer have them. std::vector Diffed = diffHighlightings(Highlightings, Old); - SemanticHighlightingParams Notification; + TheiaSemanticHighlightingParams Notification; Notification.TextDocument.uri = URIForFile::canonicalize(File, /*TUPath=*/File); Notification.TextDocument.version = decodeVersion(Version); - Notification.Lines = toSemanticHighlightingInformation(Diffed); - publishSemanticHighlighting(Notification); + Notification.Lines = toTheiaSemanticHighlightingInformation(Diffed); + publishTheiaSemanticHighlighting(Notification); } void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version, diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h index e70b7b56a4f2f..c4e9e5fb679c5 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -135,7 +135,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks { void applyConfiguration(const ConfigurationSettings &Settings); /// Sends a "publishSemanticHighlighting" notification to the LSP client. - void publishSemanticHighlighting(const SemanticHighlightingParams &); + void + publishTheiaSemanticHighlighting(const TheiaSemanticHighlightingParams &); /// Sends a "publishDiagnostics" notification to the LSP client. void publishDiagnostics(const PublishDiagnosticsParams &); diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index 5d2bfa7c8c575..3d68f85b6487d 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -58,9 +58,9 @@ namespace { struct UpdateIndexCallbacks : public ParsingCallbacks { UpdateIndexCallbacks(FileIndex *FIndex, ClangdServer::Callbacks *ServerCallbacks, - bool SemanticHighlighting) + bool TheiaSemanticHighlighting) : FIndex(FIndex), ServerCallbacks(ServerCallbacks), - SemanticHighlighting(SemanticHighlighting) {} + TheiaSemanticHighlighting(TheiaSemanticHighlighting) {} void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx, std::shared_ptr PP, @@ -75,14 +75,14 @@ struct UpdateIndexCallbacks : public ParsingCallbacks { std::vector Diagnostics = AST.getDiagnostics(); std::vector Highlightings; - if (SemanticHighlighting) + if (TheiaSemanticHighlighting) Highlightings = getSemanticHighlightings(AST); if (ServerCallbacks) Publish([&]() { ServerCallbacks->onDiagnosticsReady(Path, AST.version(), std::move(Diagnostics)); - if (SemanticHighlighting) + if (TheiaSemanticHighlighting) ServerCallbacks->onHighlightingsReady(Path, AST.version(), std::move(Highlightings)); }); @@ -103,7 +103,7 @@ struct UpdateIndexCallbacks : public ParsingCallbacks { private: FileIndex *FIndex; ClangdServer::Callbacks *ServerCallbacks; - bool SemanticHighlighting; + bool TheiaSemanticHighlighting; }; } // namespace @@ -112,7 +112,7 @@ ClangdServer::Options ClangdServer::optsForTest() { Opts.UpdateDebounce = DebouncePolicy::fixed(/*zero*/ {}); Opts.StorePreamblesInMemory = true; Opts.AsyncThreadsCount = 4; // Consistent! - Opts.SemanticHighlighting = true; + Opts.TheiaSemanticHighlighting = true; return Opts; } @@ -142,8 +142,8 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB, // critical paths. WorkScheduler( CDB, TUScheduler::Options(Opts), - std::make_unique(DynamicIdx.get(), Callbacks, - Opts.SemanticHighlighting)) { + std::make_unique( + DynamicIdx.get(), Callbacks, Opts.TheiaSemanticHighlighting)) { // Adds an index to the stack, at higher priority than existing indexes. auto AddIndex = [&](SymbolIndex *Idx) { if (this->Index != nullptr) { diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h index 4c3fe56dd7e2f..ae3dd8a065d8a 100644 --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -143,7 +143,7 @@ class ClangdServer { std::vector QueryDriverGlobs; /// Enable semantic highlighting features. - bool SemanticHighlighting = false; + bool TheiaSemanticHighlighting = false; /// Returns true if the tweak should be enabled. std::function TweakFilter = [](const Tweak &T) { diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp index 56ddbfb446f77..462aebc4b0465 100644 --- a/clang-tools-extra/clangd/Protocol.cpp +++ b/clang-tools-extra/clangd/Protocol.cpp @@ -295,7 +295,7 @@ bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R) { TextDocument->getObject("semanticHighlightingCapabilities")) { if (auto SemanticHighlightingSupport = SemanticHighlighting->getBoolean("semanticHighlighting")) - R.SemanticHighlighting = *SemanticHighlightingSupport; + R.TheiaSemanticHighlighting = *SemanticHighlightingSupport; } if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) { if (auto CategorySupport = Diagnostics->getBoolean("categorySupport")) @@ -1131,18 +1131,19 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) { return OS << toString(Enc); } -bool operator==(const SemanticHighlightingInformation &Lhs, - const SemanticHighlightingInformation &Rhs) { +bool operator==(const TheiaSemanticHighlightingInformation &Lhs, + const TheiaSemanticHighlightingInformation &Rhs) { return Lhs.Line == Rhs.Line && Lhs.Tokens == Rhs.Tokens; } -llvm::json::Value toJSON(const SemanticHighlightingInformation &Highlighting) { +llvm::json::Value +toJSON(const TheiaSemanticHighlightingInformation &Highlighting) { return llvm::json::Object{{"line", Highlighting.Line}, {"tokens", Highlighting.Tokens}, {"isInactive", Highlighting.IsInactive}}; } -llvm::json::Value toJSON(const SemanticHighlightingParams &Highlighting) { +llvm::json::Value toJSON(const TheiaSemanticHighlightingParams &Highlighting) { return llvm::json::Object{ {"textDocument", Highlighting.TextDocument}, {"lines", std::move(Highlighting.Lines)}, diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h index 6ab6bcc920b22..d08e546e3ffb2 100644 --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -433,9 +433,11 @@ struct ClientCapabilities { /// textDocument.codeAction.codeActionLiteralSupport. bool CodeActionStructure = false; - /// Client supports semantic highlighting. + /// Client supports Theia semantic highlighting extension. + /// https://github.com/microsoft/vscode-languageserver-node/pull/367 /// textDocument.semanticHighlightingCapabilities.semanticHighlighting - bool SemanticHighlighting = false; + /// FIXME: drop this support once clients support LSP 3.16 Semantic Tokens. + bool TheiaSemanticHighlighting = false; /// Supported encodings for LSP character offsets. (clangd extension). llvm::Optional> offsetEncoding; @@ -1342,7 +1344,7 @@ llvm::json::Value toJSON(const FileStatus &FStatus); /// Represents a semantic highlighting information that has to be applied on a /// specific line of the text document. -struct SemanticHighlightingInformation { +struct TheiaSemanticHighlightingInformation { /// The line these highlightings belong to. int Line = 0; /// The base64 encoded string of highlighting tokens. @@ -1353,18 +1355,19 @@ struct SemanticHighlightingInformation { /// clients should combine line style and token style if possible. bool IsInactive = false; }; -bool operator==(const SemanticHighlightingInformation &Lhs, - const SemanticHighlightingInformation &Rhs); -llvm::json::Value toJSON(const SemanticHighlightingInformation &Highlighting); +bool operator==(const TheiaSemanticHighlightingInformation &Lhs, + const TheiaSemanticHighlightingInformation &Rhs); +llvm::json::Value +toJSON(const TheiaSemanticHighlightingInformation &Highlighting); /// Parameters for the semantic highlighting (server-side) push notification. -struct SemanticHighlightingParams { +struct TheiaSemanticHighlightingParams { /// The textdocument these highlightings belong to. VersionedTextDocumentIdentifier TextDocument; /// The lines of highlightings that should be sent. - std::vector Lines; + std::vector Lines; }; -llvm::json::Value toJSON(const SemanticHighlightingParams &Highlighting); +llvm::json::Value toJSON(const TheiaSemanticHighlightingParams &Highlighting); struct SelectionRangeParams { /// The text document. diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index d5c51ebff5e1e..b69f9e8f2710e 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -445,14 +445,15 @@ bool operator==(const LineHighlightings &L, const LineHighlightings &R) { return std::tie(L.Line, L.Tokens) == std::tie(R.Line, R.Tokens); } -std::vector -toSemanticHighlightingInformation(llvm::ArrayRef Tokens) { +std::vector +toTheiaSemanticHighlightingInformation( + llvm::ArrayRef Tokens) { if (Tokens.size() == 0) return {}; // FIXME: Tokens might be multiple lines long (block comments) in this case // this needs to add multiple lines for those tokens. - std::vector Lines; + std::vector Lines; Lines.reserve(Tokens.size()); for (const auto &Line : Tokens) { llvm::SmallVector LineByteTokens; diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h b/clang-tools-extra/clangd/SemanticHighlighting.h index cf4a51a341e71..31a97b81d6c24 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.h +++ b/clang-tools-extra/clangd/SemanticHighlighting.h @@ -80,8 +80,9 @@ std::vector getSemanticHighlightings(ParsedAST &AST); llvm::StringRef toTextMateScope(HighlightingKind Kind); /// Convert to LSP's semantic highlighting information. -std::vector -toSemanticHighlightingInformation(llvm::ArrayRef Tokens); +std::vector +toTheiaSemanticHighlightingInformation( + llvm::ArrayRef Tokens); /// Return a line-by-line diff between two highlightings. /// - if the tokens on a line are the same in both highlightings, this line is diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 8672a043f1862..6a7f700e1f491 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -720,7 +720,7 @@ TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) { ASSERT_EQ(Counter.Count, 1); } -TEST(SemanticHighlighting, toSemanticHighlightingInformation) { +TEST(SemanticHighlighting, toTheiaSemanticHighlightingInformation) { auto CreatePosition = [](int Line, int Character) -> Position { Position Pos; Pos.line = Line; @@ -739,9 +739,9 @@ TEST(SemanticHighlighting, toSemanticHighlightingInformation) { {{HighlightingKind::Variable, Range{CreatePosition(1, 1), CreatePosition(1, 5)}}}, /* IsInactive = */ true}}; - std::vector ActualResults = - toSemanticHighlightingInformation(Tokens); - std::vector ExpectedResults = { + std::vector ActualResults = + toTheiaSemanticHighlightingInformation(Tokens); + std::vector ExpectedResults = { {3, "AAAACAAEAAAAAAAEAAMAAw=="}, {1, "AAAAAQAEAAA="}}; EXPECT_EQ(ActualResults, ExpectedResults); }