hoshi-lang dev
Yet another programming language
Loading...
Searching...
No Matches
protocol.h
Go to the documentation of this file.
1//
2// LSP Protocol Type Definitions for hoshi-lang
3//
4
5#ifndef HOSHI_LANG_LSP_PROTOCOL_H
6#define HOSHI_LANG_LSP_PROTOCOL_H
7
8#include <share/json.hpp>
9#include <string>
10#include <vector>
11#include <optional>
12
13namespace lsp {
14
15using json = nlohmann::json;
16
17// ============================================================
18// Base JSON-RPC types
19// ============================================================
20
21// ============================================================
22// Position & Range
23// ============================================================
24
25struct Position {
26 int line = 0; // 0-based
27 int character = 0; // 0-based
28};
29
30inline void to_json(json &j, const Position &p) {
31 j = json{{"line", p.line}, {"character", p.character}};
32}
33
34inline void from_json(const json &j, Position &p) {
35 j.at("line").get_to(p.line);
36 j.at("character").get_to(p.character);
37}
38
43
44inline void to_json(json &j, const Range &r) {
45 j = json{{"start", r.start}, {"end", r.end}};
46}
47
48inline void from_json(const json &j, Range &r) {
49 j.at("start").get_to(r.start);
50 j.at("end").get_to(r.end);
51}
52
53struct Location {
54 std::string uri;
56};
57
58inline void to_json(json &j, const Location &l) {
59 j = json{{"uri", l.uri}, {"range", l.range}};
60}
61
62inline void from_json(const json &j, Location &l) {
63 j.at("uri").get_to(l.uri);
64 j.at("range").get_to(l.range);
65}
66
67// ============================================================
68// Text Document types
69// ============================================================
70
72 std::string uri;
73};
74
75inline void to_json(json &j, const TextDocumentIdentifier &t) {
76 j = json{{"uri", t.uri}};
77}
78
79inline void from_json(const json &j, TextDocumentIdentifier &t) {
80 j.at("uri").get_to(t.uri);
81}
82
84 std::string uri;
85 int version = 0;
86};
87
88inline void to_json(json &j, const VersionedTextDocumentIdentifier &t) {
89 j = json{{"uri", t.uri}, {"version", t.version}};
90}
91
93 j.at("uri").get_to(t.uri);
94 j.at("version").get_to(t.version);
95}
96
98 std::string uri;
99 std::string languageId;
101 std::string text;
102};
103
104inline void from_json(const json &j, TextDocumentItem &t) {
105 j.at("uri").get_to(t.uri);
106 j.at("languageId").get_to(t.languageId);
107 j.at("version").get_to(t.version);
108 j.at("text").get_to(t.text);
109}
110
112 std::optional<Range> range;
113 std::optional<int> rangeLength;
114 std::string text;
115};
116
118 if (j.contains("range") && !j["range"].is_null())
119 t.range = j["range"].get<Range>();
120 if (j.contains("rangeLength") && !j["rangeLength"].is_null())
121 t.rangeLength = j["rangeLength"].get<int>();
122 j.at("text").get_to(t.text);
123}
124
125// ============================================================
126// DidOpen / DidChange / DidClose Params
127// ============================================================
128
132
133inline void from_json(const json &j, DidOpenTextDocumentParams &p) {
134 j.at("textDocument").get_to(p.textDocument);
135}
136
139 std::vector<TextDocumentContentChangeEvent> contentChanges;
140};
141
142inline void from_json(const json &j, DidChangeTextDocumentParams &p) {
143 j.at("textDocument").get_to(p.textDocument);
144 j.at("contentChanges").get_to(p.contentChanges);
145}
146
150
151inline void from_json(const json &j, DidCloseTextDocumentParams &p) {
152 j.at("textDocument").get_to(p.textDocument);
153}
154
155// ============================================================
156// Completion types
157// ============================================================
158
163
164inline void from_json(const json &j, CompletionParams &p) {
165 j.at("textDocument").get_to(p.textDocument);
166 j.at("position").get_to(p.position);
167}
168
170 Text = 1,
171 Method = 2,
172 Function = 3,
173 Constructor = 4,
174 Field = 5,
175 Variable = 6,
176 Class = 7,
177 Interface = 8,
178 Module = 9,
179 Property = 10,
180 Unit = 11,
181 Value = 12,
182 Enum = 13,
183 Keyword = 14,
184 Snippet = 15,
185 Color = 16,
186 File = 17,
187 Reference = 18,
188 Folder = 19,
189 EnumMember = 20,
190 Constant = 21,
191 Struct = 22,
192 Event = 23,
193 Operator = 24,
194 TypeParameter = 25,
195};
196
198 std::string label;
200 std::optional<std::string> detail;
201 std::optional<std::string> documentation;
202 std::optional<std::string> insertText;
203 std::optional<std::string> sortText;
204};
205
206inline void to_json(json &j, const CompletionItem &c) {
207 j = json{{"label", c.label}, {"kind", static_cast<int>(c.kind)}};
208 if (c.detail) j["detail"] = *c.detail;
209 if (c.documentation) j["documentation"] = *c.documentation;
210 if (c.insertText) j["insertText"] = *c.insertText;
211 if (c.sortText) j["sortText"] = *c.sortText;
212}
213
215 bool isIncomplete = false;
216 std::vector<CompletionItem> items;
217};
218
219inline void to_json(json &j, const CompletionList &cl) {
220 j = json{{"isIncomplete", cl.isIncomplete}, {"items", cl.items}};
221}
222
223// ============================================================
224// Hover types
225// ============================================================
226
231
232inline void from_json(const json &j, HoverParams &p) {
233 j.at("textDocument").get_to(p.textDocument);
234 j.at("position").get_to(p.position);
235}
236
238 std::string kind; // "markdown" or "plaintext"
239 std::string value;
240};
241
242inline void to_json(json &j, const MarkupContent &m) {
243 j = json{{"kind", m.kind}, {"value", m.value}};
244}
245
246struct Hover {
248 std::optional<Range> range;
249};
250
251inline void to_json(json &j, const Hover &h) {
252 j = json{{"contents", h.contents}};
253 if (h.range) j["range"] = *h.range;
254}
255
256// ============================================================
257// Definition types
258// ============================================================
259
264
265inline void from_json(const json &j, DefinitionParams &p) {
266 j.at("textDocument").get_to(p.textDocument);
267 j.at("position").get_to(p.position);
268}
269
270// Definition result is either a single Location or vector of Locations
271using DefinitionResult = std::vector<Location>;
272
273inline void to_json(json &j, const DefinitionResult &r) {
274 if (r.size() == 1)
275 j = r[0];
276 else
277 j = r;
278}
279
280// ============================================================
281// References types
282// ============================================================
283
289
290inline void from_json(const json &j, ReferenceParams &p) {
291 j.at("textDocument").get_to(p.textDocument);
292 j.at("position").get_to(p.position);
293 if (j.contains("context") && j["context"].contains("includeDeclaration"))
294 p.includeDeclaration = j["context"]["includeDeclaration"].get<bool>();
295}
296
297// ============================================================
298// Document Symbol types
299// ============================================================
300
301enum class SymbolKind {
302 File = 1,
303 Module = 2,
304 Namespace = 3,
305 Package = 4,
306 Class = 5,
307 Method = 6,
308 Property = 7,
309 Field = 8,
310 Constructor = 9,
311 Enum = 10,
312 Interface = 11,
313 Function = 12,
314 Variable = 13,
315 Constant = 14,
316 String = 15,
317 Number = 16,
318 Boolean = 17,
319 Array = 18,
320 Object = 19,
321 Key = 20,
322 Null = 21,
323 EnumMember = 22,
324 Struct = 23,
325 Event = 24,
326 Operator = 25,
327 TypeParameter = 26,
328};
329
331 std::string name;
332 std::string detail;
336 std::vector<DocumentSymbol> children;
337};
338
339inline void to_json(json &j, const DocumentSymbol &s) {
340 j = {
341 {"name", s.name},
342 {"detail", s.detail},
343 {"kind", static_cast<int>(s.kind)},
344 {"range", s.range},
345 {"selectionRange", s.selectionRange}
346 };
347 if (!s.children.empty())
348 j["children"] = s.children;
349}
350
351// ============================================================
352// PublishDiagnostics types
353// ============================================================
354
356 Error = 1,
357 Warning = 2,
358 Information = 3,
359 Hint = 4,
360};
361
368
369inline void to_json(json &j, const LspDiagnostic &d) {
370 j = {
371 {"range", d.range},
372 {"severity", static_cast<int>(d.severity)},
373 {"message", d.message}
374 };
375 if (d.source) j["source"] = *d.source;
376}
377
379 std::string uri;
380 int version = 0;
381 std::vector<LspDiagnostic> diagnostics;
382};
383
384inline void to_json(json &j, const PublishDiagnosticsParams &p) {
385 j = json{
386 {"uri", p.uri},
387 {"diagnostics", p.diagnostics}
388 };
389 if (p.version > 0) j["version"] = p.version;
390}
391
392// ============================================================
393// Initialize types
394// ============================================================
395
397 bool openClose = true;
398 int change = 1; // 1 = full, 2 = incremental
399 bool willSave = false;
400 bool willSaveWaitUntil = false;
401 bool didSave = false;
402};
403
404inline void to_json(json &j, const TextDocumentSyncOptions &o) {
405 j = json{
406 {"openClose", o.openClose},
407 {"change", o.change},
408 {"willSave", o.willSave},
409 {"willSaveWaitUntil", o.willSaveWaitUntil}
410 };
411}
412
414 bool resolveProvider = false;
415 std::vector<std::string> triggerCharacters = {".", ":", "\"", "/"};
416};
417
418inline void to_json(json &j, const CompletionOptions &o) {
419 j = json{
420 {"resolveProvider", o.resolveProvider},
421 {"triggerCharacters", o.triggerCharacters}
422 };
423}
424
433
434inline void to_json(json &j, const ServerCapabilities &c) {
435 j = json{
436 {"textDocumentSync", c.textDocumentSync},
437 {"completionProvider", c.completionProvider},
438 {"hoverProvider", c.hoverProvider},
439 {"definitionProvider", c.definitionProvider},
440 {"referencesProvider", c.referencesProvider},
441 {"documentSymbolProvider", c.documentSymbolProvider}
442 };
443}
444
446 std::string name = "hoshi-lsp";
447 std::string version = "0.1.0";
448};
449
450inline void to_json(json &j, const ServerInfo &s) {
451 j = json{{"name", s.name}, {"version", s.version}};
452}
453
458
459inline void to_json(json &j, const InitializeResult &r) {
460 j = json{
461 {"capabilities", r.capabilities},
462 {"serverInfo", r.serverInfo}
463 };
464}
465
466// ============================================================
467// Document Symbol params
468// ============================================================
469
471 std::string textDocument; // uri
472};
473
474inline void from_json(const json &j, DocumentSymbolParams &p) {
475 j.at("textDocument").at("uri").get_to(p.textDocument);
476}
477
478} // namespace lsp
479
480#endif // HOSHI_LANG_LSP_PROTOCOL_H
SymbolKind
Definition protocol.h:301
DiagnosticSeverity
Definition protocol.h:355
void to_json(json &j, const Position &p)
Definition protocol.h:30
nlohmann::json json
Definition protocol.h:15
CompletionItemKind
Definition protocol.h:169
void from_json(const json &j, Position &p)
Definition protocol.h:34
std::vector< Location > DefinitionResult
Definition protocol.h:271
std::optional< std::string > sortText
Definition protocol.h:203
std::string label
Definition protocol.h:198
std::optional< std::string > detail
Definition protocol.h:200
std::optional< std::string > insertText
Definition protocol.h:202
CompletionItemKind kind
Definition protocol.h:199
std::optional< std::string > documentation
Definition protocol.h:201
std::vector< CompletionItem > items
Definition protocol.h:216
std::vector< std::string > triggerCharacters
Definition protocol.h:415
TextDocumentIdentifier textDocument
Definition protocol.h:160
TextDocumentIdentifier textDocument
Definition protocol.h:261
std::vector< TextDocumentContentChangeEvent > contentChanges
Definition protocol.h:139
VersionedTextDocumentIdentifier textDocument
Definition protocol.h:138
TextDocumentIdentifier textDocument
Definition protocol.h:148
TextDocumentItem textDocument
Definition protocol.h:130
std::string detail
Definition protocol.h:332
std::string name
Definition protocol.h:331
std::vector< DocumentSymbol > children
Definition protocol.h:336
SymbolKind kind
Definition protocol.h:333
Position position
Definition protocol.h:229
TextDocumentIdentifier textDocument
Definition protocol.h:228
std::optional< Range > range
Definition protocol.h:248
MarkupContent contents
Definition protocol.h:247
ServerCapabilities capabilities
Definition protocol.h:455
ServerInfo serverInfo
Definition protocol.h:456
std::string uri
Definition protocol.h:54
Range range
Definition protocol.h:55
std::string message
Definition protocol.h:365
DiagnosticSeverity severity
Definition protocol.h:364
std::optional< std::string > source
Definition protocol.h:366
std::string value
Definition protocol.h:239
std::string kind
Definition protocol.h:238
std::vector< LspDiagnostic > diagnostics
Definition protocol.h:381
Position start
Definition protocol.h:40
Position end
Definition protocol.h:41
TextDocumentIdentifier textDocument
Definition protocol.h:285
CompletionOptions completionProvider
Definition protocol.h:427
TextDocumentSyncOptions textDocumentSync
Definition protocol.h:426
std::string version
Definition protocol.h:447
std::string name
Definition protocol.h:446
std::optional< Range > range
Definition protocol.h:112
std::optional< int > rangeLength
Definition protocol.h:113
std::string uri
Definition protocol.h:98
std::string languageId
Definition protocol.h:99