From e4b408b96772006a157d19b9b63e33f48b4b8ffa Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Wed, 15 Jan 2025 16:34:19 -0500 Subject: [PATCH] Detect (partially) functions declarations from identifiers --- modules/gdscript/gdscript_editor.cpp | 66 ++++++++++++---------------- modules/gdscript/gdscript_parser.cpp | 2 +- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index 1c29c9fd8649..c9de72bae393 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -3678,6 +3678,8 @@ static void _refactor_rename_symbol_add_match(const String &p_path, const GDScri // This function finds and match all instances of the symbol inside the class. static Error _refactor_rename_symbol_match_from_class_find_instances_inside(const String &p_symbol, const String &p_path, ScriptLanguage::RefactorRenameSymbolResult &r_result, const GDScriptParser::ClassNode *p_class_node, const GDScriptParser::ClassNode::Member &p_member) { LocalVector target_sources; + // The definition. + target_sources.push_back(GDScriptParser::IdentifierNode::Source::UNDEFINED_SOURCE); GDScriptParser::Node::Type expected_undefined_source_type; switch (p_member.type) { case GDScriptParser::ClassNode::Member::CLASS: { @@ -3698,17 +3700,11 @@ static Error _refactor_rename_symbol_match_from_class_find_instances_inside(cons } break; case GDScriptParser::ClassNode::Member::VARIABLE: { expected_undefined_source_type = GDScriptParser::Node::VARIABLE; - // The definition. - target_sources.push_back(GDScriptParser::IdentifierNode::Source::UNDEFINED_SOURCE); - // The use. target_sources.push_back(GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE); } break; case GDScriptParser::ClassNode::Member::ENUM: case GDScriptParser::ClassNode::Member::ENUM_VALUE: { expected_undefined_source_type = GDScriptParser::Node::ENUM; - // The definition. - target_sources.push_back(GDScriptParser::IdentifierNode::Source::UNDEFINED_SOURCE); - // The use. target_sources.push_back(GDScriptParser::IdentifierNode::Source::MEMBER_CONSTANT); } break; default: { @@ -3740,48 +3736,44 @@ static Error _refactor_rename_symbol_match_from_class_find_instances_inside(cons if (identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE) { continue; } + + GDScriptParser::Node::Type member_type; + GDScriptParser::IdentifierNode *member_identifier; switch (member_source_node->type) { - case GDScriptParser::Node::IDENTIFIER: { - // We can compare by the identifier name. - GDScriptParser::IdentifierNode *member_identifier = static_cast(member_source_node); - if (member_identifier->name != identifier->name) { - continue; - } - } break; case GDScriptParser::Node::CLASS: { - if (expected_undefined_source_type != GDScriptParser::Node::Type::CLASS) { - continue; - } - // We can compare by the variable identifier name. + member_type = GDScriptParser::Node::Type::CLASS; GDScriptParser::ClassNode *member_class = static_cast(member_source_node); - if (member_class->identifier == nullptr || member_class->identifier->name != identifier->name) { - continue; - } - } break; - case GDScriptParser::Node::VARIABLE: { - if (expected_undefined_source_type != GDScriptParser::Node::Type::VARIABLE) { - continue; - } - // We can compare by the variable identifier name. - GDScriptParser::VariableNode *member_variable = static_cast(member_source_node); - if (member_variable->identifier == nullptr || member_variable->identifier->name != identifier->name) { - continue; - } + member_identifier = member_class->identifier; } break; case GDScriptParser::Node::ENUM: { - if (expected_undefined_source_type != GDScriptParser::Node::Type::ENUM) { - continue; - } - // We can compare by the enum identifier name. + member_type = GDScriptParser::Node::Type::ENUM; GDScriptParser::EnumNode *member_enum = static_cast(member_source_node); - if (member_enum->identifier == nullptr || member_enum->identifier->name != identifier->name) { - continue; - } + member_identifier = member_enum->identifier; + } break; + case GDScriptParser::Node::FUNCTION: { + member_type = GDScriptParser::Node::Type::FUNCTION; + GDScriptParser::FunctionNode *member_function = static_cast(member_source_node); + member_identifier = member_function->identifier; + } break; + case GDScriptParser::Node::IDENTIFIER: { + member_type = GDScriptParser::Node::Type::IDENTIFIER; + member_identifier = static_cast(member_source_node); + } break; + case GDScriptParser::Node::VARIABLE: { + member_type = GDScriptParser::Node::Type::VARIABLE; + GDScriptParser::VariableNode *member_variable = static_cast(member_source_node); + member_identifier = member_variable->identifier; } break; default: { continue; } } + if (member_type != GDScriptParser::Node::IDENTIFIER && expected_undefined_source_type != member_type) { + continue; + } + if (identifier->name != member_identifier->name) { + continue; + } } else { // If the identifier has a source node, we need to compare it the current node. if (member_source_node != target_source_node) { diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 4401a8ba14ab..1c233b3890b6 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -1701,7 +1701,7 @@ GDScriptParser::FunctionNode *GDScriptParser::parse_function(bool p_is_static) { FunctionNode *function = alloc_node(); make_completion_context(COMPLETION_OVERRIDE_METHOD, function); - make_refactor_rename_context(REFACTOR_RENAME_TYPE_OVERRIDE_METHOD, function); + make_refactor_rename_context(REFACTOR_RENAME_TYPE_METHOD, function); if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected function name after "func".)")) { complete_extents(function);