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/Hashing.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/FormatVariadic.h"
24 #include "llvm/Support/JSON.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27
28 namespace clang {
29 namespace clangd {
30
31 char LSPError::ID;
32
canonicalize(llvm::StringRef AbsPath,llvm::StringRef TUPath)33 URIForFile URIForFile::canonicalize(llvm::StringRef AbsPath,
34 llvm::StringRef TUPath) {
35 assert(llvm::sys::path::is_absolute(AbsPath) && "the path is relative");
36 auto Resolved = URI::resolvePath(AbsPath, TUPath);
37 if (!Resolved) {
38 elog("URIForFile: failed to resolve path {0} with TU path {1}: "
39 "{2}.\nUsing unresolved path.",
40 AbsPath, TUPath, Resolved.takeError());
41 return URIForFile(std::string(AbsPath));
42 }
43 return URIForFile(std::move(*Resolved));
44 }
45
fromURI(const URI & U,llvm::StringRef HintPath)46 llvm::Expected<URIForFile> URIForFile::fromURI(const URI &U,
47 llvm::StringRef HintPath) {
48 auto Resolved = URI::resolve(U, HintPath);
49 if (!Resolved)
50 return Resolved.takeError();
51 return URIForFile(std::move(*Resolved));
52 }
53
fromJSON(const llvm::json::Value & E,URIForFile & R,llvm::json::Path P)54 bool fromJSON(const llvm::json::Value &E, URIForFile &R, llvm::json::Path P) {
55 if (auto S = E.getAsString()) {
56 auto Parsed = URI::parse(*S);
57 if (!Parsed) {
58 P.report("failed to parse URI");
59 return false;
60 }
61 if (Parsed->scheme() != "file" && Parsed->scheme() != "test") {
62 P.report("clangd only supports 'file' URI scheme for workspace files");
63 return false;
64 }
65 // "file" and "test" schemes do not require hint path.
66 auto U = URIForFile::fromURI(*Parsed, /*HintPath=*/"");
67 if (!U) {
68 P.report("unresolvable URI");
69 consumeError(U.takeError());
70 return false;
71 }
72 R = std::move(*U);
73 return true;
74 }
75 return false;
76 }
77
toJSON(const URIForFile & U)78 llvm::json::Value toJSON(const URIForFile &U) { return U.uri(); }
79
operator <<(llvm::raw_ostream & OS,const URIForFile & U)80 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const URIForFile &U) {
81 return OS << U.uri();
82 }
83
toJSON(const TextDocumentIdentifier & R)84 llvm::json::Value toJSON(const TextDocumentIdentifier &R) {
85 return llvm::json::Object{{"uri", R.uri}};
86 }
87
fromJSON(const llvm::json::Value & Params,TextDocumentIdentifier & R,llvm::json::Path P)88 bool fromJSON(const llvm::json::Value &Params, TextDocumentIdentifier &R,
89 llvm::json::Path P) {
90 llvm::json::ObjectMapper O(Params, P);
91 return O && O.map("uri", R.uri);
92 }
93
toJSON(const VersionedTextDocumentIdentifier & R)94 llvm::json::Value toJSON(const VersionedTextDocumentIdentifier &R) {
95 auto Result = toJSON(static_cast<const TextDocumentIdentifier &>(R));
96 Result.getAsObject()->try_emplace("version", R.version);
97 return Result;
98 }
99
fromJSON(const llvm::json::Value & Params,VersionedTextDocumentIdentifier & R,llvm::json::Path P)100 bool fromJSON(const llvm::json::Value &Params,
101 VersionedTextDocumentIdentifier &R, llvm::json::Path P) {
102 llvm::json::ObjectMapper O(Params, P);
103 return fromJSON(Params, static_cast<TextDocumentIdentifier &>(R), P) && O &&
104 O.map("version", R.version);
105 }
106
fromJSON(const llvm::json::Value & Params,Position & R,llvm::json::Path P)107 bool fromJSON(const llvm::json::Value &Params, Position &R,
108 llvm::json::Path P) {
109 llvm::json::ObjectMapper O(Params, P);
110 return O && O.map("line", R.line) && O.map("character", R.character);
111 }
112
toJSON(const Position & P)113 llvm::json::Value toJSON(const Position &P) {
114 return llvm::json::Object{
115 {"line", P.line},
116 {"character", P.character},
117 };
118 }
119
operator <<(llvm::raw_ostream & OS,const Position & P)120 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &P) {
121 return OS << P.line << ':' << P.character;
122 }
123
fromJSON(const llvm::json::Value & Params,Range & R,llvm::json::Path P)124 bool fromJSON(const llvm::json::Value &Params, Range &R, llvm::json::Path P) {
125 llvm::json::ObjectMapper O(Params, P);
126 return O && O.map("start", R.start) && O.map("end", R.end);
127 }
128
toJSON(const Range & P)129 llvm::json::Value toJSON(const Range &P) {
130 return llvm::json::Object{
131 {"start", P.start},
132 {"end", P.end},
133 };
134 }
135
operator <<(llvm::raw_ostream & OS,const Range & R)136 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Range &R) {
137 return OS << R.start << '-' << R.end;
138 }
139
toJSON(const Location & P)140 llvm::json::Value toJSON(const Location &P) {
141 return llvm::json::Object{
142 {"uri", P.uri},
143 {"range", P.range},
144 };
145 }
146
operator <<(llvm::raw_ostream & OS,const Location & L)147 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Location &L) {
148 return OS << L.range << '@' << L.uri;
149 }
150
fromJSON(const llvm::json::Value & Params,TextDocumentItem & R,llvm::json::Path P)151 bool fromJSON(const llvm::json::Value &Params, TextDocumentItem &R,
152 llvm::json::Path P) {
153 llvm::json::ObjectMapper O(Params, P);
154 return O && O.map("uri", R.uri) && O.map("languageId", R.languageId) &&
155 O.map("version", R.version) && O.map("text", R.text);
156 }
157
fromJSON(const llvm::json::Value & Params,TextEdit & R,llvm::json::Path P)158 bool fromJSON(const llvm::json::Value &Params, TextEdit &R,
159 llvm::json::Path P) {
160 llvm::json::ObjectMapper O(Params, P);
161 return O && O.map("range", R.range) && O.map("newText", R.newText);
162 }
163
toJSON(const TextEdit & P)164 llvm::json::Value toJSON(const TextEdit &P) {
165 return llvm::json::Object{
166 {"range", P.range},
167 {"newText", P.newText},
168 };
169 }
170
operator <<(llvm::raw_ostream & OS,const TextEdit & TE)171 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TextEdit &TE) {
172 OS << TE.range << " => \"";
173 llvm::printEscapedString(TE.newText, OS);
174 return OS << '"';
175 }
176
fromJSON(const llvm::json::Value & E,TraceLevel & Out,llvm::json::Path P)177 bool fromJSON(const llvm::json::Value &E, TraceLevel &Out, llvm::json::Path P) {
178 if (auto S = E.getAsString()) {
179 if (*S == "off") {
180 Out = TraceLevel::Off;
181 return true;
182 }
183 if (*S == "messages") {
184 Out = TraceLevel::Messages;
185 return true;
186 }
187 if (*S == "verbose") {
188 Out = TraceLevel::Verbose;
189 return true;
190 }
191 }
192 return false;
193 }
194
fromJSON(const llvm::json::Value & E,SymbolKind & Out,llvm::json::Path P)195 bool fromJSON(const llvm::json::Value &E, SymbolKind &Out, llvm::json::Path P) {
196 if (auto T = E.getAsInteger()) {
197 if (*T < static_cast<int>(SymbolKind::File) ||
198 *T > static_cast<int>(SymbolKind::TypeParameter))
199 return false;
200 Out = static_cast<SymbolKind>(*T);
201 return true;
202 }
203 return false;
204 }
205
fromJSON(const llvm::json::Value & E,SymbolKindBitset & Out,llvm::json::Path P)206 bool fromJSON(const llvm::json::Value &E, SymbolKindBitset &Out,
207 llvm::json::Path P) {
208 if (auto *A = E.getAsArray()) {
209 for (size_t I = 0; I < A->size(); ++I) {
210 SymbolKind KindOut;
211 if (fromJSON((*A)[I], KindOut, P.index(I)))
212 Out.set(size_t(KindOut));
213 }
214 return true;
215 }
216 return false;
217 }
218
adjustKindToCapability(SymbolKind Kind,SymbolKindBitset & SupportedSymbolKinds)219 SymbolKind adjustKindToCapability(SymbolKind Kind,
220 SymbolKindBitset &SupportedSymbolKinds) {
221 auto KindVal = static_cast<size_t>(Kind);
222 if (KindVal >= SymbolKindMin && KindVal <= SupportedSymbolKinds.size() &&
223 SupportedSymbolKinds[KindVal])
224 return Kind;
225
226 switch (Kind) {
227 // Provide some fall backs for common kinds that are close enough.
228 case SymbolKind::Struct:
229 return SymbolKind::Class;
230 case SymbolKind::EnumMember:
231 return SymbolKind::Enum;
232 default:
233 return SymbolKind::String;
234 }
235 }
236
indexSymbolKindToSymbolKind(index::SymbolKind Kind)237 SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind) {
238 switch (Kind) {
239 case index::SymbolKind::Unknown:
240 return SymbolKind::Variable;
241 case index::SymbolKind::Module:
242 return SymbolKind::Module;
243 case index::SymbolKind::Namespace:
244 return SymbolKind::Namespace;
245 case index::SymbolKind::NamespaceAlias:
246 return SymbolKind::Namespace;
247 case index::SymbolKind::Macro:
248 return SymbolKind::String;
249 case index::SymbolKind::Enum:
250 return SymbolKind::Enum;
251 case index::SymbolKind::Struct:
252 return SymbolKind::Struct;
253 case index::SymbolKind::Class:
254 return SymbolKind::Class;
255 case index::SymbolKind::Protocol:
256 return SymbolKind::Interface;
257 case index::SymbolKind::Extension:
258 return SymbolKind::Interface;
259 case index::SymbolKind::Union:
260 return SymbolKind::Class;
261 case index::SymbolKind::TypeAlias:
262 return SymbolKind::Class;
263 case index::SymbolKind::Function:
264 return SymbolKind::Function;
265 case index::SymbolKind::Variable:
266 return SymbolKind::Variable;
267 case index::SymbolKind::Field:
268 return SymbolKind::Field;
269 case index::SymbolKind::EnumConstant:
270 return SymbolKind::EnumMember;
271 case index::SymbolKind::InstanceMethod:
272 case index::SymbolKind::ClassMethod:
273 case index::SymbolKind::StaticMethod:
274 return SymbolKind::Method;
275 case index::SymbolKind::InstanceProperty:
276 case index::SymbolKind::ClassProperty:
277 case index::SymbolKind::StaticProperty:
278 return SymbolKind::Property;
279 case index::SymbolKind::Constructor:
280 case index::SymbolKind::Destructor:
281 return SymbolKind::Constructor;
282 case index::SymbolKind::ConversionFunction:
283 return SymbolKind::Function;
284 case index::SymbolKind::Parameter:
285 case index::SymbolKind::NonTypeTemplateParm:
286 return SymbolKind::Variable;
287 case index::SymbolKind::Using:
288 return SymbolKind::Namespace;
289 case index::SymbolKind::TemplateTemplateParm:
290 case index::SymbolKind::TemplateTypeParm:
291 return SymbolKind::TypeParameter;
292 }
293 llvm_unreachable("invalid symbol kind");
294 }
295
fromJSON(const llvm::json::Value & Params,ClientCapabilities & R,llvm::json::Path P)296 bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R,
297 llvm::json::Path P) {
298 const llvm::json::Object *O = Params.getAsObject();
299 if (!O) {
300 P.report("expected object");
301 return false;
302 }
303 if (auto *TextDocument = O->getObject("textDocument")) {
304 if (auto *SemanticHighlighting =
305 TextDocument->getObject("semanticHighlightingCapabilities")) {
306 if (auto SemanticHighlightingSupport =
307 SemanticHighlighting->getBoolean("semanticHighlighting"))
308 R.TheiaSemanticHighlighting = *SemanticHighlightingSupport;
309 }
310 if (TextDocument->getObject("semanticTokens"))
311 R.SemanticTokens = true;
312 if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
313 if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
314 R.DiagnosticCategory = *CategorySupport;
315 if (auto CodeActions = Diagnostics->getBoolean("codeActionsInline"))
316 R.DiagnosticFixes = *CodeActions;
317 if (auto RelatedInfo = Diagnostics->getBoolean("relatedInformation"))
318 R.DiagnosticRelatedInformation = *RelatedInfo;
319 }
320 if (auto *Completion = TextDocument->getObject("completion")) {
321 if (auto *Item = Completion->getObject("completionItem")) {
322 if (auto SnippetSupport = Item->getBoolean("snippetSupport"))
323 R.CompletionSnippets = *SnippetSupport;
324 if (const auto *DocumentationFormat =
325 Item->getArray("documentationFormat")) {
326 for (const auto &Format : *DocumentationFormat) {
327 if (fromJSON(Format, R.CompletionDocumentationFormat, P))
328 break;
329 }
330 }
331 }
332 if (auto *ItemKind = Completion->getObject("completionItemKind")) {
333 if (auto *ValueSet = ItemKind->get("valueSet")) {
334 R.CompletionItemKinds.emplace();
335 if (!fromJSON(*ValueSet, *R.CompletionItemKinds,
336 P.field("textDocument")
337 .field("completion")
338 .field("completionItemKind")
339 .field("valueSet")))
340 return false;
341 }
342 }
343 if (auto EditsNearCursor = Completion->getBoolean("editsNearCursor"))
344 R.CompletionFixes = *EditsNearCursor;
345 }
346 if (auto *CodeAction = TextDocument->getObject("codeAction")) {
347 if (CodeAction->getObject("codeActionLiteralSupport"))
348 R.CodeActionStructure = true;
349 }
350 if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) {
351 if (auto HierarchicalSupport =
352 DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport"))
353 R.HierarchicalDocumentSymbol = *HierarchicalSupport;
354 }
355 if (auto *Hover = TextDocument->getObject("hover")) {
356 if (auto *ContentFormat = Hover->getArray("contentFormat")) {
357 for (const auto &Format : *ContentFormat) {
358 if (fromJSON(Format, R.HoverContentFormat, P))
359 break;
360 }
361 }
362 }
363 if (auto *Help = TextDocument->getObject("signatureHelp")) {
364 R.HasSignatureHelp = true;
365 if (auto *Info = Help->getObject("signatureInformation")) {
366 if (auto *Parameter = Info->getObject("parameterInformation")) {
367 if (auto OffsetSupport = Parameter->getBoolean("labelOffsetSupport"))
368 R.OffsetsInSignatureHelp = *OffsetSupport;
369 }
370 }
371 }
372 if (auto *Rename = TextDocument->getObject("rename")) {
373 if (auto RenameSupport = Rename->getBoolean("prepareSupport"))
374 R.RenamePrepareSupport = *RenameSupport;
375 }
376 }
377 if (auto *Workspace = O->getObject("workspace")) {
378 if (auto *Symbol = Workspace->getObject("symbol")) {
379 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
380 if (auto *ValueSet = SymbolKind->get("valueSet")) {
381 R.WorkspaceSymbolKinds.emplace();
382 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds,
383 P.field("workspace")
384 .field("symbol")
385 .field("symbolKind")
386 .field("valueSet")))
387 return false;
388 }
389 }
390 }
391 }
392 if (auto *Window = O->getObject("window")) {
393 if (auto WorkDoneProgress = Window->getBoolean("workDoneProgress"))
394 R.WorkDoneProgress = *WorkDoneProgress;
395 if (auto Implicit = Window->getBoolean("implicitWorkDoneProgressCreate"))
396 R.ImplicitProgressCreation = *Implicit;
397 }
398 if (auto *OffsetEncoding = O->get("offsetEncoding")) {
399 R.offsetEncoding.emplace();
400 if (!fromJSON(*OffsetEncoding, *R.offsetEncoding,
401 P.field("offsetEncoding")))
402 return false;
403 }
404 return true;
405 }
406
fromJSON(const llvm::json::Value & Params,InitializeParams & R,llvm::json::Path P)407 bool fromJSON(const llvm::json::Value &Params, InitializeParams &R,
408 llvm::json::Path P) {
409 llvm::json::ObjectMapper O(Params, P);
410 if (!O)
411 return false;
412 // We deliberately don't fail if we can't parse individual fields.
413 // Failing to handle a slightly malformed initialize would be a disaster.
414 O.map("processId", R.processId);
415 O.map("rootUri", R.rootUri);
416 O.map("rootPath", R.rootPath);
417 O.map("capabilities", R.capabilities);
418 O.map("trace", R.trace);
419 O.map("initializationOptions", R.initializationOptions);
420 return true;
421 }
422
toJSON(const WorkDoneProgressCreateParams & P)423 llvm::json::Value toJSON(const WorkDoneProgressCreateParams &P) {
424 return llvm::json::Object{{"token", P.token}};
425 }
426
toJSON(const WorkDoneProgressBegin & P)427 llvm::json::Value toJSON(const WorkDoneProgressBegin &P) {
428 llvm::json::Object Result{
429 {"kind", "begin"},
430 {"title", P.title},
431 };
432 if (P.cancellable)
433 Result["cancellable"] = true;
434 if (P.percentage)
435 Result["percentage"] = 0;
436
437 // FIXME: workaround for older gcc/clang
438 return std::move(Result);
439 }
440
toJSON(const WorkDoneProgressReport & P)441 llvm::json::Value toJSON(const WorkDoneProgressReport &P) {
442 llvm::json::Object Result{{"kind", "report"}};
443 if (P.cancellable)
444 Result["cancellable"] = *P.cancellable;
445 if (P.message)
446 Result["message"] = *P.message;
447 if (P.percentage)
448 Result["percentage"] = *P.percentage;
449 // FIXME: workaround for older gcc/clang
450 return std::move(Result);
451 }
452
toJSON(const WorkDoneProgressEnd & P)453 llvm::json::Value toJSON(const WorkDoneProgressEnd &P) {
454 llvm::json::Object Result{{"kind", "end"}};
455 if (P.message)
456 Result["message"] = *P.message;
457 // FIXME: workaround for older gcc/clang
458 return std::move(Result);
459 }
460
toJSON(const MessageType & R)461 llvm::json::Value toJSON(const MessageType &R) {
462 return static_cast<int64_t>(R);
463 }
464
toJSON(const ShowMessageParams & R)465 llvm::json::Value toJSON(const ShowMessageParams &R) {
466 return llvm::json::Object{{"type", R.type}, {"message", R.message}};
467 }
468
fromJSON(const llvm::json::Value & Params,DidOpenTextDocumentParams & R,llvm::json::Path P)469 bool fromJSON(const llvm::json::Value &Params, DidOpenTextDocumentParams &R,
470 llvm::json::Path P) {
471 llvm::json::ObjectMapper O(Params, P);
472 return O && O.map("textDocument", R.textDocument);
473 }
474
fromJSON(const llvm::json::Value & Params,DidCloseTextDocumentParams & R,llvm::json::Path P)475 bool fromJSON(const llvm::json::Value &Params, DidCloseTextDocumentParams &R,
476 llvm::json::Path P) {
477 llvm::json::ObjectMapper O(Params, P);
478 return O && O.map("textDocument", R.textDocument);
479 }
480
fromJSON(const llvm::json::Value & Params,DidSaveTextDocumentParams & R,llvm::json::Path P)481 bool fromJSON(const llvm::json::Value &Params, DidSaveTextDocumentParams &R,
482 llvm::json::Path P) {
483 llvm::json::ObjectMapper O(Params, P);
484 return O && O.map("textDocument", R.textDocument);
485 }
486
fromJSON(const llvm::json::Value & Params,DidChangeTextDocumentParams & R,llvm::json::Path P)487 bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R,
488 llvm::json::Path P) {
489 llvm::json::ObjectMapper O(Params, P);
490 return O && O.map("textDocument", R.textDocument) &&
491 O.map("contentChanges", R.contentChanges) &&
492 O.map("wantDiagnostics", R.wantDiagnostics) &&
493 O.mapOptional("forceRebuild", R.forceRebuild);
494 }
495
fromJSON(const llvm::json::Value & E,FileChangeType & Out,llvm::json::Path P)496 bool fromJSON(const llvm::json::Value &E, FileChangeType &Out,
497 llvm::json::Path P) {
498 if (auto T = E.getAsInteger()) {
499 if (*T < static_cast<int>(FileChangeType::Created) ||
500 *T > static_cast<int>(FileChangeType::Deleted))
501 return false;
502 Out = static_cast<FileChangeType>(*T);
503 return true;
504 }
505 return false;
506 }
507
fromJSON(const llvm::json::Value & Params,FileEvent & R,llvm::json::Path P)508 bool fromJSON(const llvm::json::Value &Params, FileEvent &R,
509 llvm::json::Path P) {
510 llvm::json::ObjectMapper O(Params, P);
511 return O && O.map("uri", R.uri) && O.map("type", R.type);
512 }
513
fromJSON(const llvm::json::Value & Params,DidChangeWatchedFilesParams & R,llvm::json::Path P)514 bool fromJSON(const llvm::json::Value &Params, DidChangeWatchedFilesParams &R,
515 llvm::json::Path P) {
516 llvm::json::ObjectMapper O(Params, P);
517 return O && O.map("changes", R.changes);
518 }
519
fromJSON(const llvm::json::Value & Params,TextDocumentContentChangeEvent & R,llvm::json::Path P)520 bool fromJSON(const llvm::json::Value &Params,
521 TextDocumentContentChangeEvent &R, llvm::json::Path P) {
522 llvm::json::ObjectMapper O(Params, P);
523 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
524 O.map("text", R.text);
525 }
526
fromJSON(const llvm::json::Value & Params,DocumentRangeFormattingParams & R,llvm::json::Path P)527 bool fromJSON(const llvm::json::Value &Params, DocumentRangeFormattingParams &R,
528 llvm::json::Path P) {
529 llvm::json::ObjectMapper O(Params, P);
530 return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
531 }
532
fromJSON(const llvm::json::Value & Params,DocumentOnTypeFormattingParams & R,llvm::json::Path P)533 bool fromJSON(const llvm::json::Value &Params,
534 DocumentOnTypeFormattingParams &R, llvm::json::Path P) {
535 llvm::json::ObjectMapper O(Params, P);
536 return O && O.map("textDocument", R.textDocument) &&
537 O.map("position", R.position) && O.map("ch", R.ch);
538 }
539
fromJSON(const llvm::json::Value & Params,DocumentFormattingParams & R,llvm::json::Path P)540 bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R,
541 llvm::json::Path P) {
542 llvm::json::ObjectMapper O(Params, P);
543 return O && O.map("textDocument", R.textDocument);
544 }
545
fromJSON(const llvm::json::Value & Params,DocumentSymbolParams & R,llvm::json::Path P)546 bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R,
547 llvm::json::Path P) {
548 llvm::json::ObjectMapper O(Params, P);
549 return O && O.map("textDocument", R.textDocument);
550 }
551
toJSON(const DiagnosticRelatedInformation & DRI)552 llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
553 return llvm::json::Object{
554 {"location", DRI.location},
555 {"message", DRI.message},
556 };
557 }
558
toJSON(const Diagnostic & D)559 llvm::json::Value toJSON(const Diagnostic &D) {
560 llvm::json::Object Diag{
561 {"range", D.range},
562 {"severity", D.severity},
563 {"message", D.message},
564 };
565 if (D.category)
566 Diag["category"] = *D.category;
567 if (D.codeActions)
568 Diag["codeActions"] = D.codeActions;
569 if (!D.code.empty())
570 Diag["code"] = D.code;
571 if (!D.source.empty())
572 Diag["source"] = D.source;
573 if (D.relatedInformation)
574 Diag["relatedInformation"] = *D.relatedInformation;
575 // FIXME: workaround for older gcc/clang
576 return std::move(Diag);
577 }
578
fromJSON(const llvm::json::Value & Params,Diagnostic & R,llvm::json::Path P)579 bool fromJSON(const llvm::json::Value &Params, Diagnostic &R,
580 llvm::json::Path P) {
581 llvm::json::ObjectMapper O(Params, P);
582 return O && O.map("range", R.range) && O.map("message", R.message) &&
583 O.mapOptional("severity", R.severity) &&
584 O.mapOptional("category", R.category) &&
585 O.mapOptional("code", R.code) && O.mapOptional("source", R.source);
586 return true;
587 }
588
toJSON(const PublishDiagnosticsParams & PDP)589 llvm::json::Value toJSON(const PublishDiagnosticsParams &PDP) {
590 llvm::json::Object Result{
591 {"uri", PDP.uri},
592 {"diagnostics", PDP.diagnostics},
593 };
594 if (PDP.version)
595 Result["version"] = PDP.version;
596 return std::move(Result);
597 }
598
fromJSON(const llvm::json::Value & Params,CodeActionContext & R,llvm::json::Path P)599 bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R,
600 llvm::json::Path P) {
601 llvm::json::ObjectMapper O(Params, P);
602 if (!O || !O.map("diagnostics", R.diagnostics))
603 return false;
604 O.map("only", R.only);
605 return true;
606 }
607
operator <<(llvm::raw_ostream & OS,const Diagnostic & D)608 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
609 OS << D.range << " [";
610 switch (D.severity) {
611 case 1:
612 OS << "error";
613 break;
614 case 2:
615 OS << "warning";
616 break;
617 case 3:
618 OS << "note";
619 break;
620 case 4:
621 OS << "remark";
622 break;
623 default:
624 OS << "diagnostic";
625 break;
626 }
627 return OS << '(' << D.severity << "): " << D.message << "]";
628 }
629
fromJSON(const llvm::json::Value & Params,CodeActionParams & R,llvm::json::Path P)630 bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R,
631 llvm::json::Path P) {
632 llvm::json::ObjectMapper O(Params, P);
633 return O && O.map("textDocument", R.textDocument) &&
634 O.map("range", R.range) && O.map("context", R.context);
635 }
636
fromJSON(const llvm::json::Value & Params,WorkspaceEdit & R,llvm::json::Path P)637 bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R,
638 llvm::json::Path P) {
639 llvm::json::ObjectMapper O(Params, P);
640 return O && O.map("changes", R.changes);
641 }
642
643 const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
644 "clangd.applyFix";
645 const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_TWEAK =
646 "clangd.applyTweak";
647
fromJSON(const llvm::json::Value & Params,ExecuteCommandParams & R,llvm::json::Path P)648 bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R,
649 llvm::json::Path P) {
650 llvm::json::ObjectMapper O(Params, P);
651 if (!O || !O.map("command", R.command))
652 return false;
653
654 const auto *Args = Params.getAsObject()->getArray("arguments");
655 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
656 return Args && Args->size() == 1 &&
657 fromJSON(Args->front(), R.workspaceEdit,
658 P.field("arguments").index(0));
659 }
660 if (R.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK)
661 return Args && Args->size() == 1 &&
662 fromJSON(Args->front(), R.tweakArgs, P.field("arguments").index(0));
663 return false; // Unrecognized command.
664 }
665
toJSON(const SymbolInformation & P)666 llvm::json::Value toJSON(const SymbolInformation &P) {
667 llvm::json::Object O{
668 {"name", P.name},
669 {"kind", static_cast<int>(P.kind)},
670 {"location", P.location},
671 {"containerName", P.containerName},
672 };
673 if (P.score)
674 O["score"] = *P.score;
675 return std::move(O);
676 }
677
operator <<(llvm::raw_ostream & O,const SymbolInformation & SI)678 llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
679 const SymbolInformation &SI) {
680 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
681 return O;
682 }
683
operator ==(const SymbolDetails & LHS,const SymbolDetails & RHS)684 bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
685 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
686 LHS.USR == RHS.USR && LHS.ID == RHS.ID;
687 }
688
toJSON(const SymbolDetails & P)689 llvm::json::Value toJSON(const SymbolDetails &P) {
690 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
691 {"containerName", llvm::json::Value(nullptr)},
692 {"usr", llvm::json::Value(nullptr)},
693 {"id", llvm::json::Value(nullptr)}};
694
695 if (!P.name.empty())
696 Result["name"] = P.name;
697
698 if (!P.containerName.empty())
699 Result["containerName"] = P.containerName;
700
701 if (!P.USR.empty())
702 Result["usr"] = P.USR;
703
704 if (P.ID)
705 Result["id"] = P.ID.str();
706
707 // FIXME: workaround for older gcc/clang
708 return std::move(Result);
709 }
710
operator <<(llvm::raw_ostream & O,const SymbolDetails & S)711 llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
712 if (!S.containerName.empty()) {
713 O << S.containerName;
714 llvm::StringRef ContNameRef;
715 if (!ContNameRef.endswith("::")) {
716 O << " ";
717 }
718 }
719 O << S.name << " - " << toJSON(S);
720 return O;
721 }
722
fromJSON(const llvm::json::Value & Params,WorkspaceSymbolParams & R,llvm::json::Path P)723 bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R,
724 llvm::json::Path P) {
725 llvm::json::ObjectMapper O(Params, P);
726 return O && O.map("query", R.query);
727 }
728
toJSON(const Command & C)729 llvm::json::Value toJSON(const Command &C) {
730 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
731 if (C.workspaceEdit)
732 Cmd["arguments"] = {*C.workspaceEdit};
733 if (C.tweakArgs)
734 Cmd["arguments"] = {*C.tweakArgs};
735 return std::move(Cmd);
736 }
737
738 const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
739 const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
740 const llvm::StringLiteral CodeAction::INFO_KIND = "info";
741
toJSON(const CodeAction & CA)742 llvm::json::Value toJSON(const CodeAction &CA) {
743 auto CodeAction = llvm::json::Object{{"title", CA.title}};
744 if (CA.kind)
745 CodeAction["kind"] = *CA.kind;
746 if (CA.diagnostics)
747 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
748 if (CA.isPreferred)
749 CodeAction["isPreferred"] = true;
750 if (CA.edit)
751 CodeAction["edit"] = *CA.edit;
752 if (CA.command)
753 CodeAction["command"] = *CA.command;
754 return std::move(CodeAction);
755 }
756
operator <<(llvm::raw_ostream & O,const DocumentSymbol & S)757 llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
758 return O << S.name << " - " << toJSON(S);
759 }
760
toJSON(const DocumentSymbol & S)761 llvm::json::Value toJSON(const DocumentSymbol &S) {
762 llvm::json::Object Result{{"name", S.name},
763 {"kind", static_cast<int>(S.kind)},
764 {"range", S.range},
765 {"selectionRange", S.selectionRange}};
766
767 if (!S.detail.empty())
768 Result["detail"] = S.detail;
769 if (!S.children.empty())
770 Result["children"] = S.children;
771 if (S.deprecated)
772 Result["deprecated"] = true;
773 // FIXME: workaround for older gcc/clang
774 return std::move(Result);
775 }
776
toJSON(const WorkspaceEdit & WE)777 llvm::json::Value toJSON(const WorkspaceEdit &WE) {
778 if (!WE.changes)
779 return llvm::json::Object{};
780 llvm::json::Object FileChanges;
781 for (auto &Change : *WE.changes)
782 FileChanges[Change.first] = llvm::json::Array(Change.second);
783 return llvm::json::Object{{"changes", std::move(FileChanges)}};
784 }
785
fromJSON(const llvm::json::Value & Params,TweakArgs & A,llvm::json::Path P)786 bool fromJSON(const llvm::json::Value &Params, TweakArgs &A,
787 llvm::json::Path P) {
788 llvm::json::ObjectMapper O(Params, P);
789 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
790 O.map("tweakID", A.tweakID);
791 }
792
toJSON(const TweakArgs & A)793 llvm::json::Value toJSON(const TweakArgs &A) {
794 return llvm::json::Object{
795 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
796 }
797
toJSON(const ApplyWorkspaceEditParams & Params)798 llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
799 return llvm::json::Object{{"edit", Params.edit}};
800 }
801
fromJSON(const llvm::json::Value & Response,ApplyWorkspaceEditResponse & R,llvm::json::Path P)802 bool fromJSON(const llvm::json::Value &Response, ApplyWorkspaceEditResponse &R,
803 llvm::json::Path P) {
804 llvm::json::ObjectMapper O(Response, P);
805 return O && O.map("applied", R.applied) &&
806 O.map("failureReason", R.failureReason);
807 }
808
fromJSON(const llvm::json::Value & Params,TextDocumentPositionParams & R,llvm::json::Path P)809 bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R,
810 llvm::json::Path P) {
811 llvm::json::ObjectMapper O(Params, P);
812 return O && O.map("textDocument", R.textDocument) &&
813 O.map("position", R.position);
814 }
815
fromJSON(const llvm::json::Value & Params,CompletionContext & R,llvm::json::Path P)816 bool fromJSON(const llvm::json::Value &Params, CompletionContext &R,
817 llvm::json::Path P) {
818 llvm::json::ObjectMapper O(Params, P);
819 int TriggerKind;
820 if (!O || !O.map("triggerKind", TriggerKind) ||
821 !O.mapOptional("triggerCharacter", R.triggerCharacter))
822 return false;
823 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
824 return true;
825 }
826
fromJSON(const llvm::json::Value & Params,CompletionParams & R,llvm::json::Path P)827 bool fromJSON(const llvm::json::Value &Params, CompletionParams &R,
828 llvm::json::Path P) {
829 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R), P))
830 return false;
831 if (auto *Context = Params.getAsObject()->get("context"))
832 return fromJSON(*Context, R.context, P.field("context"));
833 return true;
834 }
835
toTextKind(MarkupKind Kind)836 static llvm::StringRef toTextKind(MarkupKind Kind) {
837 switch (Kind) {
838 case MarkupKind::PlainText:
839 return "plaintext";
840 case MarkupKind::Markdown:
841 return "markdown";
842 }
843 llvm_unreachable("Invalid MarkupKind");
844 }
845
fromJSON(const llvm::json::Value & V,MarkupKind & K,llvm::json::Path P)846 bool fromJSON(const llvm::json::Value &V, MarkupKind &K, llvm::json::Path P) {
847 auto Str = V.getAsString();
848 if (!Str) {
849 P.report("expected string");
850 return false;
851 }
852 if (*Str == "plaintext")
853 K = MarkupKind::PlainText;
854 else if (*Str == "markdown")
855 K = MarkupKind::Markdown;
856 else {
857 P.report("unknown markup kind");
858 return false;
859 }
860 return true;
861 }
862
operator <<(llvm::raw_ostream & OS,MarkupKind K)863 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
864 return OS << toTextKind(K);
865 }
866
toJSON(const MarkupContent & MC)867 llvm::json::Value toJSON(const MarkupContent &MC) {
868 if (MC.value.empty())
869 return nullptr;
870
871 return llvm::json::Object{
872 {"kind", toTextKind(MC.kind)},
873 {"value", MC.value},
874 };
875 }
876
toJSON(const Hover & H)877 llvm::json::Value toJSON(const Hover &H) {
878 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
879
880 if (H.range.hasValue())
881 Result["range"] = toJSON(*H.range);
882
883 return std::move(Result);
884 }
885
fromJSON(const llvm::json::Value & E,CompletionItemKind & Out,llvm::json::Path P)886 bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out,
887 llvm::json::Path P) {
888 if (auto T = E.getAsInteger()) {
889 if (*T < static_cast<int>(CompletionItemKind::Text) ||
890 *T > static_cast<int>(CompletionItemKind::TypeParameter))
891 return false;
892 Out = static_cast<CompletionItemKind>(*T);
893 return true;
894 }
895 return false;
896 }
897
898 CompletionItemKind
adjustKindToCapability(CompletionItemKind Kind,CompletionItemKindBitset & SupportedCompletionItemKinds)899 adjustKindToCapability(CompletionItemKind Kind,
900 CompletionItemKindBitset &SupportedCompletionItemKinds) {
901 auto KindVal = static_cast<size_t>(Kind);
902 if (KindVal >= CompletionItemKindMin &&
903 KindVal <= SupportedCompletionItemKinds.size() &&
904 SupportedCompletionItemKinds[KindVal])
905 return Kind;
906
907 switch (Kind) {
908 // Provide some fall backs for common kinds that are close enough.
909 case CompletionItemKind::Folder:
910 return CompletionItemKind::File;
911 case CompletionItemKind::EnumMember:
912 return CompletionItemKind::Enum;
913 case CompletionItemKind::Struct:
914 return CompletionItemKind::Class;
915 default:
916 return CompletionItemKind::Text;
917 }
918 }
919
fromJSON(const llvm::json::Value & E,CompletionItemKindBitset & Out,llvm::json::Path P)920 bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out,
921 llvm::json::Path P) {
922 if (auto *A = E.getAsArray()) {
923 for (size_t I = 0; I < A->size(); ++I) {
924 CompletionItemKind KindOut;
925 if (fromJSON((*A)[I], KindOut, P.index(I)))
926 Out.set(size_t(KindOut));
927 }
928 return true;
929 }
930 return false;
931 }
932
toJSON(const CompletionItem & CI)933 llvm::json::Value toJSON(const CompletionItem &CI) {
934 assert(!CI.label.empty() && "completion item label is required");
935 llvm::json::Object Result{{"label", CI.label}};
936 if (CI.kind != CompletionItemKind::Missing)
937 Result["kind"] = static_cast<int>(CI.kind);
938 if (!CI.detail.empty())
939 Result["detail"] = CI.detail;
940 if (CI.documentation)
941 Result["documentation"] = CI.documentation;
942 if (!CI.sortText.empty())
943 Result["sortText"] = CI.sortText;
944 if (!CI.filterText.empty())
945 Result["filterText"] = CI.filterText;
946 if (!CI.insertText.empty())
947 Result["insertText"] = CI.insertText;
948 if (CI.insertTextFormat != InsertTextFormat::Missing)
949 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
950 if (CI.textEdit)
951 Result["textEdit"] = *CI.textEdit;
952 if (!CI.additionalTextEdits.empty())
953 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
954 if (CI.deprecated)
955 Result["deprecated"] = CI.deprecated;
956 Result["score"] = CI.score;
957 return std::move(Result);
958 }
959
operator <<(llvm::raw_ostream & O,const CompletionItem & I)960 llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
961 O << I.label << " - " << toJSON(I);
962 return O;
963 }
964
operator <(const CompletionItem & L,const CompletionItem & R)965 bool operator<(const CompletionItem &L, const CompletionItem &R) {
966 return (L.sortText.empty() ? L.label : L.sortText) <
967 (R.sortText.empty() ? R.label : R.sortText);
968 }
969
toJSON(const CompletionList & L)970 llvm::json::Value toJSON(const CompletionList &L) {
971 return llvm::json::Object{
972 {"isIncomplete", L.isIncomplete},
973 {"items", llvm::json::Array(L.items)},
974 };
975 }
976
toJSON(const ParameterInformation & PI)977 llvm::json::Value toJSON(const ParameterInformation &PI) {
978 assert((PI.labelOffsets.hasValue() || !PI.labelString.empty()) &&
979 "parameter information label is required");
980 llvm::json::Object Result;
981 if (PI.labelOffsets)
982 Result["label"] =
983 llvm::json::Array({PI.labelOffsets->first, PI.labelOffsets->second});
984 else
985 Result["label"] = PI.labelString;
986 if (!PI.documentation.empty())
987 Result["documentation"] = PI.documentation;
988 return std::move(Result);
989 }
990
toJSON(const SignatureInformation & SI)991 llvm::json::Value toJSON(const SignatureInformation &SI) {
992 assert(!SI.label.empty() && "signature information label is required");
993 llvm::json::Object Result{
994 {"label", SI.label},
995 {"parameters", llvm::json::Array(SI.parameters)},
996 };
997 if (!SI.documentation.empty())
998 Result["documentation"] = SI.documentation;
999 return std::move(Result);
1000 }
1001
operator <<(llvm::raw_ostream & O,const SignatureInformation & I)1002 llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
1003 const SignatureInformation &I) {
1004 O << I.label << " - " << toJSON(I);
1005 return O;
1006 }
1007
toJSON(const SignatureHelp & SH)1008 llvm::json::Value toJSON(const SignatureHelp &SH) {
1009 assert(SH.activeSignature >= 0 &&
1010 "Unexpected negative value for number of active signatures.");
1011 assert(SH.activeParameter >= 0 &&
1012 "Unexpected negative value for active parameter index");
1013 return llvm::json::Object{
1014 {"activeSignature", SH.activeSignature},
1015 {"activeParameter", SH.activeParameter},
1016 {"signatures", llvm::json::Array(SH.signatures)},
1017 };
1018 }
1019
fromJSON(const llvm::json::Value & Params,RenameParams & R,llvm::json::Path P)1020 bool fromJSON(const llvm::json::Value &Params, RenameParams &R,
1021 llvm::json::Path P) {
1022 llvm::json::ObjectMapper O(Params, P);
1023 return O && O.map("textDocument", R.textDocument) &&
1024 O.map("position", R.position) && O.map("newName", R.newName);
1025 }
1026
toJSON(const DocumentHighlight & DH)1027 llvm::json::Value toJSON(const DocumentHighlight &DH) {
1028 return llvm::json::Object{
1029 {"range", toJSON(DH.range)},
1030 {"kind", static_cast<int>(DH.kind)},
1031 };
1032 }
1033
toJSON(const FileStatus & FStatus)1034 llvm::json::Value toJSON(const FileStatus &FStatus) {
1035 return llvm::json::Object{
1036 {"uri", FStatus.uri},
1037 {"state", FStatus.state},
1038 };
1039 }
1040
1041 constexpr unsigned SemanticTokenEncodingSize = 5;
encodeTokens(llvm::ArrayRef<SemanticToken> Toks)1042 static llvm::json::Value encodeTokens(llvm::ArrayRef<SemanticToken> Toks) {
1043 llvm::json::Array Result;
1044 for (const auto &Tok : Toks) {
1045 Result.push_back(Tok.deltaLine);
1046 Result.push_back(Tok.deltaStart);
1047 Result.push_back(Tok.length);
1048 Result.push_back(Tok.tokenType);
1049 Result.push_back(Tok.tokenModifiers);
1050 }
1051 assert(Result.size() == SemanticTokenEncodingSize * Toks.size());
1052 return std::move(Result);
1053 }
1054
operator ==(const SemanticToken & L,const SemanticToken & R)1055 bool operator==(const SemanticToken &L, const SemanticToken &R) {
1056 return std::tie(L.deltaLine, L.deltaStart, L.length, L.tokenType,
1057 L.tokenModifiers) == std::tie(R.deltaLine, R.deltaStart,
1058 R.length, R.tokenType,
1059 R.tokenModifiers);
1060 }
1061
toJSON(const SemanticTokens & Tokens)1062 llvm::json::Value toJSON(const SemanticTokens &Tokens) {
1063 return llvm::json::Object{{"resultId", Tokens.resultId},
1064 {"data", encodeTokens(Tokens.tokens)}};
1065 }
1066
toJSON(const SemanticTokensEdit & Edit)1067 llvm::json::Value toJSON(const SemanticTokensEdit &Edit) {
1068 return llvm::json::Object{
1069 {"start", SemanticTokenEncodingSize * Edit.startToken},
1070 {"deleteCount", SemanticTokenEncodingSize * Edit.deleteTokens},
1071 {"data", encodeTokens(Edit.tokens)}};
1072 }
1073
toJSON(const SemanticTokensOrDelta & TE)1074 llvm::json::Value toJSON(const SemanticTokensOrDelta &TE) {
1075 llvm::json::Object Result{{"resultId", TE.resultId}};
1076 if (TE.edits)
1077 Result["edits"] = *TE.edits;
1078 if (TE.tokens)
1079 Result["data"] = encodeTokens(*TE.tokens);
1080 return std::move(Result);
1081 }
1082
fromJSON(const llvm::json::Value & Params,SemanticTokensParams & R,llvm::json::Path P)1083 bool fromJSON(const llvm::json::Value &Params, SemanticTokensParams &R,
1084 llvm::json::Path P) {
1085 llvm::json::ObjectMapper O(Params, P);
1086 return O && O.map("textDocument", R.textDocument);
1087 }
1088
fromJSON(const llvm::json::Value & Params,SemanticTokensDeltaParams & R,llvm::json::Path P)1089 bool fromJSON(const llvm::json::Value &Params, SemanticTokensDeltaParams &R,
1090 llvm::json::Path P) {
1091 llvm::json::ObjectMapper O(Params, P);
1092 return O && O.map("textDocument", R.textDocument) &&
1093 O.map("previousResultId", R.previousResultId);
1094 }
1095
operator <<(llvm::raw_ostream & O,const DocumentHighlight & V)1096 llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
1097 const DocumentHighlight &V) {
1098 O << V.range;
1099 if (V.kind == DocumentHighlightKind::Read)
1100 O << "(r)";
1101 if (V.kind == DocumentHighlightKind::Write)
1102 O << "(w)";
1103 return O;
1104 }
1105
fromJSON(const llvm::json::Value & Params,DidChangeConfigurationParams & CCP,llvm::json::Path P)1106 bool fromJSON(const llvm::json::Value &Params,
1107 DidChangeConfigurationParams &CCP, llvm::json::Path P) {
1108 llvm::json::ObjectMapper O(Params, P);
1109 return O && O.map("settings", CCP.settings);
1110 }
1111
fromJSON(const llvm::json::Value & Params,ClangdCompileCommand & CDbUpdate,llvm::json::Path P)1112 bool fromJSON(const llvm::json::Value &Params, ClangdCompileCommand &CDbUpdate,
1113 llvm::json::Path P) {
1114 llvm::json::ObjectMapper O(Params, P);
1115 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
1116 O.map("compilationCommand", CDbUpdate.compilationCommand);
1117 }
1118
fromJSON(const llvm::json::Value & Params,ConfigurationSettings & S,llvm::json::Path P)1119 bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S,
1120 llvm::json::Path P) {
1121 llvm::json::ObjectMapper O(Params, P);
1122 if (!O)
1123 return true; // 'any' type in LSP.
1124 return O.mapOptional("compilationDatabaseChanges",
1125 S.compilationDatabaseChanges);
1126 }
1127
fromJSON(const llvm::json::Value & Params,InitializationOptions & Opts,llvm::json::Path P)1128 bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts,
1129 llvm::json::Path P) {
1130 llvm::json::ObjectMapper O(Params, P);
1131 if (!O)
1132 return true; // 'any' type in LSP.
1133
1134 return fromJSON(Params, Opts.ConfigSettings, P) &&
1135 O.map("compilationDatabasePath", Opts.compilationDatabasePath) &&
1136 O.mapOptional("fallbackFlags", Opts.fallbackFlags) &&
1137 O.mapOptional("clangdFileStatus", Opts.FileStatus);
1138 }
1139
fromJSON(const llvm::json::Value & E,TypeHierarchyDirection & Out,llvm::json::Path P)1140 bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out,
1141 llvm::json::Path P) {
1142 auto T = E.getAsInteger();
1143 if (!T)
1144 return false;
1145 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
1146 *T > static_cast<int>(TypeHierarchyDirection::Both))
1147 return false;
1148 Out = static_cast<TypeHierarchyDirection>(*T);
1149 return true;
1150 }
1151
fromJSON(const llvm::json::Value & Params,TypeHierarchyParams & R,llvm::json::Path P)1152 bool fromJSON(const llvm::json::Value &Params, TypeHierarchyParams &R,
1153 llvm::json::Path P) {
1154 llvm::json::ObjectMapper O(Params, P);
1155 return O && O.map("textDocument", R.textDocument) &&
1156 O.map("position", R.position) && O.map("resolve", R.resolve) &&
1157 O.map("direction", R.direction);
1158 }
1159
operator <<(llvm::raw_ostream & O,const TypeHierarchyItem & I)1160 llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
1161 const TypeHierarchyItem &I) {
1162 return O << I.name << " - " << toJSON(I);
1163 }
1164
toJSON(const TypeHierarchyItem & I)1165 llvm::json::Value toJSON(const TypeHierarchyItem &I) {
1166 llvm::json::Object Result{{"name", I.name},
1167 {"kind", static_cast<int>(I.kind)},
1168 {"range", I.range},
1169 {"selectionRange", I.selectionRange},
1170 {"uri", I.uri}};
1171
1172 if (I.detail)
1173 Result["detail"] = I.detail;
1174 if (I.deprecated)
1175 Result["deprecated"] = I.deprecated;
1176 if (I.parents)
1177 Result["parents"] = I.parents;
1178 if (I.children)
1179 Result["children"] = I.children;
1180 if (I.data)
1181 Result["data"] = I.data;
1182 return std::move(Result);
1183 }
1184
fromJSON(const llvm::json::Value & Params,TypeHierarchyItem & I,llvm::json::Path P)1185 bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I,
1186 llvm::json::Path P) {
1187 llvm::json::ObjectMapper O(Params, P);
1188
1189 // Required fields.
1190 return O && O.map("name", I.name) && O.map("kind", I.kind) &&
1191 O.map("uri", I.uri) && O.map("range", I.range) &&
1192 O.map("selectionRange", I.selectionRange) &&
1193 O.mapOptional("detail", I.detail) &&
1194 O.mapOptional("deprecated", I.deprecated) &&
1195 O.mapOptional("parents", I.parents) &&
1196 O.mapOptional("children", I.children) && O.mapOptional("data", I.data);
1197 }
1198
fromJSON(const llvm::json::Value & Params,ResolveTypeHierarchyItemParams & R,llvm::json::Path P)1199 bool fromJSON(const llvm::json::Value &Params,
1200 ResolveTypeHierarchyItemParams &R, llvm::json::Path P) {
1201 llvm::json::ObjectMapper O(Params, P);
1202 return O && O.map("item", R.item) && O.map("resolve", R.resolve) &&
1203 O.map("direction", R.direction);
1204 }
1205
fromJSON(const llvm::json::Value & Params,ReferenceParams & R,llvm::json::Path P)1206 bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R,
1207 llvm::json::Path P) {
1208 TextDocumentPositionParams &Base = R;
1209 return fromJSON(Params, Base, P);
1210 }
1211
toJSON(SymbolTag Tag)1212 llvm::json::Value toJSON(SymbolTag Tag) {
1213 return llvm::json::Value{static_cast<int>(Tag)};
1214 }
1215
toJSON(const CallHierarchyItem & I)1216 llvm::json::Value toJSON(const CallHierarchyItem &I) {
1217 llvm::json::Object Result{{"name", I.name},
1218 {"kind", static_cast<int>(I.kind)},
1219 {"range", I.range},
1220 {"selectionRange", I.selectionRange},
1221 {"uri", I.uri}};
1222 if (!I.tags.empty())
1223 Result["tags"] = I.tags;
1224 if (!I.detail.empty())
1225 Result["detail"] = I.detail;
1226 if (!I.data.empty())
1227 Result["data"] = I.data;
1228 return std::move(Result);
1229 }
1230
fromJSON(const llvm::json::Value & Params,CallHierarchyItem & I,llvm::json::Path P)1231 bool fromJSON(const llvm::json::Value &Params, CallHierarchyItem &I,
1232 llvm::json::Path P) {
1233 llvm::json::ObjectMapper O(Params, P);
1234
1235 // Populate the required fields only. We don't care about the
1236 // optional fields `Tags` and `Detail` for the purpose of
1237 // client --> server communication.
1238 return O && O.map("name", I.name) && O.map("kind", I.kind) &&
1239 O.map("uri", I.uri) && O.map("range", I.range) &&
1240 O.map("selectionRange", I.selectionRange) &&
1241 O.mapOptional("data", I.data);
1242 }
1243
fromJSON(const llvm::json::Value & Params,CallHierarchyIncomingCallsParams & C,llvm::json::Path P)1244 bool fromJSON(const llvm::json::Value &Params,
1245 CallHierarchyIncomingCallsParams &C, llvm::json::Path P) {
1246 llvm::json::ObjectMapper O(Params, P);
1247 return O.map("item", C.item);
1248 }
1249
toJSON(const CallHierarchyIncomingCall & C)1250 llvm::json::Value toJSON(const CallHierarchyIncomingCall &C) {
1251 return llvm::json::Object{{"from", C.from}, {"fromRanges", C.fromRanges}};
1252 }
1253
fromJSON(const llvm::json::Value & Params,CallHierarchyOutgoingCallsParams & C,llvm::json::Path P)1254 bool fromJSON(const llvm::json::Value &Params,
1255 CallHierarchyOutgoingCallsParams &C, llvm::json::Path P) {
1256 llvm::json::ObjectMapper O(Params, P);
1257 return O.map("item", C.item);
1258 }
1259
toJSON(const CallHierarchyOutgoingCall & C)1260 llvm::json::Value toJSON(const CallHierarchyOutgoingCall &C) {
1261 return llvm::json::Object{{"to", C.to}, {"fromRanges", C.fromRanges}};
1262 }
1263
toString(OffsetEncoding OE)1264 static const char *toString(OffsetEncoding OE) {
1265 switch (OE) {
1266 case OffsetEncoding::UTF8:
1267 return "utf-8";
1268 case OffsetEncoding::UTF16:
1269 return "utf-16";
1270 case OffsetEncoding::UTF32:
1271 return "utf-32";
1272 case OffsetEncoding::UnsupportedEncoding:
1273 return "unknown";
1274 }
1275 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
1276 }
toJSON(const OffsetEncoding & OE)1277 llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
fromJSON(const llvm::json::Value & V,OffsetEncoding & OE,llvm::json::Path P)1278 bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE,
1279 llvm::json::Path P) {
1280 auto Str = V.getAsString();
1281 if (!Str)
1282 return false;
1283 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
1284 .Case("utf-8", OffsetEncoding::UTF8)
1285 .Case("utf-16", OffsetEncoding::UTF16)
1286 .Case("utf-32", OffsetEncoding::UTF32)
1287 .Default(OffsetEncoding::UnsupportedEncoding);
1288 return true;
1289 }
operator <<(llvm::raw_ostream & OS,OffsetEncoding Enc)1290 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
1291 return OS << toString(Enc);
1292 }
1293
operator ==(const TheiaSemanticHighlightingInformation & Lhs,const TheiaSemanticHighlightingInformation & Rhs)1294 bool operator==(const TheiaSemanticHighlightingInformation &Lhs,
1295 const TheiaSemanticHighlightingInformation &Rhs) {
1296 return Lhs.Line == Rhs.Line && Lhs.Tokens == Rhs.Tokens;
1297 }
1298
1299 llvm::json::Value
toJSON(const TheiaSemanticHighlightingInformation & Highlighting)1300 toJSON(const TheiaSemanticHighlightingInformation &Highlighting) {
1301 return llvm::json::Object{{"line", Highlighting.Line},
1302 {"tokens", Highlighting.Tokens},
1303 {"isInactive", Highlighting.IsInactive}};
1304 }
1305
toJSON(const TheiaSemanticHighlightingParams & Highlighting)1306 llvm::json::Value toJSON(const TheiaSemanticHighlightingParams &Highlighting) {
1307 return llvm::json::Object{
1308 {"textDocument", Highlighting.TextDocument},
1309 {"lines", std::move(Highlighting.Lines)},
1310 };
1311 }
1312
fromJSON(const llvm::json::Value & Params,SelectionRangeParams & S,llvm::json::Path P)1313 bool fromJSON(const llvm::json::Value &Params, SelectionRangeParams &S,
1314 llvm::json::Path P) {
1315 llvm::json::ObjectMapper O(Params, P);
1316 return O && O.map("textDocument", S.textDocument) &&
1317 O.map("positions", S.positions);
1318 }
1319
toJSON(const SelectionRange & Out)1320 llvm::json::Value toJSON(const SelectionRange &Out) {
1321 if (Out.parent) {
1322 return llvm::json::Object{{"range", Out.range},
1323 {"parent", toJSON(*Out.parent)}};
1324 }
1325 return llvm::json::Object{{"range", Out.range}};
1326 }
1327
fromJSON(const llvm::json::Value & Params,DocumentLinkParams & R,llvm::json::Path P)1328 bool fromJSON(const llvm::json::Value &Params, DocumentLinkParams &R,
1329 llvm::json::Path P) {
1330 llvm::json::ObjectMapper O(Params, P);
1331 return O && O.map("textDocument", R.textDocument);
1332 }
1333
toJSON(const DocumentLink & DocumentLink)1334 llvm::json::Value toJSON(const DocumentLink &DocumentLink) {
1335 return llvm::json::Object{
1336 {"range", DocumentLink.range},
1337 {"target", DocumentLink.target},
1338 };
1339 }
1340
fromJSON(const llvm::json::Value & Params,FoldingRangeParams & R,llvm::json::Path P)1341 bool fromJSON(const llvm::json::Value &Params, FoldingRangeParams &R,
1342 llvm::json::Path P) {
1343 llvm::json::ObjectMapper O(Params, P);
1344 return O && O.map("textDocument", R.textDocument);
1345 }
1346
toJSON(const FoldingRange & Range)1347 llvm::json::Value toJSON(const FoldingRange &Range) {
1348 llvm::json::Object Result{
1349 {"startLine", Range.startLine},
1350 {"endLine", Range.endLine},
1351 };
1352 if (Range.startCharacter)
1353 Result["startCharacter"] = Range.startCharacter;
1354 if (Range.endCharacter)
1355 Result["endCharacter"] = Range.endCharacter;
1356 if (Range.kind)
1357 Result["kind"] = *Range.kind;
1358 return Result;
1359 }
1360
toJSON(const MemoryTree & MT)1361 llvm::json::Value toJSON(const MemoryTree &MT) {
1362 llvm::json::Object Out;
1363 int64_t Total = MT.self();
1364 Out["_self"] = Total;
1365 for (const auto &Entry : MT.children()) {
1366 auto Child = toJSON(Entry.getSecond());
1367 Total += *Child.getAsObject()->getInteger("_total");
1368 Out[Entry.first] = std::move(Child);
1369 }
1370 Out["_total"] = Total;
1371 return Out;
1372 }
1373
fromJSON(const llvm::json::Value & Params,ASTParams & R,llvm::json::Path P)1374 bool fromJSON(const llvm::json::Value &Params, ASTParams &R,
1375 llvm::json::Path P) {
1376 llvm::json::ObjectMapper O(Params, P);
1377 return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
1378 }
1379
toJSON(const ASTNode & N)1380 llvm::json::Value toJSON(const ASTNode &N) {
1381 llvm::json::Object Result{
1382 {"role", N.role},
1383 {"kind", N.kind},
1384 };
1385 if (!N.children.empty())
1386 Result["children"] = N.children;
1387 if (!N.detail.empty())
1388 Result["detail"] = N.detail;
1389 if (!N.arcana.empty())
1390 Result["arcana"] = N.arcana;
1391 if (N.range)
1392 Result["range"] = *N.range;
1393 return Result;
1394 }
1395
operator <<(llvm::raw_ostream & OS,const ASTNode & Root)1396 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const ASTNode &Root) {
1397 std::function<void(const ASTNode &, unsigned)> Print = [&](const ASTNode &N,
1398 unsigned Level) {
1399 OS.indent(2 * Level) << N.role << ": " << N.kind;
1400 if (!N.detail.empty())
1401 OS << " - " << N.detail;
1402 OS << "\n";
1403 for (const ASTNode &C : N.children)
1404 Print(C, Level + 1);
1405 };
1406 Print(Root, 0);
1407 return OS;
1408 }
1409
1410 } // namespace clangd
1411 } // namespace clang
1412