diff --git a/modules/gdscript/editor/gdscript_docgen.cpp b/modules/gdscript/editor/gdscript_docgen.cpp index 601db5414b8..35b69fab8c1 100644 --- a/modules/gdscript/editor/gdscript_docgen.cpp +++ b/modules/gdscript/editor/gdscript_docgen.cpp @@ -229,6 +229,36 @@ String GDScriptDocGen::_docvalue_from_variant(const Variant &p_variant, int p_re } } +String GDScriptDocGen::_docvalue_from_expression(const GDP::ExpressionNode *p_expression) { + ERR_FAIL_NULL_V(p_expression, String()); + + if (p_expression->is_constant) { + return _docvalue_from_variant(p_expression->reduced_value); + } + + switch (p_expression->type) { + case GDP::Node::ARRAY: { + const GDP::ArrayNode *array = static_cast(p_expression); + return array->elements.is_empty() ? "[]" : "[...]"; + } break; + case GDP::Node::CALL: { + const GDP::CallNode *call = static_cast(p_expression); + return call->function_name.operator String() + (call->arguments.is_empty() ? "()" : "(...)"); + } break; + case GDP::Node::DICTIONARY: { + const GDP::DictionaryNode *dict = static_cast(p_expression); + return dict->elements.is_empty() ? "{}" : "{...}"; + } break; + case GDP::Node::IDENTIFIER: { + const GDP::IdentifierNode *id = static_cast(p_expression); + return id->name; + } break; + default: { + return ""; + } break; + } +} + void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) { p_script->_clear_doc(); @@ -328,16 +358,12 @@ void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_ method_doc.return_type = "Variant"; } - for (const GDScriptParser::ParameterNode *p : m_func->parameters) { + for (const GDP::ParameterNode *p : m_func->parameters) { DocData::ArgumentDoc arg_doc; arg_doc.name = p->identifier->name; _doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration); if (p->initializer != nullptr) { - if (p->initializer->is_constant) { - arg_doc.default_value = _docvalue_from_variant(p->initializer->reduced_value); - } else { - arg_doc.default_value = ""; - } + arg_doc.default_value = _docvalue_from_expression(p->initializer); } method_doc.arguments.push_back(arg_doc); } @@ -359,7 +385,7 @@ void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_ signal_doc.is_experimental = m_signal->doc_data.is_experimental; signal_doc.experimental_message = m_signal->doc_data.experimental_message; - for (const GDScriptParser::ParameterNode *p : m_signal->parameters) { + for (const GDP::ParameterNode *p : m_signal->parameters) { DocData::ArgumentDoc arg_doc; arg_doc.name = p->identifier->name; _doctype_from_gdtype(p->get_datatype(), arg_doc.type, arg_doc.enumeration); @@ -405,12 +431,8 @@ void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_ break; } - if (m_var->initializer) { - if (m_var->initializer->is_constant) { - prop_doc.default_value = _docvalue_from_variant(m_var->initializer->reduced_value); - } else { - prop_doc.default_value = ""; - } + if (m_var->initializer != nullptr) { + prop_doc.default_value = _docvalue_from_expression(m_var->initializer); } prop_doc.overridden = false; diff --git a/modules/gdscript/editor/gdscript_docgen.h b/modules/gdscript/editor/gdscript_docgen.h index 651a4fb198f..0ae37c41330 100644 --- a/modules/gdscript/editor/gdscript_docgen.h +++ b/modules/gdscript/editor/gdscript_docgen.h @@ -45,6 +45,7 @@ class GDScriptDocGen { static String _get_class_name(const GDP::ClassNode &p_class); static void _doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return = false); static String _docvalue_from_variant(const Variant &p_variant, int p_recursion_level = 1); + static String _docvalue_from_expression(const GDP::ExpressionNode *p_expression); static void _generate_docs(GDScript *p_script, const GDP::ClassNode *p_class); public: