From d6da30e9c77825530702bd37a557a42f8c86d814 Mon Sep 17 00:00:00 2001 From: HolonProduction Date: Fri, 14 Mar 2025 10:56:42 +0100 Subject: [PATCH] GDScript: Fix head class range to include `class_name` --- modules/gdscript/gdscript_parser.cpp | 11 +++++----- .../scripts/lsp/first_line_class_name.gd | 5 +++++ modules/gdscript/tests/test_lsp.h | 22 ++++++++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 modules/gdscript/tests/scripts/lsp/first_line_class_name.gd diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 337150a2264..a7a1c076be4 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -672,14 +672,16 @@ void GDScriptParser::parse_program() { } } + if (current.type == GDScriptTokenizer::Token::CLASS_NAME || current.type == GDScriptTokenizer::Token::EXTENDS) { + // Set range of the class to only start at extends or class_name if present. + reset_extents(head, current); + } + while (can_have_class_or_extends) { // Order here doesn't matter, but there should be only one of each at most. switch (current.type) { case GDScriptTokenizer::Token::CLASS_NAME: PUSH_PENDING_ANNOTATIONS_TO_HEAD; - if (head->start_line == 1) { - reset_extents(head, current); - } advance(); if (head->identifier != nullptr) { push_error(R"("class_name" can only be used once.)"); @@ -689,9 +691,6 @@ void GDScriptParser::parse_program() { break; case GDScriptTokenizer::Token::EXTENDS: PUSH_PENDING_ANNOTATIONS_TO_HEAD; - if (head->start_line == 1) { - reset_extents(head, current); - } advance(); if (head->extends_used) { push_error(R"("extends" can only be used once.)"); diff --git a/modules/gdscript/tests/scripts/lsp/first_line_class_name.gd b/modules/gdscript/tests/scripts/lsp/first_line_class_name.gd new file mode 100644 index 00000000000..2a836b71019 --- /dev/null +++ b/modules/gdscript/tests/scripts/lsp/first_line_class_name.gd @@ -0,0 +1,5 @@ +class_name Test +extends Node + +func _init(): + pass diff --git a/modules/gdscript/tests/test_lsp.h b/modules/gdscript/tests/test_lsp.h index f55343f5c50..d9f9f325c8d 100644 --- a/modules/gdscript/tests/test_lsp.h +++ b/modules/gdscript/tests/test_lsp.h @@ -488,15 +488,21 @@ func f(): REQUIRE(proto); SUBCASE("selectionRange of root class must be inside range") { - String path = "res://lsp/first_line_comment.gd"; - assert_no_errors_in(path); - GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_local_script(path); - ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_results[path]; - REQUIRE(parser); - lsp::DocumentSymbol cls = parser->get_symbols(); + LocalVector paths = { + "res://lsp/first_line_comment.gd", // Comment on first line + "res://lsp/first_line_class_name.gd", // class_name (and thus selection range) before extends + }; - 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))); - 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))); + for (const String &path : paths) { + assert_no_errors_in(path); + GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_local_script(path); + ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_results[path]; + REQUIRE(parser); + lsp::DocumentSymbol cls = parser->get_symbols(); + + 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))); + 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))); + } } memdelete(proto);