GDScript: Use autoload singleton name in `GDScriptDocGen`

This commit is contained in:
Danil Alexeev 2024-01-20 23:11:55 +03:00
parent 0bcc0e92b3
commit 5e7cda3405
No known key found for this signature in database
GPG Key ID: 124453E157DA8DC7
2 changed files with 42 additions and 20 deletions

View File

@ -32,21 +32,29 @@
#include "../gdscript.h"
String GDScriptDocGen::_get_script_path(const String &p_path) {
#include "core/config/project_settings.h"
HashMap<String, String> GDScriptDocGen::singletons;
String GDScriptDocGen::_get_script_name(const String &p_path) {
const HashMap<String, String>::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_path(curr_class->fqcn);
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_path(curr_class->fqcn), full_name);
return vformat("%s.%s", _get_script_name(curr_class->fqcn), full_name);
}
full_name = vformat("%s.%s", curr_class->identifier->name, full_name);
}
@ -97,12 +105,12 @@ void GDScriptDocGen::_doctype_from_gdtype(const GDType &p_gdtype, String &r_type
return;
}
if (!p_gdtype.script_type->get_path().is_empty()) {
r_type = _get_script_path(p_gdtype.script_type->get_path());
r_type = _get_script_name(p_gdtype.script_type->get_path());
return;
}
}
if (!p_gdtype.script_path.is_empty()) {
r_type = _get_script_path(p_gdtype.script_path);
r_type = _get_script_name(p_gdtype.script_path);
return;
}
r_type = "Object";
@ -221,25 +229,26 @@ String GDScriptDocGen::_docvalue_from_variant(const Variant &p_variant, int p_re
}
}
void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
void GDScriptDocGen::_generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
p_script->_clear_doc();
DocData::ClassDoc &doc = p_script->doc;
doc.script_path = _get_script_path(p_script->get_script_path());
if (p_script->local_name == StringName()) {
doc.name = doc.script_path;
} else {
doc.name = p_script->local_name;
}
if (p_script->_owner) {
doc.name = p_script->_owner->doc.name + "." + doc.name;
doc.script_path = doc.script_path + "." + doc.name;
}
doc.is_script_doc = true;
if (p_script->local_name == StringName()) {
// This is an outer unnamed class.
doc.name = _get_script_name(p_script->get_script_path());
} else {
// This is an inner or global outer class.
doc.name = p_script->local_name;
if (p_script->_owner) {
doc.name = p_script->_owner->doc.name + "." + doc.name;
}
}
doc.script_path = p_script->get_script_path();
if (p_script->base.is_valid() && p_script->base->is_valid()) {
if (!p_script->base->doc.name.is_empty()) {
doc.inherits = p_script->base->doc.name;
@ -271,7 +280,7 @@ void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_c
// Recursively generate inner class docs.
// Needs inner GDScripts to exist: previously generated in GDScriptCompiler::make_scripts().
GDScriptDocGen::generate_docs(*p_script->subclasses[class_name], inner_class);
GDScriptDocGen::_generate_docs(*p_script->subclasses[class_name], inner_class);
} break;
case GDP::ClassNode::Member::CONSTANT: {
@ -451,3 +460,13 @@ void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_c
// Add doc to the outer-most class.
p_script->_add_doc(doc);
}
void GDScriptDocGen::generate_docs(GDScript *p_script, const GDP::ClassNode *p_class) {
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
if (E.value.is_singleton) {
singletons[E.value.path] = E.key;
}
}
_generate_docs(p_script, p_class);
singletons.clear();
}

View File

@ -39,10 +39,13 @@ class GDScriptDocGen {
using GDP = GDScriptParser;
using GDType = GDP::DataType;
static String _get_script_path(const String &p_path);
static HashMap<String, String> singletons; // Script path to singleton name.
static String _get_script_name(const String &p_path);
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 void _generate_docs(GDScript *p_script, const GDP::ClassNode *p_class);
public:
static void generate_docs(GDScript *p_script, const GDP::ClassNode *p_class);