blob: 04c26637b1dcfa502ce6400f50f9df936ae9db7c [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 }
296 }
297 if (auto *CodeAction = TextDocument->getObject("codeAction")) {
298 if (CodeAction->getObject("codeActionLiteralSupport"))
299 R.CodeActionStructure = true;
300 }
Ilya Biryukov19d75602018-11-23 15:21:19 +0000301 if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) {
302 if (auto HierarchicalSupport =
303 DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport"))
304 R.HierarchicalDocumentSymbol = *HierarchicalSupport;
305 }
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000306 }
307 if (auto *Workspace = O->getObject("workspace")) {
308 if (auto *Symbol = Workspace->getObject("symbol")) {
309 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
310 if (auto *ValueSet = SymbolKind->get("valueSet")) {
311 R.WorkspaceSymbolKinds.emplace();
312 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds))
313 return false;
314 }
315 }
316 }
317 }
Sam McCalla69698f2019-03-27 17:47:49 +0000318 if (auto *OffsetEncoding = O->get("offsetEncoding")) {
319 R.offsetEncoding.emplace();
320 if (!fromJSON(*OffsetEncoding, *R.offsetEncoding))
321 return false;
322 }
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000323 return true;
324}
325
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000326bool fromJSON(const llvm::json::Value &Params, InitializeParams &R) {
327 llvm::json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000328 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000329 return false;
Sam McCall38a04912017-11-29 11:36:46 +0000330 // We deliberately don't fail if we can't parse individual fields.
331 // Failing to handle a slightly malformed initialize would be a disaster.
Sam McCallff8b8742017-11-30 21:32:29 +0000332 O.map("processId", R.processId);
333 O.map("rootUri", R.rootUri);
334 O.map("rootPath", R.rootPath);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000335 O.map("capabilities", R.capabilities);
Sam McCallff8b8742017-11-30 21:32:29 +0000336 O.map("trace", R.trace);
Simon Marchi88016782018-08-01 11:28:49 +0000337 O.map("initializationOptions", R.initializationOptions);
Sam McCallff8b8742017-11-30 21:32:29 +0000338 return true;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000339}
340
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000341bool fromJSON(const llvm::json::Value &Params, DidOpenTextDocumentParams &R) {
342 llvm::json::ObjectMapper O(Params);
Sam McCall2eb6b402018-11-02 13:06:55 +0000343 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000344}
345
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000346bool fromJSON(const llvm::json::Value &Params, DidCloseTextDocumentParams &R) {
347 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000348 return O && O.map("textDocument", R.textDocument);
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +0000349}
350
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000351bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R) {
352 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000353 return O && O.map("textDocument", R.textDocument) &&
Eric Liu51fed182018-02-22 18:40:39 +0000354 O.map("contentChanges", R.contentChanges) &&
355 O.map("wantDiagnostics", R.wantDiagnostics);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000356}
357
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000358bool fromJSON(const llvm::json::Value &E, FileChangeType &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000359 if (auto T = E.getAsInteger()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000360 if (*T < static_cast<int>(FileChangeType::Created) ||
361 *T > static_cast<int>(FileChangeType::Deleted))
362 return false;
363 Out = static_cast<FileChangeType>(*T);
364 return true;
365 }
366 return false;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000367}
368
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000369bool fromJSON(const llvm::json::Value &Params, FileEvent &R) {
370 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000371 return O && O.map("uri", R.uri) && O.map("type", R.type);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000372}
373
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000374bool fromJSON(const llvm::json::Value &Params, DidChangeWatchedFilesParams &R) {
375 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000376 return O && O.map("changes", R.changes);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000377}
378
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000379bool fromJSON(const llvm::json::Value &Params,
380 TextDocumentContentChangeEvent &R) {
381 llvm::json::ObjectMapper O(Params);
Simon Marchi98082622018-03-26 14:41:40 +0000382 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
383 O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000384}
385
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000386bool fromJSON(const llvm::json::Value &Params, FormattingOptions &R) {
387 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000388 return O && O.map("tabSize", R.tabSize) &&
389 O.map("insertSpaces", R.insertSpaces);
390}
391
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000392llvm::json::Value toJSON(const FormattingOptions &P) {
393 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000394 {"tabSize", P.tabSize},
395 {"insertSpaces", P.insertSpaces},
396 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000397}
398
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000399bool fromJSON(const llvm::json::Value &Params,
400 DocumentRangeFormattingParams &R) {
401 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000402 return O && O.map("textDocument", R.textDocument) &&
403 O.map("range", R.range) && O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000404}
405
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000406bool fromJSON(const llvm::json::Value &Params,
407 DocumentOnTypeFormattingParams &R) {
408 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000409 return O && O.map("textDocument", R.textDocument) &&
410 O.map("position", R.position) && O.map("ch", R.ch) &&
411 O.map("options", R.options);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000412}
413
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000414bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R) {
415 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000416 return O && O.map("textDocument", R.textDocument) &&
417 O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000418}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000419
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000420bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R) {
421 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000422 return O && O.map("textDocument", R.textDocument);
423}
424
Sam McCallc9e4ee92019-04-18 15:17:07 +0000425llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
426 return llvm::json::Object{
427 {"location", DRI.location},
428 {"message", DRI.message},
429 };
430}
431
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000432llvm::json::Value toJSON(const Diagnostic &D) {
433 llvm::json::Object Diag{
Sam McCall20841d42018-10-16 16:29:41 +0000434 {"range", D.range},
435 {"severity", D.severity},
436 {"message", D.message},
437 };
Sam McCall16e70702018-10-24 07:59:38 +0000438 if (D.category)
439 Diag["category"] = *D.category;
440 if (D.codeActions)
441 Diag["codeActions"] = D.codeActions;
Sam McCall641caa52019-04-17 12:35:16 +0000442 if (!D.code.empty())
443 Diag["code"] = D.code;
444 if (!D.source.empty())
445 Diag["source"] = D.source;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000446 if (D.relatedInformation)
447 Diag["relatedInformation"] = *D.relatedInformation;
Sam McCall20841d42018-10-16 16:29:41 +0000448 return std::move(Diag);
449}
450
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000451bool fromJSON(const llvm::json::Value &Params, Diagnostic &R) {
452 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000453 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
454 return false;
455 O.map("severity", R.severity);
Sam McCall16e70702018-10-24 07:59:38 +0000456 O.map("category", R.category);
Sam McCall641caa52019-04-17 12:35:16 +0000457 O.map("code", R.code);
458 O.map("source", R.source);
Sam McCallff8b8742017-11-30 21:32:29 +0000459 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000460}
461
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000462bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R) {
463 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000464 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000465}
466
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000467llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
Sam McCall034e11a2018-01-25 17:29:17 +0000468 OS << D.range << " [";
469 switch (D.severity) {
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000470 case 1:
471 OS << "error";
472 break;
473 case 2:
474 OS << "warning";
475 break;
476 case 3:
477 OS << "note";
478 break;
479 case 4:
480 OS << "remark";
481 break;
482 default:
483 OS << "diagnostic";
484 break;
Sam McCall034e11a2018-01-25 17:29:17 +0000485 }
486 return OS << '(' << D.severity << "): " << D.message << "]";
487}
488
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000489bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R) {
490 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000491 return O && O.map("textDocument", R.textDocument) &&
492 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000493}
494
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000495bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R) {
496 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000497 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000498}
499
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000500const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000501 "clangd.applyFix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000502const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_TWEAK =
503 "clangd.applyTweak";
504
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000505bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R) {
506 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000507 if (!O || !O.map("command", R.command))
508 return false;
Sam McCallec109022017-11-28 09:37:43 +0000509
Sam McCalld20d7982018-07-09 14:25:59 +0000510 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000511 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
512 return Args && Args->size() == 1 &&
513 fromJSON(Args->front(), R.workspaceEdit);
514 }
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000515 if (R.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK)
516 return Args && Args->size() == 1 && fromJSON(Args->front(), R.tweakArgs);
Sam McCallff8b8742017-11-30 21:32:29 +0000517 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000518}
519
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000520llvm::json::Value toJSON(const SymbolInformation &P) {
521 return llvm::json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000522 {"name", P.name},
523 {"kind", static_cast<int>(P.kind)},
524 {"location", P.location},
525 {"containerName", P.containerName},
526 };
527}
528
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000529llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
530 const SymbolInformation &SI) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000531 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
532 return O;
533}
534
Jan Korousb4067012018-11-27 16:40:46 +0000535bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
536 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
537 LHS.USR == RHS.USR && LHS.ID == RHS.ID;
538}
539
540llvm::json::Value toJSON(const SymbolDetails &P) {
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000541 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000542 {"containerName", llvm::json::Value(nullptr)},
543 {"usr", llvm::json::Value(nullptr)},
544 {"id", llvm::json::Value(nullptr)}};
Jan Korousb4067012018-11-27 16:40:46 +0000545
546 if (!P.name.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000547 Result["name"] = P.name;
Jan Korousb4067012018-11-27 16:40:46 +0000548
549 if (!P.containerName.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000550 Result["containerName"] = P.containerName;
Jan Korousb4067012018-11-27 16:40:46 +0000551
552 if (!P.USR.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000553 Result["usr"] = P.USR;
Jan Korousb4067012018-11-27 16:40:46 +0000554
555 if (P.ID.hasValue())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000556 Result["id"] = P.ID.getValue().str();
Jan Korousb4067012018-11-27 16:40:46 +0000557
Jan Korous613c80d2018-11-28 10:24:07 +0000558 // Older clang cannot compile 'return Result', even though it is legal.
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000559 return llvm::json::Value(std::move(Result));
Jan Korousb4067012018-11-27 16:40:46 +0000560}
561
562llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
563 if (!S.containerName.empty()) {
564 O << S.containerName;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000565 llvm::StringRef ContNameRef;
Jan Korousb4067012018-11-27 16:40:46 +0000566 if (!ContNameRef.endswith("::")) {
567 O << " ";
568 }
569 }
570 O << S.name << " - " << toJSON(S);
571 return O;
572}
573
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000574bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R) {
575 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000576 return O && O.map("query", R.query);
577}
578
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000579llvm::json::Value toJSON(const Command &C) {
580 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000581 if (C.workspaceEdit)
582 Cmd["arguments"] = {*C.workspaceEdit};
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000583 if (C.tweakArgs)
584 Cmd["arguments"] = {*C.tweakArgs};
Eric Liuc5105f92018-02-16 14:15:55 +0000585 return std::move(Cmd);
586}
587
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000588const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000589const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
Sam McCall20841d42018-10-16 16:29:41 +0000590
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000591llvm::json::Value toJSON(const CodeAction &CA) {
592 auto CodeAction = llvm::json::Object{{"title", CA.title}};
Sam McCall20841d42018-10-16 16:29:41 +0000593 if (CA.kind)
594 CodeAction["kind"] = *CA.kind;
595 if (CA.diagnostics)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000596 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
Sam McCall20841d42018-10-16 16:29:41 +0000597 if (CA.edit)
598 CodeAction["edit"] = *CA.edit;
599 if (CA.command)
600 CodeAction["command"] = *CA.command;
601 return std::move(CodeAction);
602}
603
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000604llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
Ilya Biryukov19d75602018-11-23 15:21:19 +0000605 return O << S.name << " - " << toJSON(S);
606}
607
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000608llvm::json::Value toJSON(const DocumentSymbol &S) {
609 llvm::json::Object Result{{"name", S.name},
610 {"kind", static_cast<int>(S.kind)},
611 {"range", S.range},
612 {"selectionRange", S.selectionRange}};
Ilya Biryukov19d75602018-11-23 15:21:19 +0000613
614 if (!S.detail.empty())
615 Result["detail"] = S.detail;
616 if (!S.children.empty())
617 Result["children"] = S.children;
618 if (S.deprecated)
619 Result["deprecated"] = true;
Ilya Biryukov4174d092018-11-26 09:57:41 +0000620 // Older gcc cannot compile 'return Result', even though it is legal.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000621 return llvm::json::Value(std::move(Result));
Ilya Biryukov19d75602018-11-23 15:21:19 +0000622}
623
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000624llvm::json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000625 if (!WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000626 return llvm::json::Object{};
627 llvm::json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000628 for (auto &Change : *WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000629 FileChanges[Change.first] = llvm::json::Array(Change.second);
630 return llvm::json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000631}
632
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000633bool fromJSON(const llvm::json::Value &Params, TweakArgs &A) {
634 llvm::json::ObjectMapper O(Params);
635 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
636 O.map("tweakID", A.tweakID);
637}
638
639llvm::json::Value toJSON(const TweakArgs &A) {
640 return llvm::json::Object{
641 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
642}
643
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000644llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
645 return llvm::json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000646}
647
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000648bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R) {
649 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000650 return O && O.map("textDocument", R.textDocument) &&
651 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000652}
653
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000654bool fromJSON(const llvm::json::Value &Params, CompletionContext &R) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000655 llvm::json::ObjectMapper O(Params);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000656 if (!O)
657 return false;
658
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000659 int TriggerKind;
660 if (!O.map("triggerKind", TriggerKind))
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000661 return false;
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000662 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000663
664 if (auto *TC = Params.getAsObject()->get("triggerCharacter"))
665 return fromJSON(*TC, R.triggerCharacter);
666 return true;
667}
668
669bool fromJSON(const llvm::json::Value &Params, CompletionParams &R) {
670 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R)))
671 return false;
672 if (auto *Context = Params.getAsObject()->get("context"))
673 return fromJSON(*Context, R.context);
674 return true;
675}
676
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000677static llvm::StringRef toTextKind(MarkupKind Kind) {
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000678 switch (Kind) {
679 case MarkupKind::PlainText:
680 return "plaintext";
681 case MarkupKind::Markdown:
682 return "markdown";
683 }
684 llvm_unreachable("Invalid MarkupKind");
685}
686
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000687llvm::json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000688 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000689 return nullptr;
690
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000691 return llvm::json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000692 {"kind", toTextKind(MC.kind)},
693 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000694 };
695}
696
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000697llvm::json::Value toJSON(const Hover &H) {
698 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000699
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000700 if (H.range.hasValue())
701 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000702
703 return std::move(Result);
704}
705
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000706bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000707 if (auto T = E.getAsInteger()) {
708 if (*T < static_cast<int>(CompletionItemKind::Text) ||
709 *T > static_cast<int>(CompletionItemKind::TypeParameter))
710 return false;
711 Out = static_cast<CompletionItemKind>(*T);
712 return true;
713 }
714 return false;
715}
716
717CompletionItemKind
718adjustKindToCapability(CompletionItemKind Kind,
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000719 CompletionItemKindBitset &SupportedCompletionItemKinds) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000720 auto KindVal = static_cast<size_t>(Kind);
721 if (KindVal >= CompletionItemKindMin &&
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000722 KindVal <= SupportedCompletionItemKinds.size() &&
723 SupportedCompletionItemKinds[KindVal])
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000724 return Kind;
725
726 switch (Kind) {
727 // Provide some fall backs for common kinds that are close enough.
728 case CompletionItemKind::Folder:
729 return CompletionItemKind::File;
730 case CompletionItemKind::EnumMember:
731 return CompletionItemKind::Enum;
732 case CompletionItemKind::Struct:
733 return CompletionItemKind::Class;
734 default:
735 return CompletionItemKind::Text;
736 }
737}
738
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000739bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000740 if (auto *A = E.getAsArray()) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000741 for (size_t I = 0; I < A->size(); ++I) {
742 CompletionItemKind KindOut;
743 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000744 Out.set(size_t(KindOut));
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000745 }
746 return true;
747 }
748 return false;
749}
750
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000751llvm::json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000752 assert(!CI.label.empty() && "completion item label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000753 llvm::json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000754 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000755 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000756 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000757 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000758 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000759 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000760 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000761 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000762 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000763 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000764 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000765 Result["insertText"] = CI.insertText;
766 if (CI.insertTextFormat != InsertTextFormat::Missing)
767 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000768 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000769 Result["textEdit"] = *CI.textEdit;
770 if (!CI.additionalTextEdits.empty())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000771 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
Eric Liu6df66002018-09-06 18:52:26 +0000772 if (CI.deprecated)
773 Result["deprecated"] = CI.deprecated;
Sam McCalldd0566b2017-11-06 15:40:30 +0000774 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000775}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000776
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000777llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000778 O << I.label << " - " << toJSON(I);
779 return O;
780}
781
Sam McCallff8b8742017-11-30 21:32:29 +0000782bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000783 return (L.sortText.empty() ? L.label : L.sortText) <
784 (R.sortText.empty() ? R.label : R.sortText);
785}
786
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000787llvm::json::Value toJSON(const CompletionList &L) {
788 return llvm::json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000789 {"isIncomplete", L.isIncomplete},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000790 {"items", llvm::json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000791 };
792}
793
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000794llvm::json::Value toJSON(const ParameterInformation &PI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000795 assert(!PI.label.empty() && "parameter information label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000796 llvm::json::Object Result{{"label", PI.label}};
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000797 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000798 Result["documentation"] = PI.documentation;
799 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000800}
801
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000802llvm::json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000803 assert(!SI.label.empty() && "signature information label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000804 llvm::json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000805 {"label", SI.label},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000806 {"parameters", llvm::json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000807 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000808 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000809 Result["documentation"] = SI.documentation;
810 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000811}
812
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000813llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
814 const SignatureInformation &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000815 O << I.label << " - " << toJSON(I);
816 return O;
817}
818
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000819llvm::json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000820 assert(SH.activeSignature >= 0 &&
821 "Unexpected negative value for number of active signatures.");
822 assert(SH.activeParameter >= 0 &&
823 "Unexpected negative value for active parameter index");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000824 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000825 {"activeSignature", SH.activeSignature},
826 {"activeParameter", SH.activeParameter},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000827 {"signatures", llvm::json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000828 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000829}
Haojian Wu345099c2017-11-09 11:30:04 +0000830
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000831bool fromJSON(const llvm::json::Value &Params, RenameParams &R) {
832 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000833 return O && O.map("textDocument", R.textDocument) &&
834 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000835}
Sam McCallff8b8742017-11-30 21:32:29 +0000836
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000837llvm::json::Value toJSON(const DocumentHighlight &DH) {
838 return llvm::json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000839 {"range", toJSON(DH.range)},
840 {"kind", static_cast<int>(DH.kind)},
841 };
842}
843
Haojian Wub6188492018-12-20 15:39:12 +0000844llvm::json::Value toJSON(const FileStatus &FStatus) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000845 return llvm::json::Object{
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000846 {"uri", FStatus.uri},
847 {"state", FStatus.state},
Haojian Wub6188492018-12-20 15:39:12 +0000848 };
849}
850
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000851llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
852 const DocumentHighlight &V) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000853 O << V.range;
854 if (V.kind == DocumentHighlightKind::Read)
855 O << "(r)";
856 if (V.kind == DocumentHighlightKind::Write)
857 O << "(w)";
858 return O;
859}
860
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000861bool fromJSON(const llvm::json::Value &Params,
862 DidChangeConfigurationParams &CCP) {
863 llvm::json::ObjectMapper O(Params);
Simon Marchi5178f922018-02-22 14:00:39 +0000864 return O && O.map("settings", CCP.settings);
865}
866
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000867bool fromJSON(const llvm::json::Value &Params,
868 ClangdCompileCommand &CDbUpdate) {
869 llvm::json::ObjectMapper O(Params);
Alex Lorenzf8087862018-08-01 17:39:29 +0000870 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
871 O.map("compilationCommand", CDbUpdate.compilationCommand);
872}
873
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000874bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S) {
875 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000876 if (!O)
877 return true; // 'any' type in LSP.
878 O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
879 return true;
Simon Marchi5178f922018-02-22 14:00:39 +0000880}
881
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000882bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts) {
883 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000884 if (!O)
885 return true; // 'any' type in LSP.
886
887 fromJSON(Params, Opts.ConfigSettings);
888 O.map("compilationDatabasePath", Opts.compilationDatabasePath);
Sam McCall6980edb2018-11-02 14:07:51 +0000889 O.map("fallbackFlags", Opts.fallbackFlags);
Haojian Wub6188492018-12-20 15:39:12 +0000890 O.map("clangdFileStatus", Opts.FileStatus);
Sam McCallbc904612018-10-25 04:22:52 +0000891 return true;
Simon Marchiabeed662018-10-16 15:55:03 +0000892}
893
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000894bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out) {
895 auto T = E.getAsInteger();
896 if (!T)
897 return false;
898 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
899 *T > static_cast<int>(TypeHierarchyDirection::Both))
900 return false;
901 Out = static_cast<TypeHierarchyDirection>(*T);
902 return true;
903}
904
905bool fromJSON(const llvm::json::Value &Params, TypeHierarchyParams &R) {
906 llvm::json::ObjectMapper O(Params);
907 return O && O.map("textDocument", R.textDocument) &&
908 O.map("position", R.position) && O.map("resolve", R.resolve) &&
909 O.map("direction", R.direction);
910}
911
912llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
913 const TypeHierarchyItem &I) {
914 return O << I.name << " - " << toJSON(I);
915}
916
917llvm::json::Value toJSON(const TypeHierarchyItem &I) {
918 llvm::json::Object Result{{"name", I.name},
919 {"kind", static_cast<int>(I.kind)},
920 {"range", I.range},
921 {"selectionRange", I.selectionRange},
922 {"uri", I.uri}};
923
924 if (I.detail)
925 Result["detail"] = I.detail;
926 if (I.deprecated)
927 Result["deprecated"] = I.deprecated;
928 if (I.parents)
929 Result["parents"] = I.parents;
930 if (I.children)
931 Result["children"] = I.children;
932 return std::move(Result);
933}
934
935bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I) {
936 llvm::json::ObjectMapper O(Params);
937
938 // Required fields.
939 if (!(O && O.map("name", I.name) && O.map("kind", I.kind) &&
940 O.map("uri", I.uri) && O.map("range", I.range) &&
941 O.map("selectionRange", I.selectionRange))) {
942 return false;
943 }
944
945 // Optional fields.
946 O.map("detail", I.detail);
947 O.map("deprecated", I.deprecated);
948 O.map("parents", I.parents);
949 O.map("children", I.children);
950
951 return true;
952}
953
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000954bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R) {
Sam McCall1ad142f2018-09-05 11:53:07 +0000955 TextDocumentPositionParams &Base = R;
956 return fromJSON(Params, Base);
957}
958
Sam McCall8b25d222019-03-28 14:37:51 +0000959static const char *toString(OffsetEncoding OE) {
Sam McCalla69698f2019-03-27 17:47:49 +0000960 switch (OE) {
Sam McCall8b25d222019-03-28 14:37:51 +0000961 case OffsetEncoding::UTF8:
962 return "utf-8";
963 case OffsetEncoding::UTF16:
964 return "utf-16";
965 case OffsetEncoding::UTF32:
966 return "utf-32";
967 case OffsetEncoding::UnsupportedEncoding:
968 return "unknown";
Sam McCalla69698f2019-03-27 17:47:49 +0000969 }
Simon Pilgrim945db0b2019-03-29 13:43:00 +0000970 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
Sam McCalla69698f2019-03-27 17:47:49 +0000971}
Sam McCall8b25d222019-03-28 14:37:51 +0000972llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
Sam McCalla69698f2019-03-27 17:47:49 +0000973bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE) {
974 auto Str = V.getAsString();
975 if (!Str)
976 return false;
977 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
978 .Case("utf-8", OffsetEncoding::UTF8)
979 .Case("utf-16", OffsetEncoding::UTF16)
Sam McCall8b25d222019-03-28 14:37:51 +0000980 .Case("utf-32", OffsetEncoding::UTF32)
Sam McCalla69698f2019-03-27 17:47:49 +0000981 .Default(OffsetEncoding::UnsupportedEncoding);
982 return true;
983}
Sam McCall8b25d222019-03-28 14:37:51 +0000984llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
985 return OS << toString(Enc);
986}
Sam McCalla69698f2019-03-27 17:47:49 +0000987
Sam McCallff8b8742017-11-30 21:32:29 +0000988} // namespace clangd
989} // namespace clang