blob: 29ceb1da5456e2b58b7f7c285e8d010806b4aea4 [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:
Sam McCallc9081962019-11-15 15:46:17 +0100260 return SymbolKind::Constructor;
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000261 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")) {
Johan Vikstroma848dab2019-07-04 07:53:12 +0000276 if (auto *SemanticHighlighting =
277 TextDocument->getObject("semanticHighlightingCapabilities")) {
278 if (auto SemanticHighlightingSupport =
279 SemanticHighlighting->getBoolean("semanticHighlighting"))
280 R.SemanticHighlighting = *SemanticHighlightingSupport;
281 }
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000282 if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) {
283 if (auto CategorySupport = Diagnostics->getBoolean("categorySupport"))
284 R.DiagnosticCategory = *CategorySupport;
Sam McCall16e70702018-10-24 07:59:38 +0000285 if (auto CodeActions = Diagnostics->getBoolean("codeActionsInline"))
286 R.DiagnosticFixes = *CodeActions;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000287 if (auto RelatedInfo = Diagnostics->getBoolean("relatedInformation"))
288 R.DiagnosticRelatedInformation = *RelatedInfo;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000289 }
290 if (auto *Completion = TextDocument->getObject("completion")) {
291 if (auto *Item = Completion->getObject("completionItem")) {
292 if (auto SnippetSupport = Item->getBoolean("snippetSupport"))
293 R.CompletionSnippets = *SnippetSupport;
294 }
295 if (auto *ItemKind = Completion->getObject("completionItemKind")) {
296 if (auto *ValueSet = ItemKind->get("valueSet")) {
297 R.CompletionItemKinds.emplace();
298 if (!fromJSON(*ValueSet, *R.CompletionItemKinds))
299 return false;
300 }
301 }
Sam McCall8d412942019-06-18 11:57:26 +0000302 if (auto EditsNearCursor = Completion->getBoolean("editsNearCursor"))
303 R.CompletionFixes = *EditsNearCursor;
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000304 }
305 if (auto *CodeAction = TextDocument->getObject("codeAction")) {
306 if (CodeAction->getObject("codeActionLiteralSupport"))
307 R.CodeActionStructure = true;
308 }
Ilya Biryukov19d75602018-11-23 15:21:19 +0000309 if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) {
310 if (auto HierarchicalSupport =
311 DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport"))
312 R.HierarchicalDocumentSymbol = *HierarchicalSupport;
313 }
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000314 if (auto *Hover = TextDocument->getObject("hover")) {
315 if (auto *ContentFormat = Hover->getArray("contentFormat")) {
316 for (const auto &Format : *ContentFormat) {
317 MarkupKind K = MarkupKind::PlainText;
318 if (fromJSON(Format, K)) {
319 R.HoverContentFormat = K;
320 break;
321 }
322 }
323 }
324 }
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000325 if (auto *Help = TextDocument->getObject("signatureHelp")) {
Sam McCall5f092e32019-07-08 17:27:15 +0000326 R.HasSignatureHelp = true;
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000327 if (auto *Info = Help->getObject("signatureInformation")) {
328 if (auto *Parameter = Info->getObject("parameterInformation")) {
329 if (auto OffsetSupport = Parameter->getBoolean("labelOffsetSupport"))
330 R.OffsetsInSignatureHelp = *OffsetSupport;
331 }
332 }
333 }
Haojian Wuf429ab62019-07-24 07:49:23 +0000334 if (auto *Rename = TextDocument->getObject("rename")) {
335 if (auto RenameSupport = Rename->getBoolean("prepareSupport"))
336 R.RenamePrepareSupport = *RenameSupport;
337 }
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000338 }
339 if (auto *Workspace = O->getObject("workspace")) {
340 if (auto *Symbol = Workspace->getObject("symbol")) {
341 if (auto *SymbolKind = Symbol->getObject("symbolKind")) {
342 if (auto *ValueSet = SymbolKind->get("valueSet")) {
343 R.WorkspaceSymbolKinds.emplace();
344 if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds))
345 return false;
346 }
347 }
348 }
349 }
Sam McCalla69698f2019-03-27 17:47:49 +0000350 if (auto *OffsetEncoding = O->get("offsetEncoding")) {
351 R.offsetEncoding.emplace();
352 if (!fromJSON(*OffsetEncoding, *R.offsetEncoding))
353 return false;
354 }
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000355 return true;
356}
357
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000358bool fromJSON(const llvm::json::Value &Params, InitializeParams &R) {
359 llvm::json::ObjectMapper O(Params);
Sam McCallec109022017-11-28 09:37:43 +0000360 if (!O)
Sam McCallff8b8742017-11-30 21:32:29 +0000361 return false;
Sam McCall38a04912017-11-29 11:36:46 +0000362 // We deliberately don't fail if we can't parse individual fields.
363 // Failing to handle a slightly malformed initialize would be a disaster.
Sam McCallff8b8742017-11-30 21:32:29 +0000364 O.map("processId", R.processId);
365 O.map("rootUri", R.rootUri);
366 O.map("rootPath", R.rootPath);
Ilya Biryukov23bc73b2018-02-15 14:32:57 +0000367 O.map("capabilities", R.capabilities);
Sam McCallff8b8742017-11-30 21:32:29 +0000368 O.map("trace", R.trace);
Simon Marchi88016782018-08-01 11:28:49 +0000369 O.map("initializationOptions", R.initializationOptions);
Sam McCallff8b8742017-11-30 21:32:29 +0000370 return true;
Marc-Andre Laperle37de9712017-09-27 15:31:17 +0000371}
372
Sam McCall395fde72019-06-18 13:37:54 +0000373llvm::json::Value toJSON(const MessageType &R) {
374 return static_cast<int64_t>(R);
375}
376
377llvm::json::Value toJSON(const ShowMessageParams &R) {
378 return llvm::json::Object{{"type", R.type}, {"message", R.message}};
379}
380
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000381bool fromJSON(const llvm::json::Value &Params, DidOpenTextDocumentParams &R) {
382 llvm::json::ObjectMapper O(Params);
Sam McCall2eb6b402018-11-02 13:06:55 +0000383 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000384}
385
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000386bool fromJSON(const llvm::json::Value &Params, DidCloseTextDocumentParams &R) {
387 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000388 return O && O.map("textDocument", R.textDocument);
Krasimir Georgiev561ba5e2017-04-10 13:31:39 +0000389}
390
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000391bool fromJSON(const llvm::json::Value &Params, DidChangeTextDocumentParams &R) {
392 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000393 return O && O.map("textDocument", R.textDocument) &&
Eric Liu51fed182018-02-22 18:40:39 +0000394 O.map("contentChanges", R.contentChanges) &&
395 O.map("wantDiagnostics", R.wantDiagnostics);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000396}
397
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000398bool fromJSON(const llvm::json::Value &E, FileChangeType &Out) {
Sam McCalld20d7982018-07-09 14:25:59 +0000399 if (auto T = E.getAsInteger()) {
Sam McCallff8b8742017-11-30 21:32:29 +0000400 if (*T < static_cast<int>(FileChangeType::Created) ||
401 *T > static_cast<int>(FileChangeType::Deleted))
402 return false;
403 Out = static_cast<FileChangeType>(*T);
404 return true;
405 }
406 return false;
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000407}
408
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000409bool fromJSON(const llvm::json::Value &Params, FileEvent &R) {
410 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000411 return O && O.map("uri", R.uri) && O.map("type", R.type);
Marc-Andre Laperlebf114242017-10-02 18:00:37 +0000412}
413
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000414bool fromJSON(const llvm::json::Value &Params, DidChangeWatchedFilesParams &R) {
415 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000416 return O && O.map("changes", R.changes);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000417}
418
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000419bool fromJSON(const llvm::json::Value &Params,
420 TextDocumentContentChangeEvent &R) {
421 llvm::json::ObjectMapper O(Params);
Simon Marchi98082622018-03-26 14:41:40 +0000422 return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) &&
423 O.map("text", R.text);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000424}
425
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000426bool fromJSON(const llvm::json::Value &Params,
427 DocumentRangeFormattingParams &R) {
428 llvm::json::ObjectMapper O(Params);
Nathan Ridge087b0442019-07-13 03:24:48 +0000429 return O && O.map("textDocument", R.textDocument) && O.map("range", R.range);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000430}
431
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000432bool fromJSON(const llvm::json::Value &Params,
433 DocumentOnTypeFormattingParams &R) {
434 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000435 return O && O.map("textDocument", R.textDocument) &&
Sam McCall149786d2019-06-10 13:01:49 +0000436 O.map("position", R.position) && O.map("ch", R.ch);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000437}
438
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000439bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R) {
440 llvm::json::ObjectMapper O(Params);
Sam McCall149786d2019-06-10 13:01:49 +0000441 return O && O.map("textDocument", R.textDocument);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000442}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000443
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000444bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R) {
445 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000446 return O && O.map("textDocument", R.textDocument);
447}
448
Sam McCallc9e4ee92019-04-18 15:17:07 +0000449llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
450 return llvm::json::Object{
Nathan Ridge087b0442019-07-13 03:24:48 +0000451 {"location", DRI.location},
452 {"message", DRI.message},
Sam McCallc9e4ee92019-04-18 15:17:07 +0000453 };
454}
455
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000456llvm::json::Value toJSON(const Diagnostic &D) {
457 llvm::json::Object Diag{
Sam McCall20841d42018-10-16 16:29:41 +0000458 {"range", D.range},
459 {"severity", D.severity},
460 {"message", D.message},
461 };
Sam McCall16e70702018-10-24 07:59:38 +0000462 if (D.category)
463 Diag["category"] = *D.category;
464 if (D.codeActions)
465 Diag["codeActions"] = D.codeActions;
Sam McCall641caa52019-04-17 12:35:16 +0000466 if (!D.code.empty())
467 Diag["code"] = D.code;
468 if (!D.source.empty())
469 Diag["source"] = D.source;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000470 if (D.relatedInformation)
471 Diag["relatedInformation"] = *D.relatedInformation;
Sam McCall20841d42018-10-16 16:29:41 +0000472 return std::move(Diag);
473}
474
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000475bool fromJSON(const llvm::json::Value &Params, Diagnostic &R) {
476 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000477 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
478 return false;
479 O.map("severity", R.severity);
Sam McCall16e70702018-10-24 07:59:38 +0000480 O.map("category", R.category);
Sam McCall641caa52019-04-17 12:35:16 +0000481 O.map("code", R.code);
482 O.map("source", R.source);
Sam McCallff8b8742017-11-30 21:32:29 +0000483 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000484}
485
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000486bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R) {
487 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000488 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000489}
490
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000491llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
Sam McCall034e11a2018-01-25 17:29:17 +0000492 OS << D.range << " [";
493 switch (D.severity) {
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000494 case 1:
495 OS << "error";
496 break;
497 case 2:
498 OS << "warning";
499 break;
500 case 3:
501 OS << "note";
502 break;
503 case 4:
504 OS << "remark";
505 break;
506 default:
507 OS << "diagnostic";
508 break;
Sam McCall034e11a2018-01-25 17:29:17 +0000509 }
510 return OS << '(' << D.severity << "): " << D.message << "]";
511}
512
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000513bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R) {
514 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000515 return O && O.map("textDocument", R.textDocument) &&
516 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000517}
518
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000519bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R) {
520 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000521 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000522}
523
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000524const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000525 "clangd.applyFix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000526const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_TWEAK =
527 "clangd.applyTweak";
528
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000529bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R) {
530 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000531 if (!O || !O.map("command", R.command))
532 return false;
Sam McCallec109022017-11-28 09:37:43 +0000533
Sam McCalld20d7982018-07-09 14:25:59 +0000534 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000535 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
536 return Args && Args->size() == 1 &&
537 fromJSON(Args->front(), R.workspaceEdit);
538 }
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000539 if (R.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK)
540 return Args && Args->size() == 1 && fromJSON(Args->front(), R.tweakArgs);
Sam McCallff8b8742017-11-30 21:32:29 +0000541 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000542}
543
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000544llvm::json::Value toJSON(const SymbolInformation &P) {
545 return llvm::json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000546 {"name", P.name},
547 {"kind", static_cast<int>(P.kind)},
548 {"location", P.location},
549 {"containerName", P.containerName},
550 };
551}
552
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000553llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
554 const SymbolInformation &SI) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000555 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
556 return O;
557}
558
Jan Korousb4067012018-11-27 16:40:46 +0000559bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
560 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
561 LHS.USR == RHS.USR && LHS.ID == RHS.ID;
562}
563
564llvm::json::Value toJSON(const SymbolDetails &P) {
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000565 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000566 {"containerName", llvm::json::Value(nullptr)},
567 {"usr", llvm::json::Value(nullptr)},
568 {"id", llvm::json::Value(nullptr)}};
Jan Korousb4067012018-11-27 16:40:46 +0000569
570 if (!P.name.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000571 Result["name"] = P.name;
Jan Korousb4067012018-11-27 16:40:46 +0000572
573 if (!P.containerName.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000574 Result["containerName"] = P.containerName;
Jan Korousb4067012018-11-27 16:40:46 +0000575
576 if (!P.USR.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000577 Result["usr"] = P.USR;
Jan Korousb4067012018-11-27 16:40:46 +0000578
579 if (P.ID.hasValue())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000580 Result["id"] = P.ID.getValue().str();
Jan Korousb4067012018-11-27 16:40:46 +0000581
Jan Korous613c80d2018-11-28 10:24:07 +0000582 // Older clang cannot compile 'return Result', even though it is legal.
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000583 return llvm::json::Value(std::move(Result));
Jan Korousb4067012018-11-27 16:40:46 +0000584}
585
586llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
587 if (!S.containerName.empty()) {
588 O << S.containerName;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000589 llvm::StringRef ContNameRef;
Jan Korousb4067012018-11-27 16:40:46 +0000590 if (!ContNameRef.endswith("::")) {
591 O << " ";
592 }
593 }
594 O << S.name << " - " << toJSON(S);
595 return O;
596}
597
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000598bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R) {
599 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000600 return O && O.map("query", R.query);
601}
602
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000603llvm::json::Value toJSON(const Command &C) {
604 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000605 if (C.workspaceEdit)
606 Cmd["arguments"] = {*C.workspaceEdit};
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000607 if (C.tweakArgs)
608 Cmd["arguments"] = {*C.tweakArgs};
Eric Liuc5105f92018-02-16 14:15:55 +0000609 return std::move(Cmd);
610}
611
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000612const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000613const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
Sam McCall395fde72019-06-18 13:37:54 +0000614const llvm::StringLiteral CodeAction::INFO_KIND = "info";
Sam McCall20841d42018-10-16 16:29:41 +0000615
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000616llvm::json::Value toJSON(const CodeAction &CA) {
617 auto CodeAction = llvm::json::Object{{"title", CA.title}};
Sam McCall20841d42018-10-16 16:29:41 +0000618 if (CA.kind)
619 CodeAction["kind"] = *CA.kind;
620 if (CA.diagnostics)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000621 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
Sam McCall20841d42018-10-16 16:29:41 +0000622 if (CA.edit)
623 CodeAction["edit"] = *CA.edit;
624 if (CA.command)
625 CodeAction["command"] = *CA.command;
626 return std::move(CodeAction);
627}
628
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000629llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
Ilya Biryukov19d75602018-11-23 15:21:19 +0000630 return O << S.name << " - " << toJSON(S);
631}
632
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000633llvm::json::Value toJSON(const DocumentSymbol &S) {
634 llvm::json::Object Result{{"name", S.name},
635 {"kind", static_cast<int>(S.kind)},
636 {"range", S.range},
637 {"selectionRange", S.selectionRange}};
Ilya Biryukov19d75602018-11-23 15:21:19 +0000638
639 if (!S.detail.empty())
640 Result["detail"] = S.detail;
641 if (!S.children.empty())
642 Result["children"] = S.children;
643 if (S.deprecated)
644 Result["deprecated"] = true;
Ilya Biryukov4174d092018-11-26 09:57:41 +0000645 // Older gcc cannot compile 'return Result', even though it is legal.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000646 return llvm::json::Value(std::move(Result));
Ilya Biryukov19d75602018-11-23 15:21:19 +0000647}
648
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000649llvm::json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000650 if (!WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000651 return llvm::json::Object{};
652 llvm::json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000653 for (auto &Change : *WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000654 FileChanges[Change.first] = llvm::json::Array(Change.second);
655 return llvm::json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000656}
657
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000658bool fromJSON(const llvm::json::Value &Params, TweakArgs &A) {
659 llvm::json::ObjectMapper O(Params);
660 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
661 O.map("tweakID", A.tweakID);
662}
663
664llvm::json::Value toJSON(const TweakArgs &A) {
665 return llvm::json::Object{
666 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
667}
668
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000669llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
670 return llvm::json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000671}
672
Haojian Wuf2516342019-08-05 12:48:09 +0000673bool fromJSON(const llvm::json::Value &Response,
674 ApplyWorkspaceEditResponse &R) {
675 llvm::json::ObjectMapper O(Response);
676 if (!O || !O.map("applied", R.applied))
677 return false;
678 O.map("failureReason", R.failureReason);
679 return true;
680}
681
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000682bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R) {
683 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000684 return O && O.map("textDocument", R.textDocument) &&
685 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000686}
687
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000688bool fromJSON(const llvm::json::Value &Params, CompletionContext &R) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000689 llvm::json::ObjectMapper O(Params);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000690 if (!O)
691 return false;
692
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000693 int TriggerKind;
694 if (!O.map("triggerKind", TriggerKind))
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000695 return false;
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000696 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000697
698 if (auto *TC = Params.getAsObject()->get("triggerCharacter"))
699 return fromJSON(*TC, R.triggerCharacter);
700 return true;
701}
702
703bool fromJSON(const llvm::json::Value &Params, CompletionParams &R) {
704 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R)))
705 return false;
706 if (auto *Context = Params.getAsObject()->get("context"))
707 return fromJSON(*Context, R.context);
708 return true;
709}
710
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000711static llvm::StringRef toTextKind(MarkupKind Kind) {
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000712 switch (Kind) {
713 case MarkupKind::PlainText:
714 return "plaintext";
715 case MarkupKind::Markdown:
716 return "markdown";
717 }
718 llvm_unreachable("Invalid MarkupKind");
719}
720
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000721bool fromJSON(const llvm::json::Value &V, MarkupKind &K) {
722 auto Str = V.getAsString();
723 if (!Str) {
724 elog("Failed to parse markup kind: expected a string");
725 return false;
726 }
727 if (*Str == "plaintext")
728 K = MarkupKind::PlainText;
729 else if (*Str == "markdown")
730 K = MarkupKind::Markdown;
731 else {
732 elog("Unknown markup kind: {0}", *Str);
733 return false;
734 }
735 return true;
736}
737
738llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
739 return OS << toTextKind(K);
740}
741
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000742llvm::json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000743 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000744 return nullptr;
745
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000746 return llvm::json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000747 {"kind", toTextKind(MC.kind)},
748 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000749 };
750}
751
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000752llvm::json::Value toJSON(const Hover &H) {
753 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000754
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000755 if (H.range.hasValue())
756 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000757
758 return std::move(Result);
759}
760
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000761bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000762 if (auto T = E.getAsInteger()) {
763 if (*T < static_cast<int>(CompletionItemKind::Text) ||
764 *T > static_cast<int>(CompletionItemKind::TypeParameter))
765 return false;
766 Out = static_cast<CompletionItemKind>(*T);
767 return true;
768 }
769 return false;
770}
771
772CompletionItemKind
773adjustKindToCapability(CompletionItemKind Kind,
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000774 CompletionItemKindBitset &SupportedCompletionItemKinds) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000775 auto KindVal = static_cast<size_t>(Kind);
776 if (KindVal >= CompletionItemKindMin &&
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000777 KindVal <= SupportedCompletionItemKinds.size() &&
778 SupportedCompletionItemKinds[KindVal])
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000779 return Kind;
780
781 switch (Kind) {
782 // Provide some fall backs for common kinds that are close enough.
783 case CompletionItemKind::Folder:
784 return CompletionItemKind::File;
785 case CompletionItemKind::EnumMember:
786 return CompletionItemKind::Enum;
787 case CompletionItemKind::Struct:
788 return CompletionItemKind::Class;
789 default:
790 return CompletionItemKind::Text;
791 }
792}
793
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000794bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000795 if (auto *A = E.getAsArray()) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000796 for (size_t I = 0; I < A->size(); ++I) {
797 CompletionItemKind KindOut;
798 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000799 Out.set(size_t(KindOut));
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000800 }
801 return true;
802 }
803 return false;
804}
805
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000806llvm::json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000807 assert(!CI.label.empty() && "completion item label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000808 llvm::json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000809 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000810 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000811 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000812 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000813 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000814 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000815 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000816 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000817 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000818 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000819 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000820 Result["insertText"] = CI.insertText;
821 if (CI.insertTextFormat != InsertTextFormat::Missing)
822 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000823 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000824 Result["textEdit"] = *CI.textEdit;
825 if (!CI.additionalTextEdits.empty())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000826 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
Eric Liu6df66002018-09-06 18:52:26 +0000827 if (CI.deprecated)
828 Result["deprecated"] = CI.deprecated;
Sam McCalldd0566b2017-11-06 15:40:30 +0000829 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000830}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000831
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000832llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000833 O << I.label << " - " << toJSON(I);
834 return O;
835}
836
Sam McCallff8b8742017-11-30 21:32:29 +0000837bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000838 return (L.sortText.empty() ? L.label : L.sortText) <
839 (R.sortText.empty() ? R.label : R.sortText);
840}
841
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000842llvm::json::Value toJSON(const CompletionList &L) {
843 return llvm::json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000844 {"isIncomplete", L.isIncomplete},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000845 {"items", llvm::json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000846 };
847}
848
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000849llvm::json::Value toJSON(const ParameterInformation &PI) {
Simon Pilgrim5b41fe52019-06-04 11:31:45 +0000850 assert((PI.labelOffsets.hasValue() || !PI.labelString.empty()) &&
851 "parameter information label is required");
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000852 llvm::json::Object Result;
853 if (PI.labelOffsets)
854 Result["label"] =
855 llvm::json::Array({PI.labelOffsets->first, PI.labelOffsets->second});
856 else
857 Result["label"] = PI.labelString;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000858 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000859 Result["documentation"] = PI.documentation;
860 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000861}
862
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000863llvm::json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000864 assert(!SI.label.empty() && "signature information label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000865 llvm::json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000866 {"label", SI.label},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000867 {"parameters", llvm::json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000868 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000869 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000870 Result["documentation"] = SI.documentation;
871 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000872}
873
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000874llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
875 const SignatureInformation &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000876 O << I.label << " - " << toJSON(I);
877 return O;
878}
879
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000880llvm::json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000881 assert(SH.activeSignature >= 0 &&
882 "Unexpected negative value for number of active signatures.");
883 assert(SH.activeParameter >= 0 &&
884 "Unexpected negative value for active parameter index");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000885 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000886 {"activeSignature", SH.activeSignature},
887 {"activeParameter", SH.activeParameter},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000888 {"signatures", llvm::json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000889 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000890}
Haojian Wu345099c2017-11-09 11:30:04 +0000891
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000892bool fromJSON(const llvm::json::Value &Params, RenameParams &R) {
893 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000894 return O && O.map("textDocument", R.textDocument) &&
895 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000896}
Sam McCallff8b8742017-11-30 21:32:29 +0000897
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000898llvm::json::Value toJSON(const DocumentHighlight &DH) {
899 return llvm::json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000900 {"range", toJSON(DH.range)},
901 {"kind", static_cast<int>(DH.kind)},
902 };
903}
904
Haojian Wub6188492018-12-20 15:39:12 +0000905llvm::json::Value toJSON(const FileStatus &FStatus) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000906 return llvm::json::Object{
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000907 {"uri", FStatus.uri},
908 {"state", FStatus.state},
Haojian Wub6188492018-12-20 15:39:12 +0000909 };
910}
911
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000912llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
913 const DocumentHighlight &V) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000914 O << V.range;
915 if (V.kind == DocumentHighlightKind::Read)
916 O << "(r)";
917 if (V.kind == DocumentHighlightKind::Write)
918 O << "(w)";
919 return O;
920}
921
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000922bool fromJSON(const llvm::json::Value &Params,
923 DidChangeConfigurationParams &CCP) {
924 llvm::json::ObjectMapper O(Params);
Simon Marchi5178f922018-02-22 14:00:39 +0000925 return O && O.map("settings", CCP.settings);
926}
927
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000928bool fromJSON(const llvm::json::Value &Params,
929 ClangdCompileCommand &CDbUpdate) {
930 llvm::json::ObjectMapper O(Params);
Alex Lorenzf8087862018-08-01 17:39:29 +0000931 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
932 O.map("compilationCommand", CDbUpdate.compilationCommand);
933}
934
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000935bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S) {
936 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000937 if (!O)
938 return true; // 'any' type in LSP.
939 O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
940 return true;
Simon Marchi5178f922018-02-22 14:00:39 +0000941}
942
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000943bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts) {
944 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000945 if (!O)
946 return true; // 'any' type in LSP.
947
948 fromJSON(Params, Opts.ConfigSettings);
949 O.map("compilationDatabasePath", Opts.compilationDatabasePath);
Sam McCall6980edb2018-11-02 14:07:51 +0000950 O.map("fallbackFlags", Opts.fallbackFlags);
Haojian Wub6188492018-12-20 15:39:12 +0000951 O.map("clangdFileStatus", Opts.FileStatus);
Sam McCallbc904612018-10-25 04:22:52 +0000952 return true;
Simon Marchiabeed662018-10-16 15:55:03 +0000953}
954
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000955bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out) {
956 auto T = E.getAsInteger();
957 if (!T)
958 return false;
959 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
960 *T > static_cast<int>(TypeHierarchyDirection::Both))
961 return false;
962 Out = static_cast<TypeHierarchyDirection>(*T);
963 return true;
964}
965
966bool fromJSON(const llvm::json::Value &Params, TypeHierarchyParams &R) {
967 llvm::json::ObjectMapper O(Params);
968 return O && O.map("textDocument", R.textDocument) &&
969 O.map("position", R.position) && O.map("resolve", R.resolve) &&
970 O.map("direction", R.direction);
971}
972
973llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
974 const TypeHierarchyItem &I) {
975 return O << I.name << " - " << toJSON(I);
976}
977
978llvm::json::Value toJSON(const TypeHierarchyItem &I) {
979 llvm::json::Object Result{{"name", I.name},
980 {"kind", static_cast<int>(I.kind)},
981 {"range", I.range},
982 {"selectionRange", I.selectionRange},
983 {"uri", I.uri}};
984
985 if (I.detail)
986 Result["detail"] = I.detail;
987 if (I.deprecated)
988 Result["deprecated"] = I.deprecated;
989 if (I.parents)
990 Result["parents"] = I.parents;
991 if (I.children)
992 Result["children"] = I.children;
Nathan Ridge087b0442019-07-13 03:24:48 +0000993 if (I.data)
994 Result["data"] = I.data;
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000995 return std::move(Result);
996}
997
998bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I) {
999 llvm::json::ObjectMapper O(Params);
1000
1001 // Required fields.
1002 if (!(O && O.map("name", I.name) && O.map("kind", I.kind) &&
1003 O.map("uri", I.uri) && O.map("range", I.range) &&
1004 O.map("selectionRange", I.selectionRange))) {
1005 return false;
1006 }
1007
1008 // Optional fields.
1009 O.map("detail", I.detail);
1010 O.map("deprecated", I.deprecated);
1011 O.map("parents", I.parents);
1012 O.map("children", I.children);
Nathan Ridge087b0442019-07-13 03:24:48 +00001013 O.map("data", I.data);
Kadir Cetinkaya86658022019-03-19 09:27:04 +00001014
1015 return true;
1016}
1017
Nathan Ridge087b0442019-07-13 03:24:48 +00001018bool fromJSON(const llvm::json::Value &Params,
1019 ResolveTypeHierarchyItemParams &P) {
1020 llvm::json::ObjectMapper O(Params);
1021 return O && O.map("item", P.item) && O.map("resolve", P.resolve) &&
1022 O.map("direction", P.direction);
1023}
1024
Ilya Biryukovf2001aa2019-01-07 15:45:19 +00001025bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R) {
Sam McCall1ad142f2018-09-05 11:53:07 +00001026 TextDocumentPositionParams &Base = R;
1027 return fromJSON(Params, Base);
1028}
1029
Sam McCall8b25d222019-03-28 14:37:51 +00001030static const char *toString(OffsetEncoding OE) {
Sam McCalla69698f2019-03-27 17:47:49 +00001031 switch (OE) {
Sam McCall8b25d222019-03-28 14:37:51 +00001032 case OffsetEncoding::UTF8:
1033 return "utf-8";
1034 case OffsetEncoding::UTF16:
1035 return "utf-16";
1036 case OffsetEncoding::UTF32:
1037 return "utf-32";
1038 case OffsetEncoding::UnsupportedEncoding:
1039 return "unknown";
Sam McCalla69698f2019-03-27 17:47:49 +00001040 }
Simon Pilgrim945db0b2019-03-29 13:43:00 +00001041 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
Sam McCalla69698f2019-03-27 17:47:49 +00001042}
Sam McCall8b25d222019-03-28 14:37:51 +00001043llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
Sam McCalla69698f2019-03-27 17:47:49 +00001044bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE) {
1045 auto Str = V.getAsString();
1046 if (!Str)
1047 return false;
1048 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
1049 .Case("utf-8", OffsetEncoding::UTF8)
1050 .Case("utf-16", OffsetEncoding::UTF16)
Sam McCall8b25d222019-03-28 14:37:51 +00001051 .Case("utf-32", OffsetEncoding::UTF32)
Sam McCalla69698f2019-03-27 17:47:49 +00001052 .Default(OffsetEncoding::UnsupportedEncoding);
1053 return true;
1054}
Sam McCall8b25d222019-03-28 14:37:51 +00001055llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
1056 return OS << toString(Enc);
1057}
Sam McCalla69698f2019-03-27 17:47:49 +00001058
Johan Vikstroma848dab2019-07-04 07:53:12 +00001059bool operator==(const SemanticHighlightingInformation &Lhs,
1060 const SemanticHighlightingInformation &Rhs) {
1061 return Lhs.Line == Rhs.Line && Lhs.Tokens == Rhs.Tokens;
1062}
1063
1064llvm::json::Value toJSON(const SemanticHighlightingInformation &Highlighting) {
1065 return llvm::json::Object{{"line", Highlighting.Line},
Nathan Ridgeb2e6c2b2019-09-24 18:17:55 -04001066 {"tokens", Highlighting.Tokens},
1067 {"isInactive", Highlighting.IsInactive}};
Johan Vikstroma848dab2019-07-04 07:53:12 +00001068}
1069
1070llvm::json::Value toJSON(const SemanticHighlightingParams &Highlighting) {
1071 return llvm::json::Object{
1072 {"textDocument", Highlighting.TextDocument},
1073 {"lines", std::move(Highlighting.Lines)},
1074 };
1075}
1076
Utkarsh Saxena55925da2019-09-24 13:38:33 +00001077bool fromJSON(const llvm::json::Value &Params, SelectionRangeParams &P) {
1078 llvm::json::ObjectMapper O(Params);
1079 return O && O.map("textDocument", P.textDocument) &&
1080 O.map("positions", P.positions);
1081}
1082
1083llvm::json::Value toJSON(const SelectionRange &Out) {
1084 if (Out.parent) {
1085 return llvm::json::Object{{"range", Out.range},
1086 {"parent", toJSON(*Out.parent)}};
1087 }
1088 return llvm::json::Object{{"range", Out.range}};
1089}
Sam McCall8d7ecc12019-12-16 19:08:51 +01001090
1091bool fromJSON(const llvm::json::Value &Params, DocumentLinkParams &R) {
1092 llvm::json::ObjectMapper O(Params);
1093 return O && O.map("textDocument", R.textDocument);
1094}
1095
1096llvm::json::Value toJSON(const DocumentLink &DocumentLink) {
1097 return llvm::json::Object{
1098 {"range", DocumentLink.range},
1099 {"target", DocumentLink.target},
1100 };
1101}
1102
Sam McCallff8b8742017-11-30 21:32:29 +00001103} // namespace clangd
1104} // namespace clang