| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 1 | //===--- Protocol.cpp - Language Server Protocol Implementation -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the serialization code for the LSP structs. |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Protocol.h" |
| Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 15 | #include "Logger.h" |
| Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 16 | #include "URI.h" |
| Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 17 | #include "index/Index.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
| Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Hashing.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
| 21 | #include "llvm/Support/Format.h" |
| Marc-Andre Laperle | 85dcce4 | 2017-09-18 15:02:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FormatVariadic.h" |
| Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/JSON.h" |
| Krasimir Georgiev | 5011737 | 2017-04-07 11:03:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
| Ilya Biryukov | 574b753 | 2017-08-02 09:08:39 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
| Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 26 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 28 | namespace clang { |
| 29 | namespace clangd { |
| Sam McCall | 38a0491 | 2017-11-29 11:36:46 +0000 | [diff] [blame] | 30 | |
| Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 31 | char LSPError::ID; |
| 32 | |
| Eric Liu | 4d814a9 | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 33 | URIForFile URIForFile::canonicalize(StringRef AbsPath, StringRef TUPath) { |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 34 | assert(sys::path::is_absolute(AbsPath) && "the path is relative"); |
| Eric Liu | 4d814a9 | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 35 | 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 | |
| 45 | Expected<URIForFile> URIForFile::fromURI(const URI &U, StringRef HintPath) { |
| 46 | auto Resolved = URI::resolve(U, HintPath); |
| 47 | if (!Resolved) |
| 48 | return Resolved.takeError(); |
| 49 | return URIForFile(std::move(*Resolved)); |
| Ilya Biryukov | 7d60d20 | 2018-02-16 12:20:47 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 52 | bool fromJSON(const json::Value &E, URIForFile &R) { |
| 53 | if (auto S = E.getAsString()) { |
| Eric Liu | 4d814a9 | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 54 | auto Parsed = URI::parse(*S); |
| 55 | if (!Parsed) { |
| 56 | elog("Failed to parse URI {0}: {1}", *S, Parsed.takeError()); |
| Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| Eric Liu | 4d814a9 | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 59 | if (Parsed->scheme() != "file" && Parsed->scheme() != "test") { |
| Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 60 | elog("Clangd only supports 'file' URI scheme for workspace files: {0}", |
| 61 | *S); |
| Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| Eric Liu | 4d814a9 | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 64 | // "file" and "test" schemes do not require hint path. |
| 65 | auto U = URIForFile::fromURI(*Parsed, /*HintPath=*/""); |
| 66 | if (!U) { |
| 67 | elog("{0}", U.takeError()); |
| Sam McCall | 41d2152 | 2018-01-30 11:23:11 +0000 | [diff] [blame] | 68 | return false; |
| 69 | } |
| Eric Liu | 4d814a9 | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 70 | R = std::move(*U); |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 71 | return true; |
| 72 | } |
| 73 | return false; |
| Sam McCall | 38a0491 | 2017-11-29 11:36:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 76 | json::Value toJSON(const URIForFile &U) { return U.uri(); } |
| Krasimir Georgiev | 5011737 | 2017-04-07 11:03:26 +0000 | [diff] [blame] | 77 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 78 | raw_ostream &operator<<(raw_ostream &OS, const URIForFile &U) { |
| Eric Liu | 78ed91a7 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 79 | return OS << U.uri(); |
| Sam McCall | fffa822 | 2017-12-20 10:26:53 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 82 | json::Value toJSON(const TextDocumentIdentifier &R) { |
| 83 | return json::Object{{"uri", R.uri}}; |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 86 | bool fromJSON(const json::Value &Params, TextDocumentIdentifier &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 87 | json::ObjectMapper O(Params); |
| 88 | return O && O.map("uri", R.uri); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 91 | bool fromJSON(const json::Value &Params, Position &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 92 | json::ObjectMapper O(Params); |
| 93 | return O && O.map("line", R.line) && O.map("character", R.character); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 96 | json::Value toJSON(const Position &P) { |
| 97 | return json::Object{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 98 | {"line", P.line}, |
| 99 | {"character", P.character}, |
| 100 | }; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 103 | raw_ostream &operator<<(raw_ostream &OS, const Position &P) { |
| Sam McCall | fffa822 | 2017-12-20 10:26:53 +0000 | [diff] [blame] | 104 | return OS << P.line << ':' << P.character; |
| 105 | } |
| 106 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 107 | bool fromJSON(const json::Value &Params, Range &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 108 | json::ObjectMapper O(Params); |
| 109 | return O && O.map("start", R.start) && O.map("end", R.end); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 112 | json::Value toJSON(const Range &P) { |
| 113 | return json::Object{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 114 | {"start", P.start}, |
| 115 | {"end", P.end}, |
| 116 | }; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 119 | raw_ostream &operator<<(raw_ostream &OS, const Range &R) { |
| Sam McCall | fffa822 | 2017-12-20 10:26:53 +0000 | [diff] [blame] | 120 | return OS << R.start << '-' << R.end; |
| 121 | } |
| 122 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 123 | json::Value toJSON(const Location &P) { |
| 124 | return json::Object{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 125 | {"uri", P.uri}, |
| 126 | {"range", P.range}, |
| 127 | }; |
| Marc-Andre Laperle | 2cbf037 | 2017-06-28 16:12:10 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 130 | raw_ostream &operator<<(raw_ostream &OS, const Location &L) { |
| Sam McCall | fffa822 | 2017-12-20 10:26:53 +0000 | [diff] [blame] | 131 | return OS << L.range << '@' << L.uri; |
| 132 | } |
| 133 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 134 | bool fromJSON(const json::Value &Params, TextDocumentItem &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 135 | json::ObjectMapper O(Params); |
| 136 | return O && O.map("uri", R.uri) && O.map("languageId", R.languageId) && |
| 137 | O.map("version", R.version) && O.map("text", R.text); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 140 | bool fromJSON(const json::Value &Params, TextEdit &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 141 | json::ObjectMapper O(Params); |
| 142 | return O && O.map("range", R.range) && O.map("newText", R.newText); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 145 | json::Value toJSON(const TextEdit &P) { |
| 146 | return json::Object{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 147 | {"range", P.range}, |
| 148 | {"newText", P.newText}, |
| 149 | }; |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 152 | raw_ostream &operator<<(raw_ostream &OS, const TextEdit &TE) { |
| Sam McCall | 034e11a | 2018-01-25 17:29:17 +0000 | [diff] [blame] | 153 | OS << TE.range << " => \""; |
| Jonas Devlieghere | 1ab6a7a | 2018-05-31 17:36:31 +0000 | [diff] [blame] | 154 | printEscapedString(TE.newText, OS); |
| Sam McCall | 034e11a | 2018-01-25 17:29:17 +0000 | [diff] [blame] | 155 | return OS << '"'; |
| 156 | } |
| 157 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 158 | bool fromJSON(const json::Value &E, TraceLevel &Out) { |
| 159 | if (auto S = E.getAsString()) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 160 | if (*S == "off") { |
| 161 | Out = TraceLevel::Off; |
| 162 | return true; |
| 163 | } else if (*S == "messages") { |
| 164 | Out = TraceLevel::Messages; |
| 165 | return true; |
| 166 | } else if (*S == "verbose") { |
| 167 | Out = TraceLevel::Verbose; |
| 168 | return true; |
| 169 | } |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 174 | bool fromJSON(const json::Value &E, SymbolKind &Out) { |
| 175 | if (auto T = E.getAsInteger()) { |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 176 | if (*T < static_cast<int>(SymbolKind::File) || |
| 177 | *T > static_cast<int>(SymbolKind::TypeParameter)) |
| 178 | return false; |
| 179 | Out = static_cast<SymbolKind>(*T); |
| 180 | return true; |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 185 | bool fromJSON(const json::Value &E, SymbolKindBitset &Out) { |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 186 | if (auto *A = E.getAsArray()) { |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 187 | for (size_t I = 0; I < A->size(); ++I) { |
| 188 | SymbolKind KindOut; |
| 189 | if (fromJSON((*A)[I], KindOut)) |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 190 | Out.set(size_t(KindOut)); |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 197 | SymbolKind adjustKindToCapability(SymbolKind Kind, |
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 198 | SymbolKindBitset &SupportedSymbolKinds) { |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 199 | auto KindVal = static_cast<size_t>(Kind); |
| Ilya Biryukov | 74f2655 | 2018-07-26 12:05:31 +0000 | [diff] [blame] | 200 | if (KindVal >= SymbolKindMin && KindVal <= SupportedSymbolKinds.size() && |
| 201 | SupportedSymbolKinds[KindVal]) |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 202 | return Kind; |
| 203 | |
| 204 | switch (Kind) { |
| 205 | // Provide some fall backs for common kinds that are close enough. |
| 206 | case SymbolKind::Struct: |
| 207 | return SymbolKind::Class; |
| 208 | case SymbolKind::EnumMember: |
| 209 | return SymbolKind::Enum; |
| 210 | default: |
| 211 | return SymbolKind::String; |
| 212 | } |
| 213 | } |
| 214 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 215 | bool fromJSON(const json::Value &Params, ClientCapabilities &R) { |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 216 | const json::Object *O = Params.getAsObject(); |
| Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 217 | if (!O) |
| 218 | return false; |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 219 | if (auto *TextDocument = O->getObject("textDocument")) { |
| 220 | if (auto *Diagnostics = TextDocument->getObject("publishDiagnostics")) { |
| 221 | if (auto CategorySupport = Diagnostics->getBoolean("categorySupport")) |
| 222 | R.DiagnosticCategory = *CategorySupport; |
| Sam McCall | 16e7070 | 2018-10-24 07:59:38 +0000 | [diff] [blame] | 223 | if (auto CodeActions = Diagnostics->getBoolean("codeActionsInline")) |
| 224 | R.DiagnosticFixes = *CodeActions; |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 225 | } |
| 226 | if (auto *Completion = TextDocument->getObject("completion")) { |
| 227 | if (auto *Item = Completion->getObject("completionItem")) { |
| 228 | if (auto SnippetSupport = Item->getBoolean("snippetSupport")) |
| 229 | R.CompletionSnippets = *SnippetSupport; |
| 230 | } |
| 231 | if (auto *ItemKind = Completion->getObject("completionItemKind")) { |
| 232 | if (auto *ValueSet = ItemKind->get("valueSet")) { |
| 233 | R.CompletionItemKinds.emplace(); |
| 234 | if (!fromJSON(*ValueSet, *R.CompletionItemKinds)) |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | if (auto *CodeAction = TextDocument->getObject("codeAction")) { |
| 240 | if (CodeAction->getObject("codeActionLiteralSupport")) |
| 241 | R.CodeActionStructure = true; |
| 242 | } |
| Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 243 | if (auto *DocumentSymbol = TextDocument->getObject("documentSymbol")) { |
| 244 | if (auto HierarchicalSupport = |
| 245 | DocumentSymbol->getBoolean("hierarchicalDocumentSymbolSupport")) |
| 246 | R.HierarchicalDocumentSymbol = *HierarchicalSupport; |
| 247 | } |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 248 | } |
| 249 | if (auto *Workspace = O->getObject("workspace")) { |
| 250 | if (auto *Symbol = Workspace->getObject("symbol")) { |
| 251 | if (auto *SymbolKind = Symbol->getObject("symbolKind")) { |
| 252 | if (auto *ValueSet = SymbolKind->get("valueSet")) { |
| 253 | R.WorkspaceSymbolKinds.emplace(); |
| 254 | if (!fromJSON(*ValueSet, *R.WorkspaceSymbolKinds)) |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 263 | bool fromJSON(const json::Value &Params, InitializeParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 264 | json::ObjectMapper O(Params); |
| Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 265 | if (!O) |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 266 | return false; |
| Sam McCall | 38a0491 | 2017-11-29 11:36:46 +0000 | [diff] [blame] | 267 | // We deliberately don't fail if we can't parse individual fields. |
| 268 | // Failing to handle a slightly malformed initialize would be a disaster. |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 269 | O.map("processId", R.processId); |
| 270 | O.map("rootUri", R.rootUri); |
| 271 | O.map("rootPath", R.rootPath); |
| Ilya Biryukov | 23bc73b | 2018-02-15 14:32:57 +0000 | [diff] [blame] | 272 | O.map("capabilities", R.capabilities); |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 273 | O.map("trace", R.trace); |
| Simon Marchi | 8801678 | 2018-08-01 11:28:49 +0000 | [diff] [blame] | 274 | O.map("initializationOptions", R.initializationOptions); |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 275 | return true; |
| Marc-Andre Laperle | 37de971 | 2017-09-27 15:31:17 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 278 | bool fromJSON(const json::Value &Params, DidOpenTextDocumentParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 279 | json::ObjectMapper O(Params); |
| Sam McCall | 2eb6b40 | 2018-11-02 13:06:55 +0000 | [diff] [blame] | 280 | return O && O.map("textDocument", R.textDocument); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 283 | bool fromJSON(const json::Value &Params, DidCloseTextDocumentParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 284 | json::ObjectMapper O(Params); |
| 285 | return O && O.map("textDocument", R.textDocument); |
| Krasimir Georgiev | 561ba5e | 2017-04-10 13:31:39 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 288 | bool fromJSON(const json::Value &Params, DidChangeTextDocumentParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 289 | json::ObjectMapper O(Params); |
| 290 | return O && O.map("textDocument", R.textDocument) && |
| Eric Liu | 51fed18 | 2018-02-22 18:40:39 +0000 | [diff] [blame] | 291 | O.map("contentChanges", R.contentChanges) && |
| 292 | O.map("wantDiagnostics", R.wantDiagnostics); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 295 | bool fromJSON(const json::Value &E, FileChangeType &Out) { |
| 296 | if (auto T = E.getAsInteger()) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 297 | if (*T < static_cast<int>(FileChangeType::Created) || |
| 298 | *T > static_cast<int>(FileChangeType::Deleted)) |
| 299 | return false; |
| 300 | Out = static_cast<FileChangeType>(*T); |
| 301 | return true; |
| 302 | } |
| 303 | return false; |
| Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 306 | bool fromJSON(const json::Value &Params, FileEvent &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 307 | json::ObjectMapper O(Params); |
| 308 | return O && O.map("uri", R.uri) && O.map("type", R.type); |
| Marc-Andre Laperle | bf11424 | 2017-10-02 18:00:37 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 311 | bool fromJSON(const json::Value &Params, DidChangeWatchedFilesParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 312 | json::ObjectMapper O(Params); |
| 313 | return O && O.map("changes", R.changes); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 316 | bool fromJSON(const json::Value &Params, TextDocumentContentChangeEvent &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 317 | json::ObjectMapper O(Params); |
| Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 318 | return O && O.map("range", R.range) && O.map("rangeLength", R.rangeLength) && |
| 319 | O.map("text", R.text); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 322 | bool fromJSON(const json::Value &Params, FormattingOptions &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 323 | json::ObjectMapper O(Params); |
| 324 | return O && O.map("tabSize", R.tabSize) && |
| 325 | O.map("insertSpaces", R.insertSpaces); |
| 326 | } |
| 327 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 328 | json::Value toJSON(const FormattingOptions &P) { |
| 329 | return json::Object{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 330 | {"tabSize", P.tabSize}, |
| 331 | {"insertSpaces", P.insertSpaces}, |
| 332 | }; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 335 | bool fromJSON(const json::Value &Params, DocumentRangeFormattingParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 336 | json::ObjectMapper O(Params); |
| 337 | return O && O.map("textDocument", R.textDocument) && |
| 338 | O.map("range", R.range) && O.map("options", R.options); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 341 | bool fromJSON(const json::Value &Params, DocumentOnTypeFormattingParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 342 | json::ObjectMapper O(Params); |
| 343 | return O && O.map("textDocument", R.textDocument) && |
| 344 | O.map("position", R.position) && O.map("ch", R.ch) && |
| 345 | O.map("options", R.options); |
| Krasimir Georgiev | 1b8bfd4 | 2017-02-16 10:49:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 348 | bool fromJSON(const json::Value &Params, DocumentFormattingParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 349 | json::ObjectMapper O(Params); |
| 350 | return O && O.map("textDocument", R.textDocument) && |
| 351 | O.map("options", R.options); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 352 | } |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 353 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 354 | bool fromJSON(const json::Value &Params, DocumentSymbolParams &R) { |
| Marc-Andre Laperle | 1be6970 | 2018-07-05 19:35:01 +0000 | [diff] [blame] | 355 | json::ObjectMapper O(Params); |
| 356 | return O && O.map("textDocument", R.textDocument); |
| 357 | } |
| 358 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 359 | json::Value toJSON(const Diagnostic &D) { |
| Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 360 | json::Object Diag{ |
| 361 | {"range", D.range}, |
| 362 | {"severity", D.severity}, |
| 363 | {"message", D.message}, |
| 364 | }; |
| Sam McCall | 16e7070 | 2018-10-24 07:59:38 +0000 | [diff] [blame] | 365 | if (D.category) |
| 366 | Diag["category"] = *D.category; |
| 367 | if (D.codeActions) |
| 368 | Diag["codeActions"] = D.codeActions; |
| Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 369 | return std::move(Diag); |
| 370 | } |
| 371 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 372 | bool fromJSON(const json::Value &Params, Diagnostic &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 373 | json::ObjectMapper O(Params); |
| 374 | if (!O || !O.map("range", R.range) || !O.map("message", R.message)) |
| 375 | return false; |
| 376 | O.map("severity", R.severity); |
| Sam McCall | 16e7070 | 2018-10-24 07:59:38 +0000 | [diff] [blame] | 377 | O.map("category", R.category); |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 378 | return true; |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 381 | bool fromJSON(const json::Value &Params, CodeActionContext &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 382 | json::ObjectMapper O(Params); |
| 383 | return O && O.map("diagnostics", R.diagnostics); |
| Benjamin Kramer | f0af3e6 | 2017-03-01 16:16:29 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 386 | raw_ostream &operator<<(raw_ostream &OS, const Diagnostic &D) { |
| Sam McCall | 034e11a | 2018-01-25 17:29:17 +0000 | [diff] [blame] | 387 | OS << D.range << " ["; |
| 388 | switch (D.severity) { |
| 389 | case 1: |
| 390 | OS << "error"; |
| 391 | break; |
| 392 | case 2: |
| 393 | OS << "warning"; |
| 394 | break; |
| 395 | case 3: |
| 396 | OS << "note"; |
| 397 | break; |
| 398 | case 4: |
| 399 | OS << "remark"; |
| 400 | break; |
| 401 | default: |
| 402 | OS << "diagnostic"; |
| 403 | break; |
| 404 | } |
| 405 | return OS << '(' << D.severity << "): " << D.message << "]"; |
| 406 | } |
| 407 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 408 | bool fromJSON(const json::Value &Params, CodeActionParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 409 | json::ObjectMapper O(Params); |
| 410 | return O && O.map("textDocument", R.textDocument) && |
| 411 | O.map("range", R.range) && O.map("context", R.context); |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 414 | bool fromJSON(const json::Value &Params, WorkspaceEdit &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 415 | json::ObjectMapper O(Params); |
| 416 | return O && O.map("changes", R.changes); |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 419 | const StringLiteral ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND = |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 420 | "clangd.applyFix"; |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 421 | bool fromJSON(const json::Value &Params, ExecuteCommandParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 422 | json::ObjectMapper O(Params); |
| 423 | if (!O || !O.map("command", R.command)) |
| 424 | return false; |
| Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 425 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 426 | auto Args = Params.getAsObject()->getArray("arguments"); |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 427 | if (R.command == ExecuteCommandParams::CLANGD_APPLY_FIX_COMMAND) { |
| 428 | return Args && Args->size() == 1 && |
| 429 | fromJSON(Args->front(), R.workspaceEdit); |
| 430 | } |
| 431 | return false; // Unrecognized command. |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 434 | json::Value toJSON(const SymbolInformation &P) { |
| 435 | return json::Object{ |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 436 | {"name", P.name}, |
| 437 | {"kind", static_cast<int>(P.kind)}, |
| 438 | {"location", P.location}, |
| 439 | {"containerName", P.containerName}, |
| 440 | }; |
| 441 | } |
| 442 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 443 | raw_ostream &operator<<(raw_ostream &O, const SymbolInformation &SI) { |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 444 | O << SI.containerName << "::" << SI.name << " - " << toJSON(SI); |
| 445 | return O; |
| 446 | } |
| 447 | |
| Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 448 | bool operator==(const SymbolDetails &LHS, const SymbolDetails &RHS) { |
| 449 | return LHS.name == RHS.name && LHS.containerName == RHS.containerName && |
| 450 | LHS.USR == RHS.USR && LHS.ID == RHS.ID; |
| 451 | } |
| 452 | |
| 453 | llvm::json::Value toJSON(const SymbolDetails &P) { |
| 454 | json::Object result{{"name", llvm::json::Value(nullptr)}, |
| 455 | {"containerName", llvm::json::Value(nullptr)}, |
| 456 | {"usr", llvm::json::Value(nullptr)}, |
| 457 | {"id", llvm::json::Value(nullptr)}}; |
| 458 | |
| 459 | if (!P.name.empty()) |
| 460 | result["name"] = P.name; |
| 461 | |
| 462 | if (!P.containerName.empty()) |
| 463 | result["containerName"] = P.containerName; |
| 464 | |
| 465 | if (!P.USR.empty()) |
| 466 | result["usr"] = P.USR; |
| 467 | |
| 468 | if (P.ID.hasValue()) |
| 469 | result["id"] = P.ID.getValue().str(); |
| 470 | |
| Jan Korous | 613c80d | 2018-11-28 10:24:07 +0000 | [diff] [blame] | 471 | // Older clang cannot compile 'return Result', even though it is legal. |
| 472 | return json::Value(std::move(result)); |
| Jan Korous | b406701 | 2018-11-27 16:40:46 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const SymbolDetails &S) { |
| 476 | if (!S.containerName.empty()) { |
| 477 | O << S.containerName; |
| 478 | StringRef ContNameRef; |
| 479 | if (!ContNameRef.endswith("::")) { |
| 480 | O << " "; |
| 481 | } |
| 482 | } |
| 483 | O << S.name << " - " << toJSON(S); |
| 484 | return O; |
| 485 | } |
| 486 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 487 | bool fromJSON(const json::Value &Params, WorkspaceSymbolParams &R) { |
| Marc-Andre Laperle | b387b6e | 2018-04-23 20:00:52 +0000 | [diff] [blame] | 488 | json::ObjectMapper O(Params); |
| 489 | return O && O.map("query", R.query); |
| 490 | } |
| 491 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 492 | json::Value toJSON(const Command &C) { |
| 493 | auto Cmd = json::Object{{"title", C.title}, {"command", C.command}}; |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 494 | if (C.workspaceEdit) |
| 495 | Cmd["arguments"] = {*C.workspaceEdit}; |
| Eric Liu | c5105f9 | 2018-02-16 14:15:55 +0000 | [diff] [blame] | 496 | return std::move(Cmd); |
| 497 | } |
| 498 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 499 | const StringLiteral CodeAction::QUICKFIX_KIND = "quickfix"; |
| Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 500 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 501 | json::Value toJSON(const CodeAction &CA) { |
| Sam McCall | 20841d4 | 2018-10-16 16:29:41 +0000 | [diff] [blame] | 502 | auto CodeAction = json::Object{{"title", CA.title}}; |
| 503 | if (CA.kind) |
| 504 | CodeAction["kind"] = *CA.kind; |
| 505 | if (CA.diagnostics) |
| 506 | CodeAction["diagnostics"] = json::Array(*CA.diagnostics); |
| 507 | if (CA.edit) |
| 508 | CodeAction["edit"] = *CA.edit; |
| 509 | if (CA.command) |
| 510 | CodeAction["command"] = *CA.command; |
| 511 | return std::move(CodeAction); |
| 512 | } |
| 513 | |
| Ilya Biryukov | 4174d09 | 2018-11-26 09:57:41 +0000 | [diff] [blame] | 514 | raw_ostream &operator<<(raw_ostream &O, const DocumentSymbol &S) { |
| Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 515 | return O << S.name << " - " << toJSON(S); |
| 516 | } |
| 517 | |
| Ilya Biryukov | 4174d09 | 2018-11-26 09:57:41 +0000 | [diff] [blame] | 518 | json::Value toJSON(const DocumentSymbol &S) { |
| Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 519 | json::Object Result{{"name", S.name}, |
| 520 | {"kind", static_cast<int>(S.kind)}, |
| 521 | {"range", S.range}, |
| 522 | {"selectionRange", S.selectionRange}}; |
| 523 | |
| 524 | if (!S.detail.empty()) |
| 525 | Result["detail"] = S.detail; |
| 526 | if (!S.children.empty()) |
| 527 | Result["children"] = S.children; |
| 528 | if (S.deprecated) |
| 529 | Result["deprecated"] = true; |
| Ilya Biryukov | 4174d09 | 2018-11-26 09:57:41 +0000 | [diff] [blame] | 530 | // Older gcc cannot compile 'return Result', even though it is legal. |
| 531 | return json::Value(std::move(Result)); |
| Ilya Biryukov | 19d7560 | 2018-11-23 15:21:19 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 534 | json::Value toJSON(const WorkspaceEdit &WE) { |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 535 | if (!WE.changes) |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 536 | return json::Object{}; |
| 537 | json::Object FileChanges; |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 538 | for (auto &Change : *WE.changes) |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 539 | FileChanges[Change.first] = json::Array(Change.second); |
| 540 | return json::Object{{"changes", std::move(FileChanges)}}; |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 543 | json::Value toJSON(const ApplyWorkspaceEditParams &Params) { |
| 544 | return json::Object{{"edit", Params.edit}}; |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 547 | bool fromJSON(const json::Value &Params, TextDocumentPositionParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 548 | json::ObjectMapper O(Params); |
| 549 | return O && O.map("textDocument", R.textDocument) && |
| 550 | O.map("position", R.position); |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 553 | static StringRef toTextKind(MarkupKind Kind) { |
| 554 | switch (Kind) { |
| 555 | case MarkupKind::PlainText: |
| 556 | return "plaintext"; |
| 557 | case MarkupKind::Markdown: |
| 558 | return "markdown"; |
| 559 | } |
| 560 | llvm_unreachable("Invalid MarkupKind"); |
| 561 | } |
| 562 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 563 | json::Value toJSON(const MarkupContent &MC) { |
| Marc-Andre Laperle | 373e30a | 2018-02-16 23:12:26 +0000 | [diff] [blame] | 564 | if (MC.value.empty()) |
| Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 565 | return nullptr; |
| 566 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 567 | return json::Object{ |
| Marc-Andre Laperle | 373e30a | 2018-02-16 23:12:26 +0000 | [diff] [blame] | 568 | {"kind", toTextKind(MC.kind)}, |
| 569 | {"value", MC.value}, |
| Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 570 | }; |
| 571 | } |
| 572 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 573 | json::Value toJSON(const Hover &H) { |
| 574 | json::Object Result{{"contents", toJSON(H.contents)}}; |
| Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 575 | |
| Marc-Andre Laperle | 373e30a | 2018-02-16 23:12:26 +0000 | [diff] [blame] | 576 | if (H.range.hasValue()) |
| 577 | Result["range"] = toJSON(*H.range); |
| Marc-Andre Laperle | 3e618ed | 2018-02-16 21:38:15 +0000 | [diff] [blame] | 578 | |
| 579 | return std::move(Result); |
| 580 | } |
| 581 | |
| Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 582 | bool fromJSON(const json::Value &E, CompletionItemKind &Out) { |
| 583 | if (auto T = E.getAsInteger()) { |
| 584 | if (*T < static_cast<int>(CompletionItemKind::Text) || |
| 585 | *T > static_cast<int>(CompletionItemKind::TypeParameter)) |
| 586 | return false; |
| 587 | Out = static_cast<CompletionItemKind>(*T); |
| 588 | return true; |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | CompletionItemKind |
| 594 | adjustKindToCapability(CompletionItemKind Kind, |
| 595 | CompletionItemKindBitset &supportedCompletionItemKinds) { |
| 596 | auto KindVal = static_cast<size_t>(Kind); |
| 597 | if (KindVal >= CompletionItemKindMin && |
| 598 | KindVal <= supportedCompletionItemKinds.size() && |
| 599 | supportedCompletionItemKinds[KindVal]) |
| 600 | return Kind; |
| 601 | |
| 602 | switch (Kind) { |
| 603 | // Provide some fall backs for common kinds that are close enough. |
| 604 | case CompletionItemKind::Folder: |
| 605 | return CompletionItemKind::File; |
| 606 | case CompletionItemKind::EnumMember: |
| 607 | return CompletionItemKind::Enum; |
| 608 | case CompletionItemKind::Struct: |
| 609 | return CompletionItemKind::Class; |
| 610 | default: |
| 611 | return CompletionItemKind::Text; |
| 612 | } |
| 613 | } |
| 614 | |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 615 | bool fromJSON(const json::Value &E, CompletionItemKindBitset &Out) { |
| Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 616 | if (auto *A = E.getAsArray()) { |
| Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 617 | for (size_t I = 0; I < A->size(); ++I) { |
| 618 | CompletionItemKind KindOut; |
| 619 | if (fromJSON((*A)[I], KindOut)) |
| Sam McCall | bf6a2fc | 2018-10-17 07:33:42 +0000 | [diff] [blame] | 620 | Out.set(size_t(KindOut)); |
| Kadir Cetinkaya | 133d46f | 2018-09-27 17:13:07 +0000 | [diff] [blame] | 621 | } |
| 622 | return true; |
| 623 | } |
| 624 | return false; |
| 625 | } |
| 626 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 627 | json::Value toJSON(const CompletionItem &CI) { |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 628 | assert(!CI.label.empty() && "completion item label is required"); |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 629 | json::Object Result{{"label", CI.label}}; |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 630 | if (CI.kind != CompletionItemKind::Missing) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 631 | Result["kind"] = static_cast<int>(CI.kind); |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 632 | if (!CI.detail.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 633 | Result["detail"] = CI.detail; |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 634 | if (!CI.documentation.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 635 | Result["documentation"] = CI.documentation; |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 636 | if (!CI.sortText.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 637 | Result["sortText"] = CI.sortText; |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 638 | if (!CI.filterText.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 639 | Result["filterText"] = CI.filterText; |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 640 | if (!CI.insertText.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 641 | Result["insertText"] = CI.insertText; |
| 642 | if (CI.insertTextFormat != InsertTextFormat::Missing) |
| 643 | Result["insertTextFormat"] = static_cast<int>(CI.insertTextFormat); |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 644 | if (CI.textEdit) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 645 | Result["textEdit"] = *CI.textEdit; |
| 646 | if (!CI.additionalTextEdits.empty()) |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 647 | Result["additionalTextEdits"] = json::Array(CI.additionalTextEdits); |
| Eric Liu | 6df6600 | 2018-09-06 18:52:26 +0000 | [diff] [blame] | 648 | if (CI.deprecated) |
| 649 | Result["deprecated"] = CI.deprecated; |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 650 | return std::move(Result); |
| Krasimir Georgiev | 6d2131a | 2017-04-04 09:46:39 +0000 | [diff] [blame] | 651 | } |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 652 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 653 | raw_ostream &operator<<(raw_ostream &O, const CompletionItem &I) { |
| Marc-Andre Laperle | 90a937e | 2018-04-10 17:34:46 +0000 | [diff] [blame] | 654 | O << I.label << " - " << toJSON(I); |
| 655 | return O; |
| 656 | } |
| 657 | |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 658 | bool operator<(const CompletionItem &L, const CompletionItem &R) { |
| Sam McCall | c78ccbd | 2017-11-08 07:44:12 +0000 | [diff] [blame] | 659 | return (L.sortText.empty() ? L.label : L.sortText) < |
| 660 | (R.sortText.empty() ? R.label : R.sortText); |
| 661 | } |
| 662 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 663 | json::Value toJSON(const CompletionList &L) { |
| 664 | return json::Object{ |
| Sam McCall | a40371b | 2017-11-15 09:16:29 +0000 | [diff] [blame] | 665 | {"isIncomplete", L.isIncomplete}, |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 666 | {"items", json::Array(L.items)}, |
| Sam McCall | a40371b | 2017-11-15 09:16:29 +0000 | [diff] [blame] | 667 | }; |
| 668 | } |
| 669 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 670 | json::Value toJSON(const ParameterInformation &PI) { |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 671 | assert(!PI.label.empty() && "parameter information label is required"); |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 672 | json::Object Result{{"label", PI.label}}; |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 673 | if (!PI.documentation.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 674 | Result["documentation"] = PI.documentation; |
| 675 | return std::move(Result); |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 678 | json::Value toJSON(const SignatureInformation &SI) { |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 679 | assert(!SI.label.empty() && "signature information label is required"); |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 680 | json::Object Result{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 681 | {"label", SI.label}, |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 682 | {"parameters", json::Array(SI.parameters)}, |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 683 | }; |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 684 | if (!SI.documentation.empty()) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 685 | Result["documentation"] = SI.documentation; |
| 686 | return std::move(Result); |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 689 | raw_ostream &operator<<(raw_ostream &O, const SignatureInformation &I) { |
| Marc-Andre Laperle | 90a937e | 2018-04-10 17:34:46 +0000 | [diff] [blame] | 690 | O << I.label << " - " << toJSON(I); |
| 691 | return O; |
| 692 | } |
| 693 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 694 | json::Value toJSON(const SignatureHelp &SH) { |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 695 | assert(SH.activeSignature >= 0 && |
| 696 | "Unexpected negative value for number of active signatures."); |
| 697 | assert(SH.activeParameter >= 0 && |
| 698 | "Unexpected negative value for active parameter index"); |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 699 | return json::Object{ |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 700 | {"activeSignature", SH.activeSignature}, |
| 701 | {"activeParameter", SH.activeParameter}, |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 702 | {"signatures", json::Array(SH.signatures)}, |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 703 | }; |
| Ilya Biryukov | d9bdfe0 | 2017-10-06 11:54:17 +0000 | [diff] [blame] | 704 | } |
| Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 705 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 706 | bool fromJSON(const json::Value &Params, RenameParams &R) { |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 707 | json::ObjectMapper O(Params); |
| 708 | return O && O.map("textDocument", R.textDocument) && |
| 709 | O.map("position", R.position) && O.map("newName", R.newName); |
| Haojian Wu | 345099c | 2017-11-09 11:30:04 +0000 | [diff] [blame] | 710 | } |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 711 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 712 | json::Value toJSON(const DocumentHighlight &DH) { |
| 713 | return json::Object{ |
| Ilya Biryukov | 0e6a51f | 2017-12-12 12:27:47 +0000 | [diff] [blame] | 714 | {"range", toJSON(DH.range)}, |
| 715 | {"kind", static_cast<int>(DH.kind)}, |
| 716 | }; |
| 717 | } |
| 718 | |
| Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame^] | 719 | llvm::json::Value toJSON(const FileStatus &FStatus) { |
| 720 | return json::Object{ |
| 721 | {"uri", FStatus.uri}, {"state", FStatus.state}, |
| 722 | }; |
| 723 | } |
| 724 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 725 | raw_ostream &operator<<(raw_ostream &O, const DocumentHighlight &V) { |
| Marc-Andre Laperle | 90a937e | 2018-04-10 17:34:46 +0000 | [diff] [blame] | 726 | O << V.range; |
| 727 | if (V.kind == DocumentHighlightKind::Read) |
| 728 | O << "(r)"; |
| 729 | if (V.kind == DocumentHighlightKind::Write) |
| 730 | O << "(w)"; |
| 731 | return O; |
| 732 | } |
| 733 | |
| Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame] | 734 | bool fromJSON(const json::Value &Params, DidChangeConfigurationParams &CCP) { |
| Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 735 | json::ObjectMapper O(Params); |
| 736 | return O && O.map("settings", CCP.settings); |
| 737 | } |
| 738 | |
| Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 739 | bool fromJSON(const json::Value &Params, ClangdCompileCommand &CDbUpdate) { |
| Alex Lorenz | f808786 | 2018-08-01 17:39:29 +0000 | [diff] [blame] | 740 | json::ObjectMapper O(Params); |
| 741 | return O && O.map("workingDirectory", CDbUpdate.workingDirectory) && |
| 742 | O.map("compilationCommand", CDbUpdate.compilationCommand); |
| 743 | } |
| 744 | |
| Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 745 | bool fromJSON(const json::Value &Params, ConfigurationSettings &S) { |
| Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 746 | json::ObjectMapper O(Params); |
| Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 747 | if (!O) |
| 748 | return true; // 'any' type in LSP. |
| 749 | O.map("compilationDatabaseChanges", S.compilationDatabaseChanges); |
| 750 | return true; |
| Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 753 | bool fromJSON(const json::Value &Params, InitializationOptions &Opts) { |
| Simon Marchi | abeed66 | 2018-10-16 15:55:03 +0000 | [diff] [blame] | 754 | json::ObjectMapper O(Params); |
| Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 755 | if (!O) |
| 756 | return true; // 'any' type in LSP. |
| 757 | |
| 758 | fromJSON(Params, Opts.ConfigSettings); |
| 759 | O.map("compilationDatabasePath", Opts.compilationDatabasePath); |
| Sam McCall | 6980edb | 2018-11-02 14:07:51 +0000 | [diff] [blame] | 760 | O.map("fallbackFlags", Opts.fallbackFlags); |
| Haojian Wu | b618849 | 2018-12-20 15:39:12 +0000 | [diff] [blame^] | 761 | O.map("clangdFileStatus", Opts.FileStatus); |
| Sam McCall | bc90461 | 2018-10-25 04:22:52 +0000 | [diff] [blame] | 762 | return true; |
| Simon Marchi | abeed66 | 2018-10-16 15:55:03 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| Sam McCall | 1ad142f | 2018-09-05 11:53:07 +0000 | [diff] [blame] | 765 | bool fromJSON(const json::Value &Params, ReferenceParams &R) { |
| 766 | TextDocumentPositionParams &Base = R; |
| 767 | return fromJSON(Params, Base); |
| 768 | } |
| 769 | |
| Sam McCall | ff8b874 | 2017-11-30 21:32:29 +0000 | [diff] [blame] | 770 | } // namespace clangd |
| 771 | } // namespace clang |