Skip to content

Commit

Permalink
Added Structs to GDScript
Browse files Browse the repository at this point in the history
  • Loading branch information
nlupugla committed Oct 3, 2024
1 parent 1076403 commit 35d7383
Show file tree
Hide file tree
Showing 42 changed files with 1,571 additions and 704 deletions.
1 change: 1 addition & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class ScriptLanguage : public Object {
CODE_COMPLETION_KIND_NODE_PATH,
CODE_COMPLETION_KIND_FILE_PATH,
CODE_COMPLETION_KIND_PLAIN_TEXT,
CODE_COMPLETION_KIND_STRUCT,
CODE_COMPLETION_KIND_MAX
};

Expand Down
1 change: 1 addition & 0 deletions core/object/script_language_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,6 @@ void ScriptLanguageExtension::_bind_methods() {
BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_NODE_PATH);
BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_FILE_PATH);
BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_PLAIN_TEXT);
BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_STRUCT);
BIND_ENUM_CONSTANT(CODE_COMPLETION_KIND_MAX);
}
2 changes: 2 additions & 0 deletions doc/classes/CodeEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@
<constant name="KIND_PLAIN_TEXT" value="9" enum="CodeCompletionKind">
Marks the option as unclassified or plain text.
</constant>
<constant name="KIND_STRUCT" value="10" enum="CodeCompletionKind">
</constant>
<constant name="LOCATION_LOCAL" value="0" enum="CodeCompletionLocation">
The option is local to the location of the code completion query - e.g. a local variable. Subsequent value of location represent options from the outer class, the exact value represent how far they are (in terms of inner classes).
</constant>
Expand Down
4 changes: 3 additions & 1 deletion doc/classes/ScriptLanguageExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@
</constant>
<constant name="CODE_COMPLETION_KIND_PLAIN_TEXT" value="9" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_MAX" value="10" enum="CodeCompletionKind">
<constant name="CODE_COMPLETION_KIND_STRUCT" value="10" enum="CodeCompletionKind">
</constant>
<constant name="CODE_COMPLETION_KIND_MAX" value="11" enum="CodeCompletionKind">
</constant>
</constants>
</class>
12 changes: 12 additions & 0 deletions modules/gdscript/editor/gdscript_docgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ void GDScriptDocGen::_doctype_from_gdtype(const GDType &p_gdtype, String &r_type
}
}
return;
case GDType::STRUCT:
r_type = "Array";
// TODO: just copied from enum for now, probably needs Struct specific logic
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:
Expand Down
32 changes: 32 additions & 0 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,33 @@ GDScript *GDScript::get_root_script() {
return result;
}

const StructInfo *GDScript::get_script_struct_info(const String &p_struct_name, bool p_no_inheritance) const {
if (const StructInfo *info = structs.getptr(p_struct_name)) {
return info;
}

Vector<String> names = String(p_struct_name).split(".");
// TODO: this is to handle cases where the name is something like U"res://main.gd.MyStruct" but there should probably be a better solution
const StructInfo *info = structs.getptr(names[names.size() - 1]);
if (p_no_inheritance) {
return info;
}
// TODO: walk through inheritance chain to look for struct info.
return info;
}

void GDScript::set_script_struct_info(const StructInfo &p_struct_info) {
if (ClassDB::get_struct_info(p_struct_info.name)) {
// TODO: warn about shadowing native struct?
return;
}
if (structs.has(p_struct_info.name)) {
// TODO: warn about shadowing script struct?
return;
}
structs.insert(p_struct_info.name, p_struct_info);
}

RBSet<GDScript *> GDScript::get_dependencies() {
RBSet<GDScript *> dependencies;

Expand Down Expand Up @@ -1361,6 +1388,7 @@ void GDScript::_collect_function_dependencies(GDScriptFunction *p_func, RBSet<GD
}

void GDScript::_collect_dependencies(RBSet<GDScript *> &p_dependencies, const GDScript *p_except) {
// TODO: Do I need Struct logic here?
if (p_dependencies.has(this)) {
return;
}
Expand Down Expand Up @@ -1410,6 +1438,7 @@ GDScript::GDScript() :
}

void GDScript::_save_orphaned_subclasses(ClearData *p_clear_data) {
// TODO: Do I need Struct logic here?
struct ClassRefWithName {
ObjectID id;
String fully_qualified_name;
Expand Down Expand Up @@ -2253,6 +2282,8 @@ void GDScriptLanguage::init() {
_add_global(n, nc);
}

// TODO: Structs?

//populate singletons

List<Engine::Singleton> singletons;
Expand Down Expand Up @@ -2741,6 +2772,7 @@ void GDScriptLanguage::get_reserved_words(List<String> *p_words) const {
"namespace", // Reserved for potential future use.
"signal",
"static",
"struct",
"trait", // Reserved for potential future use.
"var",
// Other keywords.
Expand Down
3 changes: 3 additions & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class GDScript : public Script {
HashMap<StringName, Variant> constants;
HashMap<StringName, GDScriptFunction *> member_functions;
HashMap<StringName, Ref<GDScript>> subclasses;
HashMap<StringName, StructInfo> structs;
HashMap<StringName, MethodInfo> _signals;
Dictionary rpc_config;

Expand Down Expand Up @@ -259,6 +260,8 @@ class GDScript : public Script {
}
const HashMap<StringName, GDScriptFunction *> &get_member_functions() const { return member_functions; }
const Ref<GDScriptNativeClass> &get_native() const { return native; }
const StructInfo *get_script_struct_info(const String &p_struct_name, bool p_no_inheritance = false) const;
void set_script_struct_info(const StructInfo &p_struct_info);

RBSet<GDScript *> get_dependencies();
HashMap<GDScript *, RBSet<GDScript *>> get_all_dependencies();
Expand Down
Loading

0 comments on commit 35d7383

Please sign in to comment.