/**************************************************************************/ /* gdscript_docgen.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "gdscript_docgen.h" #include "../gdscript.h" #include "core/config/project_settings.h" HashMap GDScriptDocGen::singletons; String GDScriptDocGen::_get_script_name(const String &p_path) { const HashMap::ConstIterator E = singletons.find(p_path); if (E) { return E->value; } return p_path.trim_prefix("res://").quote(); } String GDScriptDocGen::_get_class_name(const GDP::ClassNode &p_class) { const GDP::ClassNode *curr_class = &p_class; if (!curr_class->identifier) { // All inner classes have an identifier, so this is the outer class. return _get_script_name(curr_class->fqcn); } String full_name = curr_class->identifier->name; while (curr_class->outer) { curr_class = curr_class->outer; if (!curr_class->identifier) { // All inner classes have an identifier, so this is the outer class. return vformat("%s.%s", _get_script_name(curr_class->fqcn), full_name); } full_name = vformat("%s.%s", curr_class->identifier->name, full_name); } return full_name; } void GDScriptDocGen::_doctype_from_gdtype(const GDType &p_gdtype, String &r_type, String &r_enum, bool p_is_return) { if (!p_gdtype.is_hard_type()) { r_type = "Variant"; return; } switch (p_gdtype.kind) { case GDType::BUILTIN: if (p_gdtype.builtin_type == Variant::NIL) { r_type = p_is_return ? "void" : "null"; return; } if (p_gdtype.builtin_type == Variant::ARRAY && p_gdtype.has_container_element_type(0)) { _doctype_from_gdtype(p_gdtype.get_container_element_type(0), r_type, r_enum); if (!r_enum.is_empty()) { r_type = "int[]"; r_enum += "[]"; return; } if (!r_type.is_empty() && r_type != "Variant") { r_type += "[]"; return; } } if (p_gdtype.builtin_type == Variant::DICTIONARY && p_gdtype.has_container_element_types()) { String key, value; _doctype_from_gdtype(p_gdtype.get_container_element_type_or_variant(0), key, r_enum); _doctype_from_gdtype(p_gdtype.get_container_element_type_or_variant(1), value, r_enum); if (key != "Variant" || value != "Variant") { r_type = "Dictionary[" + key + ", " + value + "]"; return; } } r_type = Variant::get_type_name(p_gdtype.builtin_type); return; case GDType::NATIVE: if (p_gdtype.is_meta_type) { //r_type = GDScriptNativeClass::get_class_static(); r_type = "Object"; // "GDScriptNativeClass" refers to a blank page. return; } r_type = p_gdtype.native_type; return; case GDType::SCRIPT: if (p_gdtype.is_meta_type) { r_type = p_gdtype.script_type.is_valid() ? p_gdtype.script_type->get_class() : Script::get_class_static(); return; } if (p_gdtype.script_type.is_valid()) { if (p_gdtype.script_type->get_global_name() != StringName()) { r_type = p_gdtype.script_type->get_global_name(); return; } if (!p_gdtype.script_type->get_path().is_empty()) { r_type = _get_script_name(p_gdtype.script_type->get_path()); return; } } if (!p_gdtype.script_path.is_empty()) { r_type = _get_script_name(p_gdtype.script_path); return; } r_type = "Object"; return; case GDType::CLASS: if (p_gdtype.is_meta_type) { r_type = GDScript::get_class_static(); return; } r_type = _get_class_name(*p_gdtype.class_type); return; case GDType::ENUM: if (p_gdtype.is_meta_type) { r_type = "Dictionary"; return; } r_type = "int"; r_enum = String(p_gdtype.native_type).replace("::", "."); if (r_enum.begins_with("res://")) { r_enum = r_enum.trim_prefix("res://"); int dot_pos = r_enum.rfind("."); if (dot_pos >= 0) { r_enum = r_enum.left(dot_pos).quote() + r_enum.substr(dot_pos); } } return; case GDType::VARIANT: case GDType::RESOLVING: case GDType::UNRESOLVED: r_type = "Variant"; return; } } String GDScriptDocGen::_docvalue_from_variant(const Variant &p_variant, int p_recursion_level) { constexpr int MAX_RECURSION_LEVEL = 2; switch (p_variant.get_type()) { case Variant::STRING: return String(p_variant).c_escape().quote(); case Variant::OBJECT: return ""; case Variant::DICTIONARY: { const Dictionary dict = p_variant; String result; if (dict.is_typed()) { result += "Dictionary["; Ref