1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#ifndef BASICJSONSTRUCTURES_H
6#define BASICJSONSTRUCTURES_H
7
8#include "common/type/menuext.h"
9
10#include <any>
11#include <map>
12#include <optional>
13
14// std::optional has default set empty is ( 'value' | null )
15
16namespace newlsp {
17
18extern const QString K_ID;
19extern const QString K_JSON_RPC;
20extern const QString V_2_0;
21extern const QString K_METHOD;
22extern const QString K_RESULT;
23extern const QString K_PARAMS;
24extern const QString H_CONTENT_LENGTH;
25extern const QString H_CONTENT_TYPE;
26extern const QString H_CHARSET;
27extern const QString RK_CONTENT_LENGTH; // RegExp Key
28extern const QString RK_CONTENT_TYPE;
29extern const QString RK_CHARSET;
30
31QString methodData(int id, const QString &method, const QJsonObject &params);
32QString notificationData(const QString &method, const QJsonObject &params);
33
34template<class T>
35static bool any_contrast(const std::any &any)
36{
37 if (any.type() == std::any(T()).type()) {
38 return true;
39 }
40 return false;
41}
42
43std::string toJsonValueStr(unsigned int value);
44std::string toJsonValueStr(int value);
45std::string toJsonValueStr(bool value);
46std::string toJsonValueStr(float value);
47std::string toJsonValueStr(double value);
48std::string toJsonValueStr(const std::string &value);
49std::string toJsonValueStr(const std::vector<int> &vecInt);
50std::string toJsonValueStr(const std::vector<std::string> &vecString);
51
52namespace json
53{
54std::string addScope(const std::string &src);
55std::string delScope(const std::string &obj);
56std::string mergeObjs(const std::vector<std::string> &objs);
57std::string formatKey(const std::string &key);
58
59template<class T>
60struct KV
61{
62 std::string key;
63 T value;
64 typedef std::string type_first;
65 typedef T type_second;
66 KV() = delete;
67 KV(const std::string &key, const T &value)
68 : key(key), value(value){}
69};
70
71template <class T>
72std::string addValue(const std::string &src, const KV<T> &kv)
73{
74 std::string temp;
75 if (kv.key.empty())
76 return temp;
77
78 temp = formatKey(kv.key) + ":" + toJsonValueStr(kv.value);
79
80 if (!src.empty())
81 return src + "," + temp;
82 else
83 return temp;
84}
85
86template <class T>
87std::string addValue(const std::string &src, const KV<std::optional<T>> &kv)
88{
89 std::string ret;
90 if (kv.value) {
91 ret = addValue(src, json::KV{kv.key, kv.value.value()});
92 }
93 return ret;
94}
95
96template <class T>
97std::string addValue(const std::string &src, const KV<std::vector<T>> &kv)
98{
99 std::string temp;
100 if (kv.key.empty())
101 return temp;
102
103 if (kv.value.size() < 0)
104 return temp;
105
106 temp += "[";
107 int n = kv.value.size();
108 for (int i = 0; i < n; i++) {
109 temp += toJsonValueStr(kv.value[i]);
110 if (i < n - 1)
111 temp += ",";
112 }
113 temp += "]";
114 temp = formatKey(kv.key) + ":" + temp;
115 if (!src.empty())
116 return src + "," + temp;
117 else
118 return temp;
119}
120
121template <class T>
122std::string addValue(const std::string &src, const KV<std::optional<std::vector<T>>> &kv)
123{
124 std::string ret;
125 if (kv.value) {
126 ret = addValue(src, json::KV{kv.key, kv.value.value()});
127 }
128 return ret;
129}
130
131};
132
133namespace Enum {
134
135enum_def(ResourceOperationKind, std::string)
136{
137 enum_exp Create = "create";
138 enum_exp Rename = "rename";
139 enum_exp Delete = "delete";
140};
141
142enum_def(FailureHandlingKind, std::string)
143{
144 enum_exp Abort = "abort";
145 enum_exp Transactional = "transactional";
146 enum_exp Undo = "undo";
147 enum_exp TextOnlyTransactional = "textOnlyTransactional";
148};
149
150enum_def(SymbolKind, int)
151{
152 enum_exp File = 1;
153 enum_exp Module = 2;
154 enum_exp Namespace = 3;
155 enum_exp Package = 4;
156 enum_exp Class = 5;
157 enum_exp Method = 6;
158 enum_exp Property = 7;
159 enum_exp Field = 8;
160 enum_exp Constructor = 9;
161 enum_exp Enum = 10;
162 enum_exp Interface = 11;
163 enum_exp Function = 12;
164 enum_exp Variable = 13;
165 enum_exp Constant = 14;
166 enum_exp String = 15;
167 enum_exp Number = 16;
168 enum_exp Boolean = 17;
169 enum_exp Array = 18;
170 enum_exp Object = 19;
171 enum_exp Key = 20;
172 enum_exp Null = 21;
173 enum_exp EnumMember = 22;
174 enum_exp Struct = 23;
175 enum_exp Event = 24;
176 enum_exp Operator = 25;
177 enum_exp TypeParameter = 26;
178};
179
180enum_def(SymbolTag, int)
181{
182 enum_exp Deprecated = 1;
183};
184
185enum_def(MarkupKind, std::string)
186{
187 enum_exp Markdown = "markdown";
188 enum_exp PlainText = "plaintext";
189};
190
191enum_def(CompletionItemTag, int)
192{
193 enum_exp Deprecated = 1;
194};
195
196enum_def(InsertTextMode, int)
197{
198 enum_exp asIs = 1;
199 enum_exp adjustIndentation = 2;
200};
201
202enum_def(CompletionItemKind, int)
203{
204 enum_exp Text = 1;
205 enum_exp Method = 2;
206 enum_exp Function = 3;
207 enum_exp Constructor = 4;
208 enum_exp Field = 5;
209 enum_exp Variable = 6;
210 enum_exp Class = 7;
211 enum_exp Interface = 8;
212 enum_exp Module = 9;
213 enum_exp Property = 10;
214 enum_exp Unit = 11;
215 enum_exp Value = 12;
216 enum_exp Enum = 13;
217 enum_exp Keyword = 14;
218 enum_exp Snippet = 15;
219 enum_exp Color = 16;
220 enum_exp File = 17;
221 enum_exp Reference = 18;
222 enum_exp Folder = 19;
223 enum_exp EnumMember = 20;
224 enum_exp Constant = 21;
225 enum_exp Struct = 22;
226 enum_exp Event = 23;
227 enum_exp Operator = 24;
228 enum_exp TypeParameter = 25;
229};
230
231enum_def(CodeActionKind, std::string)
232{
233 enum_exp Empty = "";
234 enum_exp QuickFix = "quickfix";
235 enum_exp Refactor = "refactor";
236 enum_exp RefactorExtract = "refactor.extract";
237 enum_exp RefactorInline = "refactor.inline";
238 enum_exp RefactorRewrite = "refactor.rewrite";
239 enum_exp Source = "source";
240 enum_exp SourceOrganizeImports = "source.organizeImports";
241 enum_exp SourceFixAll = "source.fixAll";
242};
243
244enum_def(PrepareSupportDefaultBehavior, int)
245{
246 enum_exp Identifier = 1;
247};
248
249enum_def(DiagnosticTag, int)
250{
251 enum_exp Unnecessary = 1;
252 enum_exp Deprecated = 2;
253};
254
255enum_def(FoldingRangeKind, std::string)
256{
257 enum_exp Comment = "comment";
258 enum_exp Imports = "imports";
259 enum_exp Region = "region";
260};
261
262enum_def(TokenFormat, std::string)
263{
264 enum_exp Relative = "relative";
265};
266
267enum_def(PositionEncodingKind, std::string)
268{
269 enum_exp UTF8 = "utf-8";
270 enum_exp UTF16 = "utf-16";
271 enum_exp UTF32 = "utf-32";
272};
273
274enum_def(TraceValue, std::string)
275{
276 enum_exp Off = "off";
277 enum_exp Message = "messages";
278 enum_exp Verbose = "verbose";
279};
280
281enum_def(SemanticTokenTypes, std::string)
282{
283 enum_exp Namespace = "namespace";
284 /**
285 * Represents a generic type. Acts as a fallback for types which
286 * can"t be mapped to a specific type like class or enum.
287 */
288 enum_exp Type = "type";
289 enum_exp Class = "class";
290 enum_exp Enum = "enum";
291 enum_exp Interface = "interface";
292 enum_exp Struct = "struct";
293 enum_exp TypeParameter = "typeParameter";
294 enum_exp Parameter = "parameter";
295 enum_exp Variable = "variable";
296 enum_exp Property = "property";
297 enum_exp EnumMember = "enumMember";
298 enum_exp Event = "event";
299 enum_exp Function = "function";
300 enum_exp Method = "method";
301 enum_exp Macro = "macro";
302 enum_exp Keyword = "keyword";
303 enum_exp Modifier = "modifier";
304 enum_exp Comment = "comment";
305 enum_exp String = "string";
306 enum_exp Number = "number";
307 enum_exp Regexp = "regexp";
308 enum_exp Operator = "operator";
309 /**
310 * @since 3.17.0
311 */
312 enum_exp Decorator = "decorator";
313};
314
315
316enum_def(SemanticTokenModifiers, std::string)
317{
318 enum_exp Declaration = "declaration";
319 enum_exp Definition = "definition";
320 enum_exp Readonly = "readonly";
321 enum_exp Static = "static";
322 enum_exp Deprecated = "deprecated";
323 enum_exp Abstract = "abstract";
324 enum_exp Async = "async";
325 enum_exp Modification = "modification";
326 enum_exp Documentation = "documentation";
327 enum_exp DefaultLibrary = "defaultLibrary";
328};
329
330enum_def(Properties, std::string)
331{
332 enum_exp label_location = "label.location";
333 enum_exp edit = "edit";
334};
335
336enum_def(DiagnosticSeverity, int)
337{
338 enum_exp Error = 1;
339 enum_exp Warning = 2;
340 enum_exp Information = 3;
341 enum_exp Hint = 4;
342};
343
344enum_def(CompletionTriggerKind, int) {
345 enum_exp Invoked = 1;
346 enum_exp TriggerCharacter = 2;
347 enum_exp TriggerForIncompleteCompletions = 3;
348};
349
350enum_def(InsertTextFormat, int) {
351 enum_exp PlainText = 1;
352 enum_exp Snippet = 2;
353};
354
355enum_def(InlayHintKind, int)
356{
357 enum_exp Type = 1;
358 enum_exp Parameter = 2;
359};
360
361enum_def(MonikerKind, std::string){
362 enum_exp Import = "import";
363 enum_exp Export = "export"; // source export, conflict with native grammar
364 enum_exp Local = "local";
365};
366
367enum_def(UniquenessLevel, std::string) {
368 enum_exp document = "document";
369 enum_exp project = "project";
370 enum_exp group = "group";
371 enum_exp scheme = "scheme";
372 enum_exp global = "global";
373};
374
375enum_def(SignatureHelpTriggerKind, int)
376{
377 enum_exp Invoked = 1;
378 enum_exp TriggerCharacter = 2;
379 enum_exp ContentChange = 3;
380};
381
382enum_def(TextDocumentSaveReason, int)
383{
384 enum_exp Manual = 1;
385 enum_exp AfterDelay = 2;
386 enum_exp FocusOut = 3;
387};
388
389enum_def(TextDocumentSyncKind, int)
390{
391 enum_exp None = 0;
392 enum_exp Full = 1;
393 enum_exp Incremental = 2;
394};
395
396enum_def(NotebookCellKind, int)
397{
398 enum_exp Markup = 1;
399 enum_exp Code = 2;
400};
401
402enum_def(DocumentHighlightKind, int){
403 enum_exp Text = 1;
404 enum_exp Read = 2;
405 enum_exp Write = 3;
406};
407
408enum_def(CodeActionTriggerKind, int)
409{
410 enum_exp Invoked = 1;
411 enum_exp Automatic = 2;
412};
413
414} // BasicEnum
415
416typedef std::string DocumentUri;
417typedef std::string URI ;
418
419struct ProgressToken : std::any
420{
421 ProgressToken(int val) : std::any(val){}
422 ProgressToken(const std::string &val) : std::any(val){}
423};
424std::string toJsonValueStr(const ProgressToken &val);
425
426struct Position
427{
428 int line;
429 int character;
430 Position() = default;
431 Position(int line, int character)
432 : line(line), character(character){}
433};
434std::string toJsonValueStr(const Position &val);
435
436struct Range
437{
438 Position start;
439 Position end;
440 Range() = default;
441 Range(const Position &start, const Position &end)
442 : start(start), end(end){}
443};
444
445std::string toJsonValueStr(const Range &val);
446
447struct TextDocumentItem
448{
449 DocumentUri uri;
450 std::string languageId;
451 int version;
452 std::string text;
453};
454
455struct TextDocumentIdentifier
456{
457 DocumentUri uri;
458};
459std::string toJsonValueStr(const TextDocumentIdentifier &val);
460
461struct VersionedTextDocumentIdentifier : TextDocumentIdentifier
462{
463 int version;
464};
465std::string toJsonValueStr(const VersionedTextDocumentIdentifier &val);
466
467struct OptionalVersionedTextDocumentIdentifier : TextDocumentIdentifier
468{
469 std::optional<int> version;
470};
471std::string toJsonValueStr(const OptionalVersionedTextDocumentIdentifier &val);
472
473struct TextDocumentPositionParams
474{
475 TextDocumentIdentifier textDocument;
476 Position position;
477};
478std::string toJsonValueStr(const TextDocumentPositionParams &val);
479
480struct DocumentFilter
481{
482 std::optional<std::string> language;
483 std::optional<std::string> scheme;
484 std::optional<std::string> pattern;
485};
486std::string toJsonValueStr(const DocumentFilter &val);
487
488struct DocumentSelector: std::vector<DocumentFilter>
489{
490};
491std::string toJsonValueStr(const DocumentSelector &val);
492
493struct TextEdit
494{
495 Range range;
496 std::string newText;
497};
498std::string toJsonValueStr(const TextEdit &val);
499
500struct ChangeAnnotation
501{
502 std::string label;
503 std::optional<bool> needsConfirmation;
504 std::optional<std::string> description;
505};
506std::string toJsonValueStr(const ChangeAnnotation &val);
507
508typedef std::string ChangeAnnotationIdentifier;
509
510struct AnnotatedTextEdit : TextEdit
511{
512 ChangeAnnotationIdentifier annotationId;
513};
514std::string toJsonValueStr(const AnnotatedTextEdit &val);
515
516struct TextDocumentEdit
517{
518 struct Edits : std::vector<AnnotatedTextEdit>, std::vector<TextEdit> {
519 Edits() = default;
520 Edits(const std::vector<AnnotatedTextEdit> &val) : std::vector<AnnotatedTextEdit>(val){}
521 Edits(const std::vector<TextEdit> &val) : std::vector<TextEdit>(val){}
522 };
523 OptionalVersionedTextDocumentIdentifier textDocument;
524 Edits edits;
525};
526std::string toJsonValueStr(const TextDocumentEdit::Edits &val);
527std::string toJsonValueStr(const TextDocumentEdit &val);
528
529struct Location
530{
531 DocumentUri uri;
532 Range range;
533};
534std::string toJsonValueStr(const Location &val);
535
536struct LocationLink
537{
538 Range originSelectionRange;
539 DocumentUri targetUri;
540 Range targetRange;
541 Range targetSelectionRange;
542};
543
544struct CodeDescription
545{
546 URI href;
547};
548std::string toJsonValueStr(const CodeDescription &val);
549
550struct DiagnosticRelatedInformation
551{
552 Location location;
553 std::string message;
554};
555std::string toJsonValueStr(const DiagnosticRelatedInformation &val);
556
557struct Diagnostic
558{
559 Range range;
560 std::optional<Enum::DiagnosticSeverity::type_value> severity;
561 std::optional<std::any> code; // int or string
562 std::optional<CodeDescription> codeDescription;
563 std::optional<std::string> source;
564 std::optional<std::string> message;
565 std::optional<std::vector<Enum::DiagnosticTag::type_value>> tags;
566 std::optional<std::vector<DiagnosticRelatedInformation>> relatedInformation;
567 std::optional<std::string> data; // unknown;
568};
569std::string toJsonValueStr(const Diagnostic &val);
570
571struct Command
572{
573 std::string title;
574 std::string command;
575 std::optional<std::vector<std::string>> arguments;
576};
577std::string toJsonValueStr(const Command &val);
578
579struct MarkupContent
580{
581 Enum::MarkupKind::type_value kind;
582 std::string value;
583};
584
585struct CreateFileOptions
586{
587 std::optional<bool> overwrite;
588 std::optional<bool> ignoreIfExists;
589};
590std::string toJsonValueStr(const CreateFileOptions &val);
591
592struct CreateFile
593{
594 const std::string kind{"create"};
595 DocumentUri uri;
596 std::optional<CreateFileOptions> options;
597 std::optional<ChangeAnnotationIdentifier> annotationId;
598};
599std::string toJsonValueStr(const CreateFile &val);
600
601struct RenameFileOptions
602{
603 std::optional<bool> overwrite;
604 std::optional<bool> ignoreIfExists;
605};
606std::string toJsonValueStr(const RenameFileOptions &val);
607
608struct RenameFile
609{
610 const std::string kind{"rename"};
611 DocumentUri oldUri;
612 DocumentUri newUri;
613 std::optional<RenameFileOptions> options;
614 std::optional<ChangeAnnotationIdentifier> annotationId;
615};
616std::string toJsonValueStr(const RenameFile &val);
617
618struct DeleteFileOptions
619{
620 std::optional<bool> recursive;
621 std::optional<bool> ignoreIfNotExists;
622};
623std::string toJsonValueStr(const DeleteFileOptions &val);
624
625struct DeleteFile
626{
627 const std::string kind{"delete"};
628 DocumentUri uri;
629 std::optional<DeleteFileOptions> options;
630 std::optional<ChangeAnnotationIdentifier> annotationId;
631};
632std::string toJsonValueStr(const DeleteFile &val);;
633
634struct WorkspaceEdit
635{
636 // { [uri: DocumentUri]: TextEdit[]; };
637 struct Changes : std::map<DocumentUri, std::vector<TextEdit>> {};
638 struct ChangeAnnotations : std::map<std::string, ChangeAnnotation> {};
639 struct DocumentChanges : std::any {
640 DocumentChanges() = default;
641 DocumentChanges(const std::vector<TextDocumentEdit> &val) : std::any(val){}
642 DocumentChanges(const std::vector<CreateFile> &val) : std::any(val){}
643 DocumentChanges(const std::vector<RenameFile> &val) : std::any(val){}
644 DocumentChanges(const std::vector<DeleteFile> &val) : std::any(val){}
645 };
646 std::optional<Changes> changes;
647 // ( TextDocumentEdit[] | (TextDocumentEdit | CreateFile | RenameFile | DeleteFile)[]);
648 std::optional<DocumentChanges> documentChanges;
649 std::optional<ChangeAnnotations> changeAnnotations;
650};
651std::string toJsonValueStr(const WorkspaceEdit::Changes &val);
652std::string toJsonValueStr(const WorkspaceEdit::DocumentChanges &val);
653std::string toJsonValueStr(const WorkspaceEdit::ChangeAnnotations &val);
654std::string toJsonValueStr(const WorkspaceEdit &val);
655
656struct WorkDoneProgressBegin
657{
658 const std::string kind {"begin"};
659 std::string title;
660 std::optional<bool> boolean;
661 std::optional<std::string> message;
662 std::optional<unsigned int> percentage;
663};
664
665struct WorkDoneProgressReport
666{
667 const std::string kind {"report"};
668 std::optional<bool> cancellable;
669 std::optional<std::string> message;
670 std::optional<unsigned int> percentage;
671};
672
673struct WorkDoneProgressEnd
674{
675 const std::string kind {"end"};
676 std::optional<std::string> message;
677};
678
679struct WorkDoneProgressParams
680{
681 std::optional<ProgressToken> workDoneToken;
682};
683std::string toJsonValueStr(const WorkDoneProgressParams &params);
684
685struct WorkDoneProgressOptions
686{
687 std::optional<bool> workDoneProgress;
688};
689std::string toJsonValueStr(const WorkDoneProgressOptions &params);
690
691struct PartialResultParams
692{
693 std::optional<ProgressToken> partialResultToken;
694};
695std::string toJsonValueStr(const PartialResultParams &params);
696
697} // lsp
698
699#endif // BASICJSONSTRUCTURES_H
700