| 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 |
|
| 25 |
namespace clang { |
| 26 |
namespace clangd { |
| 27 |
namespace { |
| 28 |
|
| 29 |
// Helper that doesn't treat `null` and absent fields as failures. |
| 30 |
template <typename T> |
| 31 |
bool 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 |
|
| 43 |
char LSPError::ID; |
| 44 |
|
| 45 |
URIForFile 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 |
|
| 58 |
llvm::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 |
|
| 66 |
bool 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 |
|
| 91 |
llvm::json::Value toJSON(const URIForFile &U) { return U.uri(); } |
| 92 |
|
| 93 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const URIForFile &U) { |
| 94 |
return OS << U.uri(); |
| 95 |
} |
| 96 |
|
| 97 |
llvm::json::Value toJSON(const TextDocumentIdentifier &R) { |
| 98 |
return llvm::json::Object{{"uri", R.uri}}; |
| 99 |
} |
| 100 |
|
| 101 |
bool 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 |
|
| 107 |
llvm::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 |
|
| 113 |
bool 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 |
|
| 120 |
bool 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 |
|
| 126 |
llvm::json::Value toJSON(const Position &P) { |
| 127 |
return llvm::json::Object{ |
| 128 |
{"line", P.line}, |
| 129 |
{"character", P.character}, |
| 130 |
}; |
| 131 |
} |
| 132 |
|
| 133 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &P) { |
| 134 |
return OS << P.line << ':' << P.character; |
| 135 |
} |
| 136 |
|
| 137 |
bool 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 |
|
| 142 |
llvm::json::Value toJSON(const Range &P) { |
| 143 |
return llvm::json::Object{ |
| 144 |
{"start", P.start}, |
| 145 |
{"end", P.end}, |
| 146 |
}; |
| 147 |
} |
| 148 |
|
| 149 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Range &R) { |
| 150 |
return OS << R.start << '-' << R.end; |
| 151 |
} |
| 152 |
|
| 153 |
llvm::json::Value toJSON(const Location &P) { |
| 154 |
return llvm::json::Object{ |
| 155 |
{"uri", P.uri}, |
| 156 |
{"range", P.range}, |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Location &L) { |
| 161 |
return OS << L.range << '@' << L.uri; |
| 162 |
} |
| 163 |
|
| 164 |
llvm::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 |
|
| 174 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 175 |
const ReferenceLocation &L) { |
| 176 |
return OS << L.range << '@' << L.uri << " (container: " << L.containerName |
| 177 |
<< ")"; |
| 178 |
} |
| 179 |
|
| 180 |
bool 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 |
|
| 187 |
bool 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 |
|
| 193 |
llvm::json::Value toJSON(const TextEdit &P) { |
| 194 |
return llvm::json::Object{ |
| 195 |
{"range", P.range}, |
| 196 |
{"newText", P.newText}, |
| 197 |
}; |
| 198 |
} |
| 199 |
|
| 200 |
llvm::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 |
|
| 206 |
bool fromJSON(const llvm::json::Value &E, TraceLevel &Out, llvm::json::Path P) { |
| 207 |
if (auto S = E.getAsString()) { |
| 208 |
if (*S == "off") { |
| 209 |
Out = TraceLevel::Off; |
| 210 |
return true; |
| 211 |
} |
| 212 |
if (*S == "messages") { |
| 213 |
Out = TraceLevel::Messages; |
| 214 |
return true; |
| 215 |
} |
| 216 |
if (*S == "verbose") { |
| 217 |
Out = TraceLevel::Verbose; |
| 218 |
return true; |
| 219 |
} |
| 220 |
} |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
bool 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 |
|
| 235 |
bool 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 |
|
| 248 |
SymbolKind adjustKindToCapability(SymbolKind Kind, |
| 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 |
|
| 266 |
SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind) { |
| 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 |
|
| 327 |
bool 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) { |
| 407 |
if (fromJSON(Format, R.SignatureHelpDocumentationFormat, P)) |
| 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(); |
| 455 |
if (!fromJSON(*OffsetEncoding, *R.offsetEncoding, |
| 456 |
P.field("offsetEncoding"))) |
| 457 |
return false; |
| 458 |
} |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
bool 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 |
|
| 480 |
llvm::json::Value toJSON(const WorkDoneProgressCreateParams &P) { |
| 481 |
return llvm::json::Object{{"token", P.token}}; |
| 482 |
} |
| 483 |
|
| 484 |
llvm::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 |
|
| 498 |
llvm::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 |
|
| 510 |
llvm::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 |
|
| 518 |
llvm::json::Value toJSON(const MessageType &R) { |
| 519 |
return static_cast<int64_t>(R); |
| 520 |
} |
| 521 |
|
| 522 |
llvm::json::Value toJSON(const ShowMessageParams &R) { |
| 523 |
return llvm::json::Object{{"type", R.type}, {"message", R.message}}; |
| 524 |
} |
| 525 |
|
| 526 |
bool 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 |
|
| 532 |
bool 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 |
|
| 538 |
bool 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 |
|
| 544 |
bool 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 |
|
| 553 |
bool 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 |
|
| 565 |
bool 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 |
|
| 571 |
bool 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 |
|
| 577 |
bool 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 |
|
| 584 |
bool 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 |
|
| 590 |
bool 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 |
|
| 597 |
bool 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 |
|
| 603 |
bool 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 |
|
| 609 |
llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) { |
| 610 |
return llvm::json::Object{ |
| 611 |
{"location", DRI.location}, |
| 612 |
{"message", DRI.message}, |
| 613 |
}; |
| 614 |
} |
| 615 |
|
| 616 |
llvm::json::Value toJSON(DiagnosticTag Tag) { return static_cast<int>(Tag); } |
| 617 |
|
| 618 |
llvm::json::Value toJSON(const CodeDescription &D) { |
| 619 |
return llvm::json::Object{{"href", D.href}}; |
| 620 |
} |
| 621 |
|
| 622 |
llvm::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 |
|
| 648 |
bool 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 |
|
| 662 |
llvm::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 |
|
| 672 |
bool 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 |
|
| 681 |
llvm::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 |
|
| 703 |
bool 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 |
|
| 710 |
bool 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 |
|
| 716 |
bool 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 |
|
| 740 |
llvm::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 |
|
| 752 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &O, |
| 753 |
const SymbolInformation &SI) { |
| 754 |
O << SI.containerName << "::" << SI.name << " - " << toJSON(SI); |
| 755 |
return O; |
| 756 |
} |
| 757 |
|
| 758 |
bool 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 && |
| 761 |
LHS.declarationRange == RHS.declarationRange && |
| 762 |
LHS.definitionRange == RHS.definitionRange; |
| 763 |
} |
| 764 |
|
| 765 |
llvm::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 |
|
| 793 |
llvm::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 |
|
| 805 |
bool 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 |
|
| 812 |
llvm::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 |
|
| 819 |
const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix"; |
| 820 |
const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor"; |
| 821 |
const llvm::StringLiteral CodeAction::INFO_KIND = "info"; |
| 822 |
|
| 823 |
llvm::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 |
|
| 838 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) { |
| 839 |
return O << S.name << " - " << toJSON(S); |
| 840 |
} |
| 841 |
|
| 842 |
llvm::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 |
|
| 858 |
llvm::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 |
|
| 865 |
bool 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 |
|
| 872 |
llvm::json::Value toJSON(const TweakArgs &A) { |
| 873 |
return llvm::json::Object{ |
| 874 |
{"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}}; |
| 875 |
} |
| 876 |
|
| 877 |
llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) { |
| 878 |
return llvm::json::Object{{"edit", Params.edit}}; |
| 879 |
} |
| 880 |
|
| 881 |
bool 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 |
|
| 888 |
bool 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 |
|
| 895 |
bool 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 |
|
| 906 |
bool 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 |
|
| 916 |
static llvm::StringRef toTextKind(MarkupKind Kind) { |
| 917 |
switch (Kind) { |
| 918 |
case MarkupKind::PlainText: |
| 919 |
return "plaintext"; |
| 920 |
case MarkupKind::Markdown: |
| 921 |
return "markdown"; |
| 922 |
} |
| 923 |
llvm_unreachable("Invalid MarkupKind"); |
| 924 |
} |
| 925 |
|
| 926 |
bool 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") |
| 933 |
K = MarkupKind::PlainText; |
| 934 |
else if (*Str == "markdown") |
| 935 |
K = MarkupKind::Markdown; |
| 936 |
else { |
| 937 |
P.report("unknown markup kind"); |
| 938 |
return false; |
| 939 |
} |
| 940 |
return true; |
| 941 |
} |
| 942 |
|
| 943 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) { |
| 944 |
return OS << toTextKind(K); |
| 945 |
} |
| 946 |
|
| 947 |
llvm::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 |
|
| 957 |
llvm::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 |
|
| 966 |
bool 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 |
|
| 978 |
CompletionItemKind |
| 979 |
adjustKindToCapability(CompletionItemKind Kind, |
| 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. |
| 989 |
case CompletionItemKind::Folder: |
| 990 |
return CompletionItemKind::File; |
| 991 |
case CompletionItemKind::EnumMember: |
| 992 |
return CompletionItemKind::Enum; |
| 993 |
case CompletionItemKind::Struct: |
| 994 |
return CompletionItemKind::Class; |
| 995 |
default: |
| 996 |
return CompletionItemKind::Text; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
bool 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 |
|
| 1013 |
llvm::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 |
|
| 1040 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) { |
| 1041 |
O << I.label << " - " << toJSON(I); |
| 1042 |
return O; |
| 1043 |
} |
| 1044 |
|
| 1045 |
bool 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 |
|
| 1050 |
llvm::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 |
|
| 1057 |
llvm::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 |
|
| 1071 |
llvm::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 |
|
| 1082 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &O, |
| 1083 |
const SignatureInformation &I) { |
| 1084 |
O << I.label << " - " << toJSON(I); |
| 1085 |
return O; |
| 1086 |
} |
| 1087 |
|
| 1088 |
llvm::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 |
|
| 1100 |
bool 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 |
|
| 1107 |
llvm::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 |
|
| 1114 |
llvm::json::Value toJSON(const FileStatus &FStatus) { |
| 1115 |
return llvm::json::Object{ |
| 1116 |
{"uri", FStatus.uri}, |
| 1117 |
{"state", FStatus.state}, |
| 1118 |
}; |
| 1119 |
} |
| 1120 |
|
| 1121 |
constexpr unsigned SemanticTokenEncodingSize = 5; |
| 1122 |
static 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 |
|
| 1136 |
bool 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 |
|
| 1143 |
llvm::json::Value toJSON(const SemanticTokens &Tokens) { |
| 1144 |
return llvm::json::Object{{"resultId", Tokens.resultId}, |
| 1145 |
{"data", encodeTokens(Tokens.tokens)}}; |
| 1146 |
} |
| 1147 |
|
| 1148 |
llvm::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 |
|
| 1155 |
llvm::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 |
|
| 1164 |
bool 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 |
|
| 1170 |
bool 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 |
|
| 1177 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &O, |
| 1178 |
const DocumentHighlight &V) { |
| 1179 |
O << V.range; |
| 1180 |
if (V.kind == DocumentHighlightKind::Read) |
| 1181 |
O << "(r)"; |
| 1182 |
if (V.kind == DocumentHighlightKind::Write) |
| 1183 |
O << "(w)"; |
| 1184 |
return O; |
| 1185 |
} |
| 1186 |
|
| 1187 |
bool 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 |
|
| 1193 |
bool 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 |
|
| 1200 |
bool 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", |
| 1206 |
S.compilationDatabaseChanges, P); |
| 1207 |
} |
| 1208 |
|
| 1209 |
bool 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 |
|
| 1221 |
bool 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 |
|
| 1233 |
bool 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 |
|
| 1242 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &O, |
| 1243 |
const TypeHierarchyItem &I) { |
| 1244 |
return O << I.name << " - " << toJSON(I); |
| 1245 |
} |
| 1246 |
|
| 1247 |
llvm::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 |
} |
| 1253 |
bool 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 |
|
| 1260 |
llvm::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 |
|
| 1272 |
bool 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 |
|
| 1287 |
bool 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 |
|
| 1295 |
bool 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 |
|
| 1301 |
bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R, |
| 1302 |
llvm::json::Path P) { |
| 1303 |
TextDocumentPositionParams &Base = R; |
| 1304 |
llvm::json::ObjectMapper O(Params, P); |
| 1305 |
return fromJSON(Params, Base, P) && O && O.mapOptional("context", R.context); |
| 1306 |
} |
| 1307 |
|
| 1308 |
llvm::json::Value toJSON(SymbolTag Tag) { |
| 1309 |
return llvm::json::Value{static_cast<int>(Tag)}; |
| 1310 |
} |
| 1311 |
|
| 1312 |
llvm::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 |
|
| 1327 |
bool 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 |
|
| 1340 |
bool 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 |
|
| 1346 |
llvm::json::Value toJSON(const CallHierarchyIncomingCall &C) { |
| 1347 |
return llvm::json::Object{{"from", C.from}, {"fromRanges", C.fromRanges}}; |
| 1348 |
} |
| 1349 |
|
| 1350 |
bool 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 |
|
| 1356 |
llvm::json::Value toJSON(const CallHierarchyOutgoingCall &C) { |
| 1357 |
return llvm::json::Object{{"to", C.to}, {"fromRanges", C.fromRanges}}; |
| 1358 |
} |
| 1359 |
|
| 1360 |
bool 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 |
|
| 1366 |
llvm::json::Value toJSON(const InlayHintKind &Kind) { |
| 1367 |
switch (Kind) { |
| 1368 |
case InlayHintKind::Type: |
| 1369 |
return 1; |
| 1370 |
case InlayHintKind::Parameter: |
| 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 |
|
| 1378 |
llvm::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 |
} |
| 1388 |
bool 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 |
} |
| 1392 |
bool 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 |
|
| 1397 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, InlayHintKind Kind) { |
| 1398 |
auto ToString = [](InlayHintKind K) { |
| 1399 |
switch (K) { |
| 1400 |
case InlayHintKind::Parameter: |
| 1401 |
return "parameter"; |
| 1402 |
case InlayHintKind::Type: |
| 1403 |
return "type"; |
| 1404 |
case InlayHintKind::Designator: |
| 1405 |
return "designator"; |
| 1406 |
} |
| 1407 |
llvm_unreachable("Unknown clang.clangd.InlayHintKind"); |
| 1408 |
}; |
| 1409 |
return OS << ToString(Kind); |
| 1410 |
} |
| 1411 |
|
| 1412 |
static const char *toString(OffsetEncoding OE) { |
| 1413 |
switch (OE) { |
| 1414 |
case OffsetEncoding::UTF8: |
| 1415 |
return "utf-8"; |
| 1416 |
case OffsetEncoding::UTF16: |
| 1417 |
return "utf-16"; |
| 1418 |
case OffsetEncoding::UTF32: |
| 1419 |
return "utf-32"; |
| 1420 |
case OffsetEncoding::UnsupportedEncoding: |
| 1421 |
return "unknown"; |
| 1422 |
} |
| 1423 |
llvm_unreachable("Unknown clang.clangd.OffsetEncoding"); |
| 1424 |
} |
| 1425 |
llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); } |
| 1426 |
bool 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) |
| 1435 |
.Default(OffsetEncoding::UnsupportedEncoding); |
| 1436 |
return true; |
| 1437 |
} |
| 1438 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) { |
| 1439 |
return OS << toString(Enc); |
| 1440 |
} |
| 1441 |
|
| 1442 |
bool 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 |
|
| 1449 |
llvm::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 |
|
| 1457 |
bool 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 |
|
| 1463 |
llvm::json::Value toJSON(const DocumentLink &DocumentLink) { |
| 1464 |
return llvm::json::Object{ |
| 1465 |
{"range", DocumentLink.range}, |
| 1466 |
{"target", DocumentLink.target}, |
| 1467 |
}; |
| 1468 |
} |
| 1469 |
|
| 1470 |
bool 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 |
|
| 1476 |
const llvm::StringLiteral FoldingRange::REGION_KIND = "region"; |
| 1477 |
const llvm::StringLiteral FoldingRange::COMMENT_KIND = "comment"; |
| 1478 |
const llvm::StringLiteral FoldingRange::IMPORT_KIND = "import"; |
| 1479 |
|
| 1480 |
llvm::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 |
|
| 1494 |
llvm::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 |
|
| 1507 |
bool 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 |
|
| 1513 |
llvm::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 |
|
| 1529 |
llvm::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 |
|
| 1543 |
bool 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 |
} |
| 1558 |
llvm::json::Value toJSON(const SymbolID &S) { return S.str(); } |
| 1559 |
|
| 1560 |
} // namespace clangd |
| 1561 |
} // namespace clang |