blob: bd8d0328a5b47dd8ef4e22540bdfcf0d2e3705e1 [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 }
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000306 if (auto *Hover = TextDocument->getObject("hover")) {
307 if (auto *ContentFormat = Hover->getArray("contentFormat")) {
308 for (const auto &Format : *ContentFormat) {
309 MarkupKind K = MarkupKind::PlainText;
310 if (fromJSON(Format, K)) {
311 R.HoverContentFormat = K;
312 break;
313 }
314 }
315 }
316 }
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000317 if (auto *Help = TextDocument->getObject("signatureHelp")) {
318 if (auto *Info = Help->getObject("signatureInformation")) {
319 if (auto *Parameter = Info->getObject("parameterInformation")) {
320 if (auto OffsetSupport = Parameter->getBoolean("labelOffsetSupport"))
321 R.OffsetsInSignatureHelp = *OffsetSupport;
322 }
323 }
324 }
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000325 }
326 if (auto *Workspace = O->getObject("workspace")) {
327 if (auto *Symbol = Workspace->getObject("symbol")) {
328 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
329 if (auto *ValueSet = SymbolKind->get("valueSet")) {
330 R.WorkspaceSymbolKinds.emplace();
331 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds))
332 return false;
333 }
334 }
335 }
336 }
Sam McCalla69698f2019-03-27 17:47:49 +0000337 if (auto *OffsetEncoding = O->get("offsetEncoding")) {
338 R.offsetEncoding.emplace();
339 if (!fromJSON(*OffsetEncoding, *R.offsetEncoding))
340 return false;
341 }
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000342 return true;
343}
344
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000345bool fromJSON(const llvm::json::Value &Params, InitializeParams &R) {
346 llvm::json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000347 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000348 return false;
Sam McCall38a04912017-11-29 11:36:46 +0000349 // We deliberately don't fail if we can't parse individual fields.
350 // Failing to handle a slightly malformed initialize would be a disaster.
Sam McCallff8b8742017-11-30 21:32:29 +0000351 O.map("processId", R.processId);
352 O.map("rootUri", R.rootUri);
353 O.map("rootPath", R.rootPath);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000354 O.map("capabilities", R.capabilities);
Sam McCallff8b8742017-11-30 21:32:29 +0000355 O.map("trace", R.trace);
Simon Marchi88016782018-08-01 11:28:49 +0000356 O.map("initializationOptions", R.initializationOptions);
Sam McCallff8b8742017-11-30 21:32:29 +0000357 return true;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000358}
359
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000360bool fromJSON(const llvm::json::Value &Params, DidOpenTextDocumentParams &R) {
361 llvm::json::ObjectMapper O(Params);
Sam McCall2eb6b402018-11-02 13:06:55 +0000362 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000363}
364
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000365bool fromJSON(const llvm::json::Value &Params, DidCloseTextDocumentParams &R) {
366 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000367 return O && O.map("textDocument", R.textDocument);
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +0000368}
369
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000370bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R) {
371 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000372 return O && O.map("textDocument", R.textDocument) &&
Eric Liu51fed182018-02-22 18:40:39 +0000373 O.map("contentChanges", R.contentChanges) &&
374 O.map("wantDiagnostics", R.wantDiagnostics);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000375}
376
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000377bool fromJSON(const llvm::json::Value &E, FileChangeType &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000378 if (auto T = E.getAsInteger()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000379 if (*T < static_cast<int>(FileChangeType::Created) ||
380 *T > static_cast<int>(FileChangeType::Deleted))
381 return false;
382 Out = static_cast<FileChangeType>(*T);
383 return true;
384 }
385 return false;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000386}
387
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000388bool fromJSON(const llvm::json::Value &Params, FileEvent &R) {
389 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000390 return O && O.map("uri", R.uri) && O.map("type", R.type);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000391}
392
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000393bool fromJSON(const llvm::json::Value &Params, DidChangeWatchedFilesParams &R) {
394 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000395 return O && O.map("changes", R.changes);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000396}
397
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000398bool fromJSON(const llvm::json::Value &Params,
399 TextDocumentContentChangeEvent &R) {
400 llvm::json::ObjectMapper O(Params);
Simon Marchi98082622018-03-26 14:41:40 +0000401 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
402 O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000403}
404
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000405bool fromJSON(const llvm::json::Value &Params,
406 DocumentRangeFormattingParams &R) {
407 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000408 return O && O.map("textDocument", R.textDocument) &&
Sam McCall149786d2019-06-10 13:01:49 +0000409 O.map("range", R.range);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000410}
411
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000412bool fromJSON(const llvm::json::Value &Params,
413 DocumentOnTypeFormattingParams &R) {
414 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000415 return O && O.map("textDocument", R.textDocument) &&
Sam McCall149786d2019-06-10 13:01:49 +0000416 O.map("position", R.position) && O.map("ch", R.ch);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000417}
418
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000419bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R) {
420 llvm::json::ObjectMapper O(Params);
Sam McCall149786d2019-06-10 13:01:49 +0000421 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000422}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000423
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000424bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R) {
425 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000426 return O && O.map("textDocument", R.textDocument);
427}
428
Sam McCallc9e4ee92019-04-18 15:17:07 +0000429llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
430 return llvm::json::Object{
431 {"location", DRI.location},
432 {"message", DRI.message},
433 };
434}
435
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000436llvm::json::Value toJSON(const Diagnostic &D) {
437 llvm::json::Object Diag{
Sam McCall20841d42018-10-16 16:29:41 +0000438 {"range", D.range},
439 {"severity", D.severity},
440 {"message", D.message},
441 };
Sam McCall16e70702018-10-24 07:59:38 +0000442 if (D.category)
443 Diag["category"] = *D.category;
444 if (D.codeActions)
445 Diag["codeActions"] = D.codeActions;
Sam McCall641caa52019-04-17 12:35:16 +0000446 if (!D.code.empty())
447 Diag["code"] = D.code;
448 if (!D.source.empty())
449 Diag["source"] = D.source;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000450 if (D.relatedInformation)
451 Diag["relatedInformation"] = *D.relatedInformation;
Sam McCall20841d42018-10-16 16:29:41 +0000452 return std::move(Diag);
453}
454
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000455bool fromJSON(const llvm::json::Value &Params, Diagnostic &R) {
456 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000457 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
458 return false;
459 O.map("severity", R.severity);
Sam McCall16e70702018-10-24 07:59:38 +0000460 O.map("category", R.category);
Sam McCall641caa52019-04-17 12:35:16 +0000461 O.map("code", R.code);
462 O.map("source", R.source);
Sam McCallff8b8742017-11-30 21:32:29 +0000463 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000464}
465
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000466bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R) {
467 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000468 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000469}
470
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000471llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
Sam McCall034e11a2018-01-25 17:29:17 +0000472 OS << D.range << " [";
473 switch (D.severity) {
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000474 case 1:
475 OS << "error";
476 break;
477 case 2:
478 OS << "warning";
479 break;
480 case 3:
481 OS << "note";
482 break;
483 case 4:
484 OS << "remark";
485 break;
486 default:
487 OS << "diagnostic";
488 break;
Sam McCall034e11a2018-01-25 17:29:17 +0000489 }
490 return OS << '(' << D.severity << "): " << D.message << "]";
491}
492
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000493bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R) {
494 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000495 return O && O.map("textDocument", R.textDocument) &&
496 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000497}
498
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000499bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R) {
500 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000501 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000502}
503
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000504const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000505 "clangd.applyFix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000506const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_TWEAK =
507 "clangd.applyTweak";
508
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000509bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R) {
510 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000511 if (!O || !O.map("command", R.command))
512 return false;
Sam McCallec109022017-11-28 09:37:43 +0000513
Sam McCalld20d7982018-07-09 14:25:59 +0000514 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000515 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
516 return Args && Args->size() == 1 &&
517 fromJSON(Args->front(), R.workspaceEdit);
518 }
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000519 if (R.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK)
520 return Args && Args->size() == 1 && fromJSON(Args->front(), R.tweakArgs);
Sam McCallff8b8742017-11-30 21:32:29 +0000521 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000522}
523
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000524llvm::json::Value toJSON(const SymbolInformation &P) {
525 return llvm::json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000526 {"name", P.name},
527 {"kind", static_cast<int>(P.kind)},
528 {"location", P.location},
529 {"containerName", P.containerName},
530 };
531}
532
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000533llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
534 const SymbolInformation &SI) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000535 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
536 return O;
537}
538
Jan Korousb4067012018-11-27 16:40:46 +0000539bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
540 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
541 LHS.USR == RHS.USR && LHS.ID == RHS.ID;
542}
543
544llvm::json::Value toJSON(const SymbolDetails &P) {
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000545 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000546 {"containerName", llvm::json::Value(nullptr)},
547 {"usr", llvm::json::Value(nullptr)},
548 {"id", llvm::json::Value(nullptr)}};
Jan Korousb4067012018-11-27 16:40:46 +0000549
550 if (!P.name.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000551 Result["name"] = P.name;
Jan Korousb4067012018-11-27 16:40:46 +0000552
553 if (!P.containerName.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000554 Result["containerName"] = P.containerName;
Jan Korousb4067012018-11-27 16:40:46 +0000555
556 if (!P.USR.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000557 Result["usr"] = P.USR;
Jan Korousb4067012018-11-27 16:40:46 +0000558
559 if (P.ID.hasValue())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000560 Result["id"] = P.ID.getValue().str();
Jan Korousb4067012018-11-27 16:40:46 +0000561
Jan Korous613c80d2018-11-28 10:24:07 +0000562 // Older clang cannot compile 'return Result', even though it is legal.
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000563 return llvm::json::Value(std::move(Result));
Jan Korousb4067012018-11-27 16:40:46 +0000564}
565
566llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
567 if (!S.containerName.empty()) {
568 O << S.containerName;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000569 llvm::StringRef ContNameRef;
Jan Korousb4067012018-11-27 16:40:46 +0000570 if (!ContNameRef.endswith("::")) {
571 O << " ";
572 }
573 }
574 O << S.name << " - " << toJSON(S);
575 return O;
576}
577
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000578bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R) {
579 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000580 return O && O.map("query", R.query);
581}
582
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000583llvm::json::Value toJSON(const Command &C) {
584 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000585 if (C.workspaceEdit)
586 Cmd["arguments"] = {*C.workspaceEdit};
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000587 if (C.tweakArgs)
588 Cmd["arguments"] = {*C.tweakArgs};
Eric Liuc5105f92018-02-16 14:15:55 +0000589 return std::move(Cmd);
590}
591
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000592const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000593const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
Sam McCall20841d42018-10-16 16:29:41 +0000594
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000595llvm::json::Value toJSON(const CodeAction &CA) {
596 auto CodeAction = llvm::json::Object{{"title", CA.title}};
Sam McCall20841d42018-10-16 16:29:41 +0000597 if (CA.kind)
598 CodeAction["kind"] = *CA.kind;
599 if (CA.diagnostics)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000600 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
Sam McCall20841d42018-10-16 16:29:41 +0000601 if (CA.edit)
602 CodeAction["edit"] = *CA.edit;
603 if (CA.command)
604 CodeAction["command"] = *CA.command;
605 return std::move(CodeAction);
606}
607
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000608llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
Ilya Biryukov19d75602018-11-23 15:21:19 +0000609 return O << S.name << " - " << toJSON(S);
610}
611
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000612llvm::json::Value toJSON(const DocumentSymbol &S) {
613 llvm::json::Object Result{{"name", S.name},
614 {"kind", static_cast<int>(S.kind)},
615 {"range", S.range},
616 {"selectionRange", S.selectionRange}};
Ilya Biryukov19d75602018-11-23 15:21:19 +0000617
618 if (!S.detail.empty())
619 Result["detail"] = S.detail;
620 if (!S.children.empty())
621 Result["children"] = S.children;
622 if (S.deprecated)
623 Result["deprecated"] = true;
Ilya Biryukov4174d092018-11-26 09:57:41 +0000624 // Older gcc cannot compile 'return Result', even though it is legal.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000625 return llvm::json::Value(std::move(Result));
Ilya Biryukov19d75602018-11-23 15:21:19 +0000626}
627
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000628llvm::json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000629 if (!WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000630 return llvm::json::Object{};
631 llvm::json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000632 for (auto &Change : *WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000633 FileChanges[Change.first] = llvm::json::Array(Change.second);
634 return llvm::json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000635}
636
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000637bool fromJSON(const llvm::json::Value &Params, TweakArgs &A) {
638 llvm::json::ObjectMapper O(Params);
639 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
640 O.map("tweakID", A.tweakID);
641}
642
643llvm::json::Value toJSON(const TweakArgs &A) {
644 return llvm::json::Object{
645 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
646}
647
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000648llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
649 return llvm::json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000650}
651
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000652bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R) {
653 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000654 return O && O.map("textDocument", R.textDocument) &&
655 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000656}
657
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000658bool fromJSON(const llvm::json::Value &Params, CompletionContext &R) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000659 llvm::json::ObjectMapper O(Params);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000660 if (!O)
661 return false;
662
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000663 int TriggerKind;
664 if (!O.map("triggerKind", TriggerKind))
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000665 return false;
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000666 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000667
668 if (auto *TC = Params.getAsObject()->get("triggerCharacter"))
669 return fromJSON(*TC, R.triggerCharacter);
670 return true;
671}
672
673bool fromJSON(const llvm::json::Value &Params, CompletionParams &R) {
674 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R)))
675 return false;
676 if (auto *Context = Params.getAsObject()->get("context"))
677 return fromJSON(*Context, R.context);
678 return true;
679}
680
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000681static llvm::StringRef toTextKind(MarkupKind Kind) {
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000682 switch (Kind) {
683 case MarkupKind::PlainText:
684 return "plaintext";
685 case MarkupKind::Markdown:
686 return "markdown";
687 }
688 llvm_unreachable("Invalid MarkupKind");
689}
690
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000691bool fromJSON(const llvm::json::Value &V, MarkupKind &K) {
692 auto Str = V.getAsString();
693 if (!Str) {
694 elog("Failed to parse markup kind: expected a string");
695 return false;
696 }
697 if (*Str == "plaintext")
698 K = MarkupKind::PlainText;
699 else if (*Str == "markdown")
700 K = MarkupKind::Markdown;
701 else {
702 elog("Unknown markup kind: {0}", *Str);
703 return false;
704 }
705 return true;
706}
707
708llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
709 return OS << toTextKind(K);
710}
711
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000712llvm::json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000713 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000714 return nullptr;
715
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000716 return llvm::json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000717 {"kind", toTextKind(MC.kind)},
718 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000719 };
720}
721
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000722llvm::json::Value toJSON(const Hover &H) {
723 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000724
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000725 if (H.range.hasValue())
726 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000727
728 return std::move(Result);
729}
730
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000731bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000732 if (auto T = E.getAsInteger()) {
733 if (*T < static_cast<int>(CompletionItemKind::Text) ||
734 *T > static_cast<int>(CompletionItemKind::TypeParameter))
735 return false;
736 Out = static_cast<CompletionItemKind>(*T);
737 return true;
738 }
739 return false;
740}
741
742CompletionItemKind
743adjustKindToCapability(CompletionItemKind Kind,
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000744 CompletionItemKindBitset &SupportedCompletionItemKinds) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000745 auto KindVal = static_cast<size_t>(Kind);
746 if (KindVal >= CompletionItemKindMin &&
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000747 KindVal <= SupportedCompletionItemKinds.size() &&
748 SupportedCompletionItemKinds[KindVal])
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000749 return Kind;
750
751 switch (Kind) {
752 // Provide some fall backs for common kinds that are close enough.
753 case CompletionItemKind::Folder:
754 return CompletionItemKind::File;
755 case CompletionItemKind::EnumMember:
756 return CompletionItemKind::Enum;
757 case CompletionItemKind::Struct:
758 return CompletionItemKind::Class;
759 default:
760 return CompletionItemKind::Text;
761 }
762}
763
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000764bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000765 if (auto *A = E.getAsArray()) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000766 for (size_t I = 0; I < A->size(); ++I) {
767 CompletionItemKind KindOut;
768 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000769 Out.set(size_t(KindOut));
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000770 }
771 return true;
772 }
773 return false;
774}
775
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000776llvm::json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000777 assert(!CI.label.empty() && "completion item label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000778 llvm::json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000779 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000780 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000781 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000782 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000783 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000784 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000785 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000786 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000787 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000788 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000789 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000790 Result["insertText"] = CI.insertText;
791 if (CI.insertTextFormat != InsertTextFormat::Missing)
792 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000793 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000794 Result["textEdit"] = *CI.textEdit;
795 if (!CI.additionalTextEdits.empty())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000796 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
Eric Liu6df66002018-09-06 18:52:26 +0000797 if (CI.deprecated)
798 Result["deprecated"] = CI.deprecated;
Sam McCalldd0566b2017-11-06 15:40:30 +0000799 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000800}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000801
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000802llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000803 O << I.label << " - " << toJSON(I);
804 return O;
805}
806
Sam McCallff8b8742017-11-30 21:32:29 +0000807bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000808 return (L.sortText.empty() ? L.label : L.sortText) <
809 (R.sortText.empty() ? R.label : R.sortText);
810}
811
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000812llvm::json::Value toJSON(const CompletionList &L) {
813 return llvm::json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000814 {"isIncomplete", L.isIncomplete},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000815 {"items", llvm::json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000816 };
817}
818
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000819llvm::json::Value toJSON(const ParameterInformation &PI) {
Simon Pilgrim5b41fe52019-06-04 11:31:45 +0000820 assert((PI.labelOffsets.hasValue() || !PI.labelString.empty()) &&
821 "parameter information label is required");
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000822 llvm::json::Object Result;
823 if (PI.labelOffsets)
824 Result["label"] =
825 llvm::json::Array({PI.labelOffsets->first, PI.labelOffsets->second});
826 else
827 Result["label"] = PI.labelString;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000828 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000829 Result["documentation"] = PI.documentation;
830 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000831}
832
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000833llvm::json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000834 assert(!SI.label.empty() && "signature information label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000835 llvm::json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000836 {"label", SI.label},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000837 {"parameters", llvm::json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000838 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000839 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000840 Result["documentation"] = SI.documentation;
841 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000842}
843
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000844llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
845 const SignatureInformation &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000846 O << I.label << " - " << toJSON(I);
847 return O;
848}
849
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000850llvm::json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000851 assert(SH.activeSignature >= 0 &&
852 "Unexpected negative value for number of active signatures.");
853 assert(SH.activeParameter >= 0 &&
854 "Unexpected negative value for active parameter index");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000855 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000856 {"activeSignature", SH.activeSignature},
857 {"activeParameter", SH.activeParameter},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000858 {"signatures", llvm::json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000859 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000860}
Haojian Wu345099c2017-11-09 11:30:04 +0000861
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000862bool fromJSON(const llvm::json::Value &Params, RenameParams &R) {
863 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000864 return O && O.map("textDocument", R.textDocument) &&
865 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000866}
Sam McCallff8b8742017-11-30 21:32:29 +0000867
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000868llvm::json::Value toJSON(const DocumentHighlight &DH) {
869 return llvm::json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000870 {"range", toJSON(DH.range)},
871 {"kind", static_cast<int>(DH.kind)},
872 };
873}
874
Haojian Wub6188492018-12-20 15:39:12 +0000875llvm::json::Value toJSON(const FileStatus &FStatus) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000876 return llvm::json::Object{
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000877 {"uri", FStatus.uri},
878 {"state", FStatus.state},
Haojian Wub6188492018-12-20 15:39:12 +0000879 };
880}
881
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000882llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
883 const DocumentHighlight &V) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000884 O << V.range;
885 if (V.kind == DocumentHighlightKind::Read)
886 O << "(r)";
887 if (V.kind == DocumentHighlightKind::Write)
888 O << "(w)";
889 return O;
890}
891
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000892bool fromJSON(const llvm::json::Value &Params,
893 DidChangeConfigurationParams &CCP) {
894 llvm::json::ObjectMapper O(Params);
Simon Marchi5178f922018-02-22 14:00:39 +0000895 return O && O.map("settings", CCP.settings);
896}
897
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000898bool fromJSON(const llvm::json::Value &Params,
899 ClangdCompileCommand &CDbUpdate) {
900 llvm::json::ObjectMapper O(Params);
Alex Lorenzf8087862018-08-01 17:39:29 +0000901 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
902 O.map("compilationCommand", CDbUpdate.compilationCommand);
903}
904
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000905bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S) {
906 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000907 if (!O)
908 return true; // 'any' type in LSP.
909 O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
910 return true;
Simon Marchi5178f922018-02-22 14:00:39 +0000911}
912
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000913bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts) {
914 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000915 if (!O)
916 return true; // 'any' type in LSP.
917
918 fromJSON(Params, Opts.ConfigSettings);
919 O.map("compilationDatabasePath", Opts.compilationDatabasePath);
Sam McCall6980edb2018-11-02 14:07:51 +0000920 O.map("fallbackFlags", Opts.fallbackFlags);
Haojian Wub6188492018-12-20 15:39:12 +0000921 O.map("clangdFileStatus", Opts.FileStatus);
Sam McCallbc904612018-10-25 04:22:52 +0000922 return true;
Simon Marchiabeed662018-10-16 15:55:03 +0000923}
924
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000925bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out) {
926 auto T = E.getAsInteger();
927 if (!T)
928 return false;
929 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
930 *T > static_cast<int>(TypeHierarchyDirection::Both))
931 return false;
932 Out = static_cast<TypeHierarchyDirection>(*T);
933 return true;
934}
935
936bool fromJSON(const llvm::json::Value &Params, TypeHierarchyParams &R) {
937 llvm::json::ObjectMapper O(Params);
938 return O && O.map("textDocument", R.textDocument) &&
939 O.map("position", R.position) && O.map("resolve", R.resolve) &&
940 O.map("direction", R.direction);
941}
942
943llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
944 const TypeHierarchyItem &I) {
945 return O << I.name << " - " << toJSON(I);
946}
947
948llvm::json::Value toJSON(const TypeHierarchyItem &I) {
949 llvm::json::Object Result{{"name", I.name},
950 {"kind", static_cast<int>(I.kind)},
951 {"range", I.range},
952 {"selectionRange", I.selectionRange},
953 {"uri", I.uri}};
954
955 if (I.detail)
956 Result["detail"] = I.detail;
957 if (I.deprecated)
958 Result["deprecated"] = I.deprecated;
959 if (I.parents)
960 Result["parents"] = I.parents;
961 if (I.children)
962 Result["children"] = I.children;
963 return std::move(Result);
964}
965
966bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I) {
967 llvm::json::ObjectMapper O(Params);
968
969 // Required fields.
970 if (!(O && O.map("name", I.name) && O.map("kind", I.kind) &&
971 O.map("uri", I.uri) && O.map("range", I.range) &&
972 O.map("selectionRange", I.selectionRange))) {
973 return false;
974 }
975
976 // Optional fields.
977 O.map("detail", I.detail);
978 O.map("deprecated", I.deprecated);
979 O.map("parents", I.parents);
980 O.map("children", I.children);
981
982 return true;
983}
984
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000985bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R) {
Sam McCall1ad142f2018-09-05 11:53:07 +0000986 TextDocumentPositionParams &Base = R;
987 return fromJSON(Params, Base);
988}
989
Sam McCall8b25d222019-03-28 14:37:51 +0000990static const char *toString(OffsetEncoding OE) {
Sam McCalla69698f2019-03-27 17:47:49 +0000991 switch (OE) {
Sam McCall8b25d222019-03-28 14:37:51 +0000992 case OffsetEncoding::UTF8:
993 return "utf-8";
994 case OffsetEncoding::UTF16:
995 return "utf-16";
996 case OffsetEncoding::UTF32:
997 return "utf-32";
998 case OffsetEncoding::UnsupportedEncoding:
999 return "unknown";
Sam McCalla69698f2019-03-27 17:47:49 +00001000 }
Simon Pilgrim945db0b2019-03-29 13:43:00 +00001001 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
Sam McCalla69698f2019-03-27 17:47:49 +00001002}
Sam McCall8b25d222019-03-28 14:37:51 +00001003llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
Sam McCalla69698f2019-03-27 17:47:49 +00001004bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE) {
1005 auto Str = V.getAsString();
1006 if (!Str)
1007 return false;
1008 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
1009 .Case("utf-8", OffsetEncoding::UTF8)
1010 .Case("utf-16", OffsetEncoding::UTF16)
Sam McCall8b25d222019-03-28 14:37:51 +00001011 .Case("utf-32", OffsetEncoding::UTF32)
Sam McCalla69698f2019-03-27 17:47:49 +00001012 .Default(OffsetEncoding::UnsupportedEncoding);
1013 return true;
1014}
Sam McCall8b25d222019-03-28 14:37:51 +00001015llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
1016 return OS << toString(Enc);
1017}
Sam McCalla69698f2019-03-27 17:47:49 +00001018
Sam McCallff8b8742017-11-30 21:32:29 +00001019} // namespace clangd
1020} // namespace clang