Skip to content

Commit edf6a19

Browse files
committed
[clangd] Rename theia-derived semantic highlighting protocol. NFC
This feature is being added to LSP with a different (request-response) protocol in 3.16, so this should avoid some confusion.
1 parent 2136d17 commit edf6a19

File tree

9 files changed

+48
-41
lines changed

9 files changed

+48
-41
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
485485
}
486486
}
487487

488-
ClangdServerOpts.SemanticHighlighting =
489-
Params.capabilities.SemanticHighlighting;
488+
ClangdServerOpts.TheiaSemanticHighlighting =
489+
Params.capabilities.TheiaSemanticHighlighting;
490490
if (Params.rootUri && *Params.rootUri)
491491
ClangdServerOpts.WorkspaceRoot = std::string(Params.rootUri->file());
492492
else if (Params.rootPath && !Params.rootPath->empty())
@@ -611,7 +611,7 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
611611
}}}};
612612
if (NegotiatedOffsetEncoding)
613613
Result["offsetEncoding"] = *NegotiatedOffsetEncoding;
614-
if (Params.capabilities.SemanticHighlighting)
614+
if (Params.capabilities.TheiaSemanticHighlighting)
615615
Result.getObject("capabilities")
616616
->insert(
617617
{"semanticHighlighting",
@@ -1169,8 +1169,8 @@ void ClangdLSPServer::applyConfiguration(
11691169
reparseOpenedFiles(ModifiedFiles);
11701170
}
11711171

1172-
void ClangdLSPServer::publishSemanticHighlighting(
1173-
const SemanticHighlightingParams &Params) {
1172+
void ClangdLSPServer::publishTheiaSemanticHighlighting(
1173+
const TheiaSemanticHighlightingParams &Params) {
11741174
notify("textDocument/semanticHighlighting", Params);
11751175
}
11761176

@@ -1376,12 +1376,12 @@ void ClangdLSPServer::onHighlightingsReady(
13761376
// LSP allows us to send incremental edits of highlightings. Also need to diff
13771377
// to remove highlightings from tokens that should no longer have them.
13781378
std::vector<LineHighlightings> Diffed = diffHighlightings(Highlightings, Old);
1379-
SemanticHighlightingParams Notification;
1379+
TheiaSemanticHighlightingParams Notification;
13801380
Notification.TextDocument.uri =
13811381
URIForFile::canonicalize(File, /*TUPath=*/File);
13821382
Notification.TextDocument.version = decodeVersion(Version);
1383-
Notification.Lines = toSemanticHighlightingInformation(Diffed);
1384-
publishSemanticHighlighting(Notification);
1383+
Notification.Lines = toTheiaSemanticHighlightingInformation(Diffed);
1384+
publishTheiaSemanticHighlighting(Notification);
13851385
}
13861386

13871387
void ClangdLSPServer::onDiagnosticsReady(PathRef File, llvm::StringRef Version,

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
135135
void applyConfiguration(const ConfigurationSettings &Settings);
136136

137137
/// Sends a "publishSemanticHighlighting" notification to the LSP client.
138-
void publishSemanticHighlighting(const SemanticHighlightingParams &);
138+
void
139+
publishTheiaSemanticHighlighting(const TheiaSemanticHighlightingParams &);
139140

140141
/// Sends a "publishDiagnostics" notification to the LSP client.
141142
void publishDiagnostics(const PublishDiagnosticsParams &);

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ namespace {
5858
struct UpdateIndexCallbacks : public ParsingCallbacks {
5959
UpdateIndexCallbacks(FileIndex *FIndex,
6060
ClangdServer::Callbacks *ServerCallbacks,
61-
bool SemanticHighlighting)
61+
bool TheiaSemanticHighlighting)
6262
: FIndex(FIndex), ServerCallbacks(ServerCallbacks),
63-
SemanticHighlighting(SemanticHighlighting) {}
63+
TheiaSemanticHighlighting(TheiaSemanticHighlighting) {}
6464

6565
void onPreambleAST(PathRef Path, llvm::StringRef Version, ASTContext &Ctx,
6666
std::shared_ptr<clang::Preprocessor> PP,
@@ -75,14 +75,14 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
7575

7676
std::vector<Diag> Diagnostics = AST.getDiagnostics();
7777
std::vector<HighlightingToken> Highlightings;
78-
if (SemanticHighlighting)
78+
if (TheiaSemanticHighlighting)
7979
Highlightings = getSemanticHighlightings(AST);
8080

8181
if (ServerCallbacks)
8282
Publish([&]() {
8383
ServerCallbacks->onDiagnosticsReady(Path, AST.version(),
8484
std::move(Diagnostics));
85-
if (SemanticHighlighting)
85+
if (TheiaSemanticHighlighting)
8686
ServerCallbacks->onHighlightingsReady(Path, AST.version(),
8787
std::move(Highlightings));
8888
});
@@ -103,7 +103,7 @@ struct UpdateIndexCallbacks : public ParsingCallbacks {
103103
private:
104104
FileIndex *FIndex;
105105
ClangdServer::Callbacks *ServerCallbacks;
106-
bool SemanticHighlighting;
106+
bool TheiaSemanticHighlighting;
107107
};
108108
} // namespace
109109

@@ -112,7 +112,7 @@ ClangdServer::Options ClangdServer::optsForTest() {
112112
Opts.UpdateDebounce = DebouncePolicy::fixed(/*zero*/ {});
113113
Opts.StorePreamblesInMemory = true;
114114
Opts.AsyncThreadsCount = 4; // Consistent!
115-
Opts.SemanticHighlighting = true;
115+
Opts.TheiaSemanticHighlighting = true;
116116
return Opts;
117117
}
118118

@@ -142,8 +142,8 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
142142
// critical paths.
143143
WorkScheduler(
144144
CDB, TUScheduler::Options(Opts),
145-
std::make_unique<UpdateIndexCallbacks>(DynamicIdx.get(), Callbacks,
146-
Opts.SemanticHighlighting)) {
145+
std::make_unique<UpdateIndexCallbacks>(
146+
DynamicIdx.get(), Callbacks, Opts.TheiaSemanticHighlighting)) {
147147
// Adds an index to the stack, at higher priority than existing indexes.
148148
auto AddIndex = [&](SymbolIndex *Idx) {
149149
if (this->Index != nullptr) {

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ClangdServer {
143143
std::vector<std::string> QueryDriverGlobs;
144144

145145
/// Enable semantic highlighting features.
146-
bool SemanticHighlighting = false;
146+
bool TheiaSemanticHighlighting = false;
147147

148148
/// Returns true if the tweak should be enabled.
149149
std::function<bool(const Tweak &)> TweakFilter = [](const Tweak &T) {

clang-tools-extra/clangd/Protocol.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R) {
295295
TextDocument->getObject("semanticHighlightingCapabilities")) {
296296
if (auto SemanticHighlightingSupport =
297297
SemanticHighlighting->getBoolean("semanticHighlighting"))
298-
R.SemanticHighlighting = *SemanticHighlightingSupport;
298+
R.TheiaSemanticHighlighting = *SemanticHighlightingSupport;
299299
}
300300
if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
301301
if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
@@ -1131,18 +1131,19 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
11311131
return OS << toString(Enc);
11321132
}
11331133

1134-
bool operator==(const SemanticHighlightingInformation &Lhs,
1135-
const SemanticHighlightingInformation &Rhs) {
1134+
bool operator==(const TheiaSemanticHighlightingInformation &Lhs,
1135+
const TheiaSemanticHighlightingInformation &Rhs) {
11361136
return Lhs.Line == Rhs.Line && Lhs.Tokens == Rhs.Tokens;
11371137
}
11381138

1139-
llvm::json::Value toJSON(const SemanticHighlightingInformation &Highlighting) {
1139+
llvm::json::Value
1140+
toJSON(const TheiaSemanticHighlightingInformation &Highlighting) {
11401141
return llvm::json::Object{{"line", Highlighting.Line},
11411142
{"tokens", Highlighting.Tokens},
11421143
{"isInactive", Highlighting.IsInactive}};
11431144
}
11441145

1145-
llvm::json::Value toJSON(const SemanticHighlightingParams &Highlighting) {
1146+
llvm::json::Value toJSON(const TheiaSemanticHighlightingParams &Highlighting) {
11461147
return llvm::json::Object{
11471148
{"textDocument", Highlighting.TextDocument},
11481149
{"lines", std::move(Highlighting.Lines)},

clang-tools-extra/clangd/Protocol.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,11 @@ struct ClientCapabilities {
433433
/// textDocument.codeAction.codeActionLiteralSupport.
434434
bool CodeActionStructure = false;
435435

436-
/// Client supports semantic highlighting.
436+
/// Client supports Theia semantic highlighting extension.
437+
/// https://github.com/microsoft/vscode-languageserver-node/pull/367
437438
/// textDocument.semanticHighlightingCapabilities.semanticHighlighting
438-
bool SemanticHighlighting = false;
439+
/// FIXME: drop this support once clients support LSP 3.16 Semantic Tokens.
440+
bool TheiaSemanticHighlighting = false;
439441

440442
/// Supported encodings for LSP character offsets. (clangd extension).
441443
llvm::Optional<std::vector<OffsetEncoding>> offsetEncoding;
@@ -1342,7 +1344,7 @@ llvm::json::Value toJSON(const FileStatus &FStatus);
13421344

13431345
/// Represents a semantic highlighting information that has to be applied on a
13441346
/// specific line of the text document.
1345-
struct SemanticHighlightingInformation {
1347+
struct TheiaSemanticHighlightingInformation {
13461348
/// The line these highlightings belong to.
13471349
int Line = 0;
13481350
/// The base64 encoded string of highlighting tokens.
@@ -1353,18 +1355,19 @@ struct SemanticHighlightingInformation {
13531355
/// clients should combine line style and token style if possible.
13541356
bool IsInactive = false;
13551357
};
1356-
bool operator==(const SemanticHighlightingInformation &Lhs,
1357-
const SemanticHighlightingInformation &Rhs);
1358-
llvm::json::Value toJSON(const SemanticHighlightingInformation &Highlighting);
1358+
bool operator==(const TheiaSemanticHighlightingInformation &Lhs,
1359+
const TheiaSemanticHighlightingInformation &Rhs);
1360+
llvm::json::Value
1361+
toJSON(const TheiaSemanticHighlightingInformation &Highlighting);
13591362

13601363
/// Parameters for the semantic highlighting (server-side) push notification.
1361-
struct SemanticHighlightingParams {
1364+
struct TheiaSemanticHighlightingParams {
13621365
/// The textdocument these highlightings belong to.
13631366
VersionedTextDocumentIdentifier TextDocument;
13641367
/// The lines of highlightings that should be sent.
1365-
std::vector<SemanticHighlightingInformation> Lines;
1368+
std::vector<TheiaSemanticHighlightingInformation> Lines;
13661369
};
1367-
llvm::json::Value toJSON(const SemanticHighlightingParams &Highlighting);
1370+
llvm::json::Value toJSON(const TheiaSemanticHighlightingParams &Highlighting);
13681371

13691372
struct SelectionRangeParams {
13701373
/// The text document.

clang-tools-extra/clangd/SemanticHighlighting.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,15 @@ bool operator==(const LineHighlightings &L, const LineHighlightings &R) {
445445
return std::tie(L.Line, L.Tokens) == std::tie(R.Line, R.Tokens);
446446
}
447447

448-
std::vector<SemanticHighlightingInformation>
449-
toSemanticHighlightingInformation(llvm::ArrayRef<LineHighlightings> Tokens) {
448+
std::vector<TheiaSemanticHighlightingInformation>
449+
toTheiaSemanticHighlightingInformation(
450+
llvm::ArrayRef<LineHighlightings> Tokens) {
450451
if (Tokens.size() == 0)
451452
return {};
452453

453454
// FIXME: Tokens might be multiple lines long (block comments) in this case
454455
// this needs to add multiple lines for those tokens.
455-
std::vector<SemanticHighlightingInformation> Lines;
456+
std::vector<TheiaSemanticHighlightingInformation> Lines;
456457
Lines.reserve(Tokens.size());
457458
for (const auto &Line : Tokens) {
458459
llvm::SmallVector<char, 128> LineByteTokens;

clang-tools-extra/clangd/SemanticHighlighting.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST);
8080
llvm::StringRef toTextMateScope(HighlightingKind Kind);
8181

8282
/// Convert to LSP's semantic highlighting information.
83-
std::vector<SemanticHighlightingInformation>
84-
toSemanticHighlightingInformation(llvm::ArrayRef<LineHighlightings> Tokens);
83+
std::vector<TheiaSemanticHighlightingInformation>
84+
toTheiaSemanticHighlightingInformation(
85+
llvm::ArrayRef<LineHighlightings> Tokens);
8586

8687
/// Return a line-by-line diff between two highlightings.
8788
/// - if the tokens on a line are the same in both highlightings, this line is

clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ TEST(SemanticHighlighting, GeneratesHighlightsWhenFileChange) {
720720
ASSERT_EQ(Counter.Count, 1);
721721
}
722722

723-
TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
723+
TEST(SemanticHighlighting, toTheiaSemanticHighlightingInformation) {
724724
auto CreatePosition = [](int Line, int Character) -> Position {
725725
Position Pos;
726726
Pos.line = Line;
@@ -739,9 +739,9 @@ TEST(SemanticHighlighting, toSemanticHighlightingInformation) {
739739
{{HighlightingKind::Variable,
740740
Range{CreatePosition(1, 1), CreatePosition(1, 5)}}},
741741
/* IsInactive = */ true}};
742-
std::vector<SemanticHighlightingInformation> ActualResults =
743-
toSemanticHighlightingInformation(Tokens);
744-
std::vector<SemanticHighlightingInformation> ExpectedResults = {
742+
std::vector<TheiaSemanticHighlightingInformation> ActualResults =
743+
toTheiaSemanticHighlightingInformation(Tokens);
744+
std::vector<TheiaSemanticHighlightingInformation> ExpectedResults = {
745745
{3, "AAAACAAEAAAAAAAEAAMAAw=="}, {1, "AAAAAQAEAAA="}};
746746
EXPECT_EQ(ActualResults, ExpectedResults);
747747
}

0 commit comments

Comments
 (0)