Debian Codegraph @

modules/gdscript/tests/test_lsp.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_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 #ifdef TOOLS_ENABLED
34
35 #ifndef GDSCRIPT_NO_LSP
36
37 #include "tests/test_macros.h"
38
39 #include "../language_server/gdscript_extend_parser.h"
40 #include "../language_server/gdscript_language_protocol.h"
41 #include "../language_server/gdscript_workspace.h"
42 #include "../language_server/godot_lsp.h"
43
44 #include "core/io/dir_access.h"
45 #include "core/io/file_access_pack.h"
46 #include "core/os/os.h"
47 #include "editor/doc/editor_help.h"
48 #include "editor/editor_node.h"
49
50 #include "modules/gdscript/gdscript_analyzer.h"
51 #include "modules/regex/regex.h"
52
53 #include "thirdparty/doctest/doctest.h"
54
55 class TestGDScriptLanguageProtocolInitializer {
56 public:
57 static void setup_client() {
58 GDScriptLanguageProtocol *proto = GDScriptLanguageProtocol::get_singleton();
59 Ref<GDScriptLanguageProtocol::LSPeer> peer = memnew(GDScriptLanguageProtocol::LSPeer);
60 proto->clients.insert(proto->next_client_id, peer);
61 proto->latest_client_id = proto->next_client_id;
62 proto->next_client_id++;
63 }
64 };
65
66 template <>
67 struct doctest::StringMaker<LSP::Position> {
68 static doctest::String convert(const LSP::Position &p_val) {
69 return p_val.to_string().utf8().get_data();
70 }
71 };
72
73 template <>
74 struct doctest::StringMaker<LSP::Range> {
75 static doctest::String convert(const LSP::Range &p_val) {
76 return p_val.to_string().utf8().get_data();
77 }
78 };
79
80 template <>
81 struct doctest::StringMaker<GodotPosition> {
82 static doctest::String convert(const GodotPosition &p_val) {
83 return p_val.to_string().utf8().get_data();
84 }
85 };
86
87 namespace GDScriptTests {
88
89 // LSP GDScript test scripts are located inside project of other GDScript tests:
90 // Cannot reset `ProjectSettings` (singleton) -> Cannot load another workspace and resources in there.
91 // -> Reuse GDScript test project. LSP specific scripts are then placed inside `lsp` folder.
92 // Access via `res://lsp/my_script.gd`.
93 const String root = "modules/gdscript/tests/scripts/";
94
95 /*
96 * After use:
97 * * `memdelete` returned `GDScriptLanguageProtocol`.
98 * * Call `GDScriptTests::::finish_language`.
99 */
100 GDScriptLanguageProtocol *initialize(const String &p_root) {
101 Error err = OK;
102 Ref<DirAccess> dir(DirAccess::open(p_root, &err));
103 REQUIRE_MESSAGE(err == OK, "Could not open specified root directory");
104 String absolute_root = dir->get_current_dir();
105 init_language(absolute_root);
106
107 GDScriptLanguageProtocol *proto = memnew(GDScriptLanguageProtocol);
108 TestGDScriptLanguageProtocolInitializer::setup_client();
109
110 Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
111 workspace->root = absolute_root;
112 // On windows: `C:/...` -> `C%3A/...`.
113 workspace->root_uri = "file:///" + absolute_root.lstrip("/").replace_first(":", "%3A");
114
115 return proto;
116 }
117
118 LSP::Position pos(const int p_line, const int p_character) {
119 LSP::Position p;
120 p.line = p_line;
121 p.character = p_character;
122 return p;
123 }
124
125 LSP::Range range(const LSP::Position p_start, const LSP::Position p_end) {
126 LSP::Range r;
127 r.start = p_start;
128 r.end = p_end;
129 return r;
130 }
131
132 LSP::TextDocumentPositionParams pos_in(const LSP::DocumentUri &p_uri, const LSP::Position p_pos) {
133 LSP::TextDocumentPositionParams params;
134 params.textDocument.uri = p_uri;
135 params.position = p_pos;
136 return params;
137 }
138
139 const LSP::DocumentSymbol *test_resolve_symbol_at(const String &p_uri, const LSP::Position p_pos, const String &p_expected_uri, const String &p_expected_name, const LSP::Range &p_expected_range) {
140 Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
141
142 LSP::TextDocumentPositionParams params = pos_in(p_uri, p_pos);
143 const LSP::DocumentSymbol *symbol = workspace->resolve_symbol(params);
144 CHECK(symbol);
145
146 if (symbol) {
147 CHECK_EQ(symbol->uri, p_expected_uri);
148 CHECK_EQ(symbol->name, p_expected_name);
149 CHECK_EQ(symbol->selectionRange, p_expected_range);
150 }
151
152 return symbol;
153 }
154
155 struct InlineTestData {
156 LSP::Range range;
157 String text;
158 String name;
159 String ref;
160
161 static bool try_parse(const Vector<String> &p_lines, const int p_line_number, InlineTestData &r_data) {
162 String line = p_lines[p_line_number];
163
164 RegEx regex = RegEx("^\\t*#[ |]*(?<range>(?<left><)?\\^+)(\\s+(?<name>(?!->)\\S+))?(\\s+->\\s+(?<ref>\\S+))?");
165 Ref<RegExMatch> match = regex.search(line);
166 if (match.is_null()) {
167 return false;
168 }
169
170 // Find first line without leading comment above current line.
171 int target_line = p_line_number;
172 while (target_line >= 0) {
173 String dedented = p_lines[target_line].lstrip("\t");
174 if (!dedented.begins_with("#")) {
175 break;
176 }
177 target_line--;
178 }
179 if (target_line < 0) {
180 return false;
181 }
182 r_data.range.start.line = r_data.range.end.line = target_line;
183
184 String marker = match->get_string("range");
185 int i = line.find(marker);
186 REQUIRE(i >= 0);
187 r_data.range.start.character = i;
188 if (!match->get_string("left").is_empty()) {
189 // Include `#` (comment char) in range.
190 r_data.range.start.character--;
191 }
192 r_data.range.end.character = i + marker.length();
193
194 String target = p_lines[target_line];
195 r_data.text = target.substr(r_data.range.start.character, r_data.range.end.character - r_data.range.start.character);
196
197 r_data.name = match->get_string("name");
198 r_data.ref = match->get_string("ref");
199
200 return true;
201 }
202 };
203
204 Vector<InlineTestData> read_tests(const String &p_path) {
205 Error err;
206 String source = FileAccess::get_file_as_string(p_path, &err);
207 REQUIRE_MESSAGE(err == OK, vformat("Cannot read '%s'", p_path));
208
209 // Format:
210 // ```gdscript
211 // var foo = bar + baz
212 // # | | | | ^^^ name -> ref
213 // # | | ^^^ -> ref
214 // # ^^^ name
215 //
216 // func my_func():
217 // # ^^^^^^^ name
218 // var value = foo + 42
219 // # ^^^^^ name
220 // print(value)
221 // # ^^^^^ -> ref
222 // ```
223 //
224 // * `^`: Range marker.
225 // * `name`: Unique name. Can contain any characters except whitespace chars.
226 // * `ref`: Reference to unique name.
227 //
228 // Notes:
229 // * If range should include first content-char (which is occupied by `#`): use `<` for next marker.
230 // -> Range expands 1 to left (-> includes `#`).
231 // * Note: Means: Range cannot be single char directly marked by `#`, but must be at least two chars (marked with `#<`).
232 // * Comment must start at same ident as line its marked (-> because of tab alignment...).
233 // * Use spaces to align after `#`! -> for correct alignment
234 // * Between `#` and `^` can be spaces or `|` (to better visualize what's marked below).
235 PackedStringArray lines = source.split("\n");
236
237 PackedStringArray names;
238 Vector<InlineTestData> data;
239 for (int i = 0; i < lines.size(); i++) {
240 InlineTestData d;
241 if (InlineTestData::try_parse(lines, i, d)) {
242 if (!d.name.is_empty()) {
243 // Safety check: names must be unique.
244 if (names.has(d.name)) {
245 FAIL(vformat("Duplicated name '%s' in '%s'. Names must be unique!", d.name, p_path));
246 }
247 names.append(d.name);
248 }
249
250 data.append(d);
251 }
252 }
253
254 return data;
255 }
256
257 void test_resolve_symbol(const String &p_uri, const InlineTestData &p_test_data, const Vector<InlineTestData> &p_all_data) {
258 if (p_test_data.ref.is_empty()) {
259 return;
260 }
261
262 SUBCASE(vformat("Can resolve symbol '%s' at %s to '%s'", p_test_data.text, p_test_data.range.to_string(), p_test_data.ref).utf8().get_data()) {
263 const InlineTestData *target = nullptr;
264 for (int i = 0; i < p_all_data.size(); i++) {
265 if (p_all_data[i].name == p_test_data.ref) {
266 target = &p_all_data[i];
267 break;
268 }
269 }
270 REQUIRE_MESSAGE(target, vformat("No target for ref '%s'", p_test_data.ref));
271
272 Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
273 LSP::Position pos = p_test_data.range.start;
274
275 SUBCASE("start of identifier") {
276 pos.character = p_test_data.range.start.character;
277 test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
278 }
279
280 SUBCASE("inside identifier") {
281 pos.character = (p_test_data.range.end.character + p_test_data.range.start.character) / 2;
282 test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
283 }
284
285 SUBCASE("end of identifier") {
286 pos.character = p_test_data.range.end.character;
287 test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
288 }
289 }
290 }
291
292 Vector<InlineTestData> filter_ref_towards(const Vector<InlineTestData> &p_data, const String &p_name) {
293 Vector<InlineTestData> res;
294
295 for (const InlineTestData &d : p_data) {
296 if (d.ref == p_name) {
297 res.append(d);
298 }
299 }
300
301 return res;
302 }
303
304 void test_resolve_symbols(const String &p_uri, const Vector<InlineTestData> &p_test_data, const Vector<InlineTestData> &p_all_data) {
305 for (const InlineTestData &d : p_test_data) {
306 test_resolve_symbol(p_uri, d, p_all_data);
307 }
308 }
309
310 void assert_no_errors_in(const String &p_path) {
311 Error err;
312 String source = FileAccess::get_file_as_string(p_path, &err);
313 REQUIRE_MESSAGE(err == OK, vformat("Cannot read '%s'", p_path));
314
315 GDScriptParser parser;
316 err = parser.parse(source, p_path, true);
317 REQUIRE_MESSAGE(err == OK, vformat("Errors while parsing '%s'", p_path));
318
319 GDScriptAnalyzer analyzer(&parser);
320 err = analyzer.analyze();
321 REQUIRE_MESSAGE(err == OK, vformat("Errors while analyzing '%s'", p_path));
322 }
323
324 inline LSP::Position lsp_pos(int line, int character) {
325 LSP::Position p;
326 p.line = line;
327 p.character = character;
328 return p;
329 }
330
331 void test_position_roundtrip(LSP::Position p_lsp, GodotPosition p_gd, const PackedStringArray &p_lines) {
332 GodotPosition actual_gd = GodotPosition::from_lsp(p_lsp, p_lines);
333 CHECK_EQ(p_gd, actual_gd);
334 LSP::Position actual_lsp = p_gd.to_lsp(p_lines);
335 CHECK_EQ(p_lsp, actual_lsp);
336 }
337
338 // Note:
339 // * Cursor is BETWEEN chars
340 // * `va|r` -> cursor between `a`&`r`
341 // * `var`
342 // ^
343 // -> Character on `r` -> cursor between `a`&`r`s for tests:
344 // * Line & Char:
345 // * LSP: both 0-based
346 // * Godot: both 1-based
347 TEST_SUITE("[Modules][GDScript][LSP][Editor]") {
348 TEST_CASE("Can convert positions to and from Godot") {
349 String code = R"(extends Node
350
351 var member := 42
352
353 func f():
354 var value := 42
355 return value + member)";
356 PackedStringArray lines = code.split("\n");
357
358 SUBCASE("line after end") {
359 LSP::Position lsp = lsp_pos(7, 0);
360 GodotPosition gd(8, 1);
361 test_position_roundtrip(lsp, gd, lines);
362 }
363 SUBCASE("first char in first line") {
364 LSP::Position lsp = lsp_pos(0, 0);
365 GodotPosition gd(1, 1);
366 test_position_roundtrip(lsp, gd, lines);
367 }
368
369 SUBCASE("with tabs") {
370 // On `v` in `value` in `var value := ...`.
371 LSP::Position lsp = lsp_pos(5, 6);
372 GodotPosition gd(6, 13);
373 test_position_roundtrip(lsp, gd, lines);
374 }
375
376 SUBCASE("doesn't fail with column outside of character length") {
377 LSP::Position lsp = lsp_pos(2, 100);
378 GodotPosition::from_lsp(lsp, lines);
379
380 GodotPosition gd(3, 100);
381 gd.to_lsp(lines);
382 }
383
384 SUBCASE("doesn't fail with line outside of line length") {
385 LSP::Position lsp = lsp_pos(200, 100);
386 GodotPosition::from_lsp(lsp, lines);
387
388 GodotPosition gd(300, 100);
389 gd.to_lsp(lines);
390 }
391
392 SUBCASE("special case: zero column for root class") {
393 GodotPosition gd(1, 0);
394 LSP::Position expected = lsp_pos(0, 0);
395 LSP::Position actual = gd.to_lsp(lines);
396 CHECK_EQ(actual, expected);
397 }
398 SUBCASE("special case: zero line and column for root class") {
399 GodotPosition gd(0, 0);
400 LSP::Position expected = lsp_pos(0, 0);
401 LSP::Position actual = gd.to_lsp(lines);
402 CHECK_EQ(actual, expected);
403 }
404 SUBCASE("special case: negative line for root class") {
405 GodotPosition gd(-1, 0);
406 LSP::Position expected = lsp_pos(0, 0);
407 LSP::Position actual = gd.to_lsp(lines);
408 CHECK_EQ(actual, expected);
409 }
410 SUBCASE("special case: lines.length() + 1 for root class") {
411 GodotPosition gd(lines.size() + 1, 0);
412 LSP::Position expected = lsp_pos(lines.size(), 0);
413 LSP::Position actual = gd.to_lsp(lines);
414 CHECK_EQ(actual, expected);
415 }
416 }
417 TEST_CASE("[workspace][resolve_symbol]") {
418 EditorFileSystem *efs = memnew(EditorFileSystem);
419 GDScriptLanguageProtocol *proto = initialize(root);
420 REQUIRE(proto);
421 Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
422
423 {
424 String path = "res://lsp/local_variables.gd";
425 assert_no_errors_in(path);
426 String uri = workspace->get_file_uri(path);
427 Vector<InlineTestData> all_test_data = read_tests(path);
428 SUBCASE("Can get correct ranges for public variables") {
429 Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "member");
430 test_resolve_symbols(uri, test_data, all_test_data);
431 }
432 SUBCASE("Can get correct ranges for local variables") {
433 Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "test");
434 test_resolve_symbols(uri, test_data, all_test_data);
435 }
436 SUBCASE("Can get correct ranges for local parameters") {
437 Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "arg");
438 test_resolve_symbols(uri, test_data, all_test_data);
439 }
440 }
441
442 SUBCASE("Can get correct ranges for indented variables") {
443 String path = "res://lsp/indentation.gd";
444 assert_no_errors_in(path);
445 String uri = workspace->get_file_uri(path);
446 Vector<InlineTestData> all_test_data = read_tests(path);
447 test_resolve_symbols(uri, all_test_data, all_test_data);
448 }
449
450 SUBCASE("Can get correct ranges for scopes") {
451 String path = "res://lsp/scopes.gd";
452 assert_no_errors_in(path);
453 String uri = workspace->get_file_uri(path);
454 Vector<InlineTestData> all_test_data = read_tests(path);
455 test_resolve_symbols(uri, all_test_data, all_test_data);
456 }
457
458 SUBCASE("Can get correct ranges for lambda") {
459 String path = "res://lsp/lambdas.gd";
460 assert_no_errors_in(path);
461 String uri = workspace->get_file_uri(path);
462 Vector<InlineTestData> all_test_data = read_tests(path);
463 test_resolve_symbols(uri, all_test_data, all_test_data);
464 }
465
466 SUBCASE("Can get correct ranges for inner class") {
467 String path = "res://lsp/class.gd";
468 assert_no_errors_in(path);
469 String uri = workspace->get_file_uri(path);
470 Vector<InlineTestData> all_test_data = read_tests(path);
471 test_resolve_symbols(uri, all_test_data, all_test_data);
472 }
473
474 SUBCASE("Can get correct ranges for inner class") {
475 String path = "res://lsp/enums.gd";
476 assert_no_errors_in(path);
477 String uri = workspace->get_file_uri(path);
478 Vector<InlineTestData> all_test_data = read_tests(path);
479 test_resolve_symbols(uri, all_test_data, all_test_data);
480 }
481
482 SUBCASE("Can get correct ranges for shadowing & shadowed variables") {
483 String path = "res://lsp/shadowing_initializer.gd";
484 assert_no_errors_in(path);
485 String uri = workspace->get_file_uri(path);
486 Vector<InlineTestData> all_test_data = read_tests(path);
487 test_resolve_symbols(uri, all_test_data, all_test_data);
488 }
489
490 SUBCASE("Can get correct ranges for properties and getter/setter") {
491 String path = "res://lsp/properties.gd";
492 assert_no_errors_in(path);
493 String uri = workspace->get_file_uri(path);
494 Vector<InlineTestData> all_test_data = read_tests(path);
495 test_resolve_symbols(uri, all_test_data, all_test_data);
496 }
497
498 memdelete(proto);
499 memdelete(efs);
500 finish_language();
501 }
502 TEST_CASE("[workspace][document_symbol]") {
503 EditorFileSystem *efs = memnew(EditorFileSystem);
504 GDScriptLanguageProtocol *proto = initialize(root);
505 REQUIRE(proto);
506
507 SUBCASE("selectionRange of root class must be inside range") {
508 LocalVector<String> paths = {
509 "res://lsp/first_line_comment.gd", // Comment on first line
510 "res://lsp/first_line_class_name.gd", // class_name (and thus selection range) before extends
511 };
512
513 for (const String &path : paths) {
514 assert_no_errors_in(path);
515 ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(path);
516 REQUIRE(parser);
517 LSP::DocumentSymbol cls = parser->get_symbols();
518
519 REQUIRE(((cls.range.start.line == cls.selectionRange.start.line && cls.range.start.character <= cls.selectionRange.start.character) || (cls.range.start.line < cls.selectionRange.start.line)));
520 REQUIRE(((cls.range.end.line == cls.selectionRange.end.line && cls.range.end.character >= cls.selectionRange.end.character) || (cls.range.end.line > cls.selectionRange.end.line)));
521 }
522 }
523
524 SUBCASE("Documentation is correctly set") {
525 String path = "res://lsp/doc_comments.gd";
526 assert_no_errors_in(path);
527 ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(path);
528 REQUIRE(parser);
529 LSP::DocumentSymbol cls = parser->get_symbols();
530 REQUIRE(cls.documentation.contains("brief"));
531 REQUIRE(cls.documentation.contains("description"));
532 REQUIRE(cls.documentation.contains("t1"));
533 REQUIRE(cls.documentation.contains("t2"));
534 REQUIRE(cls.documentation.contains("t3"));
535 }
536
537 memdelete(proto);
538 memdelete(efs);
539 finish_language();
540 }
541
542 TEST_CASE("BBCode to markdown conversion") {
543 // This tests the conversion from BBCode docstrings to the markdown markup sent to
544 // the LSP client on documentation requests
545
546 // Basic formatting
547 CHECK_EQ(LSP::marked_documentation("[b]bold[/b]"), "**bold**");
548 CHECK_EQ(LSP::marked_documentation("[i]italic[/i]"), "*italic*");
549 CHECK_EQ(LSP::marked_documentation("[u]underline[/u]"), "__underline__");
550 CHECK_EQ(LSP::marked_documentation("[s]strikethrough[/s]"), "~~strikethrough~~");
551 CHECK_EQ(LSP::marked_documentation("[code]code[/code]"), "`code`");
552 CHECK_EQ(LSP::marked_documentation("[kbd]Ctrl + S[/kbd]"), "`Ctrl + S`");
553
554 // Line breaks. We insert paragraphs for [br] because the BBCode to
555 // markdown conversion function simply makes the conversion line-wise and
556 // we don't distinguish markdown inline elements and blocks.
557 CHECK_EQ(LSP::marked_documentation("Line1[br]Line2"), "Line1\n\nLine2");
558
559 // These tags (center, color, font) aren't supported in markdown and should be stripped.
560 CHECK_EQ(LSP::marked_documentation("[center]Centered text[/center]"), "Centered text");
561 CHECK_EQ(LSP::marked_documentation("[color=red]red text[/color]"), "red text");
562 CHECK_EQ(LSP::marked_documentation("[font=Arial]Arial text[/font]"), "Arial text");
563
564 // The following tests are for all the link patterns specific to Godot's built-in docs that we render as inline code.
565 CHECK_EQ(LSP::marked_documentation("Class link: [Node2D], [Sprite2D]"), "Class link: `Node2D`, `Sprite2D`");
566 CHECK_EQ(LSP::marked_documentation("Single class [RigidBody2D]"), "Single class `RigidBody2D`");
567 CHECK_EQ(LSP::marked_documentation("[method Node2D.set_position]"), "`Node2D.set_position`");
568 CHECK_EQ(LSP::marked_documentation("[member Node2D.position]"), "`Node2D.position`");
569 CHECK_EQ(LSP::marked_documentation("[signal Node.ready]"), "`Node.ready`");
570 CHECK_EQ(LSP::marked_documentation("[constant Color.RED]"), "`Color.RED`");
571 CHECK_EQ(LSP::marked_documentation("[enum Node.ProcessMode]"), "`Node.ProcessMode`");
572 CHECK_EQ(LSP::marked_documentation("[annotation @GDScript.@export]"), "`@GDScript.@export`");
573 CHECK_EQ(LSP::marked_documentation("[constructor Vector2.Vector2]"), "`Vector2.Vector2`");
574 CHECK_EQ(LSP::marked_documentation("[operator Vector2.operator +]"), "`Vector2.operator +`");
575 CHECK_EQ(LSP::marked_documentation("[theme_item Button.font]"), "`Button.font`");
576 CHECK_EQ(LSP::marked_documentation("[param delta]"), "`delta`");
577
578 // Markdown links
579 CHECK_EQ(LSP::marked_documentation("[url=https://godotengine.org]link to Godot Engine[/url]"),
580 "[link to Godot Engine](https://godotengine.org)");
581 CHECK_EQ(LSP::marked_documentation("[url]https://godotengine.org/[/url]"),
582 "[https://godotengine.org/](https://godotengine.org/)");
583
584 // Code listings
585 CHECK_EQ(LSP::marked_documentation("[codeblock]\nfunc test():\n print(\"Hello, Godot!\")\n[/codeblock]"),
586 "```gdscript\nfunc test():\n print(\"Hello, Godot!\")\n```");
587 CHECK_EQ(LSP::marked_documentation("[codeblock lang=csharp]\npublic void Test()\n{\n GD.Print(\"Hello, Godot!\");\n}\n[/codeblock]"),
588 "```csharp\npublic void Test()\n{\n GD.Print(\"Hello, Godot!\");\n}\n```");
589 // Code listings with multiple languages (the codeblocks tag is used in the built-in reference)
590 // When [codeblocks] is used, we only convert the [gdscript] tag to a code block like the built-in editor.
591 // NOTE: There is always a GDScript code listing in the built-in class reference.
592 CHECK_EQ(LSP::marked_documentation("[codeblocks]\n[gdscript]\nprint(hash(\"a\")) # Prints 177670\n[/gdscript]\n[csharp]\nGD.Print(GD.Hash(\"a\")); // Prints 177670\n[/csharp]\n[/codeblocks]"),
593 "```gdscript\nprint(hash(\"a\")) # Prints 177670\n```\n");
594
595 // lb and rb are used to insert literal square brackets in markdown.
596 CHECK_EQ(LSP::marked_documentation("[lb]literal brackets[rb]"), "\\[literal brackets\\]");
597 CHECK_EQ(LSP::marked_documentation("[lb]literal[rb] with [ClassName]"), "\\[literal\\] with `ClassName`");
598
599 // We have to be careful that different patterns don't conflict with each
600 // other, especially with urls that use brackets in markdown.
601 CHECK_EQ(LSP::marked_documentation("Class [Sprite2D] with [url=https://godotengine.org]link[/url]"),
602 "Class `Sprite2D` with [link](https://godotengine.org)");
603 }
604 }
605
606 } // namespace GDScriptTests
607
608 #endif // GDSCRIPT_NO_LSP
609
610 #endif // TOOLS_ENABLED