1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#ifndef LANGUAGESERVERPROTOCOL_H
6#define LANGUAGESERVERPROTOCOL_H
7
8#include "common/type/menuext.h"
9
10#include <QUrl>
11#include <QString>
12#include <QJsonValue>
13#include <QProcess>
14#include <QVector>
15#include <QVariant>
16
17#include <any>
18#include <variant>
19#include <optional>
20
21namespace lsp {
22
23extern const QString K_ID;
24extern const QString K_JSON_RPC;
25extern const QString K_METHOD;
26extern const QString K_RESULT;
27extern const QString K_PARAMS;
28extern const QString K_CAPABILITIES;
29extern const QString K_TEXTDOCUMENT;
30extern const QString K_DOCUMENTSYMBOL;
31extern const QString K_HIERARCHICALDOCUMENTSYMBOLSUPPORT;
32extern const QString K_PUBLISHDIAGNOSTICS;
33extern const QString K_RELATEDINFOMATION;
34extern const QString K_INITIALIZATIONOPTIONS;
35extern const QString K_PROCESSID;
36extern const QString K_ROOTPATH;
37extern const QString K_ROOTURI;
38extern const QString K_URI; // value QString from file url
39extern const QString K_VERSION; // value int
40extern const QString K_LANGUAGEID;
41extern const QString K_TEXT;
42extern const QString K_CONTAINERNAME;
43extern const QString K_KIND;
44extern const QString K_LOCATION;
45extern const QString K_POSITION;
46extern const QString K_DATA;
47extern const QString K_NewName;
48extern const QString K_NewText;
49
50extern const QString H_CONTENT_LENGTH;
51extern const QString V_2_0;
52extern const QString V_INITIALIZE; //has request result
53extern const QString V_SHUTDOWN; //has request result
54extern const QString V_EXIT; //has request result
55extern const QString V_TEXTDOCUMENT_DIDOPEN; //no request result
56extern const QString V_TEXTDOCUMENT_PUBLISHDIAGNOSTICS; //server call
57extern const QString V_TEXTDOCUMENT_DIDCHANGE; //no request result, json error
58extern const QString V_TEXTDOCUMENT_DOCUMENTSYMBOL; // has request result
59extern const QString V_TEXTDOCUMENT_HOVER; // has request result
60extern const QString V_TEXTDOCUMENT_RENAME;
61extern const QString V_TEXTDOCUMENT_DEFINITION ;
62extern const QString V_TEXTDOCUMENT_DIDCLOSE;
63extern const QString V_TEXTDOCUMENT_COMPLETION;
64extern const QString V_TEXTDOCUMENT_SIGNATUREHELP;
65extern const QString V_TEXTDOCUMENT_REFERENCES;
66extern const QString V_TEXTDOCUMENT_DOCUMENTHIGHLIGHT;
67extern const QString V_TEXTDOCUMENT_SEMANTICTOKENS;
68extern const QString V_TEXTDOCUMENT_SEMANTICTOKENS_FULL;
69inline static QString V_TEXTDOCUMENT_DOCUMENTCOLOR{"textDocument/documentColor"};
70inline static QString V_TEXTDOCUMENT_FORMATTING{"textDocument/formatting"};
71inline static QString V_TEXTDOCUMENT_RANGEFORMATTING{"textDocument/rangeFormatting"};
72extern const QString K_WORKSPACEFOLDERS;
73
74extern const QString K_CONTENTCHANGES;
75extern const QString K_DIAGNOSTICS;
76extern const QString K_RANGE;
77extern const QString K_MESSAGE;
78extern const QString K_SEVERITY;
79extern const QString K_END;
80extern const QString K_START;
81extern const QString K_CHARACTER;
82extern const QString K_LINE;
83extern const QString K_CONTEXT;
84extern const QString K_INCLUDEDECLARATION;
85
86extern const QString K_ERROR;
87extern const QString K_CODE;
88
89enum_def(SemanticTokenType, QString)
90{
91 enum_exp Namespace = "namespace";
92 enum_exp Type = "type";
93 enum_exp Class = "class";
94 enum_exp Enum = "enum";
95 enum_exp Interface = "interface";
96 enum_exp Struct = "struct";
97 enum_exp TypeParameter = "typeParameter";
98 enum_exp Parameter = "parameter";
99 enum_exp Variable = "variable";
100 enum_exp Property = "property";
101 enum_exp EnumMember = "enumMember";
102 enum_exp Event = "event";
103 enum_exp Function = "function";
104 enum_exp Method = "method";
105 enum_exp Macro = "macro";
106 enum_exp Keyword = "keyword";
107 enum_exp Modifier = "modifier";
108 enum_exp Comment = "comment";
109 enum_exp String = "string";
110 enum_exp Number = "number";
111 enum_exp Regexp = "regexp";
112 enum_exp Operator = "operator";
113 enum_exp Member = "member";
114};
115
116enum_def(SemanticTokenModifier, QString)
117{
118 enum_exp Declaration = "declaration"; //声明
119 enum_exp Definition = "declaration"; //定义
120 enum_exp Readonly = "readonly";
121 enum_exp Static = "static";
122 enum_exp Deprecated = "deprecated";
123 enum_exp Abstract = "abstract";
124 enum_exp Async = "async";
125 enum_exp Modification = "modification";
126 enum_exp Documentation = "documentation";
127 enum_exp DefaultLibrary = "defaultLibrary";
128};
129
130struct Position
131{
132 int line;
133 int character;
134};
135
136struct Range
137{
138 Position start;
139 Position end;
140};
141
142typedef QUrl DocumentUri;
143
144struct Location
145{
146 Range range;
147 QUrl fileUrl;
148};
149struct Locations :public QList<Location>{};
150
151struct DiagnosticRelatedInformation
152{
153 Location location;
154 QString message;
155};
156
157struct Diagnostic
158{
159 enum Severity{
160 Unkown = 0,
161 Error = 1,
162 Warning = 2,
163 Info = 3,
164 Hint = 4,
165 };
166 QString code;
167 QString message;
168 Range range;
169 QVector<DiagnosticRelatedInformation> relatedInfomation;
170 Severity severity;
171 QString source;
172};
173struct Diagnostics : QVector<Diagnostic>{};
174
175struct DiagnosticsParams
176{
177 DocumentUri uri;
178 int version;
179 Diagnostics diagnostics;
180};
181
182struct Symbol
183{
184 QString containerName;
185 int kind;
186 Location location;
187 QString name;
188};
189typedef QList<Symbol> Symbols;
190
191enum InsertTextFormat
192{
193 PlainText = 1,
194 Snippet = 2,
195};
196
197struct TextEdit
198{
199 QString newText;
200 Range range;
201};
202
203struct AdditionalTextEdits:QList<TextEdit>{};
204
205struct Documentation
206{
207 QString kind; // markdown or plaintext
208 QString value;
209};
210
211struct CompletionItem
212{
213 enum Kind {
214 Text = 1,
215 Method = 2,
216 Function = 3,
217 Constructor = 4,
218 Field = 5,
219 Variable = 6,
220 Class = 7,
221 Interface = 8,
222 Module = 9,
223 Property = 10,
224 Unit = 11,
225 Value = 12,
226 Enum = 13,
227 Keyword = 14,
228 Snippet = 15,
229 Color = 16,
230 File = 17,
231 Reference = 18,
232 Folder = 19,
233 EnumMember = 20,
234 Constant = 21,
235 Struct = 22,
236 Event = 23,
237 Operator = 24,
238 TypeParameter = 25
239 };
240 AdditionalTextEdits additionalTextEdits;
241 struct Documentation documentation;
242 QString filterText;
243 QString insertText;
244 InsertTextFormat insertTextFormat;
245 CompletionItem::Kind kind;
246 QString label;
247 double score;
248 QString sortText;
249 TextEdit textEdit;
250};
251
252struct CompletionItems : public QList<CompletionItem>{};
253
254struct CompletionProvider
255{
256 bool isIncomplete;
257 CompletionItems items;
258};
259
260struct SignatureHelp //暂时留空
261{
262
263};
264struct SignatureHelps : QList<SignatureHelp>{};
265
266struct DefinitionProvider : public Locations{};
267
268struct Contents
269{
270 QString kind;
271 QString value;
272};
273
274struct Hover //暂时留空
275{
276 Contents contents;
277 Range range;
278};
279
280struct Highlight //暂时留空
281{
282
283};
284typedef QList<Highlight> Highlights;
285
286struct Data //from result key "data"
287{
288 Position start;
289 int length;
290 int tokenType;
291 QList<int> tokenModifiers;
292};
293
294struct SemanticTokensProvider
295{
296 struct Full{bool delta;};
297 struct Legend{
298 QStringList tokenTypes;
299 QStringList tokenModifiers;
300 };
301 Full full;
302 Legend legend;
303 bool range;
304};
305
306enum TextDocumentSyncKind
307{
308 None = 0,
309 Full = 1,
310 Incremental = 2
311};
312
313struct TextDocumentSyncOptions
314{
315 bool openColse;
316 TextDocumentSyncKind change;
317};
318
319struct TextDocumentIdentifier
320{
321 QUrl documentUri;
322};
323
324struct VersionedTextDocumentIdentifier: public TextDocumentIdentifier
325{
326 int version;
327};
328
329struct TextDocumentPositionParams
330{
331 TextDocumentIdentifier textDocument;
332 Position position;
333};
334
335struct TextDocumentItem
336{
337 QUrl DocumentUri;
338 QString languageId;
339 int version;
340 QString text;
341};
342
343struct TextDocumentContentChangeEvent
344{
345 Range range;
346 int rangeLength;
347 QString text;
348};
349
350struct DidChangeTextDocumentParams
351{
352 VersionedTextDocumentIdentifier textDocument;
353 QList<TextDocumentContentChangeEvent> contentChanges;
354};
355
356struct RenameChange : public TextDocumentIdentifier
357{
358 AdditionalTextEdits edits;
359};
360
361struct References : public Locations{};
362
363namespace new_initialize{
364
365}
366
367typedef QVariant ProgressToken; // integer | string;
368struct WorkDoneProgressParams
369{
370 ProgressToken token;
371 std::any value; // any
372};
373
374struct WorkspaceEditClientCapabilities
375{
376 struct changeAnnotationSupport{
377 /**
378 * Whether the client groups edits with equal labels into tree nodes,
379 * for instance all edits labelled with "Changes in Strings" would
380 * be a tree node.
381 */
382 std::optional<bool> groupsOnLabel;
383 };
384
385 // documentChanges?: boolean;
386 std::optional<bool> documentChanges;
387 // resourceOperations?: ResourceOperationKind[];
388 std::optional<bool> resourceOperations;
389 // failureHandling?: FailureHandlingKind;
390 std::optional<int> failureHandling;
391 // normalizesLineEndings?: boolean;
392 std::optional<bool> normalizesLineEndings;
393 // changeAnnotationSupport?: {groupsOnLabel?: boolean;}
394 std::optional<changeAnnotationSupport> changeAnnotationSupport;
395};
396
397QString fromTokenType(SemanticTokenType type);
398QString fromTokenModifier(SemanticTokenModifier modifier);
399QList<int> fromTokenModifiers(int modifiers);
400QJsonArray tokenTypes();
401QJsonArray tokenModifiers();
402
403QJsonObject workspace();
404QJsonObject initialize(const QString &workspaceFolder, const QString &language, const QString &compile);
405QJsonObject didOpen(const QString &filePath);
406QJsonObject didChange(const QString &filePath, const QByteArray &text, int version);
407QJsonObject didClose(const QString &filePath);
408QJsonObject hover(const QString &filePath, const Position &pos);
409QJsonObject symbol(const QString &filePath);
410QJsonObject rename(const QString &filePath, const Position &pos, const QString &newName);
411QJsonObject completion(const QString &filePath, const Position &pos);
412QJsonObject definition(const QString &filePath, const Position &pos);
413QJsonObject signatureHelp(const QString &filePath, const Position &pos);
414QJsonObject references(const QString &filePath, const Position &pos);
415QJsonObject documentHighlight(const QString &filePath, const Position &pos);
416QJsonObject documentSemanticTokensFull(const QString &filePath);
417QJsonObject documentSemanticTokensRange(const QString &filePath);
418QJsonObject documentSemanticTokensDelta(const QString &filePath);
419QJsonObject shutdown();
420QJsonObject exit();
421bool isRequestResult(const QJsonObject &object);
422bool isRequestError(const QJsonObject &object);
423
424
425} // namespace lsp
426
427
428Q_DECLARE_METATYPE(lsp::Range)
429
430#endif // LANGUAGESERVERPROTOCOL_H
431