blob: 17d070a3f7b6b7e23f259033a153da84339867b4 [file] [log] [blame]
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00001//===--- Protocol.cpp - Language Server Protocol Implementation -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the serialization code for the LSP structs.
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "Protocol.h"
Eric Liu78ed91a72018-01-29 15:37:46 +000014#include "Logger.h"
Ilya Biryukov7d60d202018-02-16 12:20:47 +000015#include "URI.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000016#include "clang/Basic/LLVM.h"
Jan Korousb4067012018-11-27 16:40:46 +000017#include "llvm/ADT/Hashing.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000018#include "llvm/ADT/SmallString.h"
Sam McCalla69698f2019-03-27 17:47:49 +000019#include "llvm/ADT/StringSwitch.h"
Kadir Cetinkayac6578ee2019-05-28 10:29:58 +000020#include "llvm/Support/ErrorHandling.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000021#include "llvm/Support/Format.h"
Marc-Andre Laperle85dcce42017-09-18 15:02:59 +000022#include "llvm/Support/FormatVariadic.h"
Ilya Biryukov19d75602018-11-23 15:21:19 +000023#include "llvm/Support/JSON.h"
Krasimir Georgiev50117372017-04-07 11:03:26 +000024#include "llvm/Support/Path.h"
Ilya Biryukov574b7532017-08-02 09:08:39 +000025#include "llvm/Support/raw_ostream.h"
Ilya Biryukove5128f72017-09-20 07:24:15 +000026
Sam McCallff8b8742017-11-30 21:32:29 +000027namespace clang {
28namespace clangd {
Sam McCall38a04912017-11-29 11:36:46 +000029
Sam McCalldc8f3cf2018-10-17 07:32:05 +000030char LSPError::ID;
31
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000032URIForFile URIForFile::canonicalize(llvm::StringRef AbsPath,
33 llvm::StringRef TUPath) {
34 assert(llvm::sys::path::is_absolute(AbsPath) && "the path is relative");
Eric Liu4d814a92018-11-28 10:30:42 +000035 auto Resolved = URI::resolvePath(AbsPath, TUPath);
36 if (!Resolved) {
37 elog("URIForFile: failed to resolve path {0} with TU path {1}: "
38 "{2}.\nUsing unresolved path.",
39 AbsPath, TUPath, Resolved.takeError());
40 return URIForFile(AbsPath);
41 }
42 return URIForFile(std::move(*Resolved));
43}
44
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000045llvm::Expected<URIForFile> URIForFile::fromURI(const URI &U,
46 llvm::StringRef HintPath) {
Eric Liu4d814a92018-11-28 10:30:42 +000047 auto Resolved = URI::resolve(U, HintPath);
48 if (!Resolved)
49 return Resolved.takeError();
50 return URIForFile(std::move(*Resolved));
Ilya Biryukov7d60d202018-02-16 12:20:47 +000051}
52
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000053bool fromJSON(const llvm::json::Value &E, URIForFile &R) {
Sam McCalld20d7982018-07-09 14:25:59 +000054 if (auto S = E.getAsString()) {
Eric Liu4d814a92018-11-28 10:30:42 +000055 auto Parsed = URI::parse(*S);
56 if (!Parsed) {
57 elog("Failed to parse URI {0}: {1}", *S, Parsed.takeError());
Eric Liu78ed91a72018-01-29 15:37:46 +000058 return false;
59 }
Eric Liu4d814a92018-11-28 10:30:42 +000060 if (Parsed->scheme() != "file" && Parsed->scheme() != "test") {
Sam McCallbed58852018-07-11 10:35:11 +000061 elog("Clangd only supports 'file' URI scheme for workspace files: {0}",
62 *S);
Eric Liu78ed91a72018-01-29 15:37:46 +000063 return false;
64 }
Eric Liu4d814a92018-11-28 10:30:42 +000065 // "file" and "test" schemes do not require hint path.
66 auto U = URIForFile::fromURI(*Parsed, /*HintPath=*/"");
67 if (!U) {
68 elog("{0}", U.takeError());
Sam McCall41d21522018-01-30 11:23:11 +000069 return false;
70 }
Eric Liu4d814a92018-11-28 10:30:42 +000071 R = std::move(*U);
Sam McCallff8b8742017-11-30 21:32:29 +000072 return true;
73 }
74 return false;
Sam McCall38a04912017-11-29 11:36:46 +000075}
76
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000077llvm::json::Value toJSON(const URIForFile &U) { return U.uri(); }
Krasimir Georgiev50117372017-04-07 11:03:26 +000078
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000079llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const URIForFile &U) {
Eric Liu78ed91a72018-01-29 15:37:46 +000080 return OS << U.uri();
Sam McCallfffa8222017-12-20 10:26:53 +000081}
82
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000083llvm::json::Value toJSON(const TextDocumentIdentifier &R) {
84 return llvm::json::Object{{"uri", R.uri}};
Eric Liuc5105f92018-02-16 14:15:55 +000085}
86
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000087bool fromJSON(const llvm::json::Value &Params, TextDocumentIdentifier &R) {
88 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +000089 return O && O.map("uri", R.uri);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000090}
91
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000092bool fromJSON(const llvm::json::Value &Params, Position &R) {
93 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +000094 return O && O.map("line", R.line) && O.map("character", R.character);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000095}
96
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000097llvm::json::Value toJSON(const Position &P) {
98 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +000099 {"line", P.line},
100 {"character", P.character},
101 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000102}
103
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000104llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &P) {
Sam McCallfffa8222017-12-20 10:26:53 +0000105 return OS << P.line << ':' << P.character;
106}
107
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000108bool fromJSON(const llvm::json::Value &Params, Range &R) {
109 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000110 return O && O.map("start", R.start) && O.map("end", R.end);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000111}
112
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000113llvm::json::Value toJSON(const Range &P) {
114 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000115 {"start", P.start},
116 {"end", P.end},
117 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000118}
119
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000120llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Range &R) {
Sam McCallfffa8222017-12-20 10:26:53 +0000121 return OS << R.start << '-' << R.end;
122}
123
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000124llvm::json::Value toJSON(const Location &P) {
125 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000126 {"uri", P.uri},
127 {"range", P.range},
128 };
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000129}
130
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000131llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Location &L) {
Sam McCallfffa8222017-12-20 10:26:53 +0000132 return OS << L.range << '@' << L.uri;
133}
134
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000135bool fromJSON(const llvm::json::Value &Params, TextDocumentItem &R) {
136 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000137 return O && O.map("uri", R.uri) && O.map("languageId", R.languageId) &&
138 O.map("version", R.version) && O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000139}
140
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000141bool fromJSON(const llvm::json::Value &Params, TextEdit &R) {
142 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000143 return O && O.map("range", R.range) && O.map("newText", R.newText);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000144}
145
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000146llvm::json::Value toJSON(const TextEdit &P) {
147 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000148 {"range", P.range},
149 {"newText", P.newText},
150 };
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000151}
152
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000153llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TextEdit &TE) {
Sam McCall034e11a2018-01-25 17:29:17 +0000154 OS << TE.range << " => \"";
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000155 llvm::printEscapedString(TE.newText, OS);
Sam McCall034e11a2018-01-25 17:29:17 +0000156 return OS << '"';
157}
158
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000159bool fromJSON(const llvm::json::Value &E, TraceLevel &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000160 if (auto S = E.getAsString()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000161 if (*S == "off") {
162 Out = TraceLevel::Off;
163 return true;
164 } else if (*S == "messages") {
165 Out = TraceLevel::Messages;
166 return true;
167 } else if (*S == "verbose") {
168 Out = TraceLevel::Verbose;
169 return true;
170 }
171 }
172 return false;
173}
174
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000175bool fromJSON(const llvm::json::Value &E, SymbolKind &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000176 if (auto T = E.getAsInteger()) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000177 if (*T < static_cast<int>(SymbolKind::File) ||
178 *T > static_cast<int>(SymbolKind::TypeParameter))
179 return false;
180 Out = static_cast<SymbolKind>(*T);
181 return true;
182 }
183 return false;
184}
185
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000186bool fromJSON(const llvm::json::Value &E, SymbolKindBitset &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000187 if (auto *A = E.getAsArray()) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000188 for (size_t I = 0; I < A->size(); ++I) {
189 SymbolKind KindOut;
190 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000191 Out.set(size_t(KindOut));
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000192 }
193 return true;
194 }
195 return false;
196}
197
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000198SymbolKind adjustKindToCapability(SymbolKind Kind,
Ilya Biryukov74f26552018-07-26 12:05:31 +0000199 SymbolKindBitset &SupportedSymbolKinds) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000200 auto KindVal = static_cast<size_t>(Kind);
Ilya Biryukov74f26552018-07-26 12:05:31 +0000201 if (KindVal >= SymbolKindMin && KindVal <= SupportedSymbolKinds.size() &&
202 SupportedSymbolKinds[KindVal])
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000203 return Kind;
204
205 switch (Kind) {
206 // Provide some fall backs for common kinds that are close enough.
207 case SymbolKind::Struct:
208 return SymbolKind::Class;
209 case SymbolKind::EnumMember:
210 return SymbolKind::Enum;
211 default:
212 return SymbolKind::String;
213 }
214}
215
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000216SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind) {
217 switch (Kind) {
218 case index::SymbolKind::Unknown:
219 return SymbolKind::Variable;
220 case index::SymbolKind::Module:
221 return SymbolKind::Module;
222 case index::SymbolKind::Namespace:
223 return SymbolKind::Namespace;
224 case index::SymbolKind::NamespaceAlias:
225 return SymbolKind::Namespace;
226 case index::SymbolKind::Macro:
227 return SymbolKind::String;
228 case index::SymbolKind::Enum:
229 return SymbolKind::Enum;
230 case index::SymbolKind::Struct:
231 return SymbolKind::Struct;
232 case index::SymbolKind::Class:
233 return SymbolKind::Class;
234 case index::SymbolKind::Protocol:
235 return SymbolKind::Interface;
236 case index::SymbolKind::Extension:
237 return SymbolKind::Interface;
238 case index::SymbolKind::Union:
239 return SymbolKind::Class;
240 case index::SymbolKind::TypeAlias:
241 return SymbolKind::Class;
242 case index::SymbolKind::Function:
243 return SymbolKind::Function;
244 case index::SymbolKind::Variable:
245 return SymbolKind::Variable;
246 case index::SymbolKind::Field:
247 return SymbolKind::Field;
248 case index::SymbolKind::EnumConstant:
249 return SymbolKind::EnumMember;
250 case index::SymbolKind::InstanceMethod:
251 case index::SymbolKind::ClassMethod:
252 case index::SymbolKind::StaticMethod:
253 return SymbolKind::Method;
254 case index::SymbolKind::InstanceProperty:
255 case index::SymbolKind::ClassProperty:
256 case index::SymbolKind::StaticProperty:
257 return SymbolKind::Property;
258 case index::SymbolKind::Constructor:
259 case index::SymbolKind::Destructor:
260 return SymbolKind::Method;
261 case index::SymbolKind::ConversionFunction:
262 return SymbolKind::Function;
263 case index::SymbolKind::Parameter:
264 return SymbolKind::Variable;
265 case index::SymbolKind::Using:
266 return SymbolKind::Namespace;
267 }
268 llvm_unreachable("invalid symbol kind");
269}
270
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000271bool fromJSON(const llvm::json::Value &Params, ClientCapabilities &R) {
272 const llvm::json::Object *O = Params.getAsObject();
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000273 if (!O)
274 return false;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000275 if (auto *TextDocument = O->getObject("textDocument")) {
276 if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
277 if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
278 R.DiagnosticCategory = *CategorySupport;
Sam McCall16e70702018-10-24 07:59:38 +0000279 if (auto CodeActions = Diagnostics->getBoolean("codeActionsInline"))
280 R.DiagnosticFixes = *CodeActions;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000281 if (auto RelatedInfo = Diagnostics->getBoolean("relatedInformation"))
282 R.DiagnosticRelatedInformation = *RelatedInfo;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000283 }
284 if (auto *Completion = TextDocument->getObject("completion")) {
285 if (auto *Item = Completion->getObject("completionItem")) {
286 if (auto SnippetSupport = Item->getBoolean("snippetSupport"))
287 R.CompletionSnippets = *SnippetSupport;
288 }
289 if (auto *ItemKind = Completion->getObject("completionItemKind")) {
290 if (auto *ValueSet = ItemKind->get("valueSet")) {
291 R.CompletionItemKinds.emplace();
292 if (!fromJSON(*ValueSet, *R.CompletionItemKinds))
293 return false;
294 }
295 }
Sam McCall8d412942019-06-18 11:57:26 +0000296 if (auto EditsNearCursor = Completion->getBoolean("editsNearCursor"))
297 R.CompletionFixes = *EditsNearCursor;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000298 }
299 if (auto *CodeAction = TextDocument->getObject("codeAction")) {
300 if (CodeAction->getObject("codeActionLiteralSupport"))
301 R.CodeActionStructure = true;
302 }
Ilya Biryukov19d75602018-11-23 15:21:19 +0000303 if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) {
304 if (auto HierarchicalSupport =
305 DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport"))
306 R.HierarchicalDocumentSymbol = *HierarchicalSupport;
307 }
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000308 if (auto *Hover = TextDocument->getObject("hover")) {
309 if (auto *ContentFormat = Hover->getArray("contentFormat")) {
310 for (const auto &Format : *ContentFormat) {
311 MarkupKind K = MarkupKind::PlainText;
312 if (fromJSON(Format, K)) {
313 R.HoverContentFormat = K;
314 break;
315 }
316 }
317 }
318 }
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000319 if (auto *Help = TextDocument->getObject("signatureHelp")) {
320 if (auto *Info = Help->getObject("signatureInformation")) {
321 if (auto *Parameter = Info->getObject("parameterInformation")) {
322 if (auto OffsetSupport = Parameter->getBoolean("labelOffsetSupport"))
323 R.OffsetsInSignatureHelp = *OffsetSupport;
324 }
325 }
326 }
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000327 }
328 if (auto *Workspace = O->getObject("workspace")) {
329 if (auto *Symbol = Workspace->getObject("symbol")) {
330 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
331 if (auto *ValueSet = SymbolKind->get("valueSet")) {
332 R.WorkspaceSymbolKinds.emplace();
333 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds))
334 return false;
335 }
336 }
337 }
338 }
Sam McCalla69698f2019-03-27 17:47:49 +0000339 if (auto *OffsetEncoding = O->get("offsetEncoding")) {
340 R.offsetEncoding.emplace();
341 if (!fromJSON(*OffsetEncoding, *R.offsetEncoding))
342 return false;
343 }
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000344 return true;
345}
346
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000347bool fromJSON(const llvm::json::Value &Params, InitializeParams &R) {
348 llvm::json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000349 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000350 return false;
Sam McCall38a04912017-11-29 11:36:46 +0000351 // We deliberately don't fail if we can't parse individual fields.
352 // Failing to handle a slightly malformed initialize would be a disaster.
Sam McCallff8b8742017-11-30 21:32:29 +0000353 O.map("processId", R.processId);
354 O.map("rootUri", R.rootUri);
355 O.map("rootPath", R.rootPath);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000356 O.map("capabilities", R.capabilities);
Sam McCallff8b8742017-11-30 21:32:29 +0000357 O.map("trace", R.trace);
Simon Marchi88016782018-08-01 11:28:49 +0000358 O.map("initializationOptions", R.initializationOptions);
Sam McCallff8b8742017-11-30 21:32:29 +0000359 return true;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000360}
361
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000362bool fromJSON(const llvm::json::Value &Params, DidOpenTextDocumentParams &R) {
363 llvm::json::ObjectMapper O(Params);
Sam McCall2eb6b402018-11-02 13:06:55 +0000364 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000365}
366
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000367bool fromJSON(const llvm::json::Value &Params, DidCloseTextDocumentParams &R) {
368 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000369 return O && O.map("textDocument", R.textDocument);
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +0000370}
371
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000372bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R) {
373 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000374 return O && O.map("textDocument", R.textDocument) &&
Eric Liu51fed182018-02-22 18:40:39 +0000375 O.map("contentChanges", R.contentChanges) &&
376 O.map("wantDiagnostics", R.wantDiagnostics);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000377}
378
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000379bool fromJSON(const llvm::json::Value &E, FileChangeType &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000380 if (auto T = E.getAsInteger()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000381 if (*T < static_cast<int>(FileChangeType::Created) ||
382 *T > static_cast<int>(FileChangeType::Deleted))
383 return false;
384 Out = static_cast<FileChangeType>(*T);
385 return true;
386 }
387 return false;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000388}
389
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000390bool fromJSON(const llvm::json::Value &Params, FileEvent &R) {
391 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000392 return O && O.map("uri", R.uri) && O.map("type", R.type);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000393}
394
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000395bool fromJSON(const llvm::json::Value &Params, DidChangeWatchedFilesParams &R) {
396 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000397 return O && O.map("changes", R.changes);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000398}
399
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000400bool fromJSON(const llvm::json::Value &Params,
401 TextDocumentContentChangeEvent &R) {
402 llvm::json::ObjectMapper O(Params);
Simon Marchi98082622018-03-26 14:41:40 +0000403 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
404 O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000405}
406
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000407bool fromJSON(const llvm::json::Value &Params,
408 DocumentRangeFormattingParams &R) {
409 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000410 return O && O.map("textDocument", R.textDocument) &&
Sam McCall149786d2019-06-10 13:01:49 +0000411 O.map("range", R.range);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000412}
413
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000414bool fromJSON(const llvm::json::Value &Params,
415 DocumentOnTypeFormattingParams &R) {
416 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000417 return O && O.map("textDocument", R.textDocument) &&
Sam McCall149786d2019-06-10 13:01:49 +0000418 O.map("position", R.position) && O.map("ch", R.ch);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000419}
420
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000421bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R) {
422 llvm::json::ObjectMapper O(Params);
Sam McCall149786d2019-06-10 13:01:49 +0000423 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000424}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000425
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000426bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R) {
427 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000428 return O && O.map("textDocument", R.textDocument);
429}
430
Sam McCallc9e4ee92019-04-18 15:17:07 +0000431llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
432 return llvm::json::Object{
433 {"location", DRI.location},
434 {"message", DRI.message},
435 };
436}
437
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000438llvm::json::Value toJSON(const Diagnostic &D) {
439 llvm::json::Object Diag{
Sam McCall20841d42018-10-16 16:29:41 +0000440 {"range", D.range},
441 {"severity", D.severity},
442 {"message", D.message},
443 };
Sam McCall16e70702018-10-24 07:59:38 +0000444 if (D.category)
445 Diag["category"] = *D.category;
446 if (D.codeActions)
447 Diag["codeActions"] = D.codeActions;
Sam McCall641caa52019-04-17 12:35:16 +0000448 if (!D.code.empty())
449 Diag["code"] = D.code;
450 if (!D.source.empty())
451 Diag["source"] = D.source;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000452 if (D.relatedInformation)
453 Diag["relatedInformation"] = *D.relatedInformation;
Sam McCall20841d42018-10-16 16:29:41 +0000454 return std::move(Diag);
455}
456
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000457bool fromJSON(const llvm::json::Value &Params, Diagnostic &R) {
458 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000459 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
460 return false;
461 O.map("severity", R.severity);
Sam McCall16e70702018-10-24 07:59:38 +0000462 O.map("category", R.category);
Sam McCall641caa52019-04-17 12:35:16 +0000463 O.map("code", R.code);
464 O.map("source", R.source);
Sam McCallff8b8742017-11-30 21:32:29 +0000465 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000466}
467
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000468bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R) {
469 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000470 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000471}
472
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000473llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
Sam McCall034e11a2018-01-25 17:29:17 +0000474 OS << D.range << " [";
475 switch (D.severity) {
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000476 case 1:
477 OS << "error";
478 break;
479 case 2:
480 OS << "warning";
481 break;
482 case 3:
483 OS << "note";
484 break;
485 case 4:
486 OS << "remark";
487 break;
488 default:
489 OS << "diagnostic";
490 break;
Sam McCall034e11a2018-01-25 17:29:17 +0000491 }
492 return OS << '(' << D.severity << "): " << D.message << "]";
493}
494
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000495bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R) {
496 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000497 return O && O.map("textDocument", R.textDocument) &&
498 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000499}
500
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000501bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R) {
502 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000503 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000504}
505
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000506const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000507 "clangd.applyFix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000508const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_TWEAK =
509 "clangd.applyTweak";
510
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000511bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R) {
512 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000513 if (!O || !O.map("command", R.command))
514 return false;
Sam McCallec109022017-11-28 09:37:43 +0000515
Sam McCalld20d7982018-07-09 14:25:59 +0000516 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000517 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
518 return Args && Args->size() == 1 &&
519 fromJSON(Args->front(), R.workspaceEdit);
520 }
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000521 if (R.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK)
522 return Args && Args->size() == 1 && fromJSON(Args->front(), R.tweakArgs);
Sam McCallff8b8742017-11-30 21:32:29 +0000523 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000524}
525
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000526llvm::json::Value toJSON(const SymbolInformation &P) {
527 return llvm::json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000528 {"name", P.name},
529 {"kind", static_cast<int>(P.kind)},
530 {"location", P.location},
531 {"containerName", P.containerName},
532 };
533}
534
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000535llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
536 const SymbolInformation &SI) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000537 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
538 return O;
539}
540
Jan Korousb4067012018-11-27 16:40:46 +0000541bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
542 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
543 LHS.USR == RHS.USR && LHS.ID == RHS.ID;
544}
545
546llvm::json::Value toJSON(const SymbolDetails &P) {
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000547 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000548 {"containerName", llvm::json::Value(nullptr)},
549 {"usr", llvm::json::Value(nullptr)},
550 {"id", llvm::json::Value(nullptr)}};
Jan Korousb4067012018-11-27 16:40:46 +0000551
552 if (!P.name.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000553 Result["name"] = P.name;
Jan Korousb4067012018-11-27 16:40:46 +0000554
555 if (!P.containerName.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000556 Result["containerName"] = P.containerName;
Jan Korousb4067012018-11-27 16:40:46 +0000557
558 if (!P.USR.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000559 Result["usr"] = P.USR;
Jan Korousb4067012018-11-27 16:40:46 +0000560
561 if (P.ID.hasValue())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000562 Result["id"] = P.ID.getValue().str();
Jan Korousb4067012018-11-27 16:40:46 +0000563
Jan Korous613c80d2018-11-28 10:24:07 +0000564 // Older clang cannot compile 'return Result', even though it is legal.
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000565 return llvm::json::Value(std::move(Result));
Jan Korousb4067012018-11-27 16:40:46 +0000566}
567
568llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
569 if (!S.containerName.empty()) {
570 O << S.containerName;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000571 llvm::StringRef ContNameRef;
Jan Korousb4067012018-11-27 16:40:46 +0000572 if (!ContNameRef.endswith("::")) {
573 O << " ";
574 }
575 }
576 O << S.name << " - " << toJSON(S);
577 return O;
578}
579
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000580bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R) {
581 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000582 return O && O.map("query", R.query);
583}
584
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000585llvm::json::Value toJSON(const Command &C) {
586 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000587 if (C.workspaceEdit)
588 Cmd["arguments"] = {*C.workspaceEdit};
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000589 if (C.tweakArgs)
590 Cmd["arguments"] = {*C.tweakArgs};
Eric Liuc5105f92018-02-16 14:15:55 +0000591 return std::move(Cmd);
592}
593
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000594const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000595const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
Sam McCall20841d42018-10-16 16:29:41 +0000596
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000597llvm::json::Value toJSON(const CodeAction &CA) {
598 auto CodeAction = llvm::json::Object{{"title", CA.title}};
Sam McCall20841d42018-10-16 16:29:41 +0000599 if (CA.kind)
600 CodeAction["kind"] = *CA.kind;
601 if (CA.diagnostics)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000602 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
Sam McCall20841d42018-10-16 16:29:41 +0000603 if (CA.edit)
604 CodeAction["edit"] = *CA.edit;
605 if (CA.command)
606 CodeAction["command"] = *CA.command;
607 return std::move(CodeAction);
608}
609
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000610llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
Ilya Biryukov19d75602018-11-23 15:21:19 +0000611 return O << S.name << " - " << toJSON(S);
612}
613
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000614llvm::json::Value toJSON(const DocumentSymbol &S) {
615 llvm::json::Object Result{{"name", S.name},
616 {"kind", static_cast<int>(S.kind)},
617 {"range", S.range},
618 {"selectionRange", S.selectionRange}};
Ilya Biryukov19d75602018-11-23 15:21:19 +0000619
620 if (!S.detail.empty())
621 Result["detail"] = S.detail;
622 if (!S.children.empty())
623 Result["children"] = S.children;
624 if (S.deprecated)
625 Result["deprecated"] = true;
Ilya Biryukov4174d092018-11-26 09:57:41 +0000626 // Older gcc cannot compile 'return Result', even though it is legal.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000627 return llvm::json::Value(std::move(Result));
Ilya Biryukov19d75602018-11-23 15:21:19 +0000628}
629
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000630llvm::json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000631 if (!WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000632 return llvm::json::Object{};
633 llvm::json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000634 for (auto &Change : *WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000635 FileChanges[Change.first] = llvm::json::Array(Change.second);
636 return llvm::json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000637}
638
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000639bool fromJSON(const llvm::json::Value &Params, TweakArgs &A) {
640 llvm::json::ObjectMapper O(Params);
641 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
642 O.map("tweakID", A.tweakID);
643}
644
645llvm::json::Value toJSON(const TweakArgs &A) {
646 return llvm::json::Object{
647 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
648}
649
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000650llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
651 return llvm::json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000652}
653
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000654bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R) {
655 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000656 return O && O.map("textDocument", R.textDocument) &&
657 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000658}
659
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000660bool fromJSON(const llvm::json::Value &Params, CompletionContext &R) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000661 llvm::json::ObjectMapper O(Params);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000662 if (!O)
663 return false;
664
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000665 int TriggerKind;
666 if (!O.map("triggerKind", TriggerKind))
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000667 return false;
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000668 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000669
670 if (auto *TC = Params.getAsObject()->get("triggerCharacter"))
671 return fromJSON(*TC, R.triggerCharacter);
672 return true;
673}
674
675bool fromJSON(const llvm::json::Value &Params, CompletionParams &R) {
676 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R)))
677 return false;
678 if (auto *Context = Params.getAsObject()->get("context"))
679 return fromJSON(*Context, R.context);
680 return true;
681}
682
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000683static llvm::StringRef toTextKind(MarkupKind Kind) {
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000684 switch (Kind) {
685 case MarkupKind::PlainText:
686 return "plaintext";
687 case MarkupKind::Markdown:
688 return "markdown";
689 }
690 llvm_unreachable("Invalid MarkupKind");
691}
692
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000693bool fromJSON(const llvm::json::Value &V, MarkupKind &K) {
694 auto Str = V.getAsString();
695 if (!Str) {
696 elog("Failed to parse markup kind: expected a string");
697 return false;
698 }
699 if (*Str == "plaintext")
700 K = MarkupKind::PlainText;
701 else if (*Str == "markdown")
702 K = MarkupKind::Markdown;
703 else {
704 elog("Unknown markup kind: {0}", *Str);
705 return false;
706 }
707 return true;
708}
709
710llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
711 return OS << toTextKind(K);
712}
713
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000714llvm::json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000715 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000716 return nullptr;
717
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000718 return llvm::json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000719 {"kind", toTextKind(MC.kind)},
720 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000721 };
722}
723
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000724llvm::json::Value toJSON(const Hover &H) {
725 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000726
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000727 if (H.range.hasValue())
728 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000729
730 return std::move(Result);
731}
732
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000733bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000734 if (auto T = E.getAsInteger()) {
735 if (*T < static_cast<int>(CompletionItemKind::Text) ||
736 *T > static_cast<int>(CompletionItemKind::TypeParameter))
737 return false;
738 Out = static_cast<CompletionItemKind>(*T);
739 return true;
740 }
741 return false;
742}
743
744CompletionItemKind
745adjustKindToCapability(CompletionItemKind Kind,
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000746 CompletionItemKindBitset &SupportedCompletionItemKinds) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000747 auto KindVal = static_cast<size_t>(Kind);
748 if (KindVal >= CompletionItemKindMin &&
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000749 KindVal <= SupportedCompletionItemKinds.size() &&
750 SupportedCompletionItemKinds[KindVal])
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000751 return Kind;
752
753 switch (Kind) {
754 // Provide some fall backs for common kinds that are close enough.
755 case CompletionItemKind::Folder:
756 return CompletionItemKind::File;
757 case CompletionItemKind::EnumMember:
758 return CompletionItemKind::Enum;
759 case CompletionItemKind::Struct:
760 return CompletionItemKind::Class;
761 default:
762 return CompletionItemKind::Text;
763 }
764}
765
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000766bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000767 if (auto *A = E.getAsArray()) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000768 for (size_t I = 0; I < A->size(); ++I) {
769 CompletionItemKind KindOut;
770 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000771 Out.set(size_t(KindOut));
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000772 }
773 return true;
774 }
775 return false;
776}
777
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000778llvm::json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000779 assert(!CI.label.empty() && "completion item label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000780 llvm::json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000781 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000782 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000783 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000784 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000785 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000786 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000787 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000788 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000789 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000790 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000791 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000792 Result["insertText"] = CI.insertText;
793 if (CI.insertTextFormat != InsertTextFormat::Missing)
794 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000795 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000796 Result["textEdit"] = *CI.textEdit;
797 if (!CI.additionalTextEdits.empty())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000798 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
Eric Liu6df66002018-09-06 18:52:26 +0000799 if (CI.deprecated)
800 Result["deprecated"] = CI.deprecated;
Sam McCalldd0566b2017-11-06 15:40:30 +0000801 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000802}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000803
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000804llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000805 O << I.label << " - " << toJSON(I);
806 return O;
807}
808
Sam McCallff8b8742017-11-30 21:32:29 +0000809bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000810 return (L.sortText.empty() ? L.label : L.sortText) <
811 (R.sortText.empty() ? R.label : R.sortText);
812}
813
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000814llvm::json::Value toJSON(const CompletionList &L) {
815 return llvm::json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000816 {"isIncomplete", L.isIncomplete},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000817 {"items", llvm::json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000818 };
819}
820
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000821llvm::json::Value toJSON(const ParameterInformation &PI) {
Simon Pilgrim5b41fe52019-06-04 11:31:45 +0000822 assert((PI.labelOffsets.hasValue() || !PI.labelString.empty()) &&
823 "parameter information label is required");
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000824 llvm::json::Object Result;
825 if (PI.labelOffsets)
826 Result["label"] =
827 llvm::json::Array({PI.labelOffsets->first, PI.labelOffsets->second});
828 else
829 Result["label"] = PI.labelString;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000830 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000831 Result["documentation"] = PI.documentation;
832 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000833}
834
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000835llvm::json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000836 assert(!SI.label.empty() && "signature information label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000837 llvm::json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000838 {"label", SI.label},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000839 {"parameters", llvm::json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000840 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000841 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000842 Result["documentation"] = SI.documentation;
843 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000844}
845
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000846llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
847 const SignatureInformation &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000848 O << I.label << " - " << toJSON(I);
849 return O;
850}
851
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000852llvm::json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000853 assert(SH.activeSignature >= 0 &&
854 "Unexpected negative value for number of active signatures.");
855 assert(SH.activeParameter >= 0 &&
856 "Unexpected negative value for active parameter index");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000857 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000858 {"activeSignature", SH.activeSignature},
859 {"activeParameter", SH.activeParameter},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000860 {"signatures", llvm::json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000861 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000862}
Haojian Wu345099c2017-11-09 11:30:04 +0000863
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000864bool fromJSON(const llvm::json::Value &Params, RenameParams &R) {
865 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000866 return O && O.map("textDocument", R.textDocument) &&
867 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000868}
Sam McCallff8b8742017-11-30 21:32:29 +0000869
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000870llvm::json::Value toJSON(const DocumentHighlight &DH) {
871 return llvm::json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000872 {"range", toJSON(DH.range)},
873 {"kind", static_cast<int>(DH.kind)},
874 };
875}
876
Haojian Wub6188492018-12-20 15:39:12 +0000877llvm::json::Value toJSON(const FileStatus &FStatus) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000878 return llvm::json::Object{
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000879 {"uri", FStatus.uri},
880 {"state", FStatus.state},
Haojian Wub6188492018-12-20 15:39:12 +0000881 };
882}
883
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000884llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
885 const DocumentHighlight &V) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000886 O << V.range;
887 if (V.kind == DocumentHighlightKind::Read)
888 O << "(r)";
889 if (V.kind == DocumentHighlightKind::Write)
890 O << "(w)";
891 return O;
892}
893
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000894bool fromJSON(const llvm::json::Value &Params,
895 DidChangeConfigurationParams &CCP) {
896 llvm::json::ObjectMapper O(Params);
Simon Marchi5178f922018-02-22 14:00:39 +0000897 return O && O.map("settings", CCP.settings);
898}
899
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000900bool fromJSON(const llvm::json::Value &Params,
901 ClangdCompileCommand &CDbUpdate) {
902 llvm::json::ObjectMapper O(Params);
Alex Lorenzf8087862018-08-01 17:39:29 +0000903 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
904 O.map("compilationCommand", CDbUpdate.compilationCommand);
905}
906
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000907bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S) {
908 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000909 if (!O)
910 return true; // 'any' type in LSP.
911 O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
912 return true;
Simon Marchi5178f922018-02-22 14:00:39 +0000913}
914
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000915bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts) {
916 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000917 if (!O)
918 return true; // 'any' type in LSP.
919
920 fromJSON(Params, Opts.ConfigSettings);
921 O.map("compilationDatabasePath", Opts.compilationDatabasePath);
Sam McCall6980edb2018-11-02 14:07:51 +0000922 O.map("fallbackFlags", Opts.fallbackFlags);
Haojian Wub6188492018-12-20 15:39:12 +0000923 O.map("clangdFileStatus", Opts.FileStatus);
Sam McCallbc904612018-10-25 04:22:52 +0000924 return true;
Simon Marchiabeed662018-10-16 15:55:03 +0000925}
926
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000927bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out) {
928 auto T = E.getAsInteger();
929 if (!T)
930 return false;
931 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
932 *T > static_cast<int>(TypeHierarchyDirection::Both))
933 return false;
934 Out = static_cast<TypeHierarchyDirection>(*T);
935 return true;
936}
937
938bool fromJSON(const llvm::json::Value &Params, TypeHierarchyParams &R) {
939 llvm::json::ObjectMapper O(Params);
940 return O && O.map("textDocument", R.textDocument) &&
941 O.map("position", R.position) && O.map("resolve", R.resolve) &&
942 O.map("direction", R.direction);
943}
944
945llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
946 const TypeHierarchyItem &I) {
947 return O << I.name << " - " << toJSON(I);
948}
949
950llvm::json::Value toJSON(const TypeHierarchyItem &I) {
951 llvm::json::Object Result{{"name", I.name},
952 {"kind", static_cast<int>(I.kind)},
953 {"range", I.range},
954 {"selectionRange", I.selectionRange},
955 {"uri", I.uri}};
956
957 if (I.detail)
958 Result["detail"] = I.detail;
959 if (I.deprecated)
960 Result["deprecated"] = I.deprecated;
961 if (I.parents)
962 Result["parents"] = I.parents;
963 if (I.children)
964 Result["children"] = I.children;
965 return std::move(Result);
966}
967
968bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I) {
969 llvm::json::ObjectMapper O(Params);
970
971 // Required fields.
972 if (!(O && O.map("name", I.name) && O.map("kind", I.kind) &&
973 O.map("uri", I.uri) && O.map("range", I.range) &&
974 O.map("selectionRange", I.selectionRange))) {
975 return false;
976 }
977
978 // Optional fields.
979 O.map("detail", I.detail);
980 O.map("deprecated", I.deprecated);
981 O.map("parents", I.parents);
982 O.map("children", I.children);
983
984 return true;
985}
986
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000987bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R) {
Sam McCall1ad142f2018-09-05 11:53:07 +0000988 TextDocumentPositionParams &Base = R;
989 return fromJSON(Params, Base);
990}
991
Sam McCall8b25d222019-03-28 14:37:51 +0000992static const char *toString(OffsetEncoding OE) {
Sam McCalla69698f2019-03-27 17:47:49 +0000993 switch (OE) {
Sam McCall8b25d222019-03-28 14:37:51 +0000994 case OffsetEncoding::UTF8:
995 return "utf-8";
996 case OffsetEncoding::UTF16:
997 return "utf-16";
998 case OffsetEncoding::UTF32:
999 return "utf-32";
1000 case OffsetEncoding::UnsupportedEncoding:
1001 return "unknown";
Sam McCalla69698f2019-03-27 17:47:49 +00001002 }
Simon Pilgrim945db0b2019-03-29 13:43:00 +00001003 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
Sam McCalla69698f2019-03-27 17:47:49 +00001004}
Sam McCall8b25d222019-03-28 14:37:51 +00001005llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
Sam McCalla69698f2019-03-27 17:47:49 +00001006bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE) {
1007 auto Str = V.getAsString();
1008 if (!Str)
1009 return false;
1010 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
1011 .Case("utf-8", OffsetEncoding::UTF8)
1012 .Case("utf-16", OffsetEncoding::UTF16)
Sam McCall8b25d222019-03-28 14:37:51 +00001013 .Case("utf-32", OffsetEncoding::UTF32)
Sam McCalla69698f2019-03-27 17:47:49 +00001014 .Default(OffsetEncoding::UnsupportedEncoding);
1015 return true;
1016}
Sam McCall8b25d222019-03-28 14:37:51 +00001017llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
1018 return OS << toString(Enc);
1019}
Sam McCalla69698f2019-03-27 17:47:49 +00001020
Sam McCallff8b8742017-11-30 21:32:29 +00001021} // namespace clangd
1022} // namespace clang