| 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 "llvm/Support/LSP/Protocol.h" |
| 14 | #include "llvm/ADT/StringExtras.h" |
| 15 | #include "llvm/ADT/StringSet.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
| 17 | #include "llvm/Support/JSON.h" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | #include "llvm/Support/Path.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace llvm::lsp; |
| 24 | |
| 25 | // Helper that doesn't treat `null` and absent fields as failures. |
| 26 | template <typename T> |
| 27 | static bool mapOptOrNull(const llvm::json::Value &Params, |
| 28 | llvm::StringLiteral Prop, T &Out, |
| 29 | llvm::json::Path Path) { |
| 30 | const llvm::json::Object *O = Params.getAsObject(); |
| 31 | assert(O); |
| 32 | |
| 33 | // Field is missing or null. |
| 34 | auto *V = O->get(K: Prop); |
| 35 | if (!V || V->getAsNull()) |
| 36 | return true; |
| 37 | return fromJSON(*V, Out, Path.field(Field: Prop)); |
| 38 | } |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // LSPError |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | char LSPError::ID; |
| 45 | |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | // URIForFile |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
| 50 | static bool isWindowsPath(StringRef Path) { |
| 51 | return Path.size() > 1 && llvm::isAlpha(C: Path[0]) && Path[1] == ':'; |
| 52 | } |
| 53 | |
| 54 | static bool isNetworkPath(StringRef Path) { |
| 55 | return Path.size() > 2 && Path[0] == Path[1] && |
| 56 | llvm::sys::path::is_separator(value: Path[0]); |
| 57 | } |
| 58 | |
| 59 | static bool shouldEscapeInURI(unsigned char C) { |
| 60 | // Unreserved characters. |
| 61 | if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') || |
| 62 | (C >= '0' && C <= '9')) |
| 63 | return false; |
| 64 | |
| 65 | switch (C) { |
| 66 | case '-': |
| 67 | case '_': |
| 68 | case '.': |
| 69 | case '~': |
| 70 | // '/' is only reserved when parsing. |
| 71 | case '/': |
| 72 | // ':' is only reserved for relative URI paths, which we doesn't produce. |
| 73 | case ':': |
| 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /// Encodes a string according to percent-encoding. |
| 80 | /// - Unreserved characters are not escaped. |
| 81 | /// - Reserved characters always escaped with exceptions like '/'. |
| 82 | /// - All other characters are escaped. |
| 83 | static void percentEncode(StringRef Content, std::string &Out) { |
| 84 | for (unsigned char C : Content) { |
| 85 | if (shouldEscapeInURI(C)) { |
| 86 | Out.push_back(c: '%'); |
| 87 | Out.push_back(c: llvm::hexdigit(X: C / 16)); |
| 88 | Out.push_back(c: llvm::hexdigit(X: C % 16)); |
| 89 | } else { |
| 90 | Out.push_back(c: C); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Decodes a string according to percent-encoding. |
| 96 | static std::string percentDecode(StringRef Content) { |
| 97 | std::string Result; |
| 98 | for (auto I = Content.begin(), E = Content.end(); I != E; ++I) { |
| 99 | if (*I == '%' && I + 2 < Content.end() && llvm::isHexDigit(C: *(I + 1)) && |
| 100 | llvm::isHexDigit(C: *(I + 2))) { |
| 101 | Result.push_back(c: llvm::hexFromNibbles(MSB: *(I + 1), LSB: *(I + 2))); |
| 102 | I += 2; |
| 103 | } else { |
| 104 | Result.push_back(c: *I); |
| 105 | } |
| 106 | } |
| 107 | return Result; |
| 108 | } |
| 109 | |
| 110 | /// Return the set containing the supported URI schemes. |
| 111 | static StringSet<> &getSupportedSchemes() { |
| 112 | static StringSet<> Schemes({"file" , "test" }); |
| 113 | return Schemes; |
| 114 | } |
| 115 | |
| 116 | /// Returns true if the given scheme is structurally valid, i.e. it does not |
| 117 | /// contain any invalid scheme characters. This does not check that the scheme |
| 118 | /// is actually supported. |
| 119 | static bool isStructurallyValidScheme(StringRef Scheme) { |
| 120 | if (Scheme.empty()) |
| 121 | return false; |
| 122 | if (!llvm::isAlpha(C: Scheme[0])) |
| 123 | return false; |
| 124 | return llvm::all_of(Range: llvm::drop_begin(RangeOrContainer&: Scheme), P: [](char C) { |
| 125 | return llvm::isAlnum(C) || C == '+' || C == '.' || C == '-'; |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | static llvm::Expected<std::string> uriFromAbsolutePath(StringRef AbsolutePath, |
| 130 | StringRef Scheme) { |
| 131 | std::string Body; |
| 132 | StringRef Authority; |
| 133 | StringRef Root = llvm::sys::path::root_name(path: AbsolutePath); |
| 134 | if (isNetworkPath(Path: Root)) { |
| 135 | // Windows UNC paths e.g. \\server\share => file://server/share |
| 136 | Authority = Root.drop_front(N: 2); |
| 137 | AbsolutePath.consume_front(Prefix: Root); |
| 138 | } else if (isWindowsPath(Path: Root)) { |
| 139 | // Windows paths e.g. X:\path => file:///X:/path |
| 140 | Body = "/" ; |
| 141 | } |
| 142 | Body += llvm::sys::path::convert_to_slash(path: AbsolutePath); |
| 143 | |
| 144 | std::string Uri = Scheme.str() + ":" ; |
| 145 | if (Authority.empty() && Body.empty()) |
| 146 | return Uri; |
| 147 | |
| 148 | // If authority if empty, we only print body if it starts with "/"; otherwise, |
| 149 | // the URI is invalid. |
| 150 | if (!Authority.empty() || StringRef(Body).starts_with(Prefix: "/" )) { |
| 151 | Uri.append(s: "//" ); |
| 152 | percentEncode(Content: Authority, Out&: Uri); |
| 153 | } |
| 154 | percentEncode(Content: Body, Out&: Uri); |
| 155 | return Uri; |
| 156 | } |
| 157 | |
| 158 | static llvm::Expected<std::string> getAbsolutePath(StringRef Authority, |
| 159 | StringRef Body) { |
| 160 | if (!Body.starts_with(Prefix: "/" )) |
| 161 | return llvm::createStringError( |
| 162 | EC: llvm::inconvertibleErrorCode(), |
| 163 | S: "File scheme: expect body to be an absolute path starting " |
| 164 | "with '/': " + |
| 165 | Body); |
| 166 | SmallString<128> Path; |
| 167 | if (!Authority.empty()) { |
| 168 | // Windows UNC paths e.g. file://server/share => \\server\share |
| 169 | ("//" + Authority).toVector(Out&: Path); |
| 170 | } else if (isWindowsPath(Path: Body.substr(Start: 1))) { |
| 171 | // Windows paths e.g. file:///X:/path => X:\path |
| 172 | Body.consume_front(Prefix: "/" ); |
| 173 | } |
| 174 | Path.append(RHS: Body); |
| 175 | llvm::sys::path::native(path&: Path); |
| 176 | return std::string(Path); |
| 177 | } |
| 178 | |
| 179 | static llvm::Expected<std::string> parseFilePathFromURI(StringRef OrigUri) { |
| 180 | StringRef Uri = OrigUri; |
| 181 | |
| 182 | // Decode the scheme of the URI. |
| 183 | size_t Pos = Uri.find(C: ':'); |
| 184 | if (Pos == StringRef::npos) |
| 185 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 186 | S: "Scheme must be provided in URI: " + |
| 187 | OrigUri); |
| 188 | StringRef SchemeStr = Uri.substr(Start: 0, N: Pos); |
| 189 | std::string UriScheme = percentDecode(Content: SchemeStr); |
| 190 | if (!isStructurallyValidScheme(Scheme: UriScheme)) |
| 191 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 192 | S: "Invalid scheme: " + SchemeStr + |
| 193 | " (decoded: " + UriScheme + ")" ); |
| 194 | Uri = Uri.substr(Start: Pos + 1); |
| 195 | |
| 196 | // Decode the authority of the URI. |
| 197 | std::string UriAuthority; |
| 198 | if (Uri.consume_front(Prefix: "//" )) { |
| 199 | Pos = Uri.find(C: '/'); |
| 200 | UriAuthority = percentDecode(Content: Uri.substr(Start: 0, N: Pos)); |
| 201 | Uri = Uri.substr(Start: Pos); |
| 202 | } |
| 203 | |
| 204 | // Decode the body of the URI. |
| 205 | std::string UriBody = percentDecode(Content: Uri); |
| 206 | |
| 207 | // Compute the absolute path for this uri. |
| 208 | if (!getSupportedSchemes().contains(key: UriScheme)) { |
| 209 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 210 | S: "unsupported URI scheme `" + UriScheme + |
| 211 | "' for workspace files" ); |
| 212 | } |
| 213 | return getAbsolutePath(Authority: UriAuthority, Body: UriBody); |
| 214 | } |
| 215 | |
| 216 | llvm::Expected<URIForFile> URIForFile::fromURI(StringRef Uri) { |
| 217 | llvm::Expected<std::string> FilePath = parseFilePathFromURI(OrigUri: Uri); |
| 218 | if (!FilePath) |
| 219 | return FilePath.takeError(); |
| 220 | return URIForFile(std::move(*FilePath), Uri.str()); |
| 221 | } |
| 222 | |
| 223 | llvm::Expected<URIForFile> URIForFile::fromFile(StringRef AbsoluteFilepath, |
| 224 | StringRef Scheme) { |
| 225 | llvm::Expected<std::string> Uri = |
| 226 | uriFromAbsolutePath(AbsolutePath: AbsoluteFilepath, Scheme); |
| 227 | if (!Uri) |
| 228 | return Uri.takeError(); |
| 229 | return fromURI(Uri: *Uri); |
| 230 | } |
| 231 | |
| 232 | StringRef URIForFile::scheme() const { return uri().split(Separator: ':').first; } |
| 233 | |
| 234 | void URIForFile::registerSupportedScheme(StringRef Scheme) { |
| 235 | getSupportedSchemes().insert(key: Scheme); |
| 236 | } |
| 237 | |
| 238 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, URIForFile &Result, |
| 239 | llvm::json::Path Path) { |
| 240 | if (std::optional<StringRef> Str = Value.getAsString()) { |
| 241 | llvm::Expected<URIForFile> ExpectedUri = URIForFile::fromURI(Uri: *Str); |
| 242 | if (!ExpectedUri) { |
| 243 | Path.report(Message: "unresolvable URI" ); |
| 244 | consumeError(Err: ExpectedUri.takeError()); |
| 245 | return false; |
| 246 | } |
| 247 | Result = std::move(*ExpectedUri); |
| 248 | return true; |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | llvm::json::Value llvm::lsp::toJSON(const URIForFile &Value) { |
| 254 | return Value.uri(); |
| 255 | } |
| 256 | |
| 257 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, const URIForFile &Value) { |
| 258 | return Os << Value.uri(); |
| 259 | } |
| 260 | |
| 261 | //===----------------------------------------------------------------------===// |
| 262 | // ClientCapabilities |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | |
| 265 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 266 | ClientCapabilities &Result, llvm::json::Path Path) { |
| 267 | const llvm::json::Object *O = Value.getAsObject(); |
| 268 | if (!O) { |
| 269 | Path.report(Message: "expected object" ); |
| 270 | return false; |
| 271 | } |
| 272 | if (const llvm::json::Object *TextDocument = O->getObject(K: "textDocument" )) { |
| 273 | if (const llvm::json::Object *DocumentSymbol = |
| 274 | TextDocument->getObject(K: "documentSymbol" )) { |
| 275 | if (std::optional<bool> HierarchicalSupport = |
| 276 | DocumentSymbol->getBoolean(K: "hierarchicalDocumentSymbolSupport" )) |
| 277 | Result.hierarchicalDocumentSymbol = *HierarchicalSupport; |
| 278 | } |
| 279 | if (auto *CodeAction = TextDocument->getObject(K: "codeAction" )) { |
| 280 | if (CodeAction->getObject(K: "codeActionLiteralSupport" )) |
| 281 | Result.codeActionStructure = true; |
| 282 | } |
| 283 | } |
| 284 | if (auto *Window = O->getObject(K: "window" )) { |
| 285 | if (std::optional<bool> WorkDoneProgressSupport = |
| 286 | Window->getBoolean(K: "workDoneProgress" )) |
| 287 | Result.workDoneProgress = *WorkDoneProgressSupport; |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | //===----------------------------------------------------------------------===// |
| 293 | // ClientInfo |
| 294 | //===----------------------------------------------------------------------===// |
| 295 | |
| 296 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, ClientInfo &Result, |
| 297 | llvm::json::Path Path) { |
| 298 | llvm::json::ObjectMapper O(Value, Path); |
| 299 | if (!O || !O.map(Prop: "name" , Out&: Result.name)) |
| 300 | return false; |
| 301 | |
| 302 | // Don't fail if we can't parse version. |
| 303 | O.map(Prop: "version" , Out&: Result.version); |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | // InitializeParams |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
| 311 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, TraceLevel &Result, |
| 312 | llvm::json::Path Path) { |
| 313 | if (std::optional<StringRef> Str = Value.getAsString()) { |
| 314 | if (*Str == "off" ) { |
| 315 | Result = TraceLevel::Off; |
| 316 | return true; |
| 317 | } |
| 318 | if (*Str == "messages" ) { |
| 319 | Result = TraceLevel::Messages; |
| 320 | return true; |
| 321 | } |
| 322 | if (*Str == "verbose" ) { |
| 323 | Result = TraceLevel::Verbose; |
| 324 | return true; |
| 325 | } |
| 326 | } |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 331 | InitializeParams &Result, llvm::json::Path Path) { |
| 332 | llvm::json::ObjectMapper O(Value, Path); |
| 333 | if (!O) |
| 334 | return false; |
| 335 | // We deliberately don't fail if we can't parse individual fields. |
| 336 | O.map(Prop: "capabilities" , Out&: Result.capabilities); |
| 337 | O.map(Prop: "trace" , Out&: Result.trace); |
| 338 | mapOptOrNull(Params: Value, Prop: "clientInfo" , Out&: Result.clientInfo, Path); |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | //===----------------------------------------------------------------------===// |
| 344 | // TextDocumentItem |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | |
| 347 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 348 | TextDocumentItem &Result, llvm::json::Path Path) { |
| 349 | llvm::json::ObjectMapper O(Value, Path); |
| 350 | return O && O.map(Prop: "uri" , Out&: Result.uri) && |
| 351 | O.map(Prop: "languageId" , Out&: Result.languageId) && O.map(Prop: "text" , Out&: Result.text) && |
| 352 | O.map(Prop: "version" , Out&: Result.version); |
| 353 | } |
| 354 | |
| 355 | //===----------------------------------------------------------------------===// |
| 356 | // TextDocumentIdentifier |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
| 359 | llvm::json::Value llvm::lsp::toJSON(const TextDocumentIdentifier &Value) { |
| 360 | return llvm::json::Object{{.K: "uri" , .V: Value.uri}}; |
| 361 | } |
| 362 | |
| 363 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 364 | TextDocumentIdentifier &Result, |
| 365 | llvm::json::Path Path) { |
| 366 | llvm::json::ObjectMapper O(Value, Path); |
| 367 | return O && O.map(Prop: "uri" , Out&: Result.uri); |
| 368 | } |
| 369 | |
| 370 | //===----------------------------------------------------------------------===// |
| 371 | // VersionedTextDocumentIdentifier |
| 372 | //===----------------------------------------------------------------------===// |
| 373 | |
| 374 | llvm::json::Value |
| 375 | llvm::lsp::toJSON(const VersionedTextDocumentIdentifier &Value) { |
| 376 | return llvm::json::Object{ |
| 377 | {.K: "uri" , .V: Value.uri}, |
| 378 | {.K: "version" , .V: Value.version}, |
| 379 | }; |
| 380 | } |
| 381 | |
| 382 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 383 | VersionedTextDocumentIdentifier &Result, |
| 384 | llvm::json::Path Path) { |
| 385 | llvm::json::ObjectMapper O(Value, Path); |
| 386 | return O && O.map(Prop: "uri" , Out&: Result.uri) && O.map(Prop: "version" , Out&: Result.version); |
| 387 | } |
| 388 | |
| 389 | //===----------------------------------------------------------------------===// |
| 390 | // Position |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | |
| 393 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, Position &Result, |
| 394 | llvm::json::Path Path) { |
| 395 | llvm::json::ObjectMapper O(Value, Path); |
| 396 | return O && O.map(Prop: "line" , Out&: Result.line) && |
| 397 | O.map(Prop: "character" , Out&: Result.character); |
| 398 | } |
| 399 | |
| 400 | llvm::json::Value llvm::lsp::toJSON(const Position &Value) { |
| 401 | return llvm::json::Object{ |
| 402 | {.K: "line" , .V: Value.line}, |
| 403 | {.K: "character" , .V: Value.character}, |
| 404 | }; |
| 405 | } |
| 406 | |
| 407 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, const Position &Value) { |
| 408 | return Os << Value.line << ':' << Value.character; |
| 409 | } |
| 410 | |
| 411 | //===----------------------------------------------------------------------===// |
| 412 | // Range |
| 413 | //===----------------------------------------------------------------------===// |
| 414 | |
| 415 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, Range &Result, |
| 416 | llvm::json::Path Path) { |
| 417 | llvm::json::ObjectMapper O(Value, Path); |
| 418 | return O && O.map(Prop: "start" , Out&: Result.start) && O.map(Prop: "end" , Out&: Result.end); |
| 419 | } |
| 420 | |
| 421 | llvm::json::Value llvm::lsp::toJSON(const Range &Value) { |
| 422 | return llvm::json::Object{ |
| 423 | {.K: "start" , .V: Value.start}, |
| 424 | {.K: "end" , .V: Value.end}, |
| 425 | }; |
| 426 | } |
| 427 | |
| 428 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, const Range &Value) { |
| 429 | return Os << Value.start << '-' << Value.end; |
| 430 | } |
| 431 | |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | // Location |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | |
| 436 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, Location &Result, |
| 437 | llvm::json::Path Path) { |
| 438 | llvm::json::ObjectMapper O(Value, Path); |
| 439 | return O && O.map(Prop: "uri" , Out&: Result.uri) && O.map(Prop: "range" , Out&: Result.range); |
| 440 | } |
| 441 | |
| 442 | llvm::json::Value llvm::lsp::toJSON(const Location &Value) { |
| 443 | return llvm::json::Object{ |
| 444 | {.K: "uri" , .V: Value.uri}, |
| 445 | {.K: "range" , .V: Value.range}, |
| 446 | }; |
| 447 | } |
| 448 | |
| 449 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, const Location &Value) { |
| 450 | return Os << Value.range << '@' << Value.uri; |
| 451 | } |
| 452 | |
| 453 | //===----------------------------------------------------------------------===// |
| 454 | // TextDocumentPositionParams |
| 455 | //===----------------------------------------------------------------------===// |
| 456 | |
| 457 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 458 | TextDocumentPositionParams &Result, |
| 459 | llvm::json::Path Path) { |
| 460 | llvm::json::ObjectMapper O(Value, Path); |
| 461 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument) && |
| 462 | O.map(Prop: "position" , Out&: Result.position); |
| 463 | } |
| 464 | |
| 465 | //===----------------------------------------------------------------------===// |
| 466 | // ReferenceParams |
| 467 | //===----------------------------------------------------------------------===// |
| 468 | |
| 469 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 470 | ReferenceContext &Result, llvm::json::Path Path) { |
| 471 | llvm::json::ObjectMapper O(Value, Path); |
| 472 | return O && O.mapOptional(Prop: "includeDeclaration" , Out&: Result.includeDeclaration); |
| 473 | } |
| 474 | |
| 475 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 476 | ReferenceParams &Result, llvm::json::Path Path) { |
| 477 | TextDocumentPositionParams &Base = Result; |
| 478 | llvm::json::ObjectMapper O(Value, Path); |
| 479 | return fromJSON(Value, Result&: Base, Path) && O && |
| 480 | O.mapOptional(Prop: "context" , Out&: Result.context); |
| 481 | } |
| 482 | |
| 483 | //===----------------------------------------------------------------------===// |
| 484 | // DidOpenTextDocumentParams |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | |
| 487 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 488 | DidOpenTextDocumentParams &Result, |
| 489 | llvm::json::Path Path) { |
| 490 | llvm::json::ObjectMapper O(Value, Path); |
| 491 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument); |
| 492 | } |
| 493 | |
| 494 | //===----------------------------------------------------------------------===// |
| 495 | // DidCloseTextDocumentParams |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | |
| 498 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 499 | DidCloseTextDocumentParams &Result, |
| 500 | llvm::json::Path Path) { |
| 501 | llvm::json::ObjectMapper O(Value, Path); |
| 502 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument); |
| 503 | } |
| 504 | |
| 505 | //===----------------------------------------------------------------------===// |
| 506 | // DidSaveTextDocumentParams |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | |
| 509 | bool llvm::lsp::fromJSON(const llvm::json::Value &Params, |
| 510 | DidSaveTextDocumentParams &R, llvm::json::Path P) { |
| 511 | llvm::json::ObjectMapper O(Params, P); |
| 512 | return O && O.map(Prop: "textDocument" , Out&: R.textDocument); |
| 513 | } |
| 514 | |
| 515 | //===----------------------------------------------------------------------===// |
| 516 | // DidChangeTextDocumentParams |
| 517 | //===----------------------------------------------------------------------===// |
| 518 | |
| 519 | LogicalResult |
| 520 | TextDocumentContentChangeEvent::applyTo(std::string &Contents) const { |
| 521 | // If there is no range, the full document changed. |
| 522 | if (!range) { |
| 523 | Contents = text; |
| 524 | return success(); |
| 525 | } |
| 526 | |
| 527 | // Try to map the replacement range to the content. |
| 528 | llvm::SourceMgr TmpScrMgr; |
| 529 | TmpScrMgr.AddNewSourceBuffer(F: llvm::MemoryBuffer::getMemBuffer(InputData: Contents), |
| 530 | IncludeLoc: SMLoc()); |
| 531 | SMRange RangeLoc = range->getAsSMRange(mgr&: TmpScrMgr); |
| 532 | if (!RangeLoc.isValid()) |
| 533 | return failure(); |
| 534 | |
| 535 | Contents.replace(pos: RangeLoc.Start.getPointer() - Contents.data(), |
| 536 | n: RangeLoc.End.getPointer() - RangeLoc.Start.getPointer(), |
| 537 | str: text); |
| 538 | return success(); |
| 539 | } |
| 540 | |
| 541 | LogicalResult TextDocumentContentChangeEvent::applyTo( |
| 542 | ArrayRef<TextDocumentContentChangeEvent> Changes, std::string &Contents) { |
| 543 | for (const auto &Change : Changes) |
| 544 | if (failed(Result: Change.applyTo(Contents))) |
| 545 | return failure(); |
| 546 | return success(); |
| 547 | } |
| 548 | |
| 549 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 550 | TextDocumentContentChangeEvent &Result, |
| 551 | llvm::json::Path Path) { |
| 552 | llvm::json::ObjectMapper O(Value, Path); |
| 553 | return O && O.map(Prop: "range" , Out&: Result.range) && |
| 554 | O.map(Prop: "rangeLength" , Out&: Result.rangeLength) && O.map(Prop: "text" , Out&: Result.text); |
| 555 | } |
| 556 | |
| 557 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 558 | DidChangeTextDocumentParams &Result, |
| 559 | llvm::json::Path Path) { |
| 560 | llvm::json::ObjectMapper O(Value, Path); |
| 561 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument) && |
| 562 | O.map(Prop: "contentChanges" , Out&: Result.contentChanges); |
| 563 | } |
| 564 | |
| 565 | //===----------------------------------------------------------------------===// |
| 566 | // MarkupContent |
| 567 | //===----------------------------------------------------------------------===// |
| 568 | |
| 569 | static llvm::StringRef toTextKind(MarkupKind Kind) { |
| 570 | switch (Kind) { |
| 571 | case MarkupKind::PlainText: |
| 572 | return "plaintext" ; |
| 573 | case MarkupKind::Markdown: |
| 574 | return "markdown" ; |
| 575 | } |
| 576 | llvm_unreachable("Invalid MarkupKind" ); |
| 577 | } |
| 578 | |
| 579 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, MarkupKind Kind) { |
| 580 | return Os << toTextKind(Kind); |
| 581 | } |
| 582 | |
| 583 | llvm::json::Value llvm::lsp::toJSON(const MarkupContent &Mc) { |
| 584 | if (Mc.value.empty()) |
| 585 | return nullptr; |
| 586 | |
| 587 | return llvm::json::Object{ |
| 588 | {.K: "kind" , .V: toTextKind(Kind: Mc.kind)}, |
| 589 | {.K: "value" , .V: Mc.value}, |
| 590 | }; |
| 591 | } |
| 592 | |
| 593 | //===----------------------------------------------------------------------===// |
| 594 | // Hover |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | |
| 597 | llvm::json::Value llvm::lsp::toJSON(const Hover &Hover) { |
| 598 | llvm::json::Object Result{{.K: "contents" , .V: toJSON(Mc: Hover.contents)}}; |
| 599 | if (Hover.range) |
| 600 | Result["range" ] = toJSON(Value: *Hover.range); |
| 601 | return std::move(Result); |
| 602 | } |
| 603 | |
| 604 | //===----------------------------------------------------------------------===// |
| 605 | // DocumentSymbol |
| 606 | //===----------------------------------------------------------------------===// |
| 607 | |
| 608 | llvm::json::Value llvm::lsp::toJSON(const DocumentSymbol &Symbol) { |
| 609 | llvm::json::Object Result{{.K: "name" , .V: Symbol.name}, |
| 610 | {.K: "kind" , .V: static_cast<int>(Symbol.kind)}, |
| 611 | {.K: "range" , .V: Symbol.range}, |
| 612 | {.K: "selectionRange" , .V: Symbol.selectionRange}}; |
| 613 | |
| 614 | if (!Symbol.detail.empty()) |
| 615 | Result["detail" ] = Symbol.detail; |
| 616 | if (!Symbol.children.empty()) |
| 617 | Result["children" ] = Symbol.children; |
| 618 | return std::move(Result); |
| 619 | } |
| 620 | |
| 621 | //===----------------------------------------------------------------------===// |
| 622 | // DocumentSymbolParams |
| 623 | //===----------------------------------------------------------------------===// |
| 624 | |
| 625 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 626 | DocumentSymbolParams &Result, llvm::json::Path Path) { |
| 627 | llvm::json::ObjectMapper O(Value, Path); |
| 628 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument); |
| 629 | } |
| 630 | |
| 631 | //===----------------------------------------------------------------------===// |
| 632 | // DiagnosticRelatedInformation |
| 633 | //===----------------------------------------------------------------------===// |
| 634 | |
| 635 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 636 | DiagnosticRelatedInformation &Result, |
| 637 | llvm::json::Path Path) { |
| 638 | llvm::json::ObjectMapper O(Value, Path); |
| 639 | return O && O.map(Prop: "location" , Out&: Result.location) && |
| 640 | O.map(Prop: "message" , Out&: Result.message); |
| 641 | } |
| 642 | |
| 643 | llvm::json::Value llvm::lsp::toJSON(const DiagnosticRelatedInformation &Info) { |
| 644 | return llvm::json::Object{ |
| 645 | {.K: "location" , .V: Info.location}, |
| 646 | {.K: "message" , .V: Info.message}, |
| 647 | }; |
| 648 | } |
| 649 | |
| 650 | //===----------------------------------------------------------------------===// |
| 651 | // Diagnostic |
| 652 | //===----------------------------------------------------------------------===// |
| 653 | |
| 654 | llvm::json::Value llvm::lsp::toJSON(DiagnosticTag Tag) { |
| 655 | return static_cast<int>(Tag); |
| 656 | } |
| 657 | |
| 658 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, DiagnosticTag &Result, |
| 659 | llvm::json::Path Path) { |
| 660 | if (std::optional<int64_t> I = Value.getAsInteger()) { |
| 661 | Result = (DiagnosticTag)*I; |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | llvm::json::Value llvm::lsp::toJSON(const Diagnostic &Diag) { |
| 669 | llvm::json::Object Result{ |
| 670 | {.K: "range" , .V: Diag.range}, |
| 671 | {.K: "severity" , .V: (int)Diag.severity}, |
| 672 | {.K: "message" , .V: Diag.message}, |
| 673 | }; |
| 674 | if (Diag.category) |
| 675 | Result["category" ] = *Diag.category; |
| 676 | if (!Diag.source.empty()) |
| 677 | Result["source" ] = Diag.source; |
| 678 | if (Diag.relatedInformation) |
| 679 | Result["relatedInformation" ] = *Diag.relatedInformation; |
| 680 | if (!Diag.tags.empty()) |
| 681 | Result["tags" ] = Diag.tags; |
| 682 | return std::move(Result); |
| 683 | } |
| 684 | |
| 685 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, Diagnostic &Result, |
| 686 | llvm::json::Path Path) { |
| 687 | llvm::json::ObjectMapper O(Value, Path); |
| 688 | if (!O) |
| 689 | return false; |
| 690 | int Severity = 0; |
| 691 | if (!mapOptOrNull(Params: Value, Prop: "severity" , Out&: Severity, Path)) |
| 692 | return false; |
| 693 | Result.severity = (DiagnosticSeverity)Severity; |
| 694 | |
| 695 | return O.map(Prop: "range" , Out&: Result.range) && O.map(Prop: "message" , Out&: Result.message) && |
| 696 | mapOptOrNull(Params: Value, Prop: "category" , Out&: Result.category, Path) && |
| 697 | mapOptOrNull(Params: Value, Prop: "source" , Out&: Result.source, Path) && |
| 698 | mapOptOrNull(Params: Value, Prop: "relatedInformation" , Out&: Result.relatedInformation, |
| 699 | Path) && |
| 700 | mapOptOrNull(Params: Value, Prop: "tags" , Out&: Result.tags, Path); |
| 701 | } |
| 702 | |
| 703 | //===----------------------------------------------------------------------===// |
| 704 | // PublishDiagnosticsParams |
| 705 | //===----------------------------------------------------------------------===// |
| 706 | |
| 707 | llvm::json::Value llvm::lsp::toJSON(const PublishDiagnosticsParams &Params) { |
| 708 | return llvm::json::Object{ |
| 709 | {.K: "uri" , .V: Params.uri}, |
| 710 | {.K: "diagnostics" , .V: Params.diagnostics}, |
| 711 | {.K: "version" , .V: Params.version}, |
| 712 | }; |
| 713 | } |
| 714 | |
| 715 | //===----------------------------------------------------------------------===// |
| 716 | // TextEdit |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | |
| 719 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, TextEdit &Result, |
| 720 | llvm::json::Path Path) { |
| 721 | llvm::json::ObjectMapper O(Value, Path); |
| 722 | return O && O.map(Prop: "range" , Out&: Result.range) && O.map(Prop: "newText" , Out&: Result.newText); |
| 723 | } |
| 724 | |
| 725 | llvm::json::Value llvm::lsp::toJSON(const TextEdit &Value) { |
| 726 | return llvm::json::Object{ |
| 727 | {.K: "range" , .V: Value.range}, |
| 728 | {.K: "newText" , .V: Value.newText}, |
| 729 | }; |
| 730 | } |
| 731 | |
| 732 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, const TextEdit &Value) { |
| 733 | Os << Value.range << " => \"" ; |
| 734 | llvm::printEscapedString(Name: Value.newText, Out&: Os); |
| 735 | return Os << '"'; |
| 736 | } |
| 737 | |
| 738 | //===----------------------------------------------------------------------===// |
| 739 | // CompletionItemKind |
| 740 | //===----------------------------------------------------------------------===// |
| 741 | |
| 742 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 743 | CompletionItemKind &Result, llvm::json::Path Path) { |
| 744 | if (std::optional<int64_t> IntValue = Value.getAsInteger()) { |
| 745 | if (*IntValue < static_cast<int>(CompletionItemKind::Text) || |
| 746 | *IntValue > static_cast<int>(CompletionItemKind::TypeParameter)) |
| 747 | return false; |
| 748 | Result = static_cast<CompletionItemKind>(*IntValue); |
| 749 | return true; |
| 750 | } |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | CompletionItemKind llvm::lsp::adjustKindToCapability( |
| 755 | CompletionItemKind Kind, |
| 756 | CompletionItemKindBitset &SupportedCompletionItemKinds) { |
| 757 | size_t KindVal = static_cast<size_t>(Kind); |
| 758 | if (KindVal >= kCompletionItemKindMin && |
| 759 | KindVal <= SupportedCompletionItemKinds.size() && |
| 760 | SupportedCompletionItemKinds[KindVal]) |
| 761 | return Kind; |
| 762 | |
| 763 | // Provide some fall backs for common kinds that are close enough. |
| 764 | switch (Kind) { |
| 765 | case CompletionItemKind::Folder: |
| 766 | return CompletionItemKind::File; |
| 767 | case CompletionItemKind::EnumMember: |
| 768 | return CompletionItemKind::Enum; |
| 769 | case CompletionItemKind::Struct: |
| 770 | return CompletionItemKind::Class; |
| 771 | default: |
| 772 | return CompletionItemKind::Text; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 777 | CompletionItemKindBitset &Result, |
| 778 | llvm::json::Path Path) { |
| 779 | if (const llvm::json::Array *ArrayValue = Value.getAsArray()) { |
| 780 | for (size_t I = 0, E = ArrayValue->size(); I < E; ++I) { |
| 781 | CompletionItemKind KindOut; |
| 782 | if (fromJSON(Value: (*ArrayValue)[I], Result&: KindOut, Path: Path.index(Index: I))) |
| 783 | Result.set(position: size_t(KindOut)); |
| 784 | } |
| 785 | return true; |
| 786 | } |
| 787 | return false; |
| 788 | } |
| 789 | |
| 790 | //===----------------------------------------------------------------------===// |
| 791 | // CompletionItem |
| 792 | //===----------------------------------------------------------------------===// |
| 793 | |
| 794 | llvm::json::Value llvm::lsp::toJSON(const CompletionItem &Value) { |
| 795 | assert(!Value.label.empty() && "completion item label is required" ); |
| 796 | llvm::json::Object Result{{.K: "label" , .V: Value.label}}; |
| 797 | if (Value.kind != CompletionItemKind::Missing) |
| 798 | Result["kind" ] = static_cast<int>(Value.kind); |
| 799 | if (!Value.detail.empty()) |
| 800 | Result["detail" ] = Value.detail; |
| 801 | if (Value.documentation) |
| 802 | Result["documentation" ] = Value.documentation; |
| 803 | if (!Value.sortText.empty()) |
| 804 | Result["sortText" ] = Value.sortText; |
| 805 | if (!Value.filterText.empty()) |
| 806 | Result["filterText" ] = Value.filterText; |
| 807 | if (!Value.insertText.empty()) |
| 808 | Result["insertText" ] = Value.insertText; |
| 809 | if (Value.insertTextFormat != InsertTextFormat::Missing) |
| 810 | Result["insertTextFormat" ] = static_cast<int>(Value.insertTextFormat); |
| 811 | if (Value.textEdit) |
| 812 | Result["textEdit" ] = *Value.textEdit; |
| 813 | if (!Value.additionalTextEdits.empty()) { |
| 814 | Result["additionalTextEdits" ] = |
| 815 | llvm::json::Array(Value.additionalTextEdits); |
| 816 | } |
| 817 | if (Value.deprecated) |
| 818 | Result["deprecated" ] = Value.deprecated; |
| 819 | return std::move(Result); |
| 820 | } |
| 821 | |
| 822 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, |
| 823 | const CompletionItem &Value) { |
| 824 | return Os << Value.label << " - " << toJSON(Value); |
| 825 | } |
| 826 | |
| 827 | bool llvm::lsp::operator<(const CompletionItem &Lhs, |
| 828 | const CompletionItem &Rhs) { |
| 829 | return (Lhs.sortText.empty() ? Lhs.label : Lhs.sortText) < |
| 830 | (Rhs.sortText.empty() ? Rhs.label : Rhs.sortText); |
| 831 | } |
| 832 | |
| 833 | //===----------------------------------------------------------------------===// |
| 834 | // CompletionList |
| 835 | //===----------------------------------------------------------------------===// |
| 836 | |
| 837 | llvm::json::Value llvm::lsp::toJSON(const CompletionList &Value) { |
| 838 | return llvm::json::Object{ |
| 839 | {.K: "isIncomplete" , .V: Value.isIncomplete}, |
| 840 | {.K: "items" , .V: llvm::json::Array(Value.items)}, |
| 841 | }; |
| 842 | } |
| 843 | |
| 844 | //===----------------------------------------------------------------------===// |
| 845 | // CompletionContext |
| 846 | //===----------------------------------------------------------------------===// |
| 847 | |
| 848 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 849 | CompletionContext &Result, llvm::json::Path Path) { |
| 850 | llvm::json::ObjectMapper O(Value, Path); |
| 851 | int TriggerKind; |
| 852 | if (!O || !O.map(Prop: "triggerKind" , Out&: TriggerKind) || |
| 853 | !mapOptOrNull(Params: Value, Prop: "triggerCharacter" , Out&: Result.triggerCharacter, Path)) |
| 854 | return false; |
| 855 | Result.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind); |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | //===----------------------------------------------------------------------===// |
| 860 | // CompletionParams |
| 861 | //===----------------------------------------------------------------------===// |
| 862 | |
| 863 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 864 | CompletionParams &Result, llvm::json::Path Path) { |
| 865 | if (!fromJSON(Value, Result&: static_cast<TextDocumentPositionParams &>(Result), Path)) |
| 866 | return false; |
| 867 | if (const llvm::json::Value *Context = Value.getAsObject()->get(K: "context" )) |
| 868 | return fromJSON(Value: *Context, Result&: Result.context, Path: Path.field(Field: "context" )); |
| 869 | return true; |
| 870 | } |
| 871 | |
| 872 | //===----------------------------------------------------------------------===// |
| 873 | // ParameterInformation |
| 874 | //===----------------------------------------------------------------------===// |
| 875 | |
| 876 | llvm::json::Value llvm::lsp::toJSON(const ParameterInformation &Value) { |
| 877 | assert((Value.labelOffsets || !Value.labelString.empty()) && |
| 878 | "parameter information label is required" ); |
| 879 | llvm::json::Object Result; |
| 880 | if (Value.labelOffsets) |
| 881 | Result["label" ] = llvm::json::Array( |
| 882 | {Value.labelOffsets->first, Value.labelOffsets->second}); |
| 883 | else |
| 884 | Result["label" ] = Value.labelString; |
| 885 | if (!Value.documentation.empty()) |
| 886 | Result["documentation" ] = Value.documentation; |
| 887 | return std::move(Result); |
| 888 | } |
| 889 | |
| 890 | //===----------------------------------------------------------------------===// |
| 891 | // SignatureInformation |
| 892 | //===----------------------------------------------------------------------===// |
| 893 | |
| 894 | llvm::json::Value llvm::lsp::toJSON(const SignatureInformation &Value) { |
| 895 | assert(!Value.label.empty() && "signature information label is required" ); |
| 896 | llvm::json::Object Result{ |
| 897 | {.K: "label" , .V: Value.label}, |
| 898 | {.K: "parameters" , .V: llvm::json::Array(Value.parameters)}, |
| 899 | }; |
| 900 | if (!Value.documentation.empty()) |
| 901 | Result["documentation" ] = Value.documentation; |
| 902 | return std::move(Result); |
| 903 | } |
| 904 | |
| 905 | raw_ostream &llvm::lsp::operator<<(raw_ostream &Os, |
| 906 | const SignatureInformation &Value) { |
| 907 | return Os << Value.label << " - " << toJSON(Value); |
| 908 | } |
| 909 | |
| 910 | //===----------------------------------------------------------------------===// |
| 911 | // SignatureHelp |
| 912 | //===----------------------------------------------------------------------===// |
| 913 | |
| 914 | llvm::json::Value llvm::lsp::toJSON(const SignatureHelp &Value) { |
| 915 | assert(Value.activeSignature >= 0 && |
| 916 | "Unexpected negative value for number of active signatures." ); |
| 917 | assert(Value.activeParameter >= 0 && |
| 918 | "Unexpected negative value for active parameter index" ); |
| 919 | return llvm::json::Object{ |
| 920 | {.K: "activeSignature" , .V: Value.activeSignature}, |
| 921 | {.K: "activeParameter" , .V: Value.activeParameter}, |
| 922 | {.K: "signatures" , .V: llvm::json::Array(Value.signatures)}, |
| 923 | }; |
| 924 | } |
| 925 | |
| 926 | //===----------------------------------------------------------------------===// |
| 927 | // DocumentLinkParams |
| 928 | //===----------------------------------------------------------------------===// |
| 929 | |
| 930 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 931 | DocumentLinkParams &Result, llvm::json::Path Path) { |
| 932 | llvm::json::ObjectMapper O(Value, Path); |
| 933 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument); |
| 934 | } |
| 935 | |
| 936 | //===----------------------------------------------------------------------===// |
| 937 | // DocumentLink |
| 938 | //===----------------------------------------------------------------------===// |
| 939 | |
| 940 | llvm::json::Value llvm::lsp::toJSON(const DocumentLink &Value) { |
| 941 | return llvm::json::Object{ |
| 942 | {.K: "range" , .V: Value.range}, |
| 943 | {.K: "target" , .V: Value.target}, |
| 944 | }; |
| 945 | } |
| 946 | |
| 947 | //===----------------------------------------------------------------------===// |
| 948 | // InlayHintsParams |
| 949 | //===----------------------------------------------------------------------===// |
| 950 | |
| 951 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 952 | InlayHintsParams &Result, llvm::json::Path Path) { |
| 953 | llvm::json::ObjectMapper O(Value, Path); |
| 954 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument) && |
| 955 | O.map(Prop: "range" , Out&: Result.range); |
| 956 | } |
| 957 | |
| 958 | //===----------------------------------------------------------------------===// |
| 959 | // InlayHint |
| 960 | //===----------------------------------------------------------------------===// |
| 961 | |
| 962 | llvm::json::Value llvm::lsp::toJSON(const InlayHint &Value) { |
| 963 | return llvm::json::Object{{.K: "position" , .V: Value.position}, |
| 964 | {.K: "kind" , .V: (int)Value.kind}, |
| 965 | {.K: "label" , .V: Value.label}, |
| 966 | {.K: "paddingLeft" , .V: Value.paddingLeft}, |
| 967 | {.K: "paddingRight" , .V: Value.paddingRight}}; |
| 968 | } |
| 969 | bool llvm::lsp::operator==(const InlayHint &Lhs, const InlayHint &Rhs) { |
| 970 | return std::tie(args: Lhs.position, args: Lhs.kind, args: Lhs.label) == |
| 971 | std::tie(args: Rhs.position, args: Rhs.kind, args: Rhs.label); |
| 972 | } |
| 973 | bool llvm::lsp::operator<(const InlayHint &Lhs, const InlayHint &Rhs) { |
| 974 | return std::tie(args: Lhs.position, args: Lhs.kind, args: Lhs.label) < |
| 975 | std::tie(args: Rhs.position, args: Rhs.kind, args: Rhs.label); |
| 976 | } |
| 977 | |
| 978 | llvm::raw_ostream &llvm::lsp::operator<<(llvm::raw_ostream &Os, |
| 979 | InlayHintKind Value) { |
| 980 | switch (Value) { |
| 981 | case InlayHintKind::Parameter: |
| 982 | return Os << "parameter" ; |
| 983 | case InlayHintKind::Type: |
| 984 | return Os << "type" ; |
| 985 | } |
| 986 | llvm_unreachable("Unknown InlayHintKind" ); |
| 987 | } |
| 988 | |
| 989 | //===----------------------------------------------------------------------===// |
| 990 | // CodeActionContext |
| 991 | //===----------------------------------------------------------------------===// |
| 992 | |
| 993 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 994 | CodeActionContext &Result, llvm::json::Path Path) { |
| 995 | llvm::json::ObjectMapper O(Value, Path); |
| 996 | if (!O || !O.map(Prop: "diagnostics" , Out&: Result.diagnostics)) |
| 997 | return false; |
| 998 | O.map(Prop: "only" , Out&: Result.only); |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | //===----------------------------------------------------------------------===// |
| 1003 | // CodeActionParams |
| 1004 | //===----------------------------------------------------------------------===// |
| 1005 | |
| 1006 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, |
| 1007 | CodeActionParams &Result, llvm::json::Path Path) { |
| 1008 | llvm::json::ObjectMapper O(Value, Path); |
| 1009 | return O && O.map(Prop: "textDocument" , Out&: Result.textDocument) && |
| 1010 | O.map(Prop: "range" , Out&: Result.range) && O.map(Prop: "context" , Out&: Result.context); |
| 1011 | } |
| 1012 | |
| 1013 | //===----------------------------------------------------------------------===// |
| 1014 | // WorkspaceEdit |
| 1015 | //===----------------------------------------------------------------------===// |
| 1016 | |
| 1017 | bool llvm::lsp::fromJSON(const llvm::json::Value &Value, WorkspaceEdit &Result, |
| 1018 | llvm::json::Path Path) { |
| 1019 | llvm::json::ObjectMapper O(Value, Path); |
| 1020 | return O && O.map(Prop: "changes" , Out&: Result.changes); |
| 1021 | } |
| 1022 | |
| 1023 | llvm::json::Value llvm::lsp::toJSON(const WorkspaceEdit &Value) { |
| 1024 | llvm::json::Object FileChanges; |
| 1025 | for (auto &Change : Value.changes) |
| 1026 | FileChanges[Change.first] = llvm::json::Array(Change.second); |
| 1027 | return llvm::json::Object{{.K: "changes" , .V: std::move(FileChanges)}}; |
| 1028 | } |
| 1029 | |
| 1030 | //===----------------------------------------------------------------------===// |
| 1031 | // CodeAction |
| 1032 | //===----------------------------------------------------------------------===// |
| 1033 | |
| 1034 | const llvm::StringLiteral CodeAction::kQuickFix = "quickfix" ; |
| 1035 | const llvm::StringLiteral CodeAction::kRefactor = "refactor" ; |
| 1036 | const llvm::StringLiteral CodeAction::kInfo = "info" ; |
| 1037 | |
| 1038 | llvm::json::Value llvm::lsp::toJSON(const CodeAction &Value) { |
| 1039 | llvm::json::Object CodeAction{{.K: "title" , .V: Value.title}}; |
| 1040 | if (Value.kind) |
| 1041 | CodeAction["kind" ] = *Value.kind; |
| 1042 | if (Value.diagnostics) |
| 1043 | CodeAction["diagnostics" ] = llvm::json::Array(*Value.diagnostics); |
| 1044 | if (Value.isPreferred) |
| 1045 | CodeAction["isPreferred" ] = true; |
| 1046 | if (Value.edit) |
| 1047 | CodeAction["edit" ] = *Value.edit; |
| 1048 | return std::move(CodeAction); |
| 1049 | } |
| 1050 | |
| 1051 | //===----------------------------------------------------------------------===// |
| 1052 | // ShowMessageParams |
| 1053 | //===----------------------------------------------------------------------===// |
| 1054 | |
| 1055 | llvm::json::Value llvm::lsp::toJSON(const ShowMessageParams &Params) { |
| 1056 | auto Out = llvm::json::Object{ |
| 1057 | {.K: "type" , .V: static_cast<int>(Params.type)}, |
| 1058 | {.K: "message" , .V: Params.message}, |
| 1059 | }; |
| 1060 | if (Params.actions) |
| 1061 | Out["actions" ] = *Params.actions; |
| 1062 | return Out; |
| 1063 | } |
| 1064 | |
| 1065 | llvm::json::Value llvm::lsp::toJSON(const MessageActionItem &Params) { |
| 1066 | return llvm::json::Object{{.K: "title" , .V: Params.title}}; |
| 1067 | } |
| 1068 | |