clang-tools 17.0.0git
Protocol.cpp
Go to the documentation of this file.
1//===--- Protocol.cpp - Language Server Protocol Implementation -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the serialization code for the LSP structs.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Protocol.h"
14#include "URI.h"
15#include "support/Logger.h"
16#include "clang/Basic/LLVM.h"
17#include "clang/Index/IndexSymbol.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/StringSwitch.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/JSON.h"
22#include "llvm/Support/Path.h"
23#include "llvm/Support/raw_ostream.h"
24
25namespace clang {
26namespace clangd {
27namespace {
28
29// Helper that doesn't treat `null` and absent fields as failures.
30template <typename T>
31bool mapOptOrNull(const llvm::json::Value &Params, llvm::StringLiteral Prop,
32 T &Out, llvm::json::Path P) {
33 auto *O = Params.getAsObject();
34 assert(O);
35 auto *V = O->get(Prop);
36 // Field is missing or null.
37 if (!V || V->getAsNull())
38 return true;
39 return fromJSON(*V, Out, P.field(Prop));
40}
41} // namespace
42
43char LSPError::ID;
44
45URIForFile URIForFile::canonicalize(llvm::StringRef AbsPath,
46 llvm::StringRef TUPath) {
47 assert(llvm::sys::path::is_absolute(AbsPath) && "the path is relative");
48 auto Resolved = URI::resolvePath(AbsPath, TUPath);
49 if (!Resolved) {
50 elog("URIForFile: failed to resolve path {0} with TU path {1}: "
51 "{2}.\nUsing unresolved path.",
52 AbsPath, TUPath, Resolved.takeError());
53 return URIForFile(std::string(AbsPath));
54 }
55 return URIForFile(std::move(*Resolved));
56}
57
58llvm::Expected<URIForFile> URIForFile::fromURI(const URI &U,
59 llvm::StringRef HintPath) {
60 auto Resolved = URI::resolve(U, HintPath);
61 if (!Resolved)
62 return Resolved.takeError();
63 return URIForFile(std::move(*Resolved));
64}
65
66bool fromJSON(const llvm::json::Value &E, URIForFile &R, llvm::json::Path P) {
67 if (auto S = E.getAsString()) {
68 auto Parsed = URI::parse(*S);
69 if (!Parsed) {
70 consumeError(Parsed.takeError());
71 P.report("failed to parse URI");
72 return false;
73 }
74 if (Parsed->scheme() != "file" && Parsed->scheme() != "test") {
75 P.report("clangd only supports 'file' URI scheme for workspace files");
76 return false;
77 }
78 // "file" and "test" schemes do not require hint path.
79 auto U = URIForFile::fromURI(*Parsed, /*HintPath=*/"");
80 if (!U) {
81 P.report("unresolvable URI");
82 consumeError(U.takeError());
83 return false;
84 }
85 R = std::move(*U);
86 return true;
87 }
88 return false;
89}
90
91llvm::json::Value toJSON(const URIForFile &U) { return U.uri(); }
92
93llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const URIForFile &U) {
94 return OS << U.uri();
95}
96
97llvm::json::Value toJSON(const TextDocumentIdentifier &R) {
98 return llvm::json::Object{{"uri", R.uri}};
99}
100
101bool fromJSON(const llvm::json::Value &Params, TextDocumentIdentifier &R,
102 llvm::json::Path P) {
103 llvm::json::ObjectMapper O(Params, P);
104 return O && O.map("uri", R.uri);
105}
106
107llvm::json::Value toJSON(const VersionedTextDocumentIdentifier &R) {
108 auto Result = toJSON(static_cast<const TextDocumentIdentifier &>(R));
109 Result.getAsObject()->try_emplace("version", R.version);
110 return Result;
111}
112
113bool fromJSON(const llvm::json::Value &Params,
114 VersionedTextDocumentIdentifier &R, llvm::json::Path P) {
115 llvm::json::ObjectMapper O(Params, P);
116 return fromJSON(Params, static_cast<TextDocumentIdentifier &>(R), P) && O &&
117 O.map("version", R.version);
118}
119
120bool fromJSON(const llvm::json::Value &Params, Position &R,
121 llvm::json::Path P) {
122 llvm::json::ObjectMapper O(Params, P);
123 return O && O.map("line", R.line) && O.map("character", R.character);
124}
125
126llvm::json::Value toJSON(const Position &P) {
127 return llvm::json::Object{
128 {"line", P.line},
129 {"character", P.character},
130 };
131}
132
133llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &P) {
134 return OS << P.line << ':' << P.character;
135}
136
137bool fromJSON(const llvm::json::Value &Params, Range &R, llvm::json::Path P) {
138 llvm::json::ObjectMapper O(Params, P);
139 return O && O.map("start", R.start) && O.map("end", R.end);
140}
141
142llvm::json::Value toJSON(const Range &P) {
143 return llvm::json::Object{
144 {"start", P.start},
145 {"end", P.end},
146 };
147}
148
149llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Range &R) {
150 return OS << R.start << '-' << R.end;
151}
152
153llvm::json::Value toJSON(const Location &P) {
154 return llvm::json::Object{
155 {"uri", P.uri},
156 {"range", P.range},
157 };
158}
159
160llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Location &L) {
161 return OS << L.range << '@' << L.uri;
162}
163
164llvm::json::Value toJSON(const ReferenceLocation &P) {
165 llvm::json::Object Result{
166 {"uri", P.uri},
167 {"range", P.range},
168 };
169 if (P.containerName)
170 Result.insert({"containerName", P.containerName});
171 return Result;
172}
173
174llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
175 const ReferenceLocation &L) {
176 return OS << L.range << '@' << L.uri << " (container: " << L.containerName
177 << ")";
178}
179
180bool fromJSON(const llvm::json::Value &Params, TextDocumentItem &R,
181 llvm::json::Path P) {
182 llvm::json::ObjectMapper O(Params, P);
183 return O && O.map("uri", R.uri) && O.map("languageId", R.languageId) &&
184 O.map("version", R.version) && O.map("text", R.text);
185}
186
187bool fromJSON(const llvm::json::Value &Params, TextEdit &R,
188 llvm::json::Path P) {
189 llvm::json::ObjectMapper O(Params, P);
190 return O && O.map("range", R.range) && O.map("newText", R.newText);
191}
192
193llvm::json::Value toJSON(const TextEdit &P) {
194 return llvm::json::Object{
195 {"range", P.range},
196 {"newText", P.newText},
197 };
198}
199
200llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TextEdit &TE) {
201 OS << TE.range << " => \"";
202 llvm::printEscapedString(TE.newText, OS);
203 return OS << '"';
204}
205
206bool fromJSON(const llvm::json::Value &E, TraceLevel &Out, llvm::json::Path P) {
207 if (auto S = E.getAsString()) {
208 if (*S == "off") {
210 return true;
211 }
212 if (*S == "messages") {
214 return true;
215 }
216 if (*S == "verbose") {
218 return true;
219 }
220 }
221 return false;
222}
223
224bool fromJSON(const llvm::json::Value &E, SymbolKind &Out, llvm::json::Path P) {
225 if (auto T = E.getAsInteger()) {
226 if (*T < static_cast<int>(SymbolKind::File) ||
227 *T > static_cast<int>(SymbolKind::TypeParameter))
228 return false;
229 Out = static_cast<SymbolKind>(*T);
230 return true;
231 }
232 return false;
233}
234
235bool fromJSON(const llvm::json::Value &E, SymbolKindBitset &Out,
236 llvm::json::Path P) {
237 if (auto *A = E.getAsArray()) {
238 for (size_t I = 0; I < A->size(); ++I) {
239 SymbolKind KindOut;
240 if (fromJSON((*A)[I], KindOut, P.index(I)))
241 Out.set(size_t(KindOut));
242 }
243 return true;
244 }
245 return false;
246}
247
249 SymbolKindBitset &SupportedSymbolKinds) {
250 auto KindVal = static_cast<size_t>(Kind);
251 if (KindVal >= SymbolKindMin && KindVal <= SupportedSymbolKinds.size() &&
252 SupportedSymbolKinds[KindVal])
253 return Kind;
254
255 switch (Kind) {
256 // Provide some fall backs for common kinds that are close enough.
257 case SymbolKind::Struct:
258 return SymbolKind::Class;
259 case SymbolKind::EnumMember:
260 return SymbolKind::Enum;
261 default:
262 return SymbolKind::String;
263 }
264}
265
267 switch (Kind) {
268 case index::SymbolKind::Unknown:
269 return SymbolKind::Variable;
270 case index::SymbolKind::Module:
271 return SymbolKind::Module;
272 case index::SymbolKind::Namespace:
273 return SymbolKind::Namespace;
274 case index::SymbolKind::NamespaceAlias:
275 return SymbolKind::Namespace;
276 case index::SymbolKind::Macro:
277 return SymbolKind::String;
278 case index::SymbolKind::Enum:
279 return SymbolKind::Enum;
280 case index::SymbolKind::Struct:
281 return SymbolKind::Struct;
282 case index::SymbolKind::Class:
283 return SymbolKind::Class;
284 case index::SymbolKind::Protocol:
285 return SymbolKind::Interface;
286 case index::SymbolKind::Extension:
287 return SymbolKind::Interface;
288 case index::SymbolKind::Union:
289 return SymbolKind::Class;
290 case index::SymbolKind::TypeAlias:
291 return SymbolKind::Class;
292 case index::SymbolKind::Function:
293 return SymbolKind::Function;
294 case index::SymbolKind::Variable:
295 return SymbolKind::Variable;
296 case index::SymbolKind::Field:
297 return SymbolKind::Field;
298 case index::SymbolKind::EnumConstant:
299 return SymbolKind::EnumMember;
300 case index::SymbolKind::InstanceMethod:
301 case index::SymbolKind::ClassMethod:
302 case index::SymbolKind::StaticMethod:
303 return SymbolKind::Method;
304 case index::SymbolKind::InstanceProperty:
305 case index::SymbolKind::ClassProperty:
306 case index::SymbolKind::StaticProperty:
307 return SymbolKind::Property;
308 case index::SymbolKind::Constructor:
309 case index::SymbolKind::Destructor:
310 return SymbolKind::Constructor;
311 case index::SymbolKind::ConversionFunction:
312 return SymbolKind::Function;
313 case index::SymbolKind::Parameter:
314 case index::SymbolKind::NonTypeTemplateParm:
315 return SymbolKind::Variable;
316 case index::SymbolKind::Using:
317 return SymbolKind::Namespace;
318 case index::SymbolKind::TemplateTemplateParm:
319 case index::SymbolKind::TemplateTypeParm:
320 return SymbolKind::TypeParameter;
321 case index::SymbolKind::Concept:
322 return SymbolKind::Interface;
323 }
324 llvm_unreachable("invalid symbol kind");
325}
326
327bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R,
328 llvm::json::Path P) {
329 const llvm::json::Object *O = Params.getAsObject();
330 if (!O) {
331 P.report("expected object");
332 return false;
333 }
334 if (auto *TextDocument = O->getObject("textDocument")) {
335 if (auto *SemanticHighlighting =
336 TextDocument->getObject("semanticHighlightingCapabilities")) {
337 if (auto SemanticHighlightingSupport =
338 SemanticHighlighting->getBoolean("semanticHighlighting"))
339 R.TheiaSemanticHighlighting = *SemanticHighlightingSupport;
340 }
341 if (TextDocument->getObject("semanticTokens"))
342 R.SemanticTokens = true;
343 if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
344 if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
345 R.DiagnosticCategory = *CategorySupport;
346 if (auto CodeActions = Diagnostics->getBoolean("codeActionsInline"))
347 R.DiagnosticFixes = *CodeActions;
348 if (auto RelatedInfo = Diagnostics->getBoolean("relatedInformation"))
349 R.DiagnosticRelatedInformation = *RelatedInfo;
350 }
351 if (auto *References = TextDocument->getObject("references"))
352 if (auto ContainerSupport = References->getBoolean("container"))
353 R.ReferenceContainer = *ContainerSupport;
354 if (auto *Completion = TextDocument->getObject("completion")) {
355 if (auto *Item = Completion->getObject("completionItem")) {
356 if (auto SnippetSupport = Item->getBoolean("snippetSupport"))
357 R.CompletionSnippets = *SnippetSupport;
358 if (const auto *DocumentationFormat =
359 Item->getArray("documentationFormat")) {
360 for (const auto &Format : *DocumentationFormat) {
361 if (fromJSON(Format, R.CompletionDocumentationFormat, P))
362 break;
363 }
364 }
365 }
366 if (auto *ItemKind = Completion->getObject("completionItemKind")) {
367 if (auto *ValueSet = ItemKind->get("valueSet")) {
368 R.CompletionItemKinds.emplace();
369 if (!fromJSON(*ValueSet, *R.CompletionItemKinds,
370 P.field("textDocument")
371 .field("completion")
372 .field("completionItemKind")
373 .field("valueSet")))
374 return false;
375 }
376 }
377 if (auto EditsNearCursor = Completion->getBoolean("editsNearCursor"))
378 R.CompletionFixes = *EditsNearCursor;
379 }
380 if (auto *CodeAction = TextDocument->getObject("codeAction")) {
381 if (CodeAction->getObject("codeActionLiteralSupport"))
382 R.CodeActionStructure = true;
383 }
384 if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) {
385 if (auto HierarchicalSupport =
386 DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport"))
387 R.HierarchicalDocumentSymbol = *HierarchicalSupport;
388 }
389 if (auto *Hover = TextDocument->getObject("hover")) {
390 if (auto *ContentFormat = Hover->getArray("contentFormat")) {
391 for (const auto &Format : *ContentFormat) {
392 if (fromJSON(Format, R.HoverContentFormat, P))
393 break;
394 }
395 }
396 }
397 if (auto *Help = TextDocument->getObject("signatureHelp")) {
398 R.HasSignatureHelp = true;
399 if (auto *Info = Help->getObject("signatureInformation")) {
400 if (auto *Parameter = Info->getObject("parameterInformation")) {
401 if (auto OffsetSupport = Parameter->getBoolean("labelOffsetSupport"))
402 R.OffsetsInSignatureHelp = *OffsetSupport;
403 }
404 if (const auto *DocumentationFormat =
405 Info->getArray("documentationFormat")) {
406 for (const auto &Format : *DocumentationFormat) {
408 break;
409 }
410 }
411 }
412 }
413 if (auto *Folding = TextDocument->getObject("foldingRange")) {
414 if (auto LineFolding = Folding->getBoolean("lineFoldingOnly"))
415 R.LineFoldingOnly = *LineFolding;
416 }
417 if (auto *Rename = TextDocument->getObject("rename")) {
418 if (auto RenameSupport = Rename->getBoolean("prepareSupport"))
419 R.RenamePrepareSupport = *RenameSupport;
420 }
421 }
422 if (auto *Workspace = O->getObject("workspace")) {
423 if (auto *Symbol = Workspace->getObject("symbol")) {
424 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
425 if (auto *ValueSet = SymbolKind->get("valueSet")) {
426 R.WorkspaceSymbolKinds.emplace();
427 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds,
428 P.field("workspace")
429 .field("symbol")
430 .field("symbolKind")
431 .field("valueSet")))
432 return false;
433 }
434 }
435 }
436 if (auto *SemanticTokens = Workspace->getObject("semanticTokens")) {
437 if (auto RefreshSupport = SemanticTokens->getBoolean("refreshSupport"))
438 R.SemanticTokenRefreshSupport = *RefreshSupport;
439 }
440 }
441 if (auto *Window = O->getObject("window")) {
442 if (auto WorkDoneProgress = Window->getBoolean("workDoneProgress"))
443 R.WorkDoneProgress = *WorkDoneProgress;
444 if (auto Implicit = Window->getBoolean("implicitWorkDoneProgressCreate"))
445 R.ImplicitProgressCreation = *Implicit;
446 }
447 if (auto *General = O->getObject("general")) {
448 if (auto *StaleRequestSupport = General->getObject("staleRequestSupport")) {
449 if (auto Cancel = StaleRequestSupport->getBoolean("cancel"))
450 R.CancelsStaleRequests = *Cancel;
451 }
452 }
453 if (auto *OffsetEncoding = O->get("offsetEncoding")) {
454 R.offsetEncoding.emplace();
456 P.field("offsetEncoding")))
457 return false;
458 }
459 return true;
460}
461
462bool fromJSON(const llvm::json::Value &Params, InitializeParams &R,
463 llvm::json::Path P) {
464 llvm::json::ObjectMapper O(Params, P);
465 if (!O)
466 return false;
467 // We deliberately don't fail if we can't parse individual fields.
468 // Failing to handle a slightly malformed initialize would be a disaster.
469 O.map("processId", R.processId);
470 O.map("rootUri", R.rootUri);
471 O.map("rootPath", R.rootPath);
472 O.map("capabilities", R.capabilities);
473 if (auto *RawCaps = Params.getAsObject()->getObject("capabilities"))
474 R.rawCapabilities = *RawCaps;
475 O.map("trace", R.trace);
476 O.map("initializationOptions", R.initializationOptions);
477 return true;
478}
479
480llvm::json::Value toJSON(const WorkDoneProgressCreateParams &P) {
481 return llvm::json::Object{{"token", P.token}};
482}
483
484llvm::json::Value toJSON(const WorkDoneProgressBegin &P) {
485 llvm::json::Object Result{
486 {"kind", "begin"},
487 {"title", P.title},
488 };
489 if (P.cancellable)
490 Result["cancellable"] = true;
491 if (P.percentage)
492 Result["percentage"] = 0;
493
494 // FIXME: workaround for older gcc/clang
495 return std::move(Result);
496}
497
498llvm::json::Value toJSON(const WorkDoneProgressReport &P) {
499 llvm::json::Object Result{{"kind", "report"}};
500 if (P.cancellable)
501 Result["cancellable"] = *P.cancellable;
502 if (P.message)
503 Result["message"] = *P.message;
504 if (P.percentage)
505 Result["percentage"] = *P.percentage;
506 // FIXME: workaround for older gcc/clang
507 return std::move(Result);
508}
509
510llvm::json::Value toJSON(const WorkDoneProgressEnd &P) {
511 llvm::json::Object Result{{"kind", "end"}};
512 if (P.message)
513 Result["message"] = *P.message;
514 // FIXME: workaround for older gcc/clang
515 return std::move(Result);
516}
517
518llvm::json::Value toJSON(const MessageType &R) {
519 return static_cast<int64_t>(R);
520}
521
522llvm::json::Value toJSON(const ShowMessageParams &R) {
523 return llvm::json::Object{{"type", R.type}, {"message", R.message}};
524}
525
526bool fromJSON(const llvm::json::Value &Params, DidOpenTextDocumentParams &R,
527 llvm::json::Path P) {
528 llvm::json::ObjectMapper O(Params, P);
529 return O && O.map("textDocument", R.textDocument);
530}
531
532bool fromJSON(const llvm::json::Value &Params, DidCloseTextDocumentParams &R,
533 llvm::json::Path P) {
534 llvm::json::ObjectMapper O(Params, P);
535 return O && O.map("textDocument", R.textDocument);
536}
537
538bool fromJSON(const llvm::json::Value &Params, DidSaveTextDocumentParams &R,
539 llvm::json::Path P) {
540 llvm::json::ObjectMapper O(Params, P);
541 return O && O.map("textDocument", R.textDocument);
542}
543
544bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R,
545 llvm::json::Path P) {
546 llvm::json::ObjectMapper O(Params, P);
547 return O && O.map("textDocument", R.textDocument) &&
548 O.map("contentChanges", R.contentChanges) &&
549 O.map("wantDiagnostics", R.wantDiagnostics) &&
550 mapOptOrNull(Params, "forceRebuild", R.forceRebuild, P);
551}
552
553bool fromJSON(const llvm::json::Value &E, FileChangeType &Out,
554 llvm::json::Path P) {
555 if (auto T = E.getAsInteger()) {
556 if (*T < static_cast<int>(FileChangeType::Created) ||
557 *T > static_cast<int>(FileChangeType::Deleted))
558 return false;
559 Out = static_cast<FileChangeType>(*T);
560 return true;
561 }
562 return false;
563}
564
565bool fromJSON(const llvm::json::Value &Params, FileEvent &R,
566 llvm::json::Path P) {
567 llvm::json::ObjectMapper O(Params, P);
568 return O && O.map("uri", R.uri) && O.map("type", R.type);
569}
570
571bool fromJSON(const llvm::json::Value &Params, DidChangeWatchedFilesParams &R,
572 llvm::json::Path P) {
573 llvm::json::ObjectMapper O(Params, P);
574 return O && O.map("changes", R.changes);
575}
576
577bool fromJSON(const llvm::json::Value &Params,
578 TextDocumentContentChangeEvent &R, llvm::json::Path P) {
579 llvm::json::ObjectMapper O(Params, P);
580 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
581 O.map("text", R.text);
582}
583
584bool fromJSON(const llvm::json::Value &Params, DocumentRangeFormattingParams &R,
585 llvm::json::Path P) {
586 llvm::json::ObjectMapper O(Params, P);
587 return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
588}
589
590bool fromJSON(const llvm::json::Value &Params,
591 DocumentOnTypeFormattingParams &R, llvm::json::Path P) {
592 llvm::json::ObjectMapper O(Params, P);
593 return O && O.map("textDocument", R.textDocument) &&
594 O.map("position", R.position) && O.map("ch", R.ch);
595}
596
597bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R,
598 llvm::json::Path P) {
599 llvm::json::ObjectMapper O(Params, P);
600 return O && O.map("textDocument", R.textDocument);
601}
602
603bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R,
604 llvm::json::Path P) {
605 llvm::json::ObjectMapper O(Params, P);
606 return O && O.map("textDocument", R.textDocument);
607}
608
609llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
610 return llvm::json::Object{
611 {"location", DRI.location},
612 {"message", DRI.message},
613 };
614}
615
616llvm::json::Value toJSON(DiagnosticTag Tag) { return static_cast<int>(Tag); }
617
618llvm::json::Value toJSON(const CodeDescription &D) {
619 return llvm::json::Object{{"href", D.href}};
620}
621
622llvm::json::Value toJSON(const Diagnostic &D) {
623 llvm::json::Object Diag{
624 {"range", D.range},
625 {"severity", D.severity},
626 {"message", D.message},
627 };
628 if (D.category)
629 Diag["category"] = *D.category;
630 if (D.codeActions)
631 Diag["codeActions"] = D.codeActions;
632 if (!D.code.empty())
633 Diag["code"] = D.code;
634 if (D.codeDescription)
635 Diag["codeDescription"] = *D.codeDescription;
636 if (!D.source.empty())
637 Diag["source"] = D.source;
638 if (D.relatedInformation)
639 Diag["relatedInformation"] = *D.relatedInformation;
640 if (!D.data.empty())
641 Diag["data"] = llvm::json::Object(D.data);
642 if (!D.tags.empty())
643 Diag["tags"] = llvm::json::Array{D.tags};
644 // FIXME: workaround for older gcc/clang
645 return std::move(Diag);
646}
647
648bool fromJSON(const llvm::json::Value &Params, Diagnostic &R,
649 llvm::json::Path P) {
650 llvm::json::ObjectMapper O(Params, P);
651 if (!O)
652 return false;
653 if (auto *Data = Params.getAsObject()->getObject("data"))
654 R.data = *Data;
655 return O.map("range", R.range) && O.map("message", R.message) &&
656 mapOptOrNull(Params, "severity", R.severity, P) &&
657 mapOptOrNull(Params, "category", R.category, P) &&
658 mapOptOrNull(Params, "code", R.code, P) &&
659 mapOptOrNull(Params, "source", R.source, P);
660}
661
662llvm::json::Value toJSON(const PublishDiagnosticsParams &PDP) {
663 llvm::json::Object Result{
664 {"uri", PDP.uri},
665 {"diagnostics", PDP.diagnostics},
666 };
667 if (PDP.version)
668 Result["version"] = PDP.version;
669 return std::move(Result);
670}
671
672bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R,
673 llvm::json::Path P) {
674 llvm::json::ObjectMapper O(Params, P);
675 if (!O || !O.map("diagnostics", R.diagnostics))
676 return false;
677 O.map("only", R.only);
678 return true;
679}
680
681llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
682 OS << D.range << " [";
683 switch (D.severity) {
684 case 1:
685 OS << "error";
686 break;
687 case 2:
688 OS << "warning";
689 break;
690 case 3:
691 OS << "note";
692 break;
693 case 4:
694 OS << "remark";
695 break;
696 default:
697 OS << "diagnostic";
698 break;
699 }
700 return OS << '(' << D.severity << "): " << D.message << "]";
701}
702
703bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R,
704 llvm::json::Path P) {
705 llvm::json::ObjectMapper O(Params, P);
706 return O && O.map("textDocument", R.textDocument) &&
707 O.map("range", R.range) && O.map("context", R.context);
708}
709
710bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R,
711 llvm::json::Path P) {
712 llvm::json::ObjectMapper O(Params, P);
713 return O && O.map("changes", R.changes);
714}
715
716bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R,
717 llvm::json::Path P) {
718 llvm::json::ObjectMapper O(Params, P);
719 if (!O || !O.map("command", R.command))
720 return false;
721
722 const auto *Args = Params.getAsObject()->get("arguments");
723 if (!Args)
724 return true; // Missing args is ok, argument is null.
725 const auto *ArgsArray = Args->getAsArray();
726 if (!ArgsArray) {
727 P.field("arguments").report("expected array");
728 return false;
729 }
730 if (ArgsArray->size() > 1) {
731 P.field("arguments").report("Command should have 0 or 1 argument");
732 return false;
733 }
734 if (ArgsArray->size() == 1) {
735 R.argument = ArgsArray->front();
736 }
737 return true;
738}
739
740llvm::json::Value toJSON(const SymbolInformation &P) {
741 llvm::json::Object O{
742 {"name", P.name},
743 {"kind", static_cast<int>(P.kind)},
744 {"location", P.location},
745 {"containerName", P.containerName},
746 };
747 if (P.score)
748 O["score"] = *P.score;
749 return std::move(O);
750}
751
752llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
753 const SymbolInformation &SI) {
754 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
755 return O;
756}
757
758bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
759 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
760 LHS.USR == RHS.USR && LHS.ID == RHS.ID &&
763}
764
765llvm::json::Value toJSON(const SymbolDetails &P) {
766 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
767 {"containerName", llvm::json::Value(nullptr)},
768 {"usr", llvm::json::Value(nullptr)},
769 {"id", llvm::json::Value(nullptr)}};
770
771 if (!P.name.empty())
772 Result["name"] = P.name;
773
774 if (!P.containerName.empty())
775 Result["containerName"] = P.containerName;
776
777 if (!P.USR.empty())
778 Result["usr"] = P.USR;
779
780 if (P.ID)
781 Result["id"] = P.ID.str();
782
783 if (P.declarationRange)
784 Result["declarationRange"] = *P.declarationRange;
785
786 if (P.definitionRange)
787 Result["definitionRange"] = *P.definitionRange;
788
789 // FIXME: workaround for older gcc/clang
790 return std::move(Result);
791}
792
793llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
794 if (!S.containerName.empty()) {
795 O << S.containerName;
796 llvm::StringRef ContNameRef;
797 if (!ContNameRef.endswith("::")) {
798 O << " ";
799 }
800 }
801 O << S.name << " - " << toJSON(S);
802 return O;
803}
804
805bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R,
806 llvm::json::Path P) {
807 llvm::json::ObjectMapper O(Params, P);
808 return O && O.map("query", R.query) &&
809 mapOptOrNull(Params, "limit", R.limit, P);
810}
811
812llvm::json::Value toJSON(const Command &C) {
813 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
814 if (!C.argument.getAsNull())
815 Cmd["arguments"] = llvm::json::Array{C.argument};
816 return std::move(Cmd);
817}
818
819const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
820const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
821const llvm::StringLiteral CodeAction::INFO_KIND = "info";
822
823llvm::json::Value toJSON(const CodeAction &CA) {
824 auto CodeAction = llvm::json::Object{{"title", CA.title}};
825 if (CA.kind)
826 CodeAction["kind"] = *CA.kind;
827 if (CA.diagnostics)
828 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
829 if (CA.isPreferred)
830 CodeAction["isPreferred"] = true;
831 if (CA.edit)
832 CodeAction["edit"] = *CA.edit;
833 if (CA.command)
834 CodeAction["command"] = *CA.command;
835 return std::move(CodeAction);
836}
837
838llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
839 return O << S.name << " - " << toJSON(S);
840}
841
842llvm::json::Value toJSON(const DocumentSymbol &S) {
843 llvm::json::Object Result{{"name", S.name},
844 {"kind", static_cast<int>(S.kind)},
845 {"range", S.range},
846 {"selectionRange", S.selectionRange}};
847
848 if (!S.detail.empty())
849 Result["detail"] = S.detail;
850 if (!S.children.empty())
851 Result["children"] = S.children;
852 if (S.deprecated)
853 Result["deprecated"] = true;
854 // FIXME: workaround for older gcc/clang
855 return std::move(Result);
856}
857
858llvm::json::Value toJSON(const WorkspaceEdit &WE) {
859 llvm::json::Object FileChanges;
860 for (auto &Change : WE.changes)
861 FileChanges[Change.first] = llvm::json::Array(Change.second);
862 return llvm::json::Object{{"changes", std::move(FileChanges)}};
863}
864
865bool fromJSON(const llvm::json::Value &Params, TweakArgs &A,
866 llvm::json::Path P) {
867 llvm::json::ObjectMapper O(Params, P);
868 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
869 O.map("tweakID", A.tweakID);
870}
871
872llvm::json::Value toJSON(const TweakArgs &A) {
873 return llvm::json::Object{
874 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
875}
876
877llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
878 return llvm::json::Object{{"edit", Params.edit}};
879}
880
881bool fromJSON(const llvm::json::Value &Response, ApplyWorkspaceEditResponse &R,
882 llvm::json::Path P) {
883 llvm::json::ObjectMapper O(Response, P);
884 return O && O.map("applied", R.applied) &&
885 O.map("failureReason", R.failureReason);
886}
887
888bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R,
889 llvm::json::Path P) {
890 llvm::json::ObjectMapper O(Params, P);
891 return O && O.map("textDocument", R.textDocument) &&
892 O.map("position", R.position);
893}
894
895bool fromJSON(const llvm::json::Value &Params, CompletionContext &R,
896 llvm::json::Path P) {
897 llvm::json::ObjectMapper O(Params, P);
898 int TriggerKind;
899 if (!O || !O.map("triggerKind", TriggerKind) ||
900 !mapOptOrNull(Params, "triggerCharacter", R.triggerCharacter, P))
901 return false;
902 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
903 return true;
904}
905
906bool fromJSON(const llvm::json::Value &Params, CompletionParams &R,
907 llvm::json::Path P) {
908 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R), P) ||
909 !mapOptOrNull(Params, "limit", R.limit, P))
910 return false;
911 if (auto *Context = Params.getAsObject()->get("context"))
912 return fromJSON(*Context, R.context, P.field("context"));
913 return true;
914}
915
916static llvm::StringRef toTextKind(MarkupKind Kind) {
917 switch (Kind) {
919 return "plaintext";
921 return "markdown";
922 }
923 llvm_unreachable("Invalid MarkupKind");
924}
925
926bool fromJSON(const llvm::json::Value &V, MarkupKind &K, llvm::json::Path P) {
927 auto Str = V.getAsString();
928 if (!Str) {
929 P.report("expected string");
930 return false;
931 }
932 if (*Str == "plaintext")
934 else if (*Str == "markdown")
936 else {
937 P.report("unknown markup kind");
938 return false;
939 }
940 return true;
941}
942
943llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
944 return OS << toTextKind(K);
945}
946
947llvm::json::Value toJSON(const MarkupContent &MC) {
948 if (MC.value.empty())
949 return nullptr;
950
951 return llvm::json::Object{
952 {"kind", toTextKind(MC.kind)},
953 {"value", MC.value},
954 };
955}
956
957llvm::json::Value toJSON(const Hover &H) {
958 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
959
960 if (H.range)
961 Result["range"] = toJSON(*H.range);
962
963 return std::move(Result);
964}
965
966bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out,
967 llvm::json::Path P) {
968 if (auto T = E.getAsInteger()) {
969 if (*T < static_cast<int>(CompletionItemKind::Text) ||
970 *T > static_cast<int>(CompletionItemKind::TypeParameter))
971 return false;
972 Out = static_cast<CompletionItemKind>(*T);
973 return true;
974 }
975 return false;
976}
977
980 CompletionItemKindBitset &SupportedCompletionItemKinds) {
981 auto KindVal = static_cast<size_t>(Kind);
982 if (KindVal >= CompletionItemKindMin &&
983 KindVal <= SupportedCompletionItemKinds.size() &&
984 SupportedCompletionItemKinds[KindVal])
985 return Kind;
986
987 switch (Kind) {
988 // Provide some fall backs for common kinds that are close enough.
995 default:
997 }
998}
999
1000bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out,
1001 llvm::json::Path P) {
1002 if (auto *A = E.getAsArray()) {
1003 for (size_t I = 0; I < A->size(); ++I) {
1004 CompletionItemKind KindOut;
1005 if (fromJSON((*A)[I], KindOut, P.index(I)))
1006 Out.set(size_t(KindOut));
1007 }
1008 return true;
1009 }
1010 return false;
1011}
1012
1013llvm::json::Value toJSON(const CompletionItem &CI) {
1014 assert(!CI.label.empty() && "completion item label is required");
1015 llvm::json::Object Result{{"label", CI.label}};
1016 if (CI.kind != CompletionItemKind::Missing)
1017 Result["kind"] = static_cast<int>(CI.kind);
1018 if (!CI.detail.empty())
1019 Result["detail"] = CI.detail;
1020 if (CI.documentation)
1021 Result["documentation"] = CI.documentation;
1022 if (!CI.sortText.empty())
1023 Result["sortText"] = CI.sortText;
1024 if (!CI.filterText.empty())
1025 Result["filterText"] = CI.filterText;
1026 if (!CI.insertText.empty())
1027 Result["insertText"] = CI.insertText;
1028 if (CI.insertTextFormat != InsertTextFormat::Missing)
1029 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
1030 if (CI.textEdit)
1031 Result["textEdit"] = *CI.textEdit;
1032 if (!CI.additionalTextEdits.empty())
1033 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
1034 if (CI.deprecated)
1035 Result["deprecated"] = CI.deprecated;
1036 Result["score"] = CI.score;
1037 return std::move(Result);
1038}
1039
1040llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
1041 O << I.label << " - " << toJSON(I);
1042 return O;
1043}
1044
1045bool operator<(const CompletionItem &L, const CompletionItem &R) {
1046 return (L.sortText.empty() ? L.label : L.sortText) <
1047 (R.sortText.empty() ? R.label : R.sortText);
1048}
1049
1050llvm::json::Value toJSON(const CompletionList &L) {
1051 return llvm::json::Object{
1052 {"isIncomplete", L.isIncomplete},
1053 {"items", llvm::json::Array(L.items)},
1054 };
1055}
1056
1057llvm::json::Value toJSON(const ParameterInformation &PI) {
1058 assert((PI.labelOffsets || !PI.labelString.empty()) &&
1059 "parameter information label is required");
1060 llvm::json::Object Result;
1061 if (PI.labelOffsets)
1062 Result["label"] =
1063 llvm::json::Array({PI.labelOffsets->first, PI.labelOffsets->second});
1064 else
1065 Result["label"] = PI.labelString;
1066 if (!PI.documentation.empty())
1067 Result["documentation"] = PI.documentation;
1068 return std::move(Result);
1069}
1070
1071llvm::json::Value toJSON(const SignatureInformation &SI) {
1072 assert(!SI.label.empty() && "signature information label is required");
1073 llvm::json::Object Result{
1074 {"label", SI.label},
1075 {"parameters", llvm::json::Array(SI.parameters)},
1076 };
1077 if (!SI.documentation.value.empty())
1078 Result["documentation"] = SI.documentation;
1079 return std::move(Result);
1080}
1081
1082llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
1083 const SignatureInformation &I) {
1084 O << I.label << " - " << toJSON(I);
1085 return O;
1086}
1087
1088llvm::json::Value toJSON(const SignatureHelp &SH) {
1089 assert(SH.activeSignature >= 0 &&
1090 "Unexpected negative value for number of active signatures.");
1091 assert(SH.activeParameter >= 0 &&
1092 "Unexpected negative value for active parameter index");
1093 return llvm::json::Object{
1094 {"activeSignature", SH.activeSignature},
1095 {"activeParameter", SH.activeParameter},
1096 {"signatures", llvm::json::Array(SH.signatures)},
1097 };
1098}
1099
1100bool fromJSON(const llvm::json::Value &Params, RenameParams &R,
1101 llvm::json::Path P) {
1102 llvm::json::ObjectMapper O(Params, P);
1103 return O && O.map("textDocument", R.textDocument) &&
1104 O.map("position", R.position) && O.map("newName", R.newName);
1105}
1106
1107llvm::json::Value toJSON(const DocumentHighlight &DH) {
1108 return llvm::json::Object{
1109 {"range", toJSON(DH.range)},
1110 {"kind", static_cast<int>(DH.kind)},
1111 };
1112}
1113
1114llvm::json::Value toJSON(const FileStatus &FStatus) {
1115 return llvm::json::Object{
1116 {"uri", FStatus.uri},
1117 {"state", FStatus.state},
1118 };
1119}
1120
1121constexpr unsigned SemanticTokenEncodingSize = 5;
1122static llvm::json::Value encodeTokens(llvm::ArrayRef<SemanticToken> Toks) {
1123 llvm::json::Array Result;
1124 Result.reserve(SemanticTokenEncodingSize * Toks.size());
1125 for (const auto &Tok : Toks) {
1126 Result.push_back(Tok.deltaLine);
1127 Result.push_back(Tok.deltaStart);
1128 Result.push_back(Tok.length);
1129 Result.push_back(Tok.tokenType);
1130 Result.push_back(Tok.tokenModifiers);
1131 }
1132 assert(Result.size() == SemanticTokenEncodingSize * Toks.size());
1133 return std::move(Result);
1134}
1135
1136bool operator==(const SemanticToken &L, const SemanticToken &R) {
1137 return std::tie(L.deltaLine, L.deltaStart, L.length, L.tokenType,
1138 L.tokenModifiers) == std::tie(R.deltaLine, R.deltaStart,
1139 R.length, R.tokenType,
1140 R.tokenModifiers);
1141}
1142
1143llvm::json::Value toJSON(const SemanticTokens &Tokens) {
1144 return llvm::json::Object{{"resultId", Tokens.resultId},
1145 {"data", encodeTokens(Tokens.tokens)}};
1146}
1147
1148llvm::json::Value toJSON(const SemanticTokensEdit &Edit) {
1149 return llvm::json::Object{
1150 {"start", SemanticTokenEncodingSize * Edit.startToken},
1151 {"deleteCount", SemanticTokenEncodingSize * Edit.deleteTokens},
1152 {"data", encodeTokens(Edit.tokens)}};
1153}
1154
1155llvm::json::Value toJSON(const SemanticTokensOrDelta &TE) {
1156 llvm::json::Object Result{{"resultId", TE.resultId}};
1157 if (TE.edits)
1158 Result["edits"] = *TE.edits;
1159 if (TE.tokens)
1160 Result["data"] = encodeTokens(*TE.tokens);
1161 return std::move(Result);
1162}
1163
1164bool fromJSON(const llvm::json::Value &Params, SemanticTokensParams &R,
1165 llvm::json::Path P) {
1166 llvm::json::ObjectMapper O(Params, P);
1167 return O && O.map("textDocument", R.textDocument);
1168}
1169
1170bool fromJSON(const llvm::json::Value &Params, SemanticTokensDeltaParams &R,
1171 llvm::json::Path P) {
1172 llvm::json::ObjectMapper O(Params, P);
1173 return O && O.map("textDocument", R.textDocument) &&
1174 O.map("previousResultId", R.previousResultId);
1175}
1176
1177llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
1178 const DocumentHighlight &V) {
1179 O << V.range;
1181 O << "(r)";
1183 O << "(w)";
1184 return O;
1185}
1186
1187bool fromJSON(const llvm::json::Value &Params,
1188 DidChangeConfigurationParams &CCP, llvm::json::Path P) {
1189 llvm::json::ObjectMapper O(Params, P);
1190 return O && O.map("settings", CCP.settings);
1191}
1192
1193bool fromJSON(const llvm::json::Value &Params, ClangdCompileCommand &CDbUpdate,
1194 llvm::json::Path P) {
1195 llvm::json::ObjectMapper O(Params, P);
1196 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
1197 O.map("compilationCommand", CDbUpdate.compilationCommand);
1198}
1199
1200bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S,
1201 llvm::json::Path P) {
1202 llvm::json::ObjectMapper O(Params, P);
1203 if (!O)
1204 return true; // 'any' type in LSP.
1205 return mapOptOrNull(Params, "compilationDatabaseChanges",
1207}
1208
1209bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts,
1210 llvm::json::Path P) {
1211 llvm::json::ObjectMapper O(Params, P);
1212 if (!O)
1213 return true; // 'any' type in LSP.
1214
1215 return fromJSON(Params, Opts.ConfigSettings, P) &&
1216 O.map("compilationDatabasePath", Opts.compilationDatabasePath) &&
1217 mapOptOrNull(Params, "fallbackFlags", Opts.fallbackFlags, P) &&
1218 mapOptOrNull(Params, "clangdFileStatus", Opts.FileStatus, P);
1219}
1220
1221bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out,
1222 llvm::json::Path P) {
1223 auto T = E.getAsInteger();
1224 if (!T)
1225 return false;
1226 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
1227 *T > static_cast<int>(TypeHierarchyDirection::Both))
1228 return false;
1229 Out = static_cast<TypeHierarchyDirection>(*T);
1230 return true;
1231}
1232
1233bool fromJSON(const llvm::json::Value &Params, TypeHierarchyPrepareParams &R,
1234 llvm::json::Path P) {
1235 llvm::json::ObjectMapper O(Params, P);
1236 return O && O.map("textDocument", R.textDocument) &&
1237 O.map("position", R.position) &&
1238 mapOptOrNull(Params, "resolve", R.resolve, P) &&
1239 mapOptOrNull(Params, "direction", R.direction, P);
1240}
1241
1242llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
1243 const TypeHierarchyItem &I) {
1244 return O << I.name << " - " << toJSON(I);
1245}
1246
1247llvm::json::Value toJSON(const TypeHierarchyItem::ResolveParams &RP) {
1248 llvm::json::Object Result{{"symbolID", RP.symbolID}};
1249 if (RP.parents)
1250 Result["parents"] = RP.parents;
1251 return std::move(Result);
1252}
1253bool fromJSON(const llvm::json::Value &Params,
1254 TypeHierarchyItem::ResolveParams &RP, llvm::json::Path P) {
1255 llvm::json::ObjectMapper O(Params, P);
1256 return O && O.map("symbolID", RP.symbolID) &&
1257 mapOptOrNull(Params, "parents", RP.parents, P);
1258}
1259
1260llvm::json::Value toJSON(const TypeHierarchyItem &I) {
1261 llvm::json::Object Result{
1262 {"name", I.name}, {"kind", static_cast<int>(I.kind)},
1263 {"range", I.range}, {"selectionRange", I.selectionRange},
1264 {"uri", I.uri}, {"data", I.data},
1265 };
1266
1267 if (I.detail)
1268 Result["detail"] = I.detail;
1269 return std::move(Result);
1270}
1271
1272bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I,
1273 llvm::json::Path P) {
1274 llvm::json::ObjectMapper O(Params, P);
1275
1276 // Required fields.
1277 return O && O.map("name", I.name) && O.map("kind", I.kind) &&
1278 O.map("uri", I.uri) && O.map("range", I.range) &&
1279 O.map("selectionRange", I.selectionRange) &&
1280 mapOptOrNull(Params, "detail", I.detail, P) &&
1281 mapOptOrNull(Params, "deprecated", I.deprecated, P) &&
1282 mapOptOrNull(Params, "parents", I.parents, P) &&
1283 mapOptOrNull(Params, "children", I.children, P) &&
1284 mapOptOrNull(Params, "data", I.data, P);
1285}
1286
1287bool fromJSON(const llvm::json::Value &Params,
1288 ResolveTypeHierarchyItemParams &R, llvm::json::Path P) {
1289 llvm::json::ObjectMapper O(Params, P);
1290 return O && O.map("item", R.item) &&
1291 mapOptOrNull(Params, "resolve", R.resolve, P) &&
1292 mapOptOrNull(Params, "direction", R.direction, P);
1293}
1294
1295bool fromJSON(const llvm::json::Value &Params, ReferenceContext &R,
1296 llvm::json::Path P) {
1297 llvm::json::ObjectMapper O(Params, P);
1298 return O && O.mapOptional("includeDeclaration", R.includeDeclaration);
1299}
1300
1301bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R,
1302 llvm::json::Path P) {
1304 llvm::json::ObjectMapper O(Params, P);
1305 return fromJSON(Params, Base, P) && O && O.mapOptional("context", R.context);
1306}
1307
1308llvm::json::Value toJSON(SymbolTag Tag) {
1309 return llvm::json::Value{static_cast<int>(Tag)};
1310}
1311
1312llvm::json::Value toJSON(const CallHierarchyItem &I) {
1313 llvm::json::Object Result{{"name", I.name},
1314 {"kind", static_cast<int>(I.kind)},
1315 {"range", I.range},
1316 {"selectionRange", I.selectionRange},
1317 {"uri", I.uri}};
1318 if (!I.tags.empty())
1319 Result["tags"] = I.tags;
1320 if (!I.detail.empty())
1321 Result["detail"] = I.detail;
1322 if (!I.data.empty())
1323 Result["data"] = I.data;
1324 return std::move(Result);
1325}
1326
1327bool fromJSON(const llvm::json::Value &Params, CallHierarchyItem &I,
1328 llvm::json::Path P) {
1329 llvm::json::ObjectMapper O(Params, P);
1330
1331 // Populate the required fields only. We don't care about the
1332 // optional fields `Tags` and `Detail` for the purpose of
1333 // client --> server communication.
1334 return O && O.map("name", I.name) && O.map("kind", I.kind) &&
1335 O.map("uri", I.uri) && O.map("range", I.range) &&
1336 O.map("selectionRange", I.selectionRange) &&
1337 mapOptOrNull(Params, "data", I.data, P);
1338}
1339
1340bool fromJSON(const llvm::json::Value &Params,
1341 CallHierarchyIncomingCallsParams &C, llvm::json::Path P) {
1342 llvm::json::ObjectMapper O(Params, P);
1343 return O.map("item", C.item);
1344}
1345
1346llvm::json::Value toJSON(const CallHierarchyIncomingCall &C) {
1347 return llvm::json::Object{{"from", C.from}, {"fromRanges", C.fromRanges}};
1348}
1349
1350bool fromJSON(const llvm::json::Value &Params,
1351 CallHierarchyOutgoingCallsParams &C, llvm::json::Path P) {
1352 llvm::json::ObjectMapper O(Params, P);
1353 return O.map("item", C.item);
1354}
1355
1356llvm::json::Value toJSON(const CallHierarchyOutgoingCall &C) {
1357 return llvm::json::Object{{"to", C.to}, {"fromRanges", C.fromRanges}};
1358}
1359
1360bool fromJSON(const llvm::json::Value &Params, InlayHintsParams &R,
1361 llvm::json::Path P) {
1362 llvm::json::ObjectMapper O(Params, P);
1363 return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
1364}
1365
1366llvm::json::Value toJSON(const InlayHintKind &Kind) {
1367 switch (Kind) {
1369 return 1;
1371 return 2;
1372 case InlayHintKind::Designator: // This is an extension, don't serialize.
1373 return nullptr;
1374 }
1375 llvm_unreachable("Unknown clang.clangd.InlayHintKind");
1376}
1377
1378llvm::json::Value toJSON(const InlayHint &H) {
1379 llvm::json::Object Result{{"position", H.position},
1380 {"label", H.label},
1381 {"paddingLeft", H.paddingLeft},
1382 {"paddingRight", H.paddingRight}};
1383 auto K = toJSON(H.kind);
1384 if (!K.getAsNull())
1385 Result["kind"] = std::move(K);
1386 return std::move(Result);
1387}
1388bool operator==(const InlayHint &A, const InlayHint &B) {
1389 return std::tie(A.position, A.range, A.kind, A.label) ==
1390 std::tie(B.position, B.range, B.kind, B.label);
1391}
1392bool operator<(const InlayHint &A, const InlayHint &B) {
1393 return std::tie(A.position, A.range, A.kind, A.label) <
1394 std::tie(B.position, B.range, B.kind, B.label);
1395}
1396
1397llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, InlayHintKind Kind) {
1398 auto ToString = [](InlayHintKind K) {
1399 switch (K) {
1401 return "parameter";
1403 return "type";
1405 return "designator";
1406 }
1407 llvm_unreachable("Unknown clang.clangd.InlayHintKind");
1408 };
1409 return OS << ToString(Kind);
1410}
1411
1412static const char *toString(OffsetEncoding OE) {
1413 switch (OE) {
1415 return "utf-8";
1417 return "utf-16";
1419 return "utf-32";
1421 return "unknown";
1422 }
1423 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
1424}
1425llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
1426bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE,
1427 llvm::json::Path P) {
1428 auto Str = V.getAsString();
1429 if (!Str)
1430 return false;
1431 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
1432 .Case("utf-8", OffsetEncoding::UTF8)
1433 .Case("utf-16", OffsetEncoding::UTF16)
1434 .Case("utf-32", OffsetEncoding::UTF32)
1436 return true;
1437}
1438llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
1439 return OS << toString(Enc);
1440}
1441
1442bool fromJSON(const llvm::json::Value &Params, SelectionRangeParams &S,
1443 llvm::json::Path P) {
1444 llvm::json::ObjectMapper O(Params, P);
1445 return O && O.map("textDocument", S.textDocument) &&
1446 O.map("positions", S.positions);
1447}
1448
1449llvm::json::Value toJSON(const SelectionRange &Out) {
1450 if (Out.parent) {
1451 return llvm::json::Object{{"range", Out.range},
1452 {"parent", toJSON(*Out.parent)}};
1453 }
1454 return llvm::json::Object{{"range", Out.range}};
1455}
1456
1457bool fromJSON(const llvm::json::Value &Params, DocumentLinkParams &R,
1458 llvm::json::Path P) {
1459 llvm::json::ObjectMapper O(Params, P);
1460 return O && O.map("textDocument", R.textDocument);
1461}
1462
1463llvm::json::Value toJSON(const DocumentLink &DocumentLink) {
1464 return llvm::json::Object{
1465 {"range", DocumentLink.range},
1466 {"target", DocumentLink.target},
1467 };
1468}
1469
1470bool fromJSON(const llvm::json::Value &Params, FoldingRangeParams &R,
1471 llvm::json::Path P) {
1472 llvm::json::ObjectMapper O(Params, P);
1473 return O && O.map("textDocument", R.textDocument);
1474}
1475
1476const llvm::StringLiteral FoldingRange::REGION_KIND = "region";
1477const llvm::StringLiteral FoldingRange::COMMENT_KIND = "comment";
1478const llvm::StringLiteral FoldingRange::IMPORT_KIND = "import";
1479
1480llvm::json::Value toJSON(const FoldingRange &Range) {
1481 llvm::json::Object Result{
1482 {"startLine", Range.startLine},
1483 {"endLine", Range.endLine},
1484 };
1485 if (Range.startCharacter)
1486 Result["startCharacter"] = Range.startCharacter;
1487 if (Range.endCharacter)
1488 Result["endCharacter"] = Range.endCharacter;
1489 if (!Range.kind.empty())
1490 Result["kind"] = Range.kind;
1491 return Result;
1492}
1493
1494llvm::json::Value toJSON(const MemoryTree &MT) {
1495 llvm::json::Object Out;
1496 int64_t Total = MT.self();
1497 Out["_self"] = Total;
1498 for (const auto &Entry : MT.children()) {
1499 auto Child = toJSON(Entry.getSecond());
1500 Total += *Child.getAsObject()->getInteger("_total");
1501 Out[Entry.first] = std::move(Child);
1502 }
1503 Out["_total"] = Total;
1504 return Out;
1505}
1506
1507bool fromJSON(const llvm::json::Value &Params, ASTParams &R,
1508 llvm::json::Path P) {
1509 llvm::json::ObjectMapper O(Params, P);
1510 return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
1511}
1512
1513llvm::json::Value toJSON(const ASTNode &N) {
1514 llvm::json::Object Result{
1515 {"role", N.role},
1516 {"kind", N.kind},
1517 };
1518 if (!N.children.empty())
1519 Result["children"] = N.children;
1520 if (!N.detail.empty())
1521 Result["detail"] = N.detail;
1522 if (!N.arcana.empty())
1523 Result["arcana"] = N.arcana;
1524 if (N.range)
1525 Result["range"] = *N.range;
1526 return Result;
1527}
1528
1529llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const ASTNode &Root) {
1530 std::function<void(const ASTNode &, unsigned)> Print = [&](const ASTNode &N,
1531 unsigned Level) {
1532 OS.indent(2 * Level) << N.role << ": " << N.kind;
1533 if (!N.detail.empty())
1534 OS << " - " << N.detail;
1535 OS << "\n";
1536 for (const ASTNode &C : N.children)
1537 Print(C, Level + 1);
1538 };
1539 Print(Root, 0);
1540 return OS;
1541}
1542
1543bool fromJSON(const llvm::json::Value &E, SymbolID &S, llvm::json::Path P) {
1544 auto Str = E.getAsString();
1545 if (!Str) {
1546 P.report("expected a string");
1547 return false;
1548 }
1549 auto ID = SymbolID::fromStr(*Str);
1550 if (!ID) {
1551 elog("Malformed symbolid: {0}", ID.takeError());
1552 P.report("malformed symbolid");
1553 return false;
1554 }
1555 S = *ID;
1556 return true;
1557}
1558llvm::json::Value toJSON(const SymbolID &S) { return S.str(); }
1559
1560} // namespace clangd
1561} // namespace clang
const Expr * E
BindArgumentKind Kind
CompiledFragmentImpl & Out
ASTNode Root
Definition: DumpAST.cpp:333
const Criteria C
HTMLTag Tag
Kind K
Definition: Rename.cpp:454
std::unique_ptr< CompilerInvocation > CI
WantDiagnostics Diagnostics
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
llvm::json::Object Args
Definition: Trace.cpp:138
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:98
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition: SymbolID.cpp:36
std::string str() const
Definition: SymbolID.cpp:34
A URI describes the location of a source file.
Definition: URI.h:28
static llvm::Expected< std::string > resolvePath(llvm::StringRef AbsPath, llvm::StringRef HintPath="")
Resolves AbsPath into a canonical path of its URI, by converting AbsPath to URI and resolving the URI...
Definition: URI.cpp:253
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
Definition: URI.cpp:245
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
Definition: URI.cpp:177
@ Created
The file got created.
@ Deleted
The file got deleted.
@ Info
An information message.
std::bitset< SymbolKindMax+1 > SymbolKindBitset
Definition: Protocol.h:377
constexpr auto CompletionItemKindMin
Definition: Protocol.h:334
constexpr auto SymbolKindMin
Definition: Protocol.h:375
CompletionItemKind
The kind of a completion entry.
Definition: Protocol.h:304
static const char * toString(OffsetEncoding OE)
Definition: Protocol.cpp:1412
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:317
constexpr unsigned SemanticTokenEncodingSize
Definition: Protocol.cpp:1121
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:45
SymbolKind adjustKindToCapability(SymbolKind Kind, SymbolKindBitset &SupportedSymbolKinds)
Definition: Protocol.cpp:248
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:95
SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind)
Definition: Protocol.cpp:266
static llvm::StringRef toTextKind(MarkupKind Kind)
Definition: Protocol.cpp:916
InlayHintKind
Inlay hint kinds.
Definition: Protocol.h:1574
@ Parameter
An inlay hint that is for a parameter.
@ Type
An inlay hint that for a type annotation.
@ Designator
A hint before an element of an aggregate braced initializer list, indicating what it is initializing.
static llvm::json::Value encodeTokens(llvm::ArrayRef< SemanticToken > Toks)
Definition: Protocol.cpp:1122
std::bitset< CompletionItemKindMax+1 > CompletionItemKindBitset
Definition: Protocol.h:338
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:61
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request, llvm::json::Path P)
Definition: Index.cpp:30
SymbolKind
A symbol kind.
Definition: Protocol.h:346
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Simplified description of a clang AST node.
Definition: Protocol.h:1854
std::optional< Range > range
The range of the original source file covered by this node.
Definition: Protocol.h:1872
std::vector< ASTNode > children
Nodes nested within this one, such as the operands of a BinaryOperator.
Definition: Protocol.h:1874
std::string role
The general kind of node, such as "expression" Corresponds to the base AST node type such as Expr.
Definition: Protocol.h:1857
std::string kind
The specific kind of node this is, such as "BinaryOperator".
Definition: Protocol.h:1861
std::string detail
Brief additional information, such as "||" for the particular operator.
Definition: Protocol.h:1864
std::string arcana
A one-line dump of detailed information about the node.
Definition: Protocol.h:1869
Payload for textDocument/ast request.
Definition: Protocol.h:1841
std::optional< Range > range
The position of the node to be dumped.
Definition: Protocol.h:1848
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:1843
std::optional< std::string > failureReason
Definition: Protocol.h:1148
Represents an incoming call, e.g. a caller of a method or constructor.
Definition: Protocol.h:1531
The parameter of a callHierarchy/incomingCalls request.
Definition: Protocol.h:1524
Represents programming constructs like functions or constructors in the context of call hierarchy.
Definition: Protocol.h:1491
std::string name
The name of this item.
Definition: Protocol.h:1493
URIForFile uri
The resource identifier of this item.
Definition: Protocol.h:1505
Range range
The range enclosing this symbol not including leading / trailing whitespace but everything else,...
Definition: Protocol.h:1509
SymbolKind kind
The kind of this item.
Definition: Protocol.h:1496
std::vector< SymbolTag > tags
Tags for this item.
Definition: Protocol.h:1499
std::string data
An optional 'data' field, which can be used to identify a call hierarchy item in an incomingCalls or ...
Definition: Protocol.h:1518
std::string detail
More detaill for this item, e.g. the signature of a function.
Definition: Protocol.h:1502
Range selectionRange
The range that should be selected and revealed when this symbol is being picked, e....
Definition: Protocol.h:1514
Represents an outgoing call, e.g.
Definition: Protocol.h:1550
The parameter of a callHierarchy/outgoingCalls request.
Definition: Protocol.h:1542
Clangd extension that's used in the 'compilationDatabaseChanges' in workspace/didChangeConfiguration ...
Definition: Protocol.h:526
std::vector< std::string > compilationCommand
Definition: Protocol.h:528
bool HierarchicalDocumentSymbol
Client supports hierarchical document symbols.
Definition: Protocol.h:448
bool WorkDoneProgress
The client supports progress notifications.
Definition: Protocol.h:504
bool DiagnosticCategory
Whether the client accepts diagnostics with category attached to it using the "category" extension.
Definition: Protocol.h:431
MarkupKind HoverContentFormat
The content format that should be used for Hover requests.
Definition: Protocol.h:496
bool CodeActionStructure
Client supports CodeAction return value for textDocument/codeAction.
Definition: Protocol.h:479
bool OffsetsInSignatureHelp
Client supports processing label offsets instead of a simple label string.
Definition: Protocol.h:462
bool DiagnosticFixes
Whether the client accepts diagnostics with codeActions attached inline.
Definition: Protocol.h:422
bool HasSignatureHelp
Client supports signature help.
Definition: Protocol.h:452
bool TheiaSemanticHighlighting
Client supports Theia semantic highlighting extension.
Definition: Protocol.h:489
bool SemanticTokenRefreshSupport
Whether the client implementation supports a refresh request sent from the server to the client.
Definition: Protocol.h:518
bool ImplicitProgressCreation
The client supports implicit $/progress work-done progress streams, without a preceding window/workDo...
Definition: Protocol.h:510
MarkupKind SignatureHelpDocumentationFormat
The documentation format that should be used for textDocument/signatureHelp.
Definition: Protocol.h:467
bool DiagnosticRelatedInformation
Whether the client accepts diagnostics with related locations.
Definition: Protocol.h:426
bool CompletionFixes
Client supports completions with additionalTextEdit near the cursor.
Definition: Protocol.h:440
bool RenamePrepareSupport
The client supports testing for validity of rename operations before execution.
Definition: Protocol.h:500
bool CancelsStaleRequests
Whether the client claims to cancel stale requests.
Definition: Protocol.h:514
std::optional< CompletionItemKindBitset > CompletionItemKinds
The supported set of CompletionItemKinds for textDocument/completion.
Definition: Protocol.h:471
bool CompletionSnippets
Client supports snippets as insert text.
Definition: Protocol.h:435
MarkupKind CompletionDocumentationFormat
The documentation format that should be used for textDocument/completion.
Definition: Protocol.h:475
bool SemanticTokens
Client advertises support for the semanticTokens feature.
Definition: Protocol.h:484
bool LineFoldingOnly
Client signals that it only supports folding complete lines.
Definition: Protocol.h:458
std::optional< SymbolKindBitset > WorkspaceSymbolKinds
The supported set of SymbolKinds for workspace/symbol.
Definition: Protocol.h:418
std::optional< std::vector< OffsetEncoding > > offsetEncoding
Supported encodings for LSP character offsets. (clangd extension).
Definition: Protocol.h:492
bool ReferenceContainer
Client supports displaying a container string for results of textDocument/reference (clangd extension...
Definition: Protocol.h:444
std::vector< Diagnostic > diagnostics
An array of diagnostics known on the client side overlapping the range provided to the textDocument/c...
Definition: Protocol.h:947
std::vector< std::string > only
Requested kind of actions to return.
Definition: Protocol.h:953
CodeActionContext context
Context carrying additional information.
Definition: Protocol.h:965
TextDocumentIdentifier textDocument
The document in which the command was invoked.
Definition: Protocol.h:959
Range range
The range for which the command was invoked.
Definition: Protocol.h:962
A code action represents a change that can be performed in code, e.g.
Definition: Protocol.h:1015
static const llvm::StringLiteral INFO_KIND
Definition: Protocol.h:1024
bool isPreferred
Marks this as a preferred action.
Definition: Protocol.h:1034
static const llvm::StringLiteral REFACTOR_KIND
Definition: Protocol.h:1023
std::optional< std::vector< Diagnostic > > diagnostics
The diagnostics that this code action resolves.
Definition: Protocol.h:1027
static const llvm::StringLiteral QUICKFIX_KIND
Definition: Protocol.h:1022
std::optional< WorkspaceEdit > edit
The workspace edit this code action performs.
Definition: Protocol.h:1037
std::optional< Command > command
A command this code action executes.
Definition: Protocol.h:1041
std::optional< std::string > kind
The kind of the code action.
Definition: Protocol.h:1021
std::string title
A short, human-readable, title for this code action.
Definition: Protocol.h:1017
Structure to capture a description for an error code.
Definition: Protocol.h:862
CompletionTriggerKind triggerKind
How the completion was triggered.
Definition: Protocol.h:1176
std::string triggerCharacter
The trigger character (a single character) that has trigger code complete.
Definition: Protocol.h:1179
std::string sortText
A string that should be used when comparing this item with other items.
Definition: Protocol.h:1244
std::string label
The label of this completion item.
Definition: Protocol.h:1229
Represents a collection of completion items to be presented in the editor.
Definition: Protocol.h:1294
std::optional< int > limit
Max results to return, overriding global default.
Definition: Protocol.h:1188
CompletionContext context
Definition: Protocol.h:1184
Clangd extension: parameters configurable at any time, via the workspace/didChangeConfiguration notif...
Definition: Protocol.h:536
std::map< std::string, ClangdCompileCommand > compilationDatabaseChanges
Definition: Protocol.h:539
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:94
Represents a related message and source code location for a diagnostic.
Definition: Protocol.h:840
std::string message
The message of this related diagnostic information.
Definition: Protocol.h:844
Location location
The location of this related diagnostic information.
Definition: Protocol.h:842
llvm::json::Object data
A data entry field that is preserved between a textDocument/publishDiagnostics notification and textD...
Definition: Protocol.h:913
std::string code
The diagnostic's code. Can be omitted.
Definition: Protocol.h:878
Range range
The range at which the message applies.
Definition: Protocol.h:871
std::string source
A human-readable string describing the source of this diagnostic, e.g.
Definition: Protocol.h:885
std::string message
The diagnostic's message.
Definition: Protocol.h:888
int severity
The diagnostic's severity.
Definition: Protocol.h:875
std::optional< std::string > category
The diagnostic's category.
Definition: Protocol.h:901
bool forceRebuild
Force a complete rebuild of the file, ignoring all cached state.
Definition: Protocol.h:755
VersionedTextDocumentIdentifier textDocument
The document that did change.
Definition: Protocol.h:740
std::optional< bool > wantDiagnostics
Forces diagnostics to be generated, or to not be generated, for this version of the file.
Definition: Protocol.h:749
std::vector< TextDocumentContentChangeEvent > contentChanges
The actual content changes.
Definition: Protocol.h:743
std::vector< FileEvent > changes
The actual file events.
Definition: Protocol.h:781
TextDocumentIdentifier textDocument
The document that was closed.
Definition: Protocol.h:711
TextDocumentItem textDocument
The document that was opened.
Definition: Protocol.h:704
TextDocumentIdentifier textDocument
The document that was saved.
Definition: Protocol.h:718
TextDocumentIdentifier textDocument
The document to format.
Definition: Protocol.h:825
A document highlight is a range inside a text document which deserves special attention.
Definition: Protocol.h:1376
Range range
The range this highlight applies to.
Definition: Protocol.h:1378
DocumentHighlightKind kind
The highlight kind, default is DocumentHighlightKind.Text.
Definition: Protocol.h:1381
Parameters for the document link request.
Definition: Protocol.h:1763
TextDocumentIdentifier textDocument
The document to provide document links for.
Definition: Protocol.h:1765
Position position
The position at which this request was sent.
Definition: Protocol.h:815
std::string ch
The character that has been typed.
Definition: Protocol.h:818
TextDocumentIdentifier textDocument
The document to format.
Definition: Protocol.h:812
TextDocumentIdentifier textDocument
The document to format.
Definition: Protocol.h:802
TextDocumentIdentifier textDocument
Definition: Protocol.h:832
Represents programming constructs like variables, classes, interfaces etc.
Definition: Protocol.h:1049
Range selectionRange
The range that should be selected and revealed when this symbol is being picked, e....
Definition: Protocol.h:1070
std::vector< DocumentSymbol > children
Children of this symbol, e.g. properties of a class.
Definition: Protocol.h:1073
std::string detail
More detail for this symbol, e.g the signature of a function.
Definition: Protocol.h:1054
std::string name
The name of this symbol.
Definition: Protocol.h:1051
Range range
The range enclosing this symbol not including leading/trailing whitespace but everything else like co...
Definition: Protocol.h:1066
bool deprecated
Indicates if this symbol is deprecated.
Definition: Protocol.h:1060
SymbolKind kind
The kind of this symbol.
Definition: Protocol.h:1057
A set of edits generated for a single file.
Definition: SourceCode.h:185
std::string command
The identifier of the actual command handler.
Definition: Protocol.h:996
FileChangeType type
The change type.
Definition: Protocol.h:775
URIForFile uri
The file's URI.
Definition: Protocol.h:773
Clangd extension: indicates the current state of the file in clangd, sent from server via the textDoc...
Definition: Protocol.h:1657
URIForFile uri
The text document's URI.
Definition: Protocol.h:1659
std::string state
The human-readable string presents the current state of the file, can be shown in the UI (e....
Definition: Protocol.h:1662
TextDocumentIdentifier textDocument
Definition: Protocol.h:1799
Stores information about a region of code that can be folded.
Definition: Protocol.h:1805
static const llvm::StringLiteral REGION_KIND
Definition: Protocol.h:1811
static const llvm::StringLiteral COMMENT_KIND
Definition: Protocol.h:1812
static const llvm::StringLiteral IMPORT_KIND
Definition: Protocol.h:1813
Clangd extension: parameters configurable at initialize time.
Definition: Protocol.h:546
llvm::json::Object rawCapabilities
The same data as capabilities, but not parsed (to expose to modules).
Definition: Protocol.h:587
InitializationOptions initializationOptions
User-provided initialization options.
Definition: Protocol.h:593
ClientCapabilities capabilities
The capabilities provided by the client (editor or tool)
Definition: Protocol.h:585
std::optional< TraceLevel > trace
The initial trace setting. If omitted trace is disabled ('off').
Definition: Protocol.h:590
std::optional< int > processId
The process Id of the parent process that started the server.
Definition: Protocol.h:568
std::optional< std::string > rootPath
The rootPath of the workspace.
Definition: Protocol.h:574
std::optional< URIForFile > rootUri
The rootUri of the workspace.
Definition: Protocol.h:579
Inlay hint information.
Definition: Protocol.h:1605
A parameter literal used in inlay hint requests.
Definition: Protocol.h:1561
std::optional< Range > range
The visible document range for which inlay hints should be computed.
Definition: Protocol.h:1569
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:1563
URIForFile uri
The text document's URI.
Definition: Protocol.h:213
A tree that can be used to represent memory usage of nested components while preserving the hierarchy...
Definition: MemoryTree.h:30
const llvm::DenseMap< llvm::StringRef, MemoryTree > & children() const
Returns edges to direct children of this node.
Definition: MemoryTree.cpp:42
size_t self() const
Returns total number of bytes used by this node only.
Definition: MemoryTree.h:65
A single parameter of a particular signature.
Definition: Protocol.h:1305
std::string labelString
The label of this parameter. Ignored when labelOffsets is set.
Definition: Protocol.h:1308
std::string documentation
The documentation of this parameter. Optional.
Definition: Protocol.h:1317
std::optional< std::pair< unsigned, unsigned > > labelOffsets
Inclusive start and exclusive end offsets withing the containing signature label.
Definition: Protocol.h:1314
int line
Line position in a document (zero-based).
Definition: Protocol.h:158
int character
Character offset on a line in a document (zero-based).
Definition: Protocol.h:163
std::vector< Diagnostic > diagnostics
An array of diagnostic information items.
Definition: Protocol.h:934
std::optional< int64_t > version
The version number of the document the diagnostics are published for.
Definition: Protocol.h:936
URIForFile uri
The URI for which diagnostic information is reported.
Definition: Protocol.h:932
Position start
The range's start position.
Definition: Protocol.h:187
Position end
The range's end position.
Definition: Protocol.h:190
bool includeDeclaration
Include the declaration of the current symbol.
Definition: Protocol.h:1647
Extends Locations returned by textDocument/references with extra info.
Definition: Protocol.h:233
std::optional< std::string > containerName
clangd extension: contains the name of the function or class in which the reference occurs
Definition: Protocol.h:236
ReferenceContext context
Definition: Protocol.h:1651
TextDocumentIdentifier textDocument
The document that was opened.
Definition: Protocol.h:1360
Position position
The position at which this request was sent.
Definition: Protocol.h:1363
std::string newName
The new name of the symbol.
Definition: Protocol.h:1366
Parameters for the typeHierarchy/resolve request.
Definition: Protocol.h:1470
TypeHierarchyItem item
The item to resolve.
Definition: Protocol.h:1472
int resolve
The hierarchy levels to resolve. 0 indicates no level.
Definition: Protocol.h:1475
TypeHierarchyDirection direction
The direction of the hierarchy levels to resolve.
Definition: Protocol.h:1478
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:1741
std::vector< Position > positions
The positions inside the text document.
Definition: Protocol.h:1744
Specifies a single semantic token in the document.
Definition: Protocol.h:1670
unsigned length
the length of the token. A token cannot be multiline
Definition: Protocol.h:1677
unsigned deltaStart
token start character, relative to the previous token (relative to 0 or the previous token's start if...
Definition: Protocol.h:1675
unsigned deltaLine
token line number, relative to the previous token
Definition: Protocol.h:1672
unsigned tokenType
will be looked up in SemanticTokensLegend.tokenTypes
Definition: Protocol.h:1679
unsigned tokenModifiers
each set bit will be looked up in SemanticTokensLegend.tokenModifiers
Definition: Protocol.h:1681
Body of textDocument/semanticTokens/full/delta request.
Definition: Protocol.h:1708
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:1710
std::string previousResultId
The previous result id.
Definition: Protocol.h:1712
Describes a replacement of a contiguous range of semanticTokens.
Definition: Protocol.h:1718
This models LSP SemanticTokensDelta | SemanticTokens, which is the result of textDocument/semanticTok...
Definition: Protocol.h:1730
std::optional< std::vector< SemanticToken > > tokens
Set if we computed a fresh set of tokens.
Definition: Protocol.h:1735
std::optional< std::vector< SemanticTokensEdit > > edits
Set if we computed edits relative to a previous set of tokens.
Definition: Protocol.h:1733
Body of textDocument/semanticTokens/full request.
Definition: Protocol.h:1699
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:1701
A versioned set of tokens.
Definition: Protocol.h:1686
The show message notification is sent from a server to a client to ask the client to display a partic...
Definition: Protocol.h:694
MessageType type
The message type.
Definition: Protocol.h:696
std::string message
The actual message.
Definition: Protocol.h:698
Represents the signature of a callable.
Definition: Protocol.h:1338
int activeSignature
The active signature.
Definition: Protocol.h:1344
std::vector< SignatureInformation > signatures
The resulting signatures.
Definition: Protocol.h:1341
int activeParameter
The active parameter of the active signature.
Definition: Protocol.h:1347
Represents the signature of something callable.
Definition: Protocol.h:1322
MarkupContent documentation
The documentation of this signature. Optional.
Definition: Protocol.h:1328
std::vector< ParameterInformation > parameters
The parameters of this signature.
Definition: Protocol.h:1331
std::string label
The label of this signature. Mandatory.
Definition: Protocol.h:1325
Represents information about identifier.
Definition: Protocol.h:1106
std::optional< Location > definitionRange
Definition: Protocol.h:1122
std::optional< Location > declarationRange
Definition: Protocol.h:1120
std::string USR
Unified Symbol Resolution identifier This is an opaque string uniquely identifying a symbol.
Definition: Protocol.h:1116
Represents information about programming constructs like variables, classes, interfaces etc.
Definition: Protocol.h:1080
std::string containerName
The name of the symbol containing this symbol.
Definition: Protocol.h:1091
Location location
The location of this symbol.
Definition: Protocol.h:1088
SymbolKind kind
The kind of this symbol.
Definition: Protocol.h:1085
std::optional< float > score
The score that clangd calculates to rank the returned symbols.
Definition: Protocol.h:1099
std::string name
The name of this symbol.
Definition: Protocol.h:1082
The class presents a C++ symbol, e.g.
Definition: Symbol.h:39
std::optional< Range > range
The range of the document that changed.
Definition: Protocol.h:725
std::string text
The new text of the range/document.
Definition: Protocol.h:731
std::optional< int > rangeLength
The length of the range that got replaced.
Definition: Protocol.h:728
URIForFile uri
The text document's URI.
Definition: Protocol.h:133
std::string languageId
The text document's language identifier.
Definition: Protocol.h:262
std::optional< int64_t > version
The version number of this document (it will strictly increase after each change, including undo/redo...
Definition: Protocol.h:268
URIForFile uri
The text document's URI.
Definition: Protocol.h:259
std::string text
The content of the opened text document.
Definition: Protocol.h:271
TextDocumentIdentifier textDocument
The text document.
Definition: Protocol.h:1155
Position position
The position inside the text document.
Definition: Protocol.h:1158
std::string newText
The string to be inserted.
Definition: Protocol.h:248
Range range
The range of the text document to be manipulated.
Definition: Protocol.h:244
Arguments for the 'applyTweak' command.
Definition: Protocol.h:983
Used to resolve a client provided item back.
Definition: Protocol.h:1439
std::optional< std::vector< ResolveParams > > parents
None means parents aren't resolved and empty is no parents.
Definition: Protocol.h:1442
Range range
The range enclosing this symbol not including leading/trailing whitespace but everything else,...
Definition: Protocol.h:1432
URIForFile uri
The resource identifier of this item.
Definition: Protocol.h:1428
Range selectionRange
The range that should be selected and revealed when this symbol is being picked, e....
Definition: Protocol.h:1436
SymbolKind kind
The kind of this item.
Definition: Protocol.h:1422
std::optional< std::vector< TypeHierarchyItem > > children
If this type hierarchy item is resolved, it contains the direct children of the current item.
Definition: Protocol.h:1461
std::optional< std::vector< TypeHierarchyItem > > parents
This is a clangd exntesion.
Definition: Protocol.h:1455
bool deprecated
true if the hierarchy item is deprecated.
Definition: Protocol.h:1452
std::optional< std::string > detail
More detail for this item, e.g. the signature of a function.
Definition: Protocol.h:1425
ResolveParams data
A data entry field that is preserved between a type hierarchy prepare and supertypes or subtypes requ...
Definition: Protocol.h:1448
std::string name
The name of this item.
Definition: Protocol.h:1419
The type hierarchy params is an extension of the TextDocumentPositionsParams with optional properties...
Definition: Protocol.h:1405
int resolve
The hierarchy levels to resolve.
Definition: Protocol.h:1408
TypeHierarchyDirection direction
The direction of the hierarchy levels to resolve.
Definition: Protocol.h:1412
std::string uri() const
Definition: Protocol.h:107
static llvm::Expected< URIForFile > fromURI(const URI &U, llvm::StringRef HintPath)
Definition: Protocol.cpp:58
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.
Definition: Protocol.cpp:45
std::optional< std::int64_t > version
The version number of this document.
Definition: Protocol.h:150
To start progress reporting a $/progress notification with the following payload must be sent.
Definition: Protocol.h:615
bool percentage
Optional progress percentage to display (value 100 is considered 100%).
Definition: Protocol.h:636
bool cancellable
Controls if a cancel button should show to allow the user to cancel the long-running operation.
Definition: Protocol.h:625
std::string title
Mandatory title of the progress operation.
Definition: Protocol.h:620
llvm::json::Value token
The token to be used to report progress.
Definition: Protocol.h:599
Signals the end of progress reporting.
Definition: Protocol.h:673
std::optional< std::string > message
Optional, a final message indicating to for example indicate the outcome of the operation.
Definition: Protocol.h:676
Reporting progress is done using the following payload.
Definition: Protocol.h:641
std::optional< unsigned > percentage
Optional progress percentage to display (value 100 is considered 100%).
Definition: Protocol.h:668
std::optional< bool > cancellable
Controls enablement state of a cancel button.
Definition: Protocol.h:653
std::optional< std::string > message
Optional, more detailed associated progress message.
Definition: Protocol.h:660
std::map< std::string, std::vector< TextEdit > > changes
Holds changes to existing resources.
Definition: Protocol.h:971
The parameters of a Workspace Symbol Request.
Definition: Protocol.h:1129
std::string query
A query string to filter symbols by.
Definition: Protocol.h:1132
std::optional< int > limit
Max results to return, overriding global default.
Definition: Protocol.h:1136