From 0c3bfae3efa746bbcfc498531ac5427667a35f2c Mon Sep 17 00:00:00 2001 From: PhairZ Date: Fri, 14 Mar 2025 04:18:38 +0200 Subject: [PATCH] Implement declaration and lambda function tooltips. --- modules/gdscript/editor/gdscript_docgen.cpp | 5 +++++ modules/gdscript/gdscript_editor.cpp | 9 +++++++++ modules/gdscript/gdscript_parser.cpp | 12 ++++++++++++ modules/gdscript/gdscript_parser.h | 2 +- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/gdscript/editor/gdscript_docgen.cpp b/modules/gdscript/editor/gdscript_docgen.cpp index 6ac65d4cd5..b9a8921436 100644 --- a/modules/gdscript/editor/gdscript_docgen.cpp +++ b/modules/gdscript/editor/gdscript_docgen.cpp @@ -316,6 +316,11 @@ String GDScriptDocGen::docvalue_from_expression(const GDP::ExpressionNode *p_exp const GDP::IdentifierNode *id = static_cast(p_expression); return id->name; } break; + case GDP::Node::LAMBDA: { + const GDP::LambdaNode *lambda = static_cast(p_expression); + const GDP::IdentifierNode *id = lambda->function->identifier; + return id != nullptr ? id->name : ""; + } break; default: { // Nothing to do. } break; diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index 72f4371b24..53ea56fccd 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -3465,6 +3465,7 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c switch (completion_context.type) { case GDScriptParser::COMPLETION_NONE: + case GDScriptParser::COMPLETION_DECLARATION: break; case GDScriptParser::COMPLETION_ANNOTATION: { List annotations; @@ -4412,6 +4413,8 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co case GDScriptParser::COMPLETION_METHOD: case GDScriptParser::COMPLETION_ASSIGN: case GDScriptParser::COMPLETION_CALL_ARGUMENTS: + case GDScriptParser::COMPLETION_DECLARATION: + case GDScriptParser::COMPLETION_INHERIT_TYPE: case GDScriptParser::COMPLETION_IDENTIFIER: case GDScriptParser::COMPLETION_PROPERTY_METHOD: case GDScriptParser::COMPLETION_SUBSCRIPT: { @@ -4604,8 +4607,14 @@ static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, co } } break; case GDScriptParser::COMPLETION_OVERRIDE_METHOD: { + // This logic applies to any method declaration (override or not), + // but shows parent documentation on virtual method overrides. GDScriptParser::DataType base_type = context.current_class->base_type; + if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { + return OK; + } + base_type = context.current_class->get_datatype(); if (_lookup_symbol_from_base(base_type, p_symbol, r_result) == OK) { return OK; } diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index 26731118c2..30dc91111c 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -938,6 +938,8 @@ bool GDScriptParser::has_class(const GDScriptParser::ClassNode *p_class) const { GDScriptParser::ClassNode *GDScriptParser::parse_class(bool p_is_static) { ClassNode *n_class = alloc_node(); + make_completion_context(COMPLETION_DECLARATION, n_class); + ClassNode *previous_class = current_class; current_class = n_class; n_class->outer = previous_class; @@ -994,6 +996,8 @@ void GDScriptParser::parse_class_name() { current_class->fqcn = String(current_class->identifier->name); } + make_completion_context(COMPLETION_DECLARATION, current_class); + if (match(GDScriptTokenizer::Token::EXTENDS)) { // Allow extends on the same line. parse_extends(); @@ -1227,6 +1231,8 @@ GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static) { GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static, bool p_allow_property) { VariableNode *variable = alloc_node(); + make_completion_context(COMPLETION_DECLARATION, variable); + if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected variable name after "var".)")) { complete_extents(variable); return nullptr; @@ -1463,6 +1469,8 @@ void GDScriptParser::parse_property_getter(VariableNode *p_variable) { GDScriptParser::ConstantNode *GDScriptParser::parse_constant(bool p_is_static) { ConstantNode *constant = alloc_node(); + make_completion_context(COMPLETION_DECLARATION, constant); + if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected constant name after "const".)")) { complete_extents(constant); return nullptr; @@ -1531,6 +1539,8 @@ GDScriptParser::ParameterNode *GDScriptParser::parse_parameter() { GDScriptParser::SignalNode *GDScriptParser::parse_signal(bool p_is_static) { SignalNode *signal = alloc_node(); + make_completion_context(COMPLETION_DECLARATION, signal); + if (!consume(GDScriptTokenizer::Token::IDENTIFIER, R"(Expected signal name after "signal".)")) { complete_extents(signal); return nullptr; @@ -1577,6 +1587,8 @@ GDScriptParser::EnumNode *GDScriptParser::parse_enum(bool p_is_static) { EnumNode *enum_node = alloc_node(); bool named = false; + make_completion_context(COMPLETION_DECLARATION, enum_node); + if (match(GDScriptTokenizer::Token::IDENTIFIER)) { enum_node->identifier = parse_identifier(); named = true; diff --git a/modules/gdscript/gdscript_parser.h b/modules/gdscript/gdscript_parser.h index 0b2e790e06..fecd37c118 100644 --- a/modules/gdscript/gdscript_parser.h +++ b/modules/gdscript/gdscript_parser.h @@ -1297,7 +1297,7 @@ public: COMPLETION_ATTRIBUTE_METHOD, // After id.| to look for methods. COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD, // Constants inside a built-in type (e.g. Color.BLUE) or static methods (e.g. Color.html). COMPLETION_CALL_ARGUMENTS, // Complete with nodes, input actions, enum values (or usual expressions). - // TODO: COMPLETION_DECLARATION, // Potential declaration (var, const, func). + COMPLETION_DECLARATION, // Potential declaration (var, const, class, etc.). COMPLETION_GET_NODE, // Get node with $ notation. COMPLETION_IDENTIFIER, // List available identifiers in scope. COMPLETION_INHERIT_TYPE, // Type after extends. Exclude non-viable types (built-ins, enums, void). Includes subtypes using the argument index.