Debian Codegraph @

modules/gdscript/language_server/godot_lsp.h

raw symbols SCIP
1 /**************************************************************************/
2 /* godot_lsp.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/doc_data.h"
34 #include "core/object/class_db.h"
35 #include "core/templates/list.h"
36
37 namespace LSP {
38
39 typedef String DocumentUri;
40
41 /** Format BBCode documentation from DocData to markdown */
42 static String marked_documentation(const String &p_bbcode);
43
44 /**
45 * Text documents are identified using a URI. On the protocol level, URIs are passed as strings.
46 */
47 struct TextDocumentIdentifier {
48 /**
49 * The text document's URI.
50 */
51 DocumentUri uri;
52
53 _FORCE_INLINE_ void load(const Dictionary &p_params) {
54 uri = p_params["uri"];
55 }
56
57 _FORCE_INLINE_ Dictionary to_json() const {
58 Dictionary dict;
59 dict["uri"] = uri;
60 return dict;
61 }
62 };
63
64 /**
65 * Position in a text document expressed as zero-based line and zero-based character offset.
66 * A position is between two characters like an ‘insert’ cursor in a editor.
67 * Special values like for example -1 to denote the end of a line are not supported.
68 */
69 struct Position {
70 /**
71 * Line position in a document (zero-based).
72 */
73 int line = 0;
74
75 /**
76 * Character offset on a line in a document (zero-based). Assuming that the line is
77 * represented as a string, the `character` value represents the gap between the
78 * `character` and `character + 1`.
79 *
80 * If the character value is greater than the line length it defaults back to the
81 * line length.
82 */
83 int character = 0;
84
85 _FORCE_INLINE_ bool operator==(const Position &p_other) const {
86 return line == p_other.line && character == p_other.character;
87 }
88
89 String to_string() const {
90 return vformat("(%d,%d)", line, character);
91 }
92
93 _FORCE_INLINE_ void load(const Dictionary &p_params) {
94 line = p_params["line"];
95 character = p_params["character"];
96 }
97
98 _FORCE_INLINE_ Dictionary to_json() const {
99 Dictionary dict;
100 dict["line"] = line;
101 dict["character"] = character;
102 return dict;
103 }
104 };
105
106 /**
107 * A range in a text document expressed as (zero-based) start and end positions.
108 * A range is comparable to a selection in an editor. Therefore the end position is exclusive.
109 * If you want to specify a range that contains a line including the line ending character(s) then use an end position denoting the start of the next line.
110 */
111 struct Range {
112 /**
113 * The range's start position.
114 */
115 Position start;
116
117 /**
118 * The range's end position.
119 */
120 Position end;
121
122 _FORCE_INLINE_ bool operator==(const Range &p_other) const {
123 return start == p_other.start && end == p_other.end;
124 }
125
126 bool contains(const Position &p_pos) const {
127 // Inside line range.
128 if (start.line <= p_pos.line && p_pos.line <= end.line) {
129 // If on start line: must come after start char.
130 bool start_ok = p_pos.line == start.line ? start.character <= p_pos.character : true;
131 // If on end line: must come before end char.
132 bool end_ok = p_pos.line == end.line ? p_pos.character <= end.character : true;
133 return start_ok && end_ok;
134 } else {
135 return false;
136 }
137 }
138
139 String to_string() const {
140 return vformat("[%s:%s]", start.to_string(), end.to_string());
141 }
142
143 _FORCE_INLINE_ void load(const Dictionary &p_params) {
144 start.load(p_params["start"]);
145 end.load(p_params["end"]);
146 }
147
148 _FORCE_INLINE_ Dictionary to_json() const {
149 Dictionary dict;
150 dict["start"] = start.to_json();
151 dict["end"] = end.to_json();
152 return dict;
153 }
154 };
155
156 /**
157 * Represents a location inside a resource, such as a line inside a text file.
158 */
159 struct Location {
160 DocumentUri uri;
161 Range range;
162
163 _FORCE_INLINE_ void load(const Dictionary &p_params) {
164 uri = p_params["uri"];
165 range.load(p_params["range"]);
166 }
167
168 _FORCE_INLINE_ Dictionary to_json() const {
169 Dictionary dict;
170 dict["uri"] = uri;
171 dict["range"] = range.to_json();
172 return dict;
173 }
174 };
175
176 /**
177 * Represents a link between a source and a target location.
178 */
179 struct LocationLink {
180 /**
181 * Span of the origin of this link.
182 *
183 * Used as the underlined span for mouse interaction. Defaults to the word range at
184 * the mouse position.
185 */
186 Range *originSelectionRange = nullptr;
187
188 /**
189 * The target resource identifier of this link.
190 */
191 String targetUri;
192
193 /**
194 * The full target range of this link. If the target for example is a symbol then target range is the
195 * range enclosing this symbol not including leading/trailing whitespace but everything else
196 * like comments. This information is typically used to highlight the range in the editor.
197 */
198 Range targetRange;
199
200 /**
201 * The range that should be selected and revealed when this link is being followed, e.g the name of a function.
202 * Must be contained by the `targetRange`. See also `DocumentSymbol#range`
203 */
204 Range targetSelectionRange;
205 };
206
207 /**
208 * A parameter literal used in requests to pass a text document and a position inside that document.
209 */
210 struct TextDocumentPositionParams {
211 /**
212 * The text document.
213 */
214 TextDocumentIdentifier textDocument;
215
216 /**
217 * The position inside the text document.
218 */
219 Position position;
220
221 _FORCE_INLINE_ void load(const Dictionary &p_params) {
222 textDocument.load(p_params["textDocument"]);
223 position.load(p_params["position"]);
224 }
225
226 _FORCE_INLINE_ Dictionary to_json() const {
227 Dictionary dict;
228 dict["textDocument"] = textDocument.to_json();
229 dict["position"] = position.to_json();
230 return dict;
231 }
232 };
233
234 struct ReferenceContext {
235 /**
236 * Include the declaration of the current symbol.
237 */
238 bool includeDeclaration = false;
239 };
240
241 struct ShowMessageParams {
242 /**
243 * The message type. See {@link MessageType}.
244 */
245 int type;
246
247 /**
248 * The actual message.
249 */
250 String message;
251
252 _FORCE_INLINE_ Dictionary to_json() const {
253 Dictionary dict;
254 dict["type"] = type;
255 dict["message"] = message;
256 return dict;
257 }
258 };
259
260 struct ReferenceParams : TextDocumentPositionParams {
261 ReferenceContext context;
262 };
263
264 struct DocumentLinkParams {
265 /**
266 * The document to provide document links for.
267 */
268 TextDocumentIdentifier textDocument;
269
270 _FORCE_INLINE_ void load(const Dictionary &p_params) {
271 textDocument.load(p_params["textDocument"]);
272 }
273 };
274
275 /**
276 * A document link is a range in a text document that links to an internal or external resource, like another
277 * text document or a web site.
278 */
279 struct DocumentLink {
280 /**
281 * The range this link applies to.
282 */
283 Range range;
284
285 /**
286 * The uri this link points to. If missing a resolve request is sent later.
287 */
288 DocumentUri target;
289
290 Dictionary to_json() const {
291 Dictionary dict;
292 dict["range"] = range.to_json();
293 dict["target"] = target;
294 return dict;
295 }
296 };
297
298 /**
299 * A textual edit applicable to a text document.
300 */
301 struct TextEdit {
302 /**
303 * The range of the text document to be manipulated. To insert
304 * text into a document create a range where start === end.
305 */
306 Range range;
307
308 /**
309 * The string to be inserted. For delete operations use an
310 * empty string.
311 */
312 String newText;
313 };
314
315 /**
316 * The edits to be applied.
317 */
318 struct WorkspaceEdit {
319 /**
320 * Holds changes to existing resources.
321 */
322 HashMap<String, Vector<TextEdit>> changes;
323
324 _FORCE_INLINE_ void add_edit(const String &uri, const TextEdit &edit) {
325 if (changes.has(uri)) {
326 changes[uri].push_back(edit);
327 } else {
328 Vector<TextEdit> edits;
329 edits.push_back(edit);
330 changes[uri] = edits;
331 }
332 }
333
334 _FORCE_INLINE_ Dictionary to_json() const {
335 Dictionary dict;
336
337 Dictionary out_changes;
338 for (const KeyValue<String, Vector<TextEdit>> &E : changes) {
339 Array edits;
340 for (int i = 0; i < E.value.size(); ++i) {
341 Dictionary text_edit;
342 text_edit["range"] = E.value[i].range.to_json();
343 text_edit["newText"] = E.value[i].newText;
344 edits.push_back(text_edit);
345 }
346 out_changes[E.key] = edits;
347 }
348 dict["changes"] = out_changes;
349
350 return dict;
351 }
352
353 _FORCE_INLINE_ void add_change(const String &uri, const int &line, const int &start_character, const int &end_character, const String &new_text) {
354 TextEdit new_edit;
355 new_edit.newText = new_text;
356 new_edit.range.start.line = line;
357 new_edit.range.start.character = start_character;
358 new_edit.range.end.line = line;
359 new_edit.range.end.character = end_character;
360
361 if (HashMap<String, Vector<TextEdit>>::Iterator E = changes.find(uri)) {
362 E->value.push_back(new_edit);
363 } else {
364 Vector<TextEdit> edit_list;
365 edit_list.push_back(new_edit);
366 changes.insert(uri, edit_list);
367 }
368 }
369 };
370
371 /**
372 * Represents a reference to a command.
373 * Provides a title which will be used to represent a command in the UI.
374 * Commands are identified by a string identifier.
375 * The recommended way to handle commands is to implement their execution on the server side if the client and server provides the corresponding capabilities.
376 * Alternatively the tool extension code could handle the command. The protocol currently doesn’t specify a set of well-known commands.
377 */
378 struct Command {
379 /**
380 * Title of the command, like `save`.
381 */
382 String title;
383 /**
384 * The identifier of the actual command handler.
385 */
386 String command;
387 /**
388 * Arguments that the command handler should be
389 * invoked with.
390 */
391 Array arguments;
392
393 Dictionary to_json() const {
394 Dictionary dict;
395 dict["title"] = title;
396 dict["command"] = command;
397 if (arguments.size()) {
398 dict["arguments"] = arguments;
399 }
400 return dict;
401 }
402 };
403
404 // Use namespace instead of enumeration to follow the LSP specifications.
405 // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not.
406
407 namespace TextDocumentSyncKind {
408 /**
409 * Documents should not be synced at all.
410 */
411 static const int None = 0;
412
413 /**
414 * Documents are synced by always sending the full content
415 * of the document.
416 */
417 static const int Full = 1;
418
419 /**
420 * Documents are synced by sending the full content on open.
421 * After that only incremental updates to the document are
422 * send.
423 */
424 static const int Incremental = 2;
425 }; // namespace TextDocumentSyncKind
426
427 namespace MessageType {
428 /**
429 * An error message.
430 */
431 static const int Error = 1;
432 /**
433 * A warning message.
434 */
435 static const int Warning = 2;
436 /**
437 * An information message.
438 */
439 static const int Info = 3;
440 /**
441 * A log message.
442 */
443 static const int Log = 4;
444 }; // namespace MessageType
445
446 /**
447 * Completion options.
448 */
449 struct CompletionOptions {
450 /**
451 * The server provides support to resolve additional
452 * information for a completion item.
453 */
454 bool resolveProvider = true;
455
456 /**
457 * The characters that trigger completion automatically.
458 */
459 Vector<String> triggerCharacters;
460
461 CompletionOptions() {
462 triggerCharacters.push_back(".");
463 triggerCharacters.push_back("$");
464 triggerCharacters.push_back("'");
465 triggerCharacters.push_back("\"");
466 }
467
468 Dictionary to_json() const {
469 Dictionary dict;
470 dict["resolveProvider"] = resolveProvider;
471 dict["triggerCharacters"] = triggerCharacters;
472 return dict;
473 }
474 };
475
476 /**
477 * Signature help options.
478 */
479 struct SignatureHelpOptions {
480 /**
481 * The characters that trigger signature help
482 * automatically.
483 */
484 Vector<String> triggerCharacters;
485
486 Dictionary to_json() {
487 Dictionary dict;
488 dict["triggerCharacters"] = triggerCharacters;
489 return dict;
490 }
491 };
492
493 /**
494 * Code Lens options.
495 */
496 struct CodeLensOptions {
497 /**
498 * Code lens has a resolve provider as well.
499 */
500 bool resolveProvider = false;
501
502 Dictionary to_json() {
503 Dictionary dict;
504 dict["resolveProvider"] = resolveProvider;
505 return dict;
506 }
507 };
508
509 /**
510 * Rename options
511 */
512 struct RenameOptions {
513 /**
514 * Renames should be checked and tested before being executed.
515 */
516 bool prepareProvider = true;
517
518 Dictionary to_json() {
519 Dictionary dict;
520 dict["prepareProvider"] = prepareProvider;
521 return dict;
522 }
523 };
524
525 /**
526 * Document link options.
527 */
528 struct DocumentLinkOptions {
529 /**
530 * Document links have a resolve provider as well.
531 */
532 bool resolveProvider = false;
533
534 Dictionary to_json() {
535 Dictionary dict;
536 dict["resolveProvider"] = resolveProvider;
537 return dict;
538 }
539 };
540
541 /**
542 * Execute command options.
543 */
544 struct ExecuteCommandOptions {
545 /**
546 * The commands to be executed on the server
547 */
548 Vector<String> commands;
549
550 Dictionary to_json() {
551 Dictionary dict;
552 dict["commands"] = commands;
553 return dict;
554 }
555 };
556
557 /**
558 * Save options.
559 */
560 struct SaveOptions {
561 /**
562 * The client is supposed to include the content on save.
563 */
564 bool includeText = true;
565
566 Dictionary to_json() {
567 Dictionary dict;
568 dict["includeText"] = includeText;
569 return dict;
570 }
571 };
572
573 /**
574 * Color provider options.
575 */
576 struct ColorProviderOptions {
577 Dictionary to_json() {
578 return Dictionary();
579 }
580 };
581
582 /**
583 * Folding range provider options.
584 */
585 struct FoldingRangeProviderOptions {
586 Dictionary to_json() {
587 return Dictionary();
588 }
589 };
590
591 struct TextDocumentSyncOptions {
592 /**
593 * Open and close notifications are sent to the server. If omitted open close notification should not
594 * be sent.
595 */
596 bool openClose = true;
597
598 /**
599 * Change notifications are sent to the server. See TextDocumentSyncKind.None, TextDocumentSyncKind.Full
600 * and TextDocumentSyncKind.Incremental. If omitted it defaults to TextDocumentSyncKind.None.
601 */
602 int change = TextDocumentSyncKind::Full;
603
604 /**
605 * If present will save notifications are sent to the server. If omitted the notification should not be
606 * sent.
607 */
608 bool willSave = false;
609
610 /**
611 * If present will save wait until requests are sent to the server. If omitted the request should not be
612 * sent.
613 */
614 bool willSaveWaitUntil = true;
615
616 /**
617 * If present save notifications are sent to the server. If omitted the notification should not be
618 * sent.
619 */
620 SaveOptions save;
621
622 Dictionary to_json() {
623 Dictionary dict;
624 dict["willSaveWaitUntil"] = willSaveWaitUntil;
625 dict["willSave"] = willSave;
626 dict["openClose"] = openClose;
627 dict["change"] = change;
628 dict["save"] = save.to_json();
629 return dict;
630 }
631 };
632
633 /**
634 * Static registration options to be returned in the initialize request.
635 */
636 struct StaticRegistrationOptions {
637 /**
638 * The id used to register the request. The id can be used to deregister
639 * the request again. See also Registration#id.
640 */
641 String id;
642 };
643
644 /**
645 * Format document on type options.
646 */
647 struct DocumentOnTypeFormattingOptions {
648 /**
649 * A character on which formatting should be triggered, like `}`.
650 */
651 String firstTriggerCharacter;
652
653 /**
654 * More trigger characters.
655 */
656 Vector<String> moreTriggerCharacter;
657
658 Dictionary to_json() {
659 Dictionary dict;
660 dict["firstTriggerCharacter"] = firstTriggerCharacter;
661 dict["moreTriggerCharacter"] = moreTriggerCharacter;
662 return dict;
663 }
664 };
665
666 enum class LanguageId {
667 GDSCRIPT,
668 OTHER,
669 };
670
671 struct TextDocumentItem {
672 /**
673 * The text document's URI.
674 */
675 DocumentUri uri;
676
677 /**
678 * The text document's language identifier.
679 */
680 LanguageId languageId;
681
682 /**
683 * The version number of this document (it will increase after each
684 * change, including undo/redo).
685 */
686 int version = 0;
687
688 /**
689 * The content of the opened text document.
690 */
691 String text;
692
693 void load(const Dictionary &p_dict) {
694 uri = p_dict["uri"];
695 version = p_dict["version"];
696 text = p_dict["text"];
697
698 // Clients should use "gdscript" as language id, but we can't enforce it.
699 // We normalize some known ids to make them easier to work with:
700 // Rider < 2026.1: "gd"
701 // Kate: "godot"
702 String rawLanguageId = p_dict["languageId"];
703 if (rawLanguageId == "gdscript" || rawLanguageId == "gd" || rawLanguageId == "godot") {
704 languageId = LanguageId::GDSCRIPT;
705 } else {
706 languageId = LanguageId::OTHER;
707 }
708 }
709 };
710
711 /**
712 * An event describing a change to a text document.
713 */
714 struct TextDocumentContentChangeEvent {
715 /**
716 * The new text of the range/document.
717 */
718 String text;
719
720 void load(const Dictionary &p_params) {
721 text = p_params["text"];
722 }
723 };
724
725 // Use namespace instead of enumeration to follow the LSP specifications
726 namespace DiagnosticSeverity {
727 /**
728 * Reports an error.
729 */
730 static const int Error = 1;
731 /**
732 * Reports a warning.
733 */
734 static const int Warning = 2;
735 /**
736 * Reports an information.
737 */
738 static const int Information = 3;
739 /**
740 * Reports a hint.
741 */
742 static const int Hint = 4;
743 }; // namespace DiagnosticSeverity
744
745 /**
746 * Represents a related message and source code location for a diagnostic. This should be
747 * used to point to code locations that cause or related to a diagnostics, e.g when duplicating
748 * a symbol in a scope.
749 */
750 struct DiagnosticRelatedInformation {
751 /**
752 * The location of this related diagnostic information.
753 */
754 Location location;
755
756 /**
757 * The message of this related diagnostic information.
758 */
759 String message;
760
761 Dictionary to_json() const {
762 Dictionary dict;
763 dict["location"] = location.to_json();
764 dict["message"] = message;
765 return dict;
766 }
767 };
768
769 /**
770 * Represents a diagnostic, such as a compiler error or warning.
771 * Diagnostic objects are only valid in the scope of a resource.
772 */
773 struct Diagnostic {
774 /**
775 * The range at which the message applies.
776 */
777 Range range;
778
779 /**
780 * The diagnostic's severity. Can be omitted. If omitted it is up to the
781 * client to interpret diagnostics as error, warning, info or hint.
782 */
783 int severity = 0;
784
785 /**
786 * The diagnostic's code, which might appear in the user interface.
787 */
788 int code = 0;
789
790 /**
791 * A human-readable string describing the source of this
792 * diagnostic, e.g. 'typescript' or 'super lint'.
793 */
794 String source;
795
796 /**
797 * The diagnostic's message.
798 */
799 String message;
800
801 /**
802 * An array of related diagnostic information, e.g. when symbol-names within
803 * a scope collide all definitions can be marked via this property.
804 */
805 Vector<DiagnosticRelatedInformation> relatedInformation;
806
807 Dictionary to_json() const {
808 Dictionary dict;
809 dict["range"] = range.to_json();
810 dict["code"] = code;
811 dict["severity"] = severity;
812 dict["message"] = message;
813 dict["source"] = source;
814 if (!relatedInformation.is_empty()) {
815 Array arr;
816 arr.resize(relatedInformation.size());
817 for (int i = 0; i < relatedInformation.size(); i++) {
818 arr[i] = relatedInformation[i].to_json();
819 }
820 dict["relatedInformation"] = arr;
821 }
822 return dict;
823 }
824 };
825
826 // Use namespace instead of enumeration to follow the LSP specifications
827 /**
828 * Describes the content type that a client supports in various
829 * result literals like `Hover`, `ParameterInfo` or `CompletionItem`.
830 *
831 * Please note that `MarkupKinds` must not start with a `$`. This kinds
832 * are reserved for internal usage.
833 */
834 namespace MarkupKind {
835 static const String PlainText = "plaintext";
836 static const String Markdown = "markdown";
837 }; // namespace MarkupKind
838
839 /**
840 * A `MarkupContent` literal represents a string value which content is interpreted base on its
841 * kind flag. Currently the protocol supports `plaintext` and `markdown` as markup kinds.
842 *
843 * If the kind is `markdown` then the value can contain fenced code blocks like in GitHub issues.
844 * See https://help.github.com/articles/creating-and-highlighting-code-blocks/#syntax-highlighting
845 *
846 * Here is an example how such a string can be constructed using JavaScript / TypeScript:
847 * ```typescript
848 * let markdown: MarkdownContent = {
849 * kind: MarkupKind.Markdown,
850 * value: [
851 * '# Header',
852 * 'Some text',
853 * '```typescript',
854 * 'someCode();',
855 * '```'
856 * ].join('\n')
857 * };
858 * ```
859 *
860 * *Please Note* that clients might sanitize the return markdown. A client could decide to
861 * remove HTML from the markdown to avoid script execution.
862 */
863 struct MarkupContent {
864 /**
865 * The type of the Markup.
866 */
867 String kind;
868
869 /**
870 * The content itself.
871 */
872 String value;
873
874 MarkupContent() {
875 kind = MarkupKind::Markdown;
876 }
877
878 MarkupContent(const String &p_value) {
879 value = p_value;
880 kind = MarkupKind::Markdown;
881 }
882
883 Dictionary to_json() const {
884 Dictionary dict;
885 dict["kind"] = kind;
886 dict["value"] = value;
887 return dict;
888 }
889 };
890
891 // Use namespace instead of enumeration to follow the LSP specifications
892 // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not.
893 // And here C++ compilers are unhappy with our enumeration name like `Color`, `File`, `RefCounted` etc.
894 /**
895 * The kind of a completion entry.
896 */
897 namespace CompletionItemKind {
898 static const int Text = 1;
899 static const int Method = 2;
900 static const int Function = 3;
901 static const int Constructor = 4;
902 static const int Field = 5;
903 static const int Variable = 6;
904 static const int Class = 7;
905 static const int Interface = 8;
906 static const int Module = 9;
907 static const int Property = 10;
908 static const int Unit = 11;
909 static const int Value = 12;
910 static const int Enum = 13;
911 static const int Keyword = 14;
912 static const int Snippet = 15;
913 static const int Color = 16;
914 static const int File = 17;
915 static const int RefCounted = 18;
916 static const int Folder = 19;
917 static const int EnumMember = 20;
918 static const int Constant = 21;
919 static const int Struct = 22;
920 static const int Event = 23;
921 static const int Operator = 24;
922 static const int TypeParameter = 25;
923 }; // namespace CompletionItemKind
924
925 // Use namespace instead of enumeration to follow the LSP specifications.
926 /**
927 * Defines whether the insert text in a completion item should be interpreted as
928 * plain text or a snippet.
929 */
930 namespace InsertTextFormat {
931 /**
932 * The primary text to be inserted is treated as a plain string.
933 */
934 static const int PlainText = 1;
935
936 /**
937 * The primary text to be inserted is treated as a snippet.
938 *
939 * A snippet can define tab stops and placeholders with `$1`, `$2`
940 * and `${3:foo}`. `$0` defines the final tab stop, it defaults to
941 * the end of the snippet. Placeholders with equal identifiers are linked,
942 * that is typing in one will update others too.
943 */
944 static const int Snippet = 2;
945 }; // namespace InsertTextFormat
946
947 struct CompletionItem {
948 /**
949 * The label of this completion item. By default
950 * also the text that is inserted when selecting
951 * this completion.
952 */
953 String label;
954
955 /**
956 * The kind of this completion item. Based of the kind
957 * an icon is chosen by the editor. The standardized set
958 * of available values is defined in `CompletionItemKind`.
959 */
960 int kind = 0;
961
962 /**
963 * A human-readable string with additional information
964 * about this item, like type or symbol information.
965 */
966 String detail;
967
968 /**
969 * A human-readable string that represents a doc-comment.
970 */
971 MarkupContent documentation;
972
973 /**
974 * Indicates if this item is deprecated.
975 */
976 bool deprecated = false;
977
978 /**
979 * Select this item when showing.
980 *
981 * *Note* that only one completion item can be selected and that the
982 * tool / client decides which item that is. The rule is that the *first*
983 * item of those that match best is selected.
984 */
985 bool preselect = false;
986
987 /**
988 * A string that should be used when comparing this item
989 * with other items. When omitted the label is used
990 * as the filter text for this item.
991 */
992 String sortText;
993
994 /**
995 * A string that should be used when filtering a set of
996 * completion items. When omitted the label is used as the
997 * filter text for this item.
998 */
999 String filterText;
1000
1001 /**
1002 * A string that should be inserted into a document when selecting
1003 * this completion. When omitted the label is used as the insert text
1004 * for this item.
1005 *
1006 * The `insertText` is subject to interpretation by the client side.
1007 * Some tools might not take the string literally. For example
1008 * VS Code when code complete is requested in this example
1009 * `con<cursor position>` and a completion item with an `insertText` of
1010 * `console` is provided it will only insert `sole`. Therefore it is
1011 * recommended to use `textEdit` instead since it avoids additional client
1012 * side interpretation.
1013 */
1014 String insertText;
1015
1016 /**
1017 * The format of the insert text. The format applies to both the `insertText` property
1018 * and the `newText` property of a provided `textEdit`.
1019 */
1020 int insertTextFormat = 0;
1021
1022 /**
1023 * An edit which is applied to a document when selecting this completion. When an edit is provided the value of
1024 * `insertText` is ignored.
1025 *
1026 * *Note:* The range of the edit must be a single line range and it must contain the position at which completion
1027 * has been requested.
1028 */
1029 TextEdit textEdit;
1030
1031 /**
1032 * An optional array of additional text edits that are applied when
1033 * selecting this completion. Edits must not overlap (including the same insert position)
1034 * with the main edit nor with themselves.
1035 *
1036 * Additional text edits should be used to change text unrelated to the current cursor position
1037 * (for example adding an import statement at the top of the file if the completion item will
1038 * insert an unqualified type).
1039 */
1040 Vector<TextEdit> additionalTextEdits;
1041
1042 /**
1043 * An optional set of characters that when pressed while this completion is active will accept it first and
1044 * then type that character. *Note* that all commit characters should have `length=1` and that superfluous
1045 * characters will be ignored.
1046 */
1047 Vector<String> commitCharacters;
1048
1049 /**
1050 * An optional command that is executed *after* inserting this completion. *Note* that
1051 * additional modifications to the current document should be described with the
1052 * additionalTextEdits-property.
1053 */
1054 Command command;
1055
1056 /**
1057 * A data entry field that is preserved on a completion item between
1058 * a completion and a completion resolve request.
1059 */
1060 Variant data;
1061
1062 _FORCE_INLINE_ Dictionary to_json(bool resolved = false) const {
1063 Dictionary dict;
1064 dict["label"] = label;
1065 dict["kind"] = kind;
1066 dict["data"] = data;
1067 if (!insertText.is_empty()) {
1068 dict["insertText"] = insertText;
1069 }
1070 if (resolved) {
1071 dict["detail"] = detail;
1072 dict["documentation"] = documentation.to_json();
1073 dict["deprecated"] = deprecated;
1074 dict["preselect"] = preselect;
1075 if (!sortText.is_empty()) {
1076 dict["sortText"] = sortText;
1077 }
1078 if (!filterText.is_empty()) {
1079 dict["filterText"] = filterText;
1080 }
1081 if (commitCharacters.size()) {
1082 dict["commitCharacters"] = commitCharacters;
1083 }
1084 if (!command.command.is_empty()) {
1085 dict["command"] = command.to_json();
1086 }
1087 }
1088 return dict;
1089 }
1090
1091 void load(const Dictionary &p_dict) {
1092 if (p_dict.has("label")) {
1093 label = p_dict["label"];
1094 }
1095 if (p_dict.has("kind")) {
1096 kind = p_dict["kind"];
1097 }
1098 if (p_dict.has("detail")) {
1099 detail = p_dict["detail"];
1100 }
1101 if (p_dict.has("documentation")) {
1102 Variant doc = p_dict["documentation"];
1103 if (doc.is_string()) {
1104 documentation.value = doc;
1105 } else if (doc.get_type() == Variant::DICTIONARY) {
1106 Dictionary v = doc;
1107 documentation.value = v["value"];
1108 }
1109 }
1110 if (p_dict.has("deprecated")) {
1111 deprecated = p_dict["deprecated"];
1112 }
1113 if (p_dict.has("preselect")) {
1114 preselect = p_dict["preselect"];
1115 }
1116 if (p_dict.has("sortText")) {
1117 sortText = p_dict["sortText"];
1118 }
1119 if (p_dict.has("filterText")) {
1120 filterText = p_dict["filterText"];
1121 }
1122 if (p_dict.has("insertText")) {
1123 insertText = p_dict["insertText"];
1124 }
1125 if (p_dict.has("data")) {
1126 data = p_dict["data"];
1127 }
1128 }
1129 };
1130
1131 /**
1132 * Represents a collection of [completion items](#CompletionItem) to be presented
1133 * in the editor.
1134 */
1135 struct CompletionList {
1136 /**
1137 * This list it not complete. Further typing should result in recomputing
1138 * this list.
1139 */
1140 bool isIncomplete = false;
1141
1142 /**
1143 * The completion items.
1144 */
1145 Vector<CompletionItem> items;
1146 };
1147
1148 // Use namespace instead of enumeration to follow the LSP specifications
1149 // `LSP::EnumName::EnumValue` is OK but `LSP::EnumValue` is not
1150 // And here C++ compilers are unhappy with our enumeration name like `String`, `Array`, `Object` etc
1151 /**
1152 * A symbol kind.
1153 */
1154 namespace SymbolKind {
1155 static const int File = 1;
1156 static const int Module = 2;
1157 static const int Namespace = 3;
1158 static const int Package = 4;
1159 static const int Class = 5;
1160 static const int Method = 6;
1161 static const int Property = 7;
1162 static const int Field = 8;
1163 static const int Constructor = 9;
1164 static const int Enum = 10;
1165 static const int Interface = 11;
1166 static const int Function = 12;
1167 static const int Variable = 13;
1168 static const int Constant = 14;
1169 static const int String = 15;
1170 static const int Number = 16;
1171 static const int Boolean = 17;
1172 static const int Array = 18;
1173 static const int Object = 19;
1174 static const int Key = 20;
1175 static const int Null = 21;
1176 static const int EnumMember = 22;
1177 static const int Struct = 23;
1178 static const int Event = 24;
1179 static const int Operator = 25;
1180 static const int TypeParameter = 26;
1181 }; // namespace SymbolKind
1182
1183 /**
1184 * Represents programming constructs like variables, classes, interfaces etc. that appear in a document. Document symbols can be
1185 * hierarchical and they have two ranges: one that encloses its definition and one that points to its most interesting range,
1186 * e.g. the range of an identifier.
1187 */
1188 struct DocumentSymbol {
1189 /**
1190 * The name of this symbol. Will be displayed in the user interface and therefore must not be
1191 * an empty string or a string only consisting of white spaces.
1192 */
1193 String name;
1194
1195 /**
1196 * More detail for this symbol, e.g the signature of a function.
1197 */
1198 String detail;
1199
1200 /**
1201 * Documentation for this symbol.
1202 */
1203 String documentation;
1204
1205 /**
1206 * Class name for the native symbols.
1207 */
1208 String native_class;
1209
1210 /**
1211 * The kind of this symbol.
1212 */
1213 int kind = SymbolKind::File;
1214
1215 /**
1216 * Indicates if this symbol is deprecated.
1217 */
1218 bool deprecated = false;
1219
1220 /**
1221 * If `true`: Symbol is local to script and cannot be accessed somewhere else.
1222 *
1223 * For example: local variable inside a `func`.
1224 */
1225 bool local = false;
1226
1227 /**
1228 * The range enclosing this symbol not including leading/trailing whitespace but everything else
1229 * like comments. This information is typically used to determine if the clients cursor is
1230 * inside the symbol to reveal in the symbol in the UI.
1231 */
1232 Range range;
1233
1234 /**
1235 * The range that should be selected and revealed when this symbol is being picked, e.g the name of a function.
1236 * Must be contained by the `range`.
1237 */
1238 Range selectionRange;
1239
1240 DocumentUri uri;
1241 String script_path;
1242
1243 /**
1244 * Children of this symbol, e.g. properties of a class.
1245 */
1246 Vector<DocumentSymbol> children;
1247
1248 Dictionary to_json(bool with_doc = false) const {
1249 Dictionary dict;
1250 dict["name"] = name;
1251 dict["detail"] = detail;
1252 dict["kind"] = kind;
1253 dict["deprecated"] = deprecated;
1254 dict["range"] = range.to_json();
1255 dict["selectionRange"] = selectionRange.to_json();
1256 if (with_doc) {
1257 dict["documentation"] = documentation;
1258 dict["native_class"] = native_class;
1259 }
1260 if (!children.is_empty()) {
1261 Array arr;
1262 for (int i = 0; i < children.size(); i++) {
1263 if (children[i].local) {
1264 continue;
1265 }
1266 arr.push_back(children[i].to_json(with_doc));
1267 }
1268 if (!children.is_empty()) {
1269 dict["children"] = arr;
1270 }
1271 }
1272 return dict;
1273 }
1274
1275 _FORCE_INLINE_ MarkupContent render() const {
1276 MarkupContent markdown;
1277 if (detail.length()) {
1278 markdown.value = "\t" + detail + "\n\n";
1279 }
1280 if (documentation.length()) {
1281 markdown.value += marked_documentation(documentation) + "\n\n";
1282 }
1283 if (script_path.length()) {
1284 markdown.value += "Defined in [" + script_path + "](" + uri + ")";
1285 }
1286 return markdown;
1287 }
1288
1289 _FORCE_INLINE_ CompletionItem make_completion_item(bool resolved = false) const {
1290 LSP::CompletionItem item;
1291 item.label = name;
1292
1293 if (resolved) {
1294 item.documentation = render();
1295 }
1296
1297 switch (kind) {
1298 case LSP::SymbolKind::Enum:
1299 item.kind = LSP::CompletionItemKind::Enum;
1300 break;
1301 case LSP::SymbolKind::Class:
1302 item.kind = LSP::CompletionItemKind::Class;
1303 break;
1304 case LSP::SymbolKind::Property:
1305 item.kind = LSP::CompletionItemKind::Property;
1306 break;
1307 case LSP::SymbolKind::Method:
1308 case LSP::SymbolKind::Function:
1309 item.kind = LSP::CompletionItemKind::Method;
1310 break;
1311 case LSP::SymbolKind::Event:
1312 item.kind = LSP::CompletionItemKind::Event;
1313 break;
1314 case LSP::SymbolKind::Constant:
1315 item.kind = LSP::CompletionItemKind::Constant;
1316 break;
1317 case LSP::SymbolKind::Variable:
1318 item.kind = LSP::CompletionItemKind::Variable;
1319 break;
1320 case LSP::SymbolKind::File:
1321 item.kind = LSP::CompletionItemKind::File;
1322 break;
1323 default:
1324 item.kind = LSP::CompletionItemKind::Text;
1325 break;
1326 }
1327
1328 return item;
1329 }
1330 };
1331
1332 struct ApplyWorkspaceEditParams {
1333 WorkspaceEdit edit;
1334
1335 Dictionary to_json() {
1336 Dictionary dict;
1337
1338 dict["edit"] = edit.to_json();
1339
1340 return dict;
1341 }
1342 };
1343
1344 struct NativeSymbolInspectParams {
1345 String native_class;
1346 String symbol_name;
1347
1348 void load(const Dictionary &p_params) {
1349 native_class = p_params["native_class"];
1350 symbol_name = p_params["symbol_name"];
1351 }
1352 };
1353
1354 /**
1355 * Enum of known range kinds
1356 */
1357 namespace FoldingRangeKind {
1358 /**
1359 * Folding range for a comment
1360 */
1361 static const String Comment = "comment";
1362 /**
1363 * Folding range for a imports or includes
1364 */
1365 static const String Imports = "imports";
1366 /**
1367 * Folding range for a region (e.g. `#region`)
1368 */
1369 static const String Region = "region";
1370 } // namespace FoldingRangeKind
1371
1372 /**
1373 * Represents a folding range.
1374 */
1375 struct FoldingRange {
1376 /**
1377 * The zero-based line number from where the folded range starts.
1378 */
1379 int startLine = 0;
1380
1381 /**
1382 * The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line.
1383 */
1384 int startCharacter = 0;
1385
1386 /**
1387 * The zero-based line number where the folded range ends.
1388 */
1389 int endLine = 0;
1390
1391 /**
1392 * The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line.
1393 */
1394 int endCharacter = 0;
1395
1396 /**
1397 * Describes the kind of the folding range such as `comment' or 'region'. The kind
1398 * is used to categorize folding ranges and used by commands like 'Fold all comments'. See
1399 * [FoldingRangeKind](#FoldingRangeKind) for an enumeration of standardized kinds.
1400 */
1401 String kind = FoldingRangeKind::Region;
1402
1403 _FORCE_INLINE_ Dictionary to_json() const {
1404 Dictionary dict;
1405 dict["startLine"] = startLine;
1406 dict["startCharacter"] = startCharacter;
1407 dict["endLine"] = endLine;
1408 dict["endCharacter"] = endCharacter;
1409 return dict;
1410 }
1411 };
1412
1413 // Use namespace instead of enumeration to follow the LSP specifications
1414 /**
1415 * How a completion was triggered
1416 */
1417 namespace CompletionTriggerKind {
1418 /**
1419 * Completion was triggered by typing an identifier (24x7 code
1420 * complete), manual invocation (e.g Ctrl+Space) or via API.
1421 */
1422 static const int Invoked = 1;
1423
1424 /**
1425 * Completion was triggered by a trigger character specified by
1426 * the `triggerCharacters` properties of the `CompletionRegistrationOptions`.
1427 */
1428 static const int TriggerCharacter = 2;
1429
1430 /**
1431 * Completion was re-triggered as the current completion list is incomplete.
1432 */
1433 static const int TriggerForIncompleteCompletions = 3;
1434 } // namespace CompletionTriggerKind
1435
1436 /**
1437 * Contains additional information about the context in which a completion request is triggered.
1438 */
1439 struct CompletionContext {
1440 /**
1441 * How the completion was triggered.
1442 */
1443 int triggerKind = CompletionTriggerKind::Invoked;
1444
1445 /**
1446 * The trigger character (a single character) that has trigger code complete.
1447 * Is undefined if `triggerKind !== CompletionTriggerKind.TriggerCharacter`
1448 */
1449 String triggerCharacter;
1450
1451 void load(const Dictionary &p_params) {
1452 triggerKind = int(p_params["triggerKind"]);
1453 triggerCharacter = p_params.get("triggerCharacter", "");
1454 }
1455 };
1456
1457 struct CompletionParams : public TextDocumentPositionParams {
1458 /**
1459 * The completion context. This is only available if the client specifies
1460 * to send this using `ClientCapabilities.textDocument.completion.contextSupport === true`
1461 */
1462 CompletionContext context;
1463
1464 void load(const Dictionary &p_params) {
1465 TextDocumentPositionParams::load(p_params);
1466
1467 if (p_params.has("context")) {
1468 context.load(p_params["context"]);
1469 }
1470 }
1471
1472 Dictionary to_json() {
1473 Dictionary ctx;
1474 ctx["triggerCharacter"] = context.triggerCharacter;
1475 ctx["triggerKind"] = context.triggerKind;
1476
1477 Dictionary dict;
1478 dict = TextDocumentPositionParams::to_json();
1479 dict["context"] = ctx;
1480 return dict;
1481 }
1482 };
1483
1484 /**
1485 * The result of a hover request.
1486 */
1487 struct Hover {
1488 /**
1489 * The hover's content
1490 */
1491 MarkupContent contents;
1492
1493 /**
1494 * An optional range is a range inside a text document
1495 * that is used to visualize a hover, e.g. by changing the background color.
1496 */
1497 Range range;
1498
1499 _FORCE_INLINE_ Dictionary to_json() const {
1500 Dictionary dict;
1501 dict["range"] = range.to_json();
1502 dict["contents"] = contents.to_json();
1503 return dict;
1504 }
1505 };
1506
1507 /**
1508 * Represents a parameter of a callable-signature. A parameter can
1509 * have a label and a doc-comment.
1510 */
1511 struct ParameterInformation {
1512 /**
1513 * The label of this parameter information.
1514 *
1515 * Either a string or an inclusive start and exclusive end offsets within its containing
1516 * signature label. (see SignatureInformation.label). The offsets are based on a UTF-16
1517 * string representation as `Position` and `Range` does.
1518 *
1519 * *Note*: a label of type string should be a substring of its containing signature label.
1520 * Its intended use case is to highlight the parameter label part in the `SignatureInformation.label`.
1521 */
1522 String label;
1523
1524 /**
1525 * The human-readable doc-comment of this parameter. Will be shown
1526 * in the UI but can be omitted.
1527 */
1528 MarkupContent documentation;
1529
1530 Dictionary to_json() const {
1531 Dictionary dict;
1532 dict["label"] = label;
1533 dict["documentation"] = documentation.to_json();
1534 return dict;
1535 }
1536 };
1537
1538 /**
1539 * Represents the signature of something callable. A signature
1540 * can have a label, like a function-name, a doc-comment, and
1541 * a set of parameters.
1542 */
1543 struct SignatureInformation {
1544 /**
1545 * The label of this signature. Will be shown in
1546 * the UI.
1547 */
1548 String label;
1549
1550 /**
1551 * The human-readable doc-comment of this signature. Will be shown
1552 * in the UI but can be omitted.
1553 */
1554 MarkupContent documentation;
1555
1556 /**
1557 * The parameters of this signature.
1558 */
1559 Vector<ParameterInformation> parameters;
1560
1561 Dictionary to_json() const {
1562 Dictionary dict;
1563 dict["label"] = label;
1564 dict["documentation"] = documentation.to_json();
1565 Array args;
1566 for (int i = 0; i < parameters.size(); i++) {
1567 args.push_back(parameters[i].to_json());
1568 }
1569 dict["parameters"] = args;
1570 return dict;
1571 }
1572 };
1573
1574 /**
1575 * Signature help represents the signature of something
1576 * callable. There can be multiple signature but only one
1577 * active and only one active parameter.
1578 */
1579 struct SignatureHelp {
1580 /**
1581 * One or more signatures.
1582 */
1583 Vector<SignatureInformation> signatures;
1584
1585 /**
1586 * The active signature. If omitted or the value lies outside the
1587 * range of `signatures` the value defaults to zero or is ignored if
1588 * `signatures.length === 0`. Whenever possible implementers should
1589 * make an active decision about the active signature and shouldn't
1590 * rely on a default value.
1591 * In future version of the protocol this property might become
1592 * mandatory to better express this.
1593 */
1594 int activeSignature = 0;
1595
1596 /**
1597 * The active parameter of the active signature. If omitted or the value
1598 * lies outside the range of `signatures[activeSignature].parameters`
1599 * defaults to 0 if the active signature has parameters. If
1600 * the active signature has no parameters it is ignored.
1601 * In future version of the protocol this property might become
1602 * mandatory to better express the active parameter if the
1603 * active signature does have any.
1604 */
1605 int activeParameter = 0;
1606
1607 Dictionary to_json() const {
1608 Dictionary dict;
1609 Array sigs;
1610 for (int i = 0; i < signatures.size(); i++) {
1611 sigs.push_back(signatures[i].to_json());
1612 }
1613 dict["signatures"] = sigs;
1614 dict["activeSignature"] = activeSignature;
1615 dict["activeParameter"] = activeParameter;
1616 return dict;
1617 }
1618 };
1619
1620 /**
1621 * A pattern to describe in which file operation requests or notifications
1622 * the server is interested in.
1623 */
1624 struct FileOperationPattern {
1625 /**
1626 * The glob pattern to match.
1627 */
1628 String glob = "**/*.gd";
1629
1630 /**
1631 * Whether to match `file`s or `folder`s with this pattern.
1632 *
1633 * Matches both if undefined.
1634 */
1635 String matches = "file";
1636
1637 Dictionary to_json() const {
1638 Dictionary dict;
1639
1640 dict["glob"] = glob;
1641 dict["matches"] = matches;
1642
1643 return dict;
1644 }
1645 };
1646
1647 /**
1648 * A filter to describe in which file operation requests or notifications
1649 * the server is interested in.
1650 */
1651 struct FileOperationFilter {
1652 /**
1653 * The actual file operation pattern.
1654 */
1655 FileOperationPattern pattern;
1656
1657 Dictionary to_json() const {
1658 Dictionary dict;
1659
1660 dict["pattern"] = pattern.to_json();
1661
1662 return dict;
1663 }
1664 };
1665
1666 /**
1667 * The options to register for file operations.
1668 */
1669 struct FileOperationRegistrationOptions {
1670 /**
1671 * The actual filters.
1672 */
1673 Vector<FileOperationFilter> filters;
1674
1675 FileOperationRegistrationOptions() {
1676 filters.push_back(FileOperationFilter());
1677 }
1678
1679 Dictionary to_json() const {
1680 Dictionary dict;
1681
1682 Array filts;
1683 for (int i = 0; i < filters.size(); i++) {
1684 filts.push_back(filters[i].to_json());
1685 }
1686 dict["filters"] = filts;
1687
1688 return dict;
1689 }
1690 };
1691
1692 /**
1693 * The server is interested in file notifications/requests.
1694 */
1695 struct FileOperations {
1696 /**
1697 * The server is interested in receiving didDeleteFiles file notifications.
1698 */
1699 FileOperationRegistrationOptions didDelete;
1700
1701 Dictionary to_json() const {
1702 Dictionary dict;
1703
1704 dict["didDelete"] = didDelete.to_json();
1705
1706 return dict;
1707 }
1708 };
1709
1710 /**
1711 * Workspace specific server capabilities
1712 */
1713 struct Workspace {
1714 Dictionary to_json() const {
1715 Dictionary dict;
1716 return dict;
1717 }
1718 };
1719
1720 struct ServerCapabilities {
1721 /**
1722 * Defines how text documents are synced. Is either a detailed structure defining each notification or
1723 * for backwards compatibility the TextDocumentSyncKind number. If omitted it defaults to `TextDocumentSyncKind.None`.
1724 */
1725 TextDocumentSyncOptions textDocumentSync;
1726
1727 /**
1728 * The server provides hover support.
1729 */
1730 bool hoverProvider = true;
1731
1732 /**
1733 * The server provides completion support.
1734 */
1735 CompletionOptions completionProvider;
1736
1737 /**
1738 * The server provides signature help support.
1739 */
1740 SignatureHelpOptions signatureHelpProvider;
1741
1742 /**
1743 * The server provides goto definition support.
1744 */
1745 bool definitionProvider = true;
1746
1747 /**
1748 * The server provides Goto Type Definition support.
1749 *
1750 * Since 3.6.0
1751 */
1752 bool typeDefinitionProvider = false;
1753
1754 /**
1755 * The server provides Goto Implementation support.
1756 *
1757 * Since 3.6.0
1758 */
1759 bool implementationProvider = false;
1760
1761 /**
1762 * The server provides find references support.
1763 */
1764 bool referencesProvider = true;
1765
1766 /**
1767 * The server provides document highlight support.
1768 */
1769 bool documentHighlightProvider = false;
1770
1771 /**
1772 * The server provides document symbol support.
1773 */
1774 bool documentSymbolProvider = true;
1775
1776 /**
1777 * The server provides workspace symbol support.
1778 */
1779 bool workspaceSymbolProvider = false;
1780
1781 /**
1782 * The server supports workspace folder.
1783 */
1784 Workspace workspace;
1785
1786 /**
1787 * The server provides code actions. The `CodeActionOptions` return type is only
1788 * valid if the client signals code action literal support via the property
1789 * `textDocument.codeAction.codeActionLiteralSupport`.
1790 */
1791 bool codeActionProvider = false;
1792
1793 /**
1794 * The server provides code lens.
1795 */
1796 CodeLensOptions codeLensProvider;
1797
1798 /**
1799 * The server provides document formatting.
1800 */
1801 bool documentFormattingProvider = false;
1802
1803 /**
1804 * The server provides document range formatting.
1805 */
1806 bool documentRangeFormattingProvider = false;
1807
1808 /**
1809 * The server provides document formatting on typing.
1810 */
1811 DocumentOnTypeFormattingOptions documentOnTypeFormattingProvider;
1812
1813 /**
1814 * The server provides rename support. RenameOptions may only be
1815 * specified if the client states that it supports
1816 * `prepareSupport` in its initial `initialize` request.
1817 */
1818 RenameOptions renameProvider;
1819
1820 /**
1821 * The server provides document link support.
1822 */
1823 DocumentLinkOptions documentLinkProvider;
1824
1825 /**
1826 * The server provides color provider support.
1827 *
1828 * Since 3.6.0
1829 */
1830 ColorProviderOptions colorProvider;
1831
1832 /**
1833 * The server provides folding provider support.
1834 *
1835 * Since 3.10.0
1836 */
1837 FoldingRangeProviderOptions foldingRangeProvider;
1838
1839 /**
1840 * The server provides go to declaration support.
1841 *
1842 * Since 3.14.0
1843 */
1844 bool declarationProvider = true;
1845
1846 /**
1847 * The server provides execute command support.
1848 */
1849 ExecuteCommandOptions executeCommandProvider;
1850
1851 _FORCE_INLINE_ Dictionary to_json() {
1852 Dictionary dict;
1853 dict["textDocumentSync"] = textDocumentSync.to_json();
1854 dict["completionProvider"] = completionProvider.to_json();
1855 signatureHelpProvider.triggerCharacters.push_back(",");
1856 signatureHelpProvider.triggerCharacters.push_back("(");
1857 dict["signatureHelpProvider"] = signatureHelpProvider.to_json();
1858 //dict["codeLensProvider"] = codeLensProvider.to_json();
1859 dict["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.to_json();
1860 dict["renameProvider"] = renameProvider.to_json();
1861 dict["documentLinkProvider"] = documentLinkProvider.to_json();
1862 dict["colorProvider"] = false; // colorProvider.to_json();
1863 dict["foldingRangeProvider"] = false; //foldingRangeProvider.to_json();
1864 dict["executeCommandProvider"] = executeCommandProvider.to_json();
1865 dict["hoverProvider"] = hoverProvider;
1866 dict["definitionProvider"] = definitionProvider;
1867 dict["typeDefinitionProvider"] = typeDefinitionProvider;
1868 dict["implementationProvider"] = implementationProvider;
1869 dict["referencesProvider"] = referencesProvider;
1870 dict["documentHighlightProvider"] = documentHighlightProvider;
1871 dict["documentSymbolProvider"] = documentSymbolProvider;
1872 dict["workspaceSymbolProvider"] = workspaceSymbolProvider;
1873 dict["workspace"] = workspace.to_json();
1874 dict["codeActionProvider"] = codeActionProvider;
1875 dict["documentFormattingProvider"] = documentFormattingProvider;
1876 dict["documentRangeFormattingProvider"] = documentRangeFormattingProvider;
1877 dict["declarationProvider"] = declarationProvider;
1878 return dict;
1879 }
1880 };
1881
1882 struct InitializeResult {
1883 /**
1884 * The capabilities the language server provides.
1885 */
1886 ServerCapabilities capabilities;
1887
1888 _FORCE_INLINE_ Dictionary to_json() {
1889 Dictionary dict;
1890 dict["capabilities"] = capabilities.to_json();
1891 return dict;
1892 }
1893 };
1894
1895 struct GodotNativeClassInfo {
1896 String name;
1897 const DocData::ClassDoc *class_doc = nullptr;
1898 const ClassDB::ClassInfo *class_info = nullptr;
1899
1900 Dictionary to_json() const {
1901 Dictionary dict;
1902 dict["name"] = name;
1903 dict["inherits"] = class_doc->inherits;
1904 return dict;
1905 }
1906 };
1907
1908 /** Features not included in the standard lsp specifications */
1909 struct GodotCapabilities {
1910 /**
1911 * Native class list
1912 */
1913 List<GodotNativeClassInfo> native_classes;
1914
1915 Dictionary to_json() const {
1916 Dictionary dict;
1917 Array classes;
1918 for (const GodotNativeClassInfo &native_class : native_classes) {
1919 classes.push_back(native_class.to_json());
1920 }
1921 dict["native_classes"] = classes;
1922 return dict;
1923 }
1924 };
1925
1926 /** Format BBCode documentation from DocData to markdown */
1927 static String marked_documentation(const String &p_bbcode) {
1928 String markdown = p_bbcode.strip_edges();
1929
1930 Vector<String> lines = markdown.split("\n");
1931 bool in_codeblock_tag = false;
1932 // This is for handling the special [codeblocks] syntax used by the built-in class reference.
1933 bool in_codeblocks_tag = false;
1934 bool in_codeblocks_gdscript_tag = false;
1935
1936 markdown = "";
1937 for (int i = 0; i < lines.size(); i++) {
1938 String line = lines[i];
1939
1940 // For [codeblocks] tags we locate a child [gdscript] tag and turn that
1941 // into a GDScript code listing. Other languages and the surrounding tag
1942 // are skipped.
1943 if (line.contains("[codeblocks]")) {
1944 in_codeblocks_tag = true;
1945 continue;
1946 }
1947 if (in_codeblocks_tag && line.contains("[/codeblocks]")) {
1948 in_codeblocks_tag = false;
1949 continue;
1950 }
1951 if (in_codeblocks_tag) {
1952 if (line.contains("[gdscript]")) {
1953 in_codeblocks_gdscript_tag = true;
1954 line = "```gdscript";
1955 } else if (in_codeblocks_gdscript_tag && line.contains("[/gdscript]")) {
1956 line = "```";
1957 in_codeblocks_gdscript_tag = false;
1958 } else if (!in_codeblocks_gdscript_tag) {
1959 continue;
1960 }
1961 }
1962
1963 // We need to account for both [codeblock] and [codeblock lang=...].
1964 String codeblock_lang = "gdscript";
1965 int block_start = line.find("[codeblock");
1966 if (block_start != -1) {
1967 int bracket_pos = line.find_char(']', block_start);
1968 if (bracket_pos != -1) {
1969 int lang_start = line.find("lang=", block_start);
1970 if (lang_start != -1 && lang_start < bracket_pos) {
1971 constexpr int LANG_PARAM_LENGTH = 5; // Length of "lang=".
1972 int lang_value_start = lang_start + LANG_PARAM_LENGTH;
1973 int lang_end = bracket_pos;
1974 if (lang_value_start < lang_end) {
1975 codeblock_lang = line.substr(lang_value_start, lang_end - lang_value_start);
1976 }
1977 }
1978 in_codeblock_tag = true;
1979 line = "```" + codeblock_lang;
1980 }
1981 }
1982
1983 if (in_codeblock_tag && line.contains("[/codeblock]")) {
1984 line = "```";
1985 in_codeblock_tag = false;
1986 }
1987
1988 if (!in_codeblock_tag) {
1989 line = line.strip_edges();
1990 line = line.replace("[br]", "\n\n");
1991
1992 line = line.replace("[code]", "`");
1993 line = line.replace("[/code]", "`");
1994 line = line.replace("[i]", "*");
1995 line = line.replace("[/i]", "*");
1996 line = line.replace("[b]", "**");
1997 line = line.replace("[/b]", "**");
1998 line = line.replace("[u]", "__");
1999 line = line.replace("[/u]", "__");
2000 line = line.replace("[s]", "~~");
2001 line = line.replace("[/s]", "~~");
2002 line = line.replace("[kbd]", "`");
2003 line = line.replace("[/kbd]", "`");
2004 line = line.replace("[center]", "");
2005 line = line.replace("[/center]", "");
2006 line = line.replace("[/font]", "");
2007 line = line.replace("[/color]", "");
2008 line = line.replace("[/img]", "");
2009
2010 // Convert remaining simple bracketed class names to backticks and literal brackets.
2011 // This handles cases like [Node2D], [Sprite2D], etc. and [lb] and [rb].
2012 int pos = 0;
2013 while ((pos = line.find_char('[', pos)) != -1) {
2014 // Replace the special cases for [lb] and [rb] first and walk
2015 // past them to avoid conflicts with class names.
2016 const bool is_within_bounds = pos + 4 <= line.length();
2017 if (is_within_bounds && line.substr(pos, 4) == "[lb]") {
2018 line = line.substr(0, pos) + "\\[" + line.substr(pos + 4);
2019 // We advance past the newly inserted `\\` and `[` characters (2 chars) so the
2020 // next `line.find()` does not stop at the same position.
2021 pos += 2;
2022 continue;
2023 } else if (is_within_bounds && line.substr(pos, 4) == "[rb]") {
2024 line = line.substr(0, pos) + "\\]" + line.substr(pos + 4);
2025 pos += 2;
2026 continue;
2027 }
2028
2029 // Replace class names in brackets.
2030 int end_pos = line.find_char(']', pos);
2031 if (end_pos == -1) {
2032 break;
2033 }
2034
2035 String content = line.substr(pos + 1, end_pos - pos - 1);
2036 // We only convert if it looks like a simple class name (no spaces, no special chars).
2037 // GDScript supports unicode characters as identifiers so we only exclude markers of other BBCode tags to avoid conflicts.
2038 bool is_class_name = (!content.is_empty() && content != "url" && !content.contains_char(' ') && !content.contains_char('=') && !content.contains_char('/'));
2039 if (is_class_name) {
2040 line = line.substr(0, pos) + "`" + content + "`" + line.substr(end_pos + 1);
2041 pos += content.length() + 2;
2042 } else {
2043 pos = end_pos + 1;
2044 }
2045 }
2046
2047 constexpr int URL_OPEN_TAG_LENGTH = 5; // Length of "[url=".
2048 constexpr int URL_CLOSE_TAG_LENGTH = 6; // Length of "[/url]".
2049
2050 // This is for the case [url=$url]$text[/url].
2051 pos = 0;
2052 while ((pos = line.find("[url=", pos)) != -1) {
2053 int url_end = line.find_char(']', pos);
2054 int close_start = line.find("[/url]", url_end);
2055 if (url_end == -1 || close_start == -1) {
2056 break;
2057 }
2058
2059 String url = line.substr(pos + URL_OPEN_TAG_LENGTH, url_end - pos - URL_OPEN_TAG_LENGTH);
2060 String text = line.substr(url_end + 1, close_start - url_end - 1);
2061 String replacement = "[" + text + "](" + url + ")";
2062 line = line.substr(0, pos) + replacement + line.substr(close_start + URL_CLOSE_TAG_LENGTH);
2063 pos += replacement.length();
2064 }
2065
2066 // This is for the case [url]$url[/url].
2067 pos = 0;
2068 while ((pos = line.find("[url]", pos)) != -1) {
2069 int close_pos = line.find("[/url]", pos);
2070 if (close_pos == -1) {
2071 break;
2072 }
2073
2074 String url = line.substr(pos + URL_OPEN_TAG_LENGTH, close_pos - pos - URL_OPEN_TAG_LENGTH);
2075 String replacement = "[" + url + "](" + url + ")";
2076 line = line.substr(0, pos) + replacement + line.substr(close_pos + URL_CLOSE_TAG_LENGTH);
2077 pos += replacement.length();
2078 }
2079
2080 // Replace the various link types with inline code ([class MyNode] to `MyNode`).
2081 // Uses a while loop because there can occasionally be multiple links of the same type in a single line.
2082 const Vector<String> link_start_patterns = {
2083 "[class ", "[method ", "[member ", "[signal ", "[enum ", "[constant ",
2084 "[annotation ", "[constructor ", "[operator ", "[theme_item ", "[param "
2085 };
2086 for (const String &pattern : link_start_patterns) {
2087 int pattern_pos = 0;
2088 while ((pattern_pos = line.find(pattern, pattern_pos)) != -1) {
2089 int end_pos = line.find_char(']', pattern_pos);
2090 if (end_pos == -1) {
2091 break;
2092 }
2093
2094 String content = line.substr(pattern_pos + pattern.length(), end_pos - pattern_pos - pattern.length());
2095 String replacement = "`" + content + "`";
2096 line = line.substr(0, pattern_pos) + replacement + line.substr(end_pos + 1);
2097 pattern_pos += replacement.length();
2098 }
2099 }
2100
2101 // Remove tags with attributes like [color=red], as they don't have a direct Markdown
2102 // equivalent supported by external tools.
2103 const String attribute_tags[] = {
2104 "color", "font", "img"
2105 };
2106 for (const String &tag_name : attribute_tags) {
2107 int tag_pos = 0;
2108 while ((tag_pos = line.find("[" + tag_name + "=", tag_pos)) != -1) {
2109 int end_pos = line.find_char(']', tag_pos);
2110 if (end_pos == -1) {
2111 break;
2112 }
2113
2114 line = line.substr(0, tag_pos) + line.substr(end_pos + 1);
2115 }
2116 }
2117 }
2118
2119 if (i < lines.size() - 1) {
2120 line += "\n";
2121 }
2122 markdown += line;
2123 }
2124 return markdown;
2125 }
2126 } // namespace LSP