blob: 4714c6c11da579861869d14a92f07407b11ae61c [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, FormattingOptions &R) {
406 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000407 return O && O.map("tabSize", R.tabSize) &&
408 O.map("insertSpaces", R.insertSpaces);
409}
410
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000411llvm::json::Value toJSON(const FormattingOptions &P) {
412 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000413 {"tabSize", P.tabSize},
414 {"insertSpaces", P.insertSpaces},
415 };
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000416}
417
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000418bool fromJSON(const llvm::json::Value &Params,
419 DocumentRangeFormattingParams &R) {
420 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000421 return O && O.map("textDocument", R.textDocument) &&
422 O.map("range", R.range) && O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000423}
424
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000425bool fromJSON(const llvm::json::Value &Params,
426 DocumentOnTypeFormattingParams &R) {
427 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000428 return O && O.map("textDocument", R.textDocument) &&
429 O.map("position", R.position) && O.map("ch", R.ch) &&
430 O.map("options", R.options);
Krasimir Georgiev1b8bfd42017-02-16 10:49:46 +0000431}
432
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000433bool fromJSON(const llvm::json::Value &Params, DocumentFormattingParams &R) {
434 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000435 return O && O.map("textDocument", R.textDocument) &&
436 O.map("options", R.options);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000437}
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000438
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000439bool fromJSON(const llvm::json::Value &Params, DocumentSymbolParams &R) {
440 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperle1be69702018-07-05 19:35:01 +0000441 return O && O.map("textDocument", R.textDocument);
442}
443
Sam McCallc9e4ee92019-04-18 15:17:07 +0000444llvm::json::Value toJSON(const DiagnosticRelatedInformation &DRI) {
445 return llvm::json::Object{
446 {"location", DRI.location},
447 {"message", DRI.message},
448 };
449}
450
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000451llvm::json::Value toJSON(const Diagnostic &D) {
452 llvm::json::Object Diag{
Sam McCall20841d42018-10-16 16:29:41 +0000453 {"range", D.range},
454 {"severity", D.severity},
455 {"message", D.message},
456 };
Sam McCall16e70702018-10-24 07:59:38 +0000457 if (D.category)
458 Diag["category"] = *D.category;
459 if (D.codeActions)
460 Diag["codeActions"] = D.codeActions;
Sam McCall641caa52019-04-17 12:35:16 +0000461 if (!D.code.empty())
462 Diag["code"] = D.code;
463 if (!D.source.empty())
464 Diag["source"] = D.source;
Sam McCallc9e4ee92019-04-18 15:17:07 +0000465 if (D.relatedInformation)
466 Diag["relatedInformation"] = *D.relatedInformation;
Sam McCall20841d42018-10-16 16:29:41 +0000467 return std::move(Diag);
468}
469
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000470bool fromJSON(const llvm::json::Value &Params, Diagnostic &R) {
471 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000472 if (!O || !O.map("range", R.range) || !O.map("message", R.message))
473 return false;
474 O.map("severity", R.severity);
Sam McCall16e70702018-10-24 07:59:38 +0000475 O.map("category", R.category);
Sam McCall641caa52019-04-17 12:35:16 +0000476 O.map("code", R.code);
477 O.map("source", R.source);
Sam McCallff8b8742017-11-30 21:32:29 +0000478 return true;
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000479}
480
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000481bool fromJSON(const llvm::json::Value &Params, CodeActionContext &R) {
482 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000483 return O && O.map("diagnostics", R.diagnostics);
Benjamin Kramerf0af3e62017-03-01 16:16:29 +0000484}
485
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000486llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diagnostic &D) {
Sam McCall034e11a2018-01-25 17:29:17 +0000487 OS << D.range << " [";
488 switch (D.severity) {
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000489 case 1:
490 OS << "error";
491 break;
492 case 2:
493 OS << "warning";
494 break;
495 case 3:
496 OS << "note";
497 break;
498 case 4:
499 OS << "remark";
500 break;
501 default:
502 OS << "diagnostic";
503 break;
Sam McCall034e11a2018-01-25 17:29:17 +0000504 }
505 return OS << '(' << D.severity << "): " << D.message << "]";
506}
507
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000508bool fromJSON(const llvm::json::Value &Params, CodeActionParams &R) {
509 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000510 return O && O.map("textDocument", R.textDocument) &&
511 O.map("range", R.range) && O.map("context", R.context);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000512}
513
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000514bool fromJSON(const llvm::json::Value &Params, WorkspaceEdit &R) {
515 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000516 return O && O.map("changes", R.changes);
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000517}
518
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000519const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND =
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000520 "clangd.applyFix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000521const llvm::StringLiteral ExecuteCommandParams::CLANGD_APPLY_TWEAK =
522 "clangd.applyTweak";
523
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000524bool fromJSON(const llvm::json::Value &Params, ExecuteCommandParams &R) {
525 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000526 if (!O || !O.map("command", R.command))
527 return false;
Sam McCallec109022017-11-28 09:37:43 +0000528
Sam McCalld20d7982018-07-09 14:25:59 +0000529 auto Args = Params.getAsObject()->getArray("arguments");
Sam McCallff8b8742017-11-30 21:32:29 +0000530 if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) {
531 return Args && Args->size() == 1 &&
532 fromJSON(Args->front(), R.workspaceEdit);
533 }
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000534 if (R.command == ExecuteCommandParams::CLANGD_APPLY_TWEAK)
535 return Args && Args->size() == 1 && fromJSON(Args->front(), R.tweakArgs);
Sam McCallff8b8742017-11-30 21:32:29 +0000536 return false; // Unrecognized command.
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000537}
538
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000539llvm::json::Value toJSON(const SymbolInformation &P) {
540 return llvm::json::Object{
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000541 {"name", P.name},
542 {"kind", static_cast<int>(P.kind)},
543 {"location", P.location},
544 {"containerName", P.containerName},
545 };
546}
547
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000548llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
549 const SymbolInformation &SI) {
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000550 O << SI.containerName << "::" << SI.name << " - " << toJSON(SI);
551 return O;
552}
553
Jan Korousb4067012018-11-27 16:40:46 +0000554bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) {
555 return LHS.name == RHS.name && LHS.containerName == RHS.containerName &&
556 LHS.USR == RHS.USR && LHS.ID == RHS.ID;
557}
558
559llvm::json::Value toJSON(const SymbolDetails &P) {
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000560 llvm::json::Object Result{{"name", llvm::json::Value(nullptr)},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000561 {"containerName", llvm::json::Value(nullptr)},
562 {"usr", llvm::json::Value(nullptr)},
563 {"id", llvm::json::Value(nullptr)}};
Jan Korousb4067012018-11-27 16:40:46 +0000564
565 if (!P.name.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000566 Result["name"] = P.name;
Jan Korousb4067012018-11-27 16:40:46 +0000567
568 if (!P.containerName.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000569 Result["containerName"] = P.containerName;
Jan Korousb4067012018-11-27 16:40:46 +0000570
571 if (!P.USR.empty())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000572 Result["usr"] = P.USR;
Jan Korousb4067012018-11-27 16:40:46 +0000573
574 if (P.ID.hasValue())
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000575 Result["id"] = P.ID.getValue().str();
Jan Korousb4067012018-11-27 16:40:46 +0000576
Jan Korous613c80d2018-11-28 10:24:07 +0000577 // Older clang cannot compile 'return Result', even though it is legal.
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000578 return llvm::json::Value(std::move(Result));
Jan Korousb4067012018-11-27 16:40:46 +0000579}
580
581llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) {
582 if (!S.containerName.empty()) {
583 O << S.containerName;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000584 llvm::StringRef ContNameRef;
Jan Korousb4067012018-11-27 16:40:46 +0000585 if (!ContNameRef.endswith("::")) {
586 O << " ";
587 }
588 }
589 O << S.name << " - " << toJSON(S);
590 return O;
591}
592
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000593bool fromJSON(const llvm::json::Value &Params, WorkspaceSymbolParams &R) {
594 llvm::json::ObjectMapper O(Params);
Marc-Andre Laperleb387b6e2018-04-23 20:00:52 +0000595 return O && O.map("query", R.query);
596}
597
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000598llvm::json::Value toJSON(const Command &C) {
599 auto Cmd = llvm::json::Object{{"title", C.title}, {"command", C.command}};
Eric Liuc5105f92018-02-16 14:15:55 +0000600 if (C.workspaceEdit)
601 Cmd["arguments"] = {*C.workspaceEdit};
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000602 if (C.tweakArgs)
603 Cmd["arguments"] = {*C.tweakArgs};
Eric Liuc5105f92018-02-16 14:15:55 +0000604 return std::move(Cmd);
605}
606
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000607const llvm::StringLiteral CodeAction::QUICKFIX_KIND = "quickfix";
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000608const llvm::StringLiteral CodeAction::REFACTOR_KIND = "refactor";
Sam McCall20841d42018-10-16 16:29:41 +0000609
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000610llvm::json::Value toJSON(const CodeAction &CA) {
611 auto CodeAction = llvm::json::Object{{"title", CA.title}};
Sam McCall20841d42018-10-16 16:29:41 +0000612 if (CA.kind)
613 CodeAction["kind"] = *CA.kind;
614 if (CA.diagnostics)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000615 CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
Sam McCall20841d42018-10-16 16:29:41 +0000616 if (CA.edit)
617 CodeAction["edit"] = *CA.edit;
618 if (CA.command)
619 CodeAction["command"] = *CA.command;
620 return std::move(CodeAction);
621}
622
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000623llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const DocumentSymbol &S) {
Ilya Biryukov19d75602018-11-23 15:21:19 +0000624 return O << S.name << " - " << toJSON(S);
625}
626
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000627llvm::json::Value toJSON(const DocumentSymbol &S) {
628 llvm::json::Object Result{{"name", S.name},
629 {"kind", static_cast<int>(S.kind)},
630 {"range", S.range},
631 {"selectionRange", S.selectionRange}};
Ilya Biryukov19d75602018-11-23 15:21:19 +0000632
633 if (!S.detail.empty())
634 Result["detail"] = S.detail;
635 if (!S.children.empty())
636 Result["children"] = S.children;
637 if (S.deprecated)
638 Result["deprecated"] = true;
Ilya Biryukov4174d092018-11-26 09:57:41 +0000639 // Older gcc cannot compile 'return Result', even though it is legal.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000640 return llvm::json::Value(std::move(Result));
Ilya Biryukov19d75602018-11-23 15:21:19 +0000641}
642
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000643llvm::json::Value toJSON(const WorkspaceEdit &WE) {
Sam McCalldd0566b2017-11-06 15:40:30 +0000644 if (!WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000645 return llvm::json::Object{};
646 llvm::json::Object FileChanges;
Sam McCalldd0566b2017-11-06 15:40:30 +0000647 for (auto &Change : *WE.changes)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000648 FileChanges[Change.first] = llvm::json::Array(Change.second);
649 return llvm::json::Object{{"changes", std::move(FileChanges)}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000650}
651
Ilya Biryukovcce67a32019-01-29 14:17:36 +0000652bool fromJSON(const llvm::json::Value &Params, TweakArgs &A) {
653 llvm::json::ObjectMapper O(Params);
654 return O && O.map("file", A.file) && O.map("selection", A.selection) &&
655 O.map("tweakID", A.tweakID);
656}
657
658llvm::json::Value toJSON(const TweakArgs &A) {
659 return llvm::json::Object{
660 {"tweakID", A.tweakID}, {"selection", A.selection}, {"file", A.file}};
661}
662
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000663llvm::json::Value toJSON(const ApplyWorkspaceEditParams &Params) {
664 return llvm::json::Object{{"edit", Params.edit}};
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000665}
666
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000667bool fromJSON(const llvm::json::Value &Params, TextDocumentPositionParams &R) {
668 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000669 return O && O.map("textDocument", R.textDocument) &&
670 O.map("position", R.position);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000671}
672
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000673bool fromJSON(const llvm::json::Value &Params, CompletionContext &R) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000674 llvm::json::ObjectMapper O(Params);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000675 if (!O)
676 return false;
677
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000678 int TriggerKind;
679 if (!O.map("triggerKind", TriggerKind))
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000680 return false;
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000681 R.triggerKind = static_cast<CompletionTriggerKind>(TriggerKind);
Ilya Biryukovb0826bd2019-01-03 13:37:12 +0000682
683 if (auto *TC = Params.getAsObject()->get("triggerCharacter"))
684 return fromJSON(*TC, R.triggerCharacter);
685 return true;
686}
687
688bool fromJSON(const llvm::json::Value &Params, CompletionParams &R) {
689 if (!fromJSON(Params, static_cast<TextDocumentPositionParams &>(R)))
690 return false;
691 if (auto *Context = Params.getAsObject()->get("context"))
692 return fromJSON(*Context, R.context);
693 return true;
694}
695
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000696static llvm::StringRef toTextKind(MarkupKind Kind) {
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000697 switch (Kind) {
698 case MarkupKind::PlainText:
699 return "plaintext";
700 case MarkupKind::Markdown:
701 return "markdown";
702 }
703 llvm_unreachable("Invalid MarkupKind");
704}
705
Ilya Biryukovf9169d02019-05-29 10:01:00 +0000706bool fromJSON(const llvm::json::Value &V, MarkupKind &K) {
707 auto Str = V.getAsString();
708 if (!Str) {
709 elog("Failed to parse markup kind: expected a string");
710 return false;
711 }
712 if (*Str == "plaintext")
713 K = MarkupKind::PlainText;
714 else if (*Str == "markdown")
715 K = MarkupKind::Markdown;
716 else {
717 elog("Unknown markup kind: {0}", *Str);
718 return false;
719 }
720 return true;
721}
722
723llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MarkupKind K) {
724 return OS << toTextKind(K);
725}
726
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000727llvm::json::Value toJSON(const MarkupContent &MC) {
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000728 if (MC.value.empty())
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000729 return nullptr;
730
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000731 return llvm::json::Object{
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000732 {"kind", toTextKind(MC.kind)},
733 {"value", MC.value},
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000734 };
735}
736
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000737llvm::json::Value toJSON(const Hover &H) {
738 llvm::json::Object Result{{"contents", toJSON(H.contents)}};
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000739
Marc-Andre Laperle373e30a2018-02-16 23:12:26 +0000740 if (H.range.hasValue())
741 Result["range"] = toJSON(*H.range);
Marc-Andre Laperle3e618ed2018-02-16 21:38:15 +0000742
743 return std::move(Result);
744}
745
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000746bool fromJSON(const llvm::json::Value &E, CompletionItemKind &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000747 if (auto T = E.getAsInteger()) {
748 if (*T < static_cast<int>(CompletionItemKind::Text) ||
749 *T > static_cast<int>(CompletionItemKind::TypeParameter))
750 return false;
751 Out = static_cast<CompletionItemKind>(*T);
752 return true;
753 }
754 return false;
755}
756
757CompletionItemKind
758adjustKindToCapability(CompletionItemKind Kind,
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000759 CompletionItemKindBitset &SupportedCompletionItemKinds) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000760 auto KindVal = static_cast<size_t>(Kind);
761 if (KindVal >= CompletionItemKindMin &&
Haojian Wuaa3ed5a2019-01-25 15:14:03 +0000762 KindVal <= SupportedCompletionItemKinds.size() &&
763 SupportedCompletionItemKinds[KindVal])
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000764 return Kind;
765
766 switch (Kind) {
767 // Provide some fall backs for common kinds that are close enough.
768 case CompletionItemKind::Folder:
769 return CompletionItemKind::File;
770 case CompletionItemKind::EnumMember:
771 return CompletionItemKind::Enum;
772 case CompletionItemKind::Struct:
773 return CompletionItemKind::Class;
774 default:
775 return CompletionItemKind::Text;
776 }
777}
778
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000779bool fromJSON(const llvm::json::Value &E, CompletionItemKindBitset &Out) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000780 if (auto *A = E.getAsArray()) {
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000781 for (size_t I = 0; I < A->size(); ++I) {
782 CompletionItemKind KindOut;
783 if (fromJSON((*A)[I], KindOut))
Sam McCallbf6a2fc2018-10-17 07:33:42 +0000784 Out.set(size_t(KindOut));
Kadir Cetinkaya133d46f2018-09-27 17:13:07 +0000785 }
786 return true;
787 }
788 return false;
789}
790
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000791llvm::json::Value toJSON(const CompletionItem &CI) {
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000792 assert(!CI.label.empty() && "completion item label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000793 llvm::json::Object Result{{"label", CI.label}};
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000794 if (CI.kind != CompletionItemKind::Missing)
Sam McCalldd0566b2017-11-06 15:40:30 +0000795 Result["kind"] = static_cast<int>(CI.kind);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000796 if (!CI.detail.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000797 Result["detail"] = CI.detail;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000798 if (!CI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000799 Result["documentation"] = CI.documentation;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000800 if (!CI.sortText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000801 Result["sortText"] = CI.sortText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000802 if (!CI.filterText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000803 Result["filterText"] = CI.filterText;
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000804 if (!CI.insertText.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000805 Result["insertText"] = CI.insertText;
806 if (CI.insertTextFormat != InsertTextFormat::Missing)
807 Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000808 if (CI.textEdit)
Sam McCalldd0566b2017-11-06 15:40:30 +0000809 Result["textEdit"] = *CI.textEdit;
810 if (!CI.additionalTextEdits.empty())
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000811 Result["additionalTextEdits"] = llvm::json::Array(CI.additionalTextEdits);
Eric Liu6df66002018-09-06 18:52:26 +0000812 if (CI.deprecated)
813 Result["deprecated"] = CI.deprecated;
Sam McCalldd0566b2017-11-06 15:40:30 +0000814 return std::move(Result);
Krasimir Georgiev6d2131a2017-04-04 09:46:39 +0000815}
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000816
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000817llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000818 O << I.label << " - " << toJSON(I);
819 return O;
820}
821
Sam McCallff8b8742017-11-30 21:32:29 +0000822bool operator<(const CompletionItem &L, const CompletionItem &R) {
Sam McCallc78ccbd2017-11-08 07:44:12 +0000823 return (L.sortText.empty() ? L.label : L.sortText) <
824 (R.sortText.empty() ? R.label : R.sortText);
825}
826
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000827llvm::json::Value toJSON(const CompletionList &L) {
828 return llvm::json::Object{
Sam McCalla40371b2017-11-15 09:16:29 +0000829 {"isIncomplete", L.isIncomplete},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000830 {"items", llvm::json::Array(L.items)},
Sam McCalla40371b2017-11-15 09:16:29 +0000831 };
832}
833
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000834llvm::json::Value toJSON(const ParameterInformation &PI) {
Simon Pilgrim5b41fe52019-06-04 11:31:45 +0000835 assert((PI.labelOffsets.hasValue() || !PI.labelString.empty()) &&
836 "parameter information label is required");
Ilya Biryukov4ef0f822019-06-04 09:36:59 +0000837 llvm::json::Object Result;
838 if (PI.labelOffsets)
839 Result["label"] =
840 llvm::json::Array({PI.labelOffsets->first, PI.labelOffsets->second});
841 else
842 Result["label"] = PI.labelString;
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000843 if (!PI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000844 Result["documentation"] = PI.documentation;
845 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000846}
847
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000848llvm::json::Value toJSON(const SignatureInformation &SI) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000849 assert(!SI.label.empty() && "signature information label is required");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000850 llvm::json::Object Result{
Sam McCalldd0566b2017-11-06 15:40:30 +0000851 {"label", SI.label},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000852 {"parameters", llvm::json::Array(SI.parameters)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000853 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000854 if (!SI.documentation.empty())
Sam McCalldd0566b2017-11-06 15:40:30 +0000855 Result["documentation"] = SI.documentation;
856 return std::move(Result);
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000857}
858
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000859llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
860 const SignatureInformation &I) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000861 O << I.label << " - " << toJSON(I);
862 return O;
863}
864
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000865llvm::json::Value toJSON(const SignatureHelp &SH) {
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000866 assert(SH.activeSignature >= 0 &&
867 "Unexpected negative value for number of active signatures.");
868 assert(SH.activeParameter >= 0 &&
869 "Unexpected negative value for active parameter index");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000870 return llvm::json::Object{
Sam McCalldd0566b2017-11-06 15:40:30 +0000871 {"activeSignature", SH.activeSignature},
872 {"activeParameter", SH.activeParameter},
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000873 {"signatures", llvm::json::Array(SH.signatures)},
Sam McCalldd0566b2017-11-06 15:40:30 +0000874 };
Ilya Biryukovd9bdfe02017-10-06 11:54:17 +0000875}
Haojian Wu345099c2017-11-09 11:30:04 +0000876
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000877bool fromJSON(const llvm::json::Value &Params, RenameParams &R) {
878 llvm::json::ObjectMapper O(Params);
Sam McCallff8b8742017-11-30 21:32:29 +0000879 return O && O.map("textDocument", R.textDocument) &&
880 O.map("position", R.position) && O.map("newName", R.newName);
Haojian Wu345099c2017-11-09 11:30:04 +0000881}
Sam McCallff8b8742017-11-30 21:32:29 +0000882
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000883llvm::json::Value toJSON(const DocumentHighlight &DH) {
884 return llvm::json::Object{
Ilya Biryukov0e6a51f2017-12-12 12:27:47 +0000885 {"range", toJSON(DH.range)},
886 {"kind", static_cast<int>(DH.kind)},
887 };
888}
889
Haojian Wub6188492018-12-20 15:39:12 +0000890llvm::json::Value toJSON(const FileStatus &FStatus) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000891 return llvm::json::Object{
Ilya Biryukov22fa4652019-01-03 13:28:05 +0000892 {"uri", FStatus.uri},
893 {"state", FStatus.state},
Haojian Wub6188492018-12-20 15:39:12 +0000894 };
895}
896
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000897llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
898 const DocumentHighlight &V) {
Marc-Andre Laperle90a937e2018-04-10 17:34:46 +0000899 O << V.range;
900 if (V.kind == DocumentHighlightKind::Read)
901 O << "(r)";
902 if (V.kind == DocumentHighlightKind::Write)
903 O << "(w)";
904 return O;
905}
906
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000907bool fromJSON(const llvm::json::Value &Params,
908 DidChangeConfigurationParams &CCP) {
909 llvm::json::ObjectMapper O(Params);
Simon Marchi5178f922018-02-22 14:00:39 +0000910 return O && O.map("settings", CCP.settings);
911}
912
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000913bool fromJSON(const llvm::json::Value &Params,
914 ClangdCompileCommand &CDbUpdate) {
915 llvm::json::ObjectMapper O(Params);
Alex Lorenzf8087862018-08-01 17:39:29 +0000916 return O && O.map("workingDirectory", CDbUpdate.workingDirectory) &&
917 O.map("compilationCommand", CDbUpdate.compilationCommand);
918}
919
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000920bool fromJSON(const llvm::json::Value &Params, ConfigurationSettings &S) {
921 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000922 if (!O)
923 return true; // 'any' type in LSP.
924 O.map("compilationDatabaseChanges", S.compilationDatabaseChanges);
925 return true;
Simon Marchi5178f922018-02-22 14:00:39 +0000926}
927
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000928bool fromJSON(const llvm::json::Value &Params, InitializationOptions &Opts) {
929 llvm::json::ObjectMapper O(Params);
Sam McCallbc904612018-10-25 04:22:52 +0000930 if (!O)
931 return true; // 'any' type in LSP.
932
933 fromJSON(Params, Opts.ConfigSettings);
934 O.map("compilationDatabasePath", Opts.compilationDatabasePath);
Sam McCall6980edb2018-11-02 14:07:51 +0000935 O.map("fallbackFlags", Opts.fallbackFlags);
Haojian Wub6188492018-12-20 15:39:12 +0000936 O.map("clangdFileStatus", Opts.FileStatus);
Sam McCallbc904612018-10-25 04:22:52 +0000937 return true;
Simon Marchiabeed662018-10-16 15:55:03 +0000938}
939
Kadir Cetinkaya86658022019-03-19 09:27:04 +0000940bool fromJSON(const llvm::json::Value &E, TypeHierarchyDirection &Out) {
941 auto T = E.getAsInteger();
942 if (!T)
943 return false;
944 if (*T < static_cast<int>(TypeHierarchyDirection::Children) ||
945 *T > static_cast<int>(TypeHierarchyDirection::Both))
946 return false;
947 Out = static_cast<TypeHierarchyDirection>(*T);
948 return true;
949}
950
951bool fromJSON(const llvm::json::Value &Params, TypeHierarchyParams &R) {
952 llvm::json::ObjectMapper O(Params);
953 return O && O.map("textDocument", R.textDocument) &&
954 O.map("position", R.position) && O.map("resolve", R.resolve) &&
955 O.map("direction", R.direction);
956}
957
958llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
959 const TypeHierarchyItem &I) {
960 return O << I.name << " - " << toJSON(I);
961}
962
963llvm::json::Value toJSON(const TypeHierarchyItem &I) {
964 llvm::json::Object Result{{"name", I.name},
965 {"kind", static_cast<int>(I.kind)},
966 {"range", I.range},
967 {"selectionRange", I.selectionRange},
968 {"uri", I.uri}};
969
970 if (I.detail)
971 Result["detail"] = I.detail;
972 if (I.deprecated)
973 Result["deprecated"] = I.deprecated;
974 if (I.parents)
975 Result["parents"] = I.parents;
976 if (I.children)
977 Result["children"] = I.children;
978 return std::move(Result);
979}
980
981bool fromJSON(const llvm::json::Value &Params, TypeHierarchyItem &I) {
982 llvm::json::ObjectMapper O(Params);
983
984 // Required fields.
985 if (!(O && O.map("name", I.name) && O.map("kind", I.kind) &&
986 O.map("uri", I.uri) && O.map("range", I.range) &&
987 O.map("selectionRange", I.selectionRange))) {
988 return false;
989 }
990
991 // Optional fields.
992 O.map("detail", I.detail);
993 O.map("deprecated", I.deprecated);
994 O.map("parents", I.parents);
995 O.map("children", I.children);
996
997 return true;
998}
999
Ilya Biryukovf2001aa2019-01-07 15:45:19 +00001000bool fromJSON(const llvm::json::Value &Params, ReferenceParams &R) {
Sam McCall1ad142f2018-09-05 11:53:07 +00001001 TextDocumentPositionParams &Base = R;
1002 return fromJSON(Params, Base);
1003}
1004
Sam McCall8b25d222019-03-28 14:37:51 +00001005static const char *toString(OffsetEncoding OE) {
Sam McCalla69698f2019-03-27 17:47:49 +00001006 switch (OE) {
Sam McCall8b25d222019-03-28 14:37:51 +00001007 case OffsetEncoding::UTF8:
1008 return "utf-8";
1009 case OffsetEncoding::UTF16:
1010 return "utf-16";
1011 case OffsetEncoding::UTF32:
1012 return "utf-32";
1013 case OffsetEncoding::UnsupportedEncoding:
1014 return "unknown";
Sam McCalla69698f2019-03-27 17:47:49 +00001015 }
Simon Pilgrim945db0b2019-03-29 13:43:00 +00001016 llvm_unreachable("Unknown clang.clangd.OffsetEncoding");
Sam McCalla69698f2019-03-27 17:47:49 +00001017}
Sam McCall8b25d222019-03-28 14:37:51 +00001018llvm::json::Value toJSON(const OffsetEncoding &OE) { return toString(OE); }
Sam McCalla69698f2019-03-27 17:47:49 +00001019bool fromJSON(const llvm::json::Value &V, OffsetEncoding &OE) {
1020 auto Str = V.getAsString();
1021 if (!Str)
1022 return false;
1023 OE = llvm::StringSwitch<OffsetEncoding>(*Str)
1024 .Case("utf-8", OffsetEncoding::UTF8)
1025 .Case("utf-16", OffsetEncoding::UTF16)
Sam McCall8b25d222019-03-28 14:37:51 +00001026 .Case("utf-32", OffsetEncoding::UTF32)
Sam McCalla69698f2019-03-27 17:47:49 +00001027 .Default(OffsetEncoding::UnsupportedEncoding);
1028 return true;
1029}
Sam McCall8b25d222019-03-28 14:37:51 +00001030llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OffsetEncoding Enc) {
1031 return OS << toString(Enc);
1032}
Sam McCalla69698f2019-03-27 17:47:49 +00001033
Sam McCallff8b8742017-11-30 21:32:29 +00001034} // namespace clangd
1035} // namespace clang