blob: 1bb5961a5b43240c6ea16a9c831f55e8ba9bd8f6 [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 McCallc008af62018-10-20 15:30:37 +000024using namespace llvm;
Sam McCallff8b8742017-11-30 21:32:29 +000025namespace clang {
26namespace clangd {
Sam McCall38a04912017-11-29 11:36:46 +000027
Sam McCalldc8f3cf2018-10-17 07:32:05 +000028char LSPError::ID;
29
Ilya Biryukov7d60d202018-02-16 12:20:47 +000030URIForFile::URIForFile(std::string AbsPath) {
Sam McCallc008af62018-10-20 15:30:37 +000031 assert(sys::path::is_absolute(AbsPath) && "the path is relative");
Ilya Biryukov7d60d202018-02-16 12:20:47 +000032 File = std::move(AbsPath);
33}
34
Sam McCalld20d7982018-07-09 14:25:59 +000035bool fromJSON(const json::Value &E, URIForFile &R) {
36 if (auto S = E.getAsString()) {
Eric Liu78ed91a72018-01-29 15:37:46 +000037 auto U = URI::parse(*S);
38 if (!U) {
Sam McCallbed58852018-07-11 10:35:11 +000039 elog("Failed to parse URI {0}: {1}", *S, U.takeError());
Eric Liu78ed91a72018-01-29 15:37:46 +000040 return false;
41 }
Eric Liu5740ff52018-01-31 16:26:27 +000042 if (U->scheme() != "file" && U->scheme() != "test") {
Sam McCallbed58852018-07-11 10:35:11 +000043 elog("Clangd only supports 'file' URI scheme for workspace files: {0}",
44 *S);
Eric Liu78ed91a72018-01-29 15:37:46 +000045 return false;
46 }
Sam McCall41d21522018-01-30 11:23:11 +000047 auto Path = URI::resolve(*U);
48 if (!Path) {
Sam McCallbed58852018-07-11 10:35:11 +000049 log("{0}", Path.takeError());
Sam McCall41d21522018-01-30 11:23:11 +000050 return false;
51 }
Ilya Biryukov7d60d202018-02-16 12:20:47 +000052 R = URIForFile(*Path);
Sam McCallff8b8742017-11-30 21:32:29 +000053 return true;
54 }
55 return false;
Sam McCall38a04912017-11-29 11:36:46 +000056}
57
Sam McCalld20d7982018-07-09 14:25:59 +000058json::Value toJSON(const URIForFile &U) { return U.uri(); }
Krasimir Georgiev50117372017-04-07 11:03:26 +000059
Sam McCallc008af62018-10-20 15:30:37 +000060raw_ostream &operator<<(raw_ostream &OS, const URIForFile &U) {
Eric Liu78ed91a72018-01-29 15:37:46 +000061 return OS << U.uri();
Sam McCallfffa8222017-12-20 10:26:53 +000062}
63
Sam McCalld20d7982018-07-09 14:25:59 +000064json::Value toJSON(const TextDocumentIdentifier &R) {
65 return json::Object{{"uri", R.uri}};
Eric Liuc5105f92018-02-16 14:15:55 +000066}
67
Sam McCalld20d7982018-07-09 14:25:59 +000068bool fromJSON(const json::Value &Params, TextDocumentIdentifier &R) {
Sam McCallff8b8742017-11-30 21:32:29 +000069 json::ObjectMapper O(Params);
70 return O && O.map("uri", R.uri);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000071}
72
Sam McCalld20d7982018-07-09 14:25:59 +000073bool fromJSON(const json::Value &Params, Position &R) {
Sam McCallff8b8742017-11-30 21:32:29 +000074 json::ObjectMapper O(Params);
75 return O && O.map("line", R.line) && O.map("character", R.character);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000076}
77
Sam McCalld20d7982018-07-09 14:25:59 +000078json::Value toJSON(const Position &P) {
79 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +000080 {"line", P.line},
81 {"character", P.character},
82 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000083}
84
Sam McCallc008af62018-10-20 15:30:37 +000085raw_ostream &operator<<(raw_ostream &OS, const Position &P) {
Sam McCallfffa8222017-12-20 10:26:53 +000086 return OS << P.line << ':' << P.character;
87}
88
Sam McCalld20d7982018-07-09 14:25:59 +000089bool fromJSON(const json::Value &Params, Range &R) {
Sam McCallff8b8742017-11-30 21:32:29 +000090 json::ObjectMapper O(Params);
91 return O && O.map("start", R.start) && O.map("end", R.end);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000092}
93
Sam McCalld20d7982018-07-09 14:25:59 +000094json::Value toJSON(const Range &P) {
95 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +000096 {"start", P.start},
97 {"end", P.end},
98 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000099}
100
Sam McCallc008af62018-10-20 15:30:37 +0000101raw_ostream &operator<<(raw_ostream &OS, const Range &R) {
Sam McCallfffa8222017-12-20 10:26:53 +0000102 return OS << R.start << '-' << R.end;
103}
104
Sam McCalld20d7982018-07-09 14:25:59 +0000105json::Value toJSON(const Location &P) {
106 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000107 {"uri", P.uri},
108 {"range", P.range},
109 };
Marc-Andre Laperle2cbf0372017-06-28 16:12:10 +0000110}
111
Sam McCallc008af62018-10-20 15:30:37 +0000112raw_ostream &operator<<(raw_ostream &OS, const Location &L) {
Sam McCallfffa8222017-12-20 10:26:53 +0000113 return OS << L.range << '@' << L.uri;
114}
115
Sam McCalld20d7982018-07-09 14:25:59 +0000116bool fromJSON(const json::Value &Params, TextDocumentItem &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000117 json::ObjectMapper O(Params);
118 return O && O.map("uri", R.uri) && O.map("languageId", R.languageId) &&
119 O.map("version", R.version) && O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000120}
121
Sam McCalld20d7982018-07-09 14:25:59 +0000122bool fromJSON(const json::Value &Params, Metadata &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000123 json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000124 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000125 return false;
126 O.map("extraFlags", R.extraFlags);
127 return true;
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000128}
129
Sam McCalld20d7982018-07-09 14:25:59 +0000130bool fromJSON(const json::Value &Params, TextEdit &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000131 json::ObjectMapper O(Params);
132 return O && O.map("range", R.range) && O.map("newText", R.newText);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000133}
134
Sam McCalld20d7982018-07-09 14:25:59 +0000135json::Value toJSON(const TextEdit &P) {
136 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000137 {"range", P.range},
138 {"newText", P.newText},
139 };
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000140}
141
Sam McCallc008af62018-10-20 15:30:37 +0000142raw_ostream &operator<<(raw_ostream &OS, const TextEdit &TE) {
Sam McCall034e11a2018-01-25 17:29:17 +0000143 OS << TE.range << " => \"";
Jonas Devlieghere1ab6a7a2018-05-31 17:36:31 +0000144 printEscapedString(TE.newText, OS);
Sam McCall034e11a2018-01-25 17:29:17 +0000145 return OS << '"';
146}
147
Sam McCalld20d7982018-07-09 14:25:59 +0000148bool fromJSON(const json::Value &E, TraceLevel &Out) {
149 if (auto S = E.getAsString()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000150 if (*S == "off") {
151 Out = TraceLevel::Off;
152 return true;
153 } else if (*S == "messages") {
154 Out = TraceLevel::Messages;
155 return true;
156 } else if (*S == "verbose") {
157 Out = TraceLevel::Verbose;
158 return true;
159 }
160 }
161 return false;
162}
163
Sam McCalld20d7982018-07-09 14:25:59 +0000164bool fromJSON(const json::Value &E, SymbolKind &Out) {
165 if (auto T = E.getAsInteger()) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000166 if (*T < static_cast<int>(SymbolKind::File) ||
167 *T > static_cast<int>(SymbolKind::TypeParameter))
168 return false;
169 Out = static_cast<SymbolKind>(*T);
170 return true;
171 }
172 return false;
173}
174
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000175bool fromJSON(const json::Value &E, SymbolKindBitset &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000176 if (auto *A = E.getAsArray()) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000177 for (size_t I = 0; I < A->size(); ++I) {
178 SymbolKind KindOut;
179 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000180 Out.set(size_t(KindOut));
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000181 }
182 return true;
183 }
184 return false;
185}
186
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000187SymbolKind adjustKindToCapability(SymbolKind Kind,
Ilya Biryukov74f26552018-07-26 12:05:31 +0000188 SymbolKindBitset &SupportedSymbolKinds) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000189 auto KindVal = static_cast<size_t>(Kind);
Ilya Biryukov74f26552018-07-26 12:05:31 +0000190 if (KindVal >= SymbolKindMin && KindVal <= SupportedSymbolKinds.size() &&
191 SupportedSymbolKinds[KindVal])
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000192 return Kind;
193
194 switch (Kind) {
195 // Provide some fall backs for common kinds that are close enough.
196 case SymbolKind::Struct:
197 return SymbolKind::Class;
198 case SymbolKind::EnumMember:
199 return SymbolKind::Enum;
200 default:
201 return SymbolKind::String;
202 }
203}
204
Sam McCalld20d7982018-07-09 14:25:59 +0000205bool fromJSON(const json::Value &Params, ClientCapabilities &R) {
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000206 const json::Object *O = Params.getAsObject();
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000207 if (!O)
208 return false;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000209 if (auto *TextDocument = O->getObject("textDocument")) {
210 if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
211 if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
212 R.DiagnosticCategory = *CategorySupport;
Sam McCall16e70702018-10-24 07:59:38 +0000213 if (auto CodeActions = Diagnostics->getBoolean("codeActionsInline"))
214 R.DiagnosticFixes = *CodeActions;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000215 }
216 if (auto *Completion = TextDocument->getObject("completion")) {
217 if (auto *Item = Completion->getObject("completionItem")) {
218 if (auto SnippetSupport = Item->getBoolean("snippetSupport"))
219 R.CompletionSnippets = *SnippetSupport;
220 }
221 if (auto *ItemKind = Completion->getObject("completionItemKind")) {
222 if (auto *ValueSet = ItemKind->get("valueSet")) {
223 R.CompletionItemKinds.emplace();
224 if (!fromJSON(*ValueSet, *R.CompletionItemKinds))
225 return false;
226 }
227 }
228 }
229 if (auto *CodeAction = TextDocument->getObject("codeAction")) {
230 if (CodeAction->getObject("codeActionLiteralSupport"))
231 R.CodeActionStructure = true;
232 }
233 }
234 if (auto *Workspace = O->getObject("workspace")) {
235 if (auto *Symbol = Workspace->getObject("symbol")) {
236 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
237 if (auto *ValueSet = SymbolKind->get("valueSet")) {
238 R.WorkspaceSymbolKinds.emplace();
239 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds))
240 return false;
241 }
242 }
243 }
244 }
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000245 return true;
246}
247
Sam McCalld20d7982018-07-09 14:25:59 +0000248bool fromJSON(const json::Value &Params, InitializeParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000249 json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000250 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000251 return false;
Sam McCall38a04912017-11-29 11:36:46 +0000252 // We deliberately don't fail if we can't parse individual fields.
253 // Failing to handle a slightly malformed initialize would be a disaster.
Sam McCallff8b8742017-11-30 21:32:29 +0000254 O.map("processId", R.processId);
255 O.map("rootUri", R.rootUri);
256 O.map("rootPath", R.rootPath);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000257 O.map("capabilities", R.capabilities);
Sam McCallff8b8742017-11-30 21:32:29 +0000258 O.map("trace", R.trace);
Simon Marchi88016782018-08-01 11:28:49 +0000259 O.map("initializationOptions", R.initializationOptions);
Sam McCallff8b8742017-11-30 21:32:29 +0000260 return true;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000261}
262
Sam McCalld20d7982018-07-09 14:25:59 +0000263bool fromJSON(const json::Value &Params, DidOpenTextDocumentParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000264 json::ObjectMapper O(Params);
265 return O && O.map("textDocument", R.textDocument) &&
266 O.map("metadata", R.metadata);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000267}
268
Sam McCalld20d7982018-07-09 14:25:59 +0000269bool fromJSON(const json::Value &Params, DidCloseTextDocumentParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000270 json::ObjectMapper O(Params);
271 return O && O.map("textDocument", R.textDocument);
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +0000272}
273
Sam McCalld20d7982018-07-09 14:25:59 +0000274bool fromJSON(const json::Value &Params, DidChangeTextDocumentParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000275 json::ObjectMapper O(Params);
276 return O && O.map("textDocument", R.textDocument) &&
Eric Liu51fed182018-02-22 18:40:39 +0000277 O.map("contentChanges", R.contentChanges) &&
278 O.map("wantDiagnostics", R.wantDiagnostics);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000279}
280
Sam McCalld20d7982018-07-09 14:25:59 +0000281bool fromJSON(const json::Value &E, FileChangeType &Out) {
282 if (auto T = E.getAsInteger()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000283 if (*T < static_cast<int>(FileChangeType::Created) ||
284 *T > static_cast<int>(FileChangeType::Deleted))
285 return false;
286 Out = static_cast<FileChangeType>(*T);
287 return true;
288 }
289 return false;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000290}
291
Sam McCalld20d7982018-07-09 14:25:59 +0000292bool fromJSON(const json::Value &Params, FileEvent &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000293 json::ObjectMapper O(Params);
294 return O && O.map("uri", R.uri) && O.map("type", R.type);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000295}
296
Sam McCalld20d7982018-07-09 14:25:59 +0000297bool fromJSON(const json::Value &Params, DidChangeWatchedFilesParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000298 json::ObjectMapper O(Params);
299 return O && O.map("changes", R.changes);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000300}
301
Sam McCalld20d7982018-07-09 14:25:59 +0000302bool fromJSON(const json::Value &Params, TextDocumentContentChangeEvent &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000303 json::ObjectMapper O(Params);
Simon Marchi98082622018-03-26 14:41:40 +0000304 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
305 O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000306}
307
Sam McCalld20d7982018-07-09 14:25:59 +0000308bool fromJSON(const json::Value &Params, FormattingOptions &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000309 json::ObjectMapper O(Params);
310 return O && O.map("tabSize", R.tabSize) &&
311 O.map("insertSpaces", R.insertSpaces);
312}
313
Sam McCalld20d7982018-07-09 14:25:59 +0000314json::Value toJSON(const FormattingOptions &P) {
315 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000316 {"tabSize", P.tabSize},
317 {"insertSpaces", P.insertSpaces},
318 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000319}
320
Sam McCalld20d7982018-07-09 14:25:59 +0000321bool fromJSON(const json::Value &Params, DocumentRangeFormattingParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000322 json::ObjectMapper O(Params);
323 return O && O.map("textDocument", R.textDocument) &&
324 O.map("range", R.range) && O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000325}
326
Sam McCalld20d7982018-07-09 14:25:59 +0000327bool fromJSON(const json::Value &Params, DocumentOnTypeFormattingParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000328 json::ObjectMapper O(Params);
329 return O && O.map("textDocument", R.textDocument) &&
330 O.map("position", R.position) && O.map("ch", R.ch) &&
331 O.map("options", R.options);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000332}
333
Sam McCalld20d7982018-07-09 14:25:59 +0000334bool fromJSON(const json::Value &Params, DocumentFormattingParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000335 json::ObjectMapper O(Params);
336 return O && O.map("textDocument", R.textDocument) &&
337 O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000338}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000339
Sam McCalld20d7982018-07-09 14:25:59 +0000340bool fromJSON(const json::Value &Params, DocumentSymbolParams &R) {
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000341 json::ObjectMapper O(Params);
342 return O && O.map("textDocument", R.textDocument);
343}
344
Sam McCallc008af62018-10-20 15:30:37 +0000345json::Value toJSON(const Diagnostic &D) {
Sam McCall20841d42018-10-16 16:29:41 +0000346 json::Object Diag{
347 {"range", D.range},
348 {"severity", D.severity},
349 {"message", D.message},
350 };
Sam McCall16e70702018-10-24 07:59:38 +0000351 if (D.category)
352 Diag["category"] = *D.category;
353 if (D.codeActions)
354 Diag["codeActions"] = D.codeActions;
Sam McCall20841d42018-10-16 16:29:41 +0000355 return std::move(Diag);
356}
357
Sam McCalld20d7982018-07-09 14:25:59 +0000358bool fromJSON(const json::Value &Params, Diagnostic &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000359 json::ObjectMapper O(Params);
360 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
361 return false;
362 O.map("severity", R.severity);
Sam McCall16e70702018-10-24 07:59:38 +0000363 O.map("category", R.category);
Sam McCallff8b8742017-11-30 21:32:29 +0000364 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000365}
366
Sam McCalld20d7982018-07-09 14:25:59 +0000367bool fromJSON(const json::Value &Params, CodeActionContext &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000368 json::ObjectMapper O(Params);
369 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000370}
371
Sam McCallc008af62018-10-20 15:30:37 +0000372raw_ostream &operator<<(raw_ostream &OS, const Diagnostic &D) {
Sam McCall034e11a2018-01-25 17:29:17 +0000373 OS << D.range << " [";
374 switch (D.severity) {
375 case 1:
376 OS << "error";
377 break;
378 case 2:
379 OS << "warning";
380 break;
381 case 3:
382 OS << "note";
383 break;
384 case 4:
385 OS << "remark";
386 break;
387 default:
388 OS << "diagnostic";
389 break;
390 }
391 return OS << '(' << D.severity << "): " << D.message << "]";
392}
393
Sam McCalld20d7982018-07-09 14:25:59 +0000394bool fromJSON(const json::Value &Params, CodeActionParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000395 json::ObjectMapper O(Params);
396 return O && O.map("textDocument", R.textDocument) &&
397 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000398}
399
Sam McCalld20d7982018-07-09 14:25:59 +0000400bool fromJSON(const json::Value &Params, WorkspaceEdit &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000401 json::ObjectMapper O(Params);
402 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000403}
404
Sam McCallc008af62018-10-20 15:30:37 +0000405const StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000406 "clangd.applyFix";
Sam McCalld20d7982018-07-09 14:25:59 +0000407bool fromJSON(const json::Value &Params, ExecuteCommandParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000408 json::ObjectMapper O(Params);
409 if (!O || !O.map("command", R.command))
410 return false;
Sam McCallec109022017-11-28 09:37:43 +0000411
Sam McCalld20d7982018-07-09 14:25:59 +0000412 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000413 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
414 return Args && Args->size() == 1 &&
415 fromJSON(Args->front(), R.workspaceEdit);
416 }
417 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000418}
419
Sam McCalld20d7982018-07-09 14:25:59 +0000420json::Value toJSON(const SymbolInformation &P) {
421 return json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000422 {"name", P.name},
423 {"kind", static_cast<int>(P.kind)},
424 {"location", P.location},
425 {"containerName", P.containerName},
426 };
427}
428
Sam McCallc008af62018-10-20 15:30:37 +0000429raw_ostream &operator<<(raw_ostream &O, const SymbolInformation &SI) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000430 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
431 return O;
432}
433
Sam McCalld20d7982018-07-09 14:25:59 +0000434bool fromJSON(const json::Value &Params, WorkspaceSymbolParams &R) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000435 json::ObjectMapper O(Params);
436 return O && O.map("query", R.query);
437}
438
Sam McCalld20d7982018-07-09 14:25:59 +0000439json::Value toJSON(const Command &C) {
440 auto Cmd = json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000441 if (C.workspaceEdit)
442 Cmd["arguments"] = {*C.workspaceEdit};
Eric Liuc5105f92018-02-16 14:15:55 +0000443 return std::move(Cmd);
444}
445
Sam McCallc008af62018-10-20 15:30:37 +0000446const StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
Sam McCall20841d42018-10-16 16:29:41 +0000447
Sam McCallc008af62018-10-20 15:30:37 +0000448json::Value toJSON(const CodeAction &CA) {
Sam McCall20841d42018-10-16 16:29:41 +0000449 auto CodeAction = json::Object{{"title", CA.title}};
450 if (CA.kind)
451 CodeAction["kind"] = *CA.kind;
452 if (CA.diagnostics)
453 CodeAction["diagnostics"] = json::Array(*CA.diagnostics);
454 if (CA.edit)
455 CodeAction["edit"] = *CA.edit;
456 if (CA.command)
457 CodeAction["command"] = *CA.command;
458 return std::move(CodeAction);
459}
460
Sam McCalld20d7982018-07-09 14:25:59 +0000461json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000462 if (!WE.changes)
Sam McCalld20d7982018-07-09 14:25:59 +0000463 return json::Object{};
464 json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000465 for (auto &Change : *WE.changes)
Sam McCalld20d7982018-07-09 14:25:59 +0000466 FileChanges[Change.first] = json::Array(Change.second);
467 return json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000468}
469
Sam McCalld20d7982018-07-09 14:25:59 +0000470json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
471 return json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000472}
473
Sam McCalld20d7982018-07-09 14:25:59 +0000474bool fromJSON(const json::Value &Params, TextDocumentPositionParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000475 json::ObjectMapper O(Params);
476 return O && O.map("textDocument", R.textDocument) &&
477 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000478}
479
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000480static StringRef toTextKind(MarkupKind Kind) {
481 switch (Kind) {
482 case MarkupKind::PlainText:
483 return "plaintext";
484 case MarkupKind::Markdown:
485 return "markdown";
486 }
487 llvm_unreachable("Invalid MarkupKind");
488}
489
Sam McCalld20d7982018-07-09 14:25:59 +0000490json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000491 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000492 return nullptr;
493
Sam McCalld20d7982018-07-09 14:25:59 +0000494 return json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000495 {"kind", toTextKind(MC.kind)},
496 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000497 };
498}
499
Sam McCalld20d7982018-07-09 14:25:59 +0000500json::Value toJSON(const Hover &H) {
501 json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000502
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000503 if (H.range.hasValue())
504 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000505
506 return std::move(Result);
507}
508
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000509bool fromJSON(const json::Value &E, CompletionItemKind &Out) {
510 if (auto T = E.getAsInteger()) {
511 if (*T < static_cast<int>(CompletionItemKind::Text) ||
512 *T > static_cast<int>(CompletionItemKind::TypeParameter))
513 return false;
514 Out = static_cast<CompletionItemKind>(*T);
515 return true;
516 }
517 return false;
518}
519
520CompletionItemKind
521adjustKindToCapability(CompletionItemKind Kind,
522 CompletionItemKindBitset &supportedCompletionItemKinds) {
523 auto KindVal = static_cast<size_t>(Kind);
524 if (KindVal >= CompletionItemKindMin &&
525 KindVal <= supportedCompletionItemKinds.size() &&
526 supportedCompletionItemKinds[KindVal])
527 return Kind;
528
529 switch (Kind) {
530 // Provide some fall backs for common kinds that are close enough.
531 case CompletionItemKind::Folder:
532 return CompletionItemKind::File;
533 case CompletionItemKind::EnumMember:
534 return CompletionItemKind::Enum;
535 case CompletionItemKind::Struct:
536 return CompletionItemKind::Class;
537 default:
538 return CompletionItemKind::Text;
539 }
540}
541
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000542bool fromJSON(const json::Value &E, CompletionItemKindBitset &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000543 if (auto *A = E.getAsArray()) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000544 for (size_t I = 0; I < A->size(); ++I) {
545 CompletionItemKind KindOut;
546 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000547 Out.set(size_t(KindOut));
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000548 }
549 return true;
550 }
551 return false;
552}
553
Sam McCalld20d7982018-07-09 14:25:59 +0000554json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000555 assert(!CI.label.empty() && "completion item label is required");
Sam McCalld20d7982018-07-09 14:25:59 +0000556 json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000557 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000558 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000559 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000560 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000561 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000562 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000563 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000564 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000565 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000566 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000567 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000568 Result["insertText"] = CI.insertText;
569 if (CI.insertTextFormat != InsertTextFormat::Missing)
570 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000571 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000572 Result["textEdit"] = *CI.textEdit;
573 if (!CI.additionalTextEdits.empty())
Sam McCalld20d7982018-07-09 14:25:59 +0000574 Result["additionalTextEdits"] = json::Array(CI.additionalTextEdits);
Eric Liu6df66002018-09-06 18:52:26 +0000575 if (CI.deprecated)
576 Result["deprecated"] = CI.deprecated;
Sam McCalldd0566b2017-11-06 15:40:30 +0000577 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000578}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000579
Sam McCallc008af62018-10-20 15:30:37 +0000580raw_ostream &operator<<(raw_ostream &O, const CompletionItem &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000581 O << I.label << " - " << toJSON(I);
582 return O;
583}
584
Sam McCallff8b8742017-11-30 21:32:29 +0000585bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000586 return (L.sortText.empty() ? L.label : L.sortText) <
587 (R.sortText.empty() ? R.label : R.sortText);
588}
589
Sam McCalld20d7982018-07-09 14:25:59 +0000590json::Value toJSON(const CompletionList &L) {
591 return json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000592 {"isIncomplete", L.isIncomplete},
Sam McCalld20d7982018-07-09 14:25:59 +0000593 {"items", json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000594 };
595}
596
Sam McCalld20d7982018-07-09 14:25:59 +0000597json::Value toJSON(const ParameterInformation &PI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000598 assert(!PI.label.empty() && "parameter information label is required");
Sam McCalld20d7982018-07-09 14:25:59 +0000599 json::Object Result{{"label", PI.label}};
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000600 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000601 Result["documentation"] = PI.documentation;
602 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000603}
604
Sam McCalld20d7982018-07-09 14:25:59 +0000605json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000606 assert(!SI.label.empty() && "signature information label is required");
Sam McCalld20d7982018-07-09 14:25:59 +0000607 json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000608 {"label", SI.label},
Sam McCalld20d7982018-07-09 14:25:59 +0000609 {"parameters", json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000610 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000611 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000612 Result["documentation"] = SI.documentation;
613 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000614}
615
Sam McCallc008af62018-10-20 15:30:37 +0000616raw_ostream &operator<<(raw_ostream &O, const SignatureInformation &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000617 O << I.label << " - " << toJSON(I);
618 return O;
619}
620
Sam McCalld20d7982018-07-09 14:25:59 +0000621json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000622 assert(SH.activeSignature >= 0 &&
623 "Unexpected negative value for number of active signatures.");
624 assert(SH.activeParameter >= 0 &&
625 "Unexpected negative value for active parameter index");
Sam McCalld20d7982018-07-09 14:25:59 +0000626 return json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000627 {"activeSignature", SH.activeSignature},
628 {"activeParameter", SH.activeParameter},
Sam McCalld20d7982018-07-09 14:25:59 +0000629 {"signatures", json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000630 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000631}
Haojian Wu345099c2017-11-09 11:30:04 +0000632
Sam McCalld20d7982018-07-09 14:25:59 +0000633bool fromJSON(const json::Value &Params, RenameParams &R) {
Sam McCallff8b8742017-11-30 21:32:29 +0000634 json::ObjectMapper O(Params);
635 return O && O.map("textDocument", R.textDocument) &&
636 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000637}
Sam McCallff8b8742017-11-30 21:32:29 +0000638
Sam McCalld20d7982018-07-09 14:25:59 +0000639json::Value toJSON(const DocumentHighlight &DH) {
640 return json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000641 {"range", toJSON(DH.range)},
642 {"kind", static_cast<int>(DH.kind)},
643 };
644}
645
Sam McCallc008af62018-10-20 15:30:37 +0000646raw_ostream &operator<<(raw_ostream &O, const DocumentHighlight &V) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000647 O << V.range;
648 if (V.kind == DocumentHighlightKind::Read)
649 O << "(r)";
650 if (V.kind == DocumentHighlightKind::Write)
651 O << "(w)";
652 return O;
653}
654
Sam McCalld20d7982018-07-09 14:25:59 +0000655bool fromJSON(const json::Value &Params, DidChangeConfigurationParams &CCP) {
Simon Marchi5178f922018-02-22 14:00:39 +0000656 json::ObjectMapper O(Params);
657 return O && O.map("settings", CCP.settings);
658}
659
Sam McCallc008af62018-10-20 15:30:37 +0000660bool fromJSON(const json::Value &Params, ClangdCompileCommand &CDbUpdate) {
Alex Lorenzf8087862018-08-01 17:39:29 +0000661 json::ObjectMapper O(Params);
662 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
663 O.map("compilationCommand", CDbUpdate.compilationCommand);
664}
665
Sam McCallbc904612018-10-25 04:22:52 +0000666bool fromJSON(const json::Value &Params, ConfigurationSettings &S) {
Simon Marchi5178f922018-02-22 14:00:39 +0000667 json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000668 if (!O)
669 return true; // 'any' type in LSP.
670 O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
671 return true;
Simon Marchi5178f922018-02-22 14:00:39 +0000672}
673
Sam McCallbc904612018-10-25 04:22:52 +0000674bool fromJSON(const json::Value &Params, InitializationOptions &Opts) {
Simon Marchiabeed662018-10-16 15:55:03 +0000675 json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000676 if (!O)
677 return true; // 'any' type in LSP.
678
679 fromJSON(Params, Opts.ConfigSettings);
680 O.map("compilationDatabasePath", Opts.compilationDatabasePath);
681 return true;
Simon Marchiabeed662018-10-16 15:55:03 +0000682}
683
Sam McCall1ad142f2018-09-05 11:53:07 +0000684bool fromJSON(const json::Value &Params, ReferenceParams &R) {
685 TextDocumentPositionParams &Base = R;
686 return fromJSON(Params, Base);
687}
688
Sam McCallff8b8742017-11-30 21:32:29 +0000689} // namespace clangd
690} // namespace clang