blob: 5ecd7195c07b764536034cd37295aacd7a744410 [file] [log] [blame]
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00001//===--- Protocol.cpp - Language Server Protocol Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the serialization code for the LSP structs.
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "Protocol.h"
Eric Liu78ed91a72018-01-29 15:37:46 +000015#include "Logger.h"
Ilya Biryukov7d60d202018-02-16 12:20:47 +000016#include "URI.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000017#include "clang/Basic/LLVM.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/Support/Format.h"
Marc-Andre Laperle85dcce42017-09-18 15:02:59 +000020#include "llvm/Support/FormatVariadic.h"
Krasimir Georgiev50117372017-04-07 11:03:26 +000021#include "llvm/Support/Path.h"
Ilya Biryukov574b7532017-08-02 09:08:39 +000022#include "llvm/Support/raw_ostream.h"
Ilya Biryukove5128f72017-09-20 07:24:15 +000023
Sam McCallff8b8742017-11-30 21:32:29 +000024namespace clang {
25namespace clangd {
Sam McCalld20d7982018-07-09 14:25:59 +000026using namespace llvm;
Sam McCall38a04912017-11-29 11:36:46 +000027
Ilya Biryukov7d60d202018-02-16 12:20:47 +000028URIForFile::URIForFile(std::string AbsPath) {
29 assert(llvm::sys::path::is_absolute(AbsPath) && "the path is relative");
30 File = std::move(AbsPath);
31}
32
Sam McCalld20d7982018-07-09 14:25:59 +000033bool fromJSON(const json::Value &E, URIForFile &R) {
34 if (auto S = E.getAsString()) {
Eric Liu78ed91a72018-01-29 15:37:46 +000035 auto U = URI::parse(*S);
36 if (!U) {
Sam McCallbed58852018-07-11 10:35:11 +000037 elog("Failed to parse URI {0}: {1}", *S, U.takeError());
Eric Liu78ed91a72018-01-29 15:37:46 +000038 return false;
39 }
Eric Liu5740ff52018-01-31 16:26:27 +000040 if (U->scheme() != "file" && U->scheme() != "test") {
Sam McCallbed58852018-07-11 10:35:11 +000041 elog("Clangd only supports 'file' URI scheme for workspace files: {0}",
42 *S);
Eric Liu78ed91a72018-01-29 15:37:46 +000043 return false;
44 }
Sam McCall41d21522018-01-30 11:23:11 +000045 auto Path = URI::resolve(*U);
46 if (!Path) {
Sam McCallbed58852018-07-11 10:35:11 +000047 log("{0}", Path.takeError());
Sam McCall41d21522018-01-30 11:23:11 +000048 return false;
49 }
Ilya Biryukov7d60d202018-02-16 12:20:47 +000050 R = URIForFile(*Path);
Sam McCallff8b8742017-11-30 21:32:29 +000051 return true;
52 }
53 return false;
Sam McCall38a04912017-11-29 11:36:46 +000054}
55
Sam McCalld20d7982018-07-09 14:25:59 +000056json::Value toJSON(const URIForFile &U) { return U.uri(); }
Krasimir Georgiev50117372017-04-07 11:03:26 +000057
Eric Liu78ed91a72018-01-29 15:37:46 +000058llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const URIForFile &U) {
59 return OS << U.uri();
Sam McCallfffa8222017-12-20 10:26:53 +000060}
61
Sam McCalld20d7982018-07-09 14:25:59 +000062json::Value toJSON(const TextDocumentIdentifier &R) {
63 return json::Object{{"uri", R.uri}};
Eric Liuc5105f92018-02-16 14:15:55 +000064}
65
Sam McCalld20d7982018-07-09 14:25:59 +000066bool fromJSON(const json::Value &Params, TextDocumentIdentifier &R) {
Sam McCallff8b8742017-11-30 21:32:29 +000067 json::ObjectMapper O(Params);
68 return O && O.map("uri", R.uri);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000069}
70
Sam McCalld20d7982018-07-09 14:25:59 +000071bool fromJSON(const json::Value &Params, Position &R) {
Sam McCallff8b8742017-11-30 21:32:29 +000072 json::ObjectMapper O(Params);
73 return O && O.map("line", R.line) && O.map("character", R.character);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000074}
75
Sam McCalld20d7982018-07-09 14:25:59 +000076json::Value toJSON(const Position &P) {
77 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +000078 {"line", P.line},
79 {"character", P.character},
80 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000081}
82
Sam McCallfffa8222017-12-20 10:26:53 +000083llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Position &P) {
84 return OS << P.line << ':' << P.character;
85}
86
Sam McCalld20d7982018-07-09 14:25:59 +000087bool fromJSON(const json::Value &Params, Range &R) {
Sam McCallff8b8742017-11-30 21:32:29 +000088 json::ObjectMapper O(Params);
89 return O && O.map("start", R.start) && O.map("end", R.end);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000090}
91
Sam McCalld20d7982018-07-09 14:25:59 +000092json::Value toJSON(const Range &P) {
93 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +000094 {"start", P.start},
95 {"end", P.end},
96 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000097}
98
Sam McCallfffa8222017-12-20 10:26:53 +000099llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Range &R) {
100 return OS << R.start << '-' << R.end;
101}
102
Sam McCalld20d7982018-07-09 14:25:59 +0000103json::Value toJSON(const Location &P) {
104 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000105 {"uri", P.uri},
106 {"range", P.range},
107 };
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000108}
109
Sam McCallfffa8222017-12-20 10:26:53 +0000110llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Location &L) {
111 return OS << L.range << '@' << L.uri;
112}
113
Sam McCalld20d7982018-07-09 14:25:59 +0000114bool fromJSON(const json::Value &Params, TextDocumentItem &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000115 json::ObjectMapper O(Params);
116 return O && O.map("uri", R.uri) && O.map("languageId", R.languageId) &&
117 O.map("version", R.version) && O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000118}
119
Sam McCalld20d7982018-07-09 14:25:59 +0000120bool fromJSON(const json::Value &Params, Metadata &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000121 json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000122 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000123 return false;
124 O.map("extraFlags", R.extraFlags);
125 return true;
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000126}
127
Sam McCalld20d7982018-07-09 14:25:59 +0000128bool fromJSON(const json::Value &Params, TextEdit &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000129 json::ObjectMapper O(Params);
130 return O && O.map("range", R.range) && O.map("newText", R.newText);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000131}
132
Sam McCalld20d7982018-07-09 14:25:59 +0000133json::Value toJSON(const TextEdit &P) {
134 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000135 {"range", P.range},
136 {"newText", P.newText},
137 };
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000138}
139
Sam McCall034e11a2018-01-25 17:29:17 +0000140llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const TextEdit &TE) {
141 OS << TE.range << " => \"";
Jonas Devlieghere1ab6a7a2018-05-31 17:36:31 +0000142 printEscapedString(TE.newText, OS);
Sam McCall034e11a2018-01-25 17:29:17 +0000143 return OS << '"';
144}
145
Sam McCalld20d7982018-07-09 14:25:59 +0000146bool fromJSON(const json::Value &E, TraceLevel &Out) {
147 if (auto S = E.getAsString()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000148 if (*S == "off") {
149 Out = TraceLevel::Off;
150 return true;
151 } else if (*S == "messages") {
152 Out = TraceLevel::Messages;
153 return true;
154 } else if (*S == "verbose") {
155 Out = TraceLevel::Verbose;
156 return true;
157 }
158 }
159 return false;
160}
161
Sam McCalld20d7982018-07-09 14:25:59 +0000162bool fromJSON(const json::Value &Params, CompletionItemClientCapabilities &R) {
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000163 json::ObjectMapper O(Params);
164 if (!O)
165 return false;
166 O.map("snippetSupport", R.snippetSupport);
167 O.map("commitCharacterSupport", R.commitCharacterSupport);
168 return true;
169}
170
Sam McCalld20d7982018-07-09 14:25:59 +0000171bool fromJSON(const json::Value &Params, CompletionClientCapabilities &R) {
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000172 json::ObjectMapper O(Params);
173 if (!O)
174 return false;
175 O.map("dynamicRegistration", R.dynamicRegistration);
176 O.map("completionItem", R.completionItem);
177 O.map("contextSupport", R.contextSupport);
178 return true;
179}
180
Sam McCalld20d7982018-07-09 14:25:59 +0000181bool fromJSON(const json::Value &E, SymbolKind &Out) {
182 if (auto T = E.getAsInteger()) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000183 if (*T < static_cast<int>(SymbolKind::File) ||
184 *T > static_cast<int>(SymbolKind::TypeParameter))
185 return false;
186 Out = static_cast<SymbolKind>(*T);
187 return true;
188 }
189 return false;
190}
191
Sam McCalld20d7982018-07-09 14:25:59 +0000192bool fromJSON(const json::Value &E, std::vector<SymbolKind> &Out) {
193 if (auto *A = E.getAsArray()) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000194 Out.clear();
195 for (size_t I = 0; I < A->size(); ++I) {
196 SymbolKind KindOut;
197 if (fromJSON((*A)[I], KindOut))
198 Out.push_back(KindOut);
199 }
200 return true;
201 }
202 return false;
203}
204
Sam McCalld20d7982018-07-09 14:25:59 +0000205bool fromJSON(const json::Value &Params, SymbolKindCapabilities &R) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000206 json::ObjectMapper O(Params);
207 return O && O.map("valueSet", R.valueSet);
208}
209
210SymbolKind adjustKindToCapability(SymbolKind Kind,
Ilya Biryukov74f26552018-07-26 12:05:31 +0000211 SymbolKindBitset &SupportedSymbolKinds) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000212 auto KindVal = static_cast<size_t>(Kind);
Ilya Biryukov74f26552018-07-26 12:05:31 +0000213 if (KindVal >= SymbolKindMin && KindVal <= SupportedSymbolKinds.size() &&
214 SupportedSymbolKinds[KindVal])
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000215 return Kind;
216
217 switch (Kind) {
218 // Provide some fall backs for common kinds that are close enough.
219 case SymbolKind::Struct:
220 return SymbolKind::Class;
221 case SymbolKind::EnumMember:
222 return SymbolKind::Enum;
223 default:
224 return SymbolKind::String;
225 }
226}
227
Sam McCalld20d7982018-07-09 14:25:59 +0000228bool fromJSON(const json::Value &Params, WorkspaceSymbolCapabilities &R) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000229 json::ObjectMapper O(Params);
230 return O && O.map("symbolKind", R.symbolKind);
231}
232
Sam McCalld20d7982018-07-09 14:25:59 +0000233bool fromJSON(const json::Value &Params, WorkspaceClientCapabilities &R) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000234 json::ObjectMapper O(Params);
235 return O && O.map("symbol", R.symbol);
236}
237
Sam McCalld20d7982018-07-09 14:25:59 +0000238bool fromJSON(const json::Value &Params, TextDocumentClientCapabilities &R) {
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000239 json::ObjectMapper O(Params);
240 if (!O)
241 return false;
242 O.map("completion", R.completion);
243 return true;
244}
245
Sam McCalld20d7982018-07-09 14:25:59 +0000246bool fromJSON(const json::Value &Params, ClientCapabilities &R) {
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000247 json::ObjectMapper O(Params);
248 if (!O)
249 return false;
250 O.map("textDocument", R.textDocument);
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000251 O.map("workspace", R.workspace);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000252 return true;
253}
254
Sam McCalld20d7982018-07-09 14:25:59 +0000255bool fromJSON(const json::Value &Params, InitializeParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000256 json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000257 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000258 return false;
Sam McCall38a04912017-11-29 11:36:46 +0000259 // We deliberately don't fail if we can't parse individual fields.
260 // Failing to handle a slightly malformed initialize would be a disaster.
Sam McCallff8b8742017-11-30 21:32:29 +0000261 O.map("processId", R.processId);
262 O.map("rootUri", R.rootUri);
263 O.map("rootPath", R.rootPath);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000264 O.map("capabilities", R.capabilities);
Sam McCallff8b8742017-11-30 21:32:29 +0000265 O.map("trace", R.trace);
Simon Marchi88016782018-08-01 11:28:49 +0000266 O.map("initializationOptions", R.initializationOptions);
Sam McCallff8b8742017-11-30 21:32:29 +0000267 return true;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000268}
269
Sam McCalld20d7982018-07-09 14:25:59 +0000270bool fromJSON(const json::Value &Params, DidOpenTextDocumentParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000271 json::ObjectMapper O(Params);
272 return O && O.map("textDocument", R.textDocument) &&
273 O.map("metadata", R.metadata);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000274}
275
Sam McCalld20d7982018-07-09 14:25:59 +0000276bool fromJSON(const json::Value &Params, DidCloseTextDocumentParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000277 json::ObjectMapper O(Params);
278 return O && O.map("textDocument", R.textDocument);
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +0000279}
280
Sam McCalld20d7982018-07-09 14:25:59 +0000281bool fromJSON(const json::Value &Params, DidChangeTextDocumentParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000282 json::ObjectMapper O(Params);
283 return O && O.map("textDocument", R.textDocument) &&
Eric Liu51fed182018-02-22 18:40:39 +0000284 O.map("contentChanges", R.contentChanges) &&
285 O.map("wantDiagnostics", R.wantDiagnostics);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000286}
287
Sam McCalld20d7982018-07-09 14:25:59 +0000288bool fromJSON(const json::Value &E, FileChangeType &Out) {
289 if (auto T = E.getAsInteger()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000290 if (*T < static_cast<int>(FileChangeType::Created) ||
291 *T > static_cast<int>(FileChangeType::Deleted))
292 return false;
293 Out = static_cast<FileChangeType>(*T);
294 return true;
295 }
296 return false;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000297}
298
Sam McCalld20d7982018-07-09 14:25:59 +0000299bool fromJSON(const json::Value &Params, FileEvent &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000300 json::ObjectMapper O(Params);
301 return O && O.map("uri", R.uri) && O.map("type", R.type);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000302}
303
Sam McCalld20d7982018-07-09 14:25:59 +0000304bool fromJSON(const json::Value &Params, DidChangeWatchedFilesParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000305 json::ObjectMapper O(Params);
306 return O && O.map("changes", R.changes);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000307}
308
Sam McCalld20d7982018-07-09 14:25:59 +0000309bool fromJSON(const json::Value &Params, TextDocumentContentChangeEvent &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000310 json::ObjectMapper O(Params);
Simon Marchi98082622018-03-26 14:41:40 +0000311 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
312 O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000313}
314
Sam McCalld20d7982018-07-09 14:25:59 +0000315bool fromJSON(const json::Value &Params, FormattingOptions &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000316 json::ObjectMapper O(Params);
317 return O && O.map("tabSize", R.tabSize) &&
318 O.map("insertSpaces", R.insertSpaces);
319}
320
Sam McCalld20d7982018-07-09 14:25:59 +0000321json::Value toJSON(const FormattingOptions &P) {
322 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000323 {"tabSize", P.tabSize},
324 {"insertSpaces", P.insertSpaces},
325 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000326}
327
Sam McCalld20d7982018-07-09 14:25:59 +0000328bool fromJSON(const json::Value &Params, DocumentRangeFormattingParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000329 json::ObjectMapper O(Params);
330 return O && O.map("textDocument", R.textDocument) &&
331 O.map("range", R.range) && O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000332}
333
Sam McCalld20d7982018-07-09 14:25:59 +0000334bool fromJSON(const json::Value &Params, DocumentOnTypeFormattingParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000335 json::ObjectMapper O(Params);
336 return O && O.map("textDocument", R.textDocument) &&
337 O.map("position", R.position) && O.map("ch", R.ch) &&
338 O.map("options", R.options);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000339}
340
Sam McCalld20d7982018-07-09 14:25:59 +0000341bool fromJSON(const json::Value &Params, DocumentFormattingParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000342 json::ObjectMapper O(Params);
343 return O && O.map("textDocument", R.textDocument) &&
344 O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000345}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000346
Sam McCalld20d7982018-07-09 14:25:59 +0000347bool fromJSON(const json::Value &Params, DocumentSymbolParams &R) {
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000348 json::ObjectMapper O(Params);
349 return O && O.map("textDocument", R.textDocument);
350}
351
Sam McCalld20d7982018-07-09 14:25:59 +0000352bool fromJSON(const json::Value &Params, Diagnostic &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000353 json::ObjectMapper O(Params);
354 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
355 return false;
356 O.map("severity", R.severity);
357 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000358}
359
Sam McCalld20d7982018-07-09 14:25:59 +0000360bool fromJSON(const json::Value &Params, CodeActionContext &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000361 json::ObjectMapper O(Params);
362 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000363}
364
Sam McCall034e11a2018-01-25 17:29:17 +0000365llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
366 OS << D.range << " [";
367 switch (D.severity) {
368 case 1:
369 OS << "error";
370 break;
371 case 2:
372 OS << "warning";
373 break;
374 case 3:
375 OS << "note";
376 break;
377 case 4:
378 OS << "remark";
379 break;
380 default:
381 OS << "diagnostic";
382 break;
383 }
384 return OS << '(' << D.severity << "): " << D.message << "]";
385}
386
Sam McCalld20d7982018-07-09 14:25:59 +0000387bool fromJSON(const json::Value &Params, CodeActionParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000388 json::ObjectMapper O(Params);
389 return O && O.map("textDocument", R.textDocument) &&
390 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000391}
392
Sam McCalld20d7982018-07-09 14:25:59 +0000393bool fromJSON(const json::Value &Params, WorkspaceEdit &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000394 json::ObjectMapper O(Params);
395 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000396}
397
Benjamin Kramerb4c5c2d2017-12-28 15:03:02 +0000398const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000399 "clangd.applyFix";
Sam McCalld20d7982018-07-09 14:25:59 +0000400bool fromJSON(const json::Value &Params, ExecuteCommandParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000401 json::ObjectMapper O(Params);
402 if (!O || !O.map("command", R.command))
403 return false;
Sam McCallec109022017-11-28 09:37:43 +0000404
Sam McCalld20d7982018-07-09 14:25:59 +0000405 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000406 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
407 return Args && Args->size() == 1 &&
408 fromJSON(Args->front(), R.workspaceEdit);
409 }
410 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000411}
412
Sam McCalld20d7982018-07-09 14:25:59 +0000413json::Value toJSON(const SymbolInformation &P) {
414 return json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000415 {"name", P.name},
416 {"kind", static_cast<int>(P.kind)},
417 {"location", P.location},
418 {"containerName", P.containerName},
419 };
420}
421
422llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
423 const SymbolInformation &SI) {
424 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
425 return O;
426}
427
Sam McCalld20d7982018-07-09 14:25:59 +0000428bool fromJSON(const json::Value &Params, WorkspaceSymbolParams &R) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000429 json::ObjectMapper O(Params);
430 return O && O.map("query", R.query);
431}
432
Sam McCalld20d7982018-07-09 14:25:59 +0000433json::Value toJSON(const Command &C) {
434 auto Cmd = json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000435 if (C.workspaceEdit)
436 Cmd["arguments"] = {*C.workspaceEdit};
Eric Liuc5105f92018-02-16 14:15:55 +0000437 return std::move(Cmd);
438}
439
Sam McCalld20d7982018-07-09 14:25:59 +0000440json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000441 if (!WE.changes)
Sam McCalld20d7982018-07-09 14:25:59 +0000442 return json::Object{};
443 json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000444 for (auto &Change : *WE.changes)
Sam McCalld20d7982018-07-09 14:25:59 +0000445 FileChanges[Change.first] = json::Array(Change.second);
446 return json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000447}
448
Sam McCalld20d7982018-07-09 14:25:59 +0000449json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
450 return json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000451}
452
Sam McCalld20d7982018-07-09 14:25:59 +0000453bool fromJSON(const json::Value &Params, TextDocumentPositionParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000454 json::ObjectMapper O(Params);
455 return O && O.map("textDocument", R.textDocument) &&
456 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000457}
458
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000459static StringRef toTextKind(MarkupKind Kind) {
460 switch (Kind) {
461 case MarkupKind::PlainText:
462 return "plaintext";
463 case MarkupKind::Markdown:
464 return "markdown";
465 }
466 llvm_unreachable("Invalid MarkupKind");
467}
468
Sam McCalld20d7982018-07-09 14:25:59 +0000469json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000470 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000471 return nullptr;
472
Sam McCalld20d7982018-07-09 14:25:59 +0000473 return json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000474 {"kind", toTextKind(MC.kind)},
475 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000476 };
477}
478
Sam McCalld20d7982018-07-09 14:25:59 +0000479json::Value toJSON(const Hover &H) {
480 json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000481
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000482 if (H.range.hasValue())
483 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000484
485 return std::move(Result);
486}
487
Sam McCalld20d7982018-07-09 14:25:59 +0000488json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000489 assert(!CI.label.empty() && "completion item label is required");
Sam McCalld20d7982018-07-09 14:25:59 +0000490 json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000491 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000492 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000493 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000494 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000495 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000496 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000497 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000498 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000499 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000500 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000501 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000502 Result["insertText"] = CI.insertText;
503 if (CI.insertTextFormat != InsertTextFormat::Missing)
504 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000505 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000506 Result["textEdit"] = *CI.textEdit;
507 if (!CI.additionalTextEdits.empty())
Sam McCalld20d7982018-07-09 14:25:59 +0000508 Result["additionalTextEdits"] = json::Array(CI.additionalTextEdits);
Sam McCalldd0566b2017-11-06 15:40:30 +0000509 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000510}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000511
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000512llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
513 O << I.label << " - " << toJSON(I);
514 return O;
515}
516
Sam McCallff8b8742017-11-30 21:32:29 +0000517bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000518 return (L.sortText.empty() ? L.label : L.sortText) <
519 (R.sortText.empty() ? R.label : R.sortText);
520}
521
Sam McCalld20d7982018-07-09 14:25:59 +0000522json::Value toJSON(const CompletionList &L) {
523 return json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000524 {"isIncomplete", L.isIncomplete},
Sam McCalld20d7982018-07-09 14:25:59 +0000525 {"items", json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000526 };
527}
528
Sam McCalld20d7982018-07-09 14:25:59 +0000529json::Value toJSON(const ParameterInformation &PI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000530 assert(!PI.label.empty() && "parameter information label is required");
Sam McCalld20d7982018-07-09 14:25:59 +0000531 json::Object Result{{"label", PI.label}};
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000532 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000533 Result["documentation"] = PI.documentation;
534 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000535}
536
Sam McCalld20d7982018-07-09 14:25:59 +0000537json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000538 assert(!SI.label.empty() && "signature information label is required");
Sam McCalld20d7982018-07-09 14:25:59 +0000539 json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000540 {"label", SI.label},
Sam McCalld20d7982018-07-09 14:25:59 +0000541 {"parameters", json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000542 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000543 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000544 Result["documentation"] = SI.documentation;
545 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000546}
547
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000548llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
549 const SignatureInformation &I) {
550 O << I.label << " - " << toJSON(I);
551 return O;
552}
553
Sam McCalld20d7982018-07-09 14:25:59 +0000554json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000555 assert(SH.activeSignature >= 0 &&
556 "Unexpected negative value for number of active signatures.");
557 assert(SH.activeParameter >= 0 &&
558 "Unexpected negative value for active parameter index");
Sam McCalld20d7982018-07-09 14:25:59 +0000559 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000560 {"activeSignature", SH.activeSignature},
561 {"activeParameter", SH.activeParameter},
Sam McCalld20d7982018-07-09 14:25:59 +0000562 {"signatures", json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000563 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000564}
Haojian Wu345099c2017-11-09 11:30:04 +0000565
Sam McCalld20d7982018-07-09 14:25:59 +0000566bool fromJSON(const json::Value &Params, RenameParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000567 json::ObjectMapper O(Params);
568 return O && O.map("textDocument", R.textDocument) &&
569 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000570}
Sam McCallff8b8742017-11-30 21:32:29 +0000571
Sam McCalld20d7982018-07-09 14:25:59 +0000572json::Value toJSON(const DocumentHighlight &DH) {
573 return json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000574 {"range", toJSON(DH.range)},
575 {"kind", static_cast<int>(DH.kind)},
576 };
577}
578
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000579llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
580 const DocumentHighlight &V) {
581 O << V.range;
582 if (V.kind == DocumentHighlightKind::Read)
583 O << "(r)";
584 if (V.kind == DocumentHighlightKind::Write)
585 O << "(w)";
586 return O;
587}
588
Sam McCalld20d7982018-07-09 14:25:59 +0000589bool fromJSON(const json::Value &Params, DidChangeConfigurationParams &CCP) {
Simon Marchi5178f922018-02-22 14:00:39 +0000590 json::ObjectMapper O(Params);
591 return O && O.map("settings", CCP.settings);
592}
593
Sam McCalld20d7982018-07-09 14:25:59 +0000594bool fromJSON(const json::Value &Params,
595 ClangdConfigurationParamsChange &CCPC) {
Simon Marchi5178f922018-02-22 14:00:39 +0000596 json::ObjectMapper O(Params);
597 return O && O.map("compilationDatabasePath", CCPC.compilationDatabasePath);
598}
599
Sam McCallff8b8742017-11-30 21:32:29 +0000600} // namespace clangd
601} // namespace clang