GDScript: Fix head class range to include class_name

This commit is contained in:
HolonProduction 2025-03-14 10:56:42 +01:00
parent a77a28c029
commit d6da30e9c7
3 changed files with 24 additions and 14 deletions

View file

@ -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) { while (can_have_class_or_extends) {
// Order here doesn't matter, but there should be only one of each at most. // Order here doesn't matter, but there should be only one of each at most.
switch (current.type) { switch (current.type) {
case GDScriptTokenizer::Token::CLASS_NAME: case GDScriptTokenizer::Token::CLASS_NAME:
PUSH_PENDING_ANNOTATIONS_TO_HEAD; PUSH_PENDING_ANNOTATIONS_TO_HEAD;
if (head->start_line == 1) {
reset_extents(head, current);
}
advance(); advance();
if (head->identifier != nullptr) { if (head->identifier != nullptr) {
push_error(R"("class_name" can only be used once.)"); push_error(R"("class_name" can only be used once.)");
@ -689,9 +691,6 @@ void GDScriptParser::parse_program() {
break; break;
case GDScriptTokenizer::Token::EXTENDS: case GDScriptTokenizer::Token::EXTENDS:
PUSH_PENDING_ANNOTATIONS_TO_HEAD; PUSH_PENDING_ANNOTATIONS_TO_HEAD;
if (head->start_line == 1) {
reset_extents(head, current);
}
advance(); advance();
if (head->extends_used) { if (head->extends_used) {
push_error(R"("extends" can only be used once.)"); push_error(R"("extends" can only be used once.)");

View file

@ -0,0 +1,5 @@
class_name Test
extends Node
func _init():
pass

View file

@ -488,15 +488,21 @@ func f():
REQUIRE(proto); REQUIRE(proto);
SUBCASE("selectionRange of root class must be inside range") { SUBCASE("selectionRange of root class must be inside range") {
String path = "res://lsp/first_line_comment.gd"; LocalVector<String> paths = {
assert_no_errors_in(path); "res://lsp/first_line_comment.gd", // Comment on first line
GDScriptLanguageProtocol::get_singleton()->get_workspace()->parse_local_script(path); "res://lsp/first_line_class_name.gd", // class_name (and thus selection range) before extends
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))); for (const String &path : paths) {
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))); 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); memdelete(proto);