From c7f68a27ec4b825302998eeb5a400f869cd21cf7 Mon Sep 17 00:00:00 2001 From: kobewi Date: Sun, 9 Oct 2022 15:12:52 +0200 Subject: [PATCH] Add UID support to GDScript files --- editor/plugins/script_text_editor.cpp | 1 + modules/gdscript/doc_classes/@GDScript.xml | 10 +- modules/gdscript/gdscript.cpp | 122 ++++++++++++++++-- modules/gdscript/gdscript.h | 6 + modules/gdscript/gdscript_parser.cpp | 38 +++++- modules/gdscript/gdscript_parser.h | 4 + .../scripts/parser/errors/uid_duplicate.gd | 5 + .../scripts/parser/errors/uid_duplicate.out | 2 + .../scripts/parser/errors/uid_invalid.gd | 4 + .../scripts/parser/errors/uid_invalid.out | 2 + .../scripts/parser/errors/uid_too_late.gd | 5 + .../scripts/parser/errors/uid_too_late.out | 2 + .../tests/scripts/parser/features/uid.gd | 5 + .../tests/scripts/parser/features/uid.out | 1 + modules/gdscript/tests/test_gdscript_uid.h | 115 +++++++++++++++++ 15 files changed, 307 insertions(+), 15 deletions(-) create mode 100644 modules/gdscript/tests/scripts/parser/errors/uid_duplicate.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/uid_duplicate.out create mode 100644 modules/gdscript/tests/scripts/parser/errors/uid_invalid.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/uid_invalid.out create mode 100644 modules/gdscript/tests/scripts/parser/errors/uid_too_late.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/uid_too_late.out create mode 100644 modules/gdscript/tests/scripts/parser/features/uid.gd create mode 100644 modules/gdscript/tests/scripts/parser/features/uid.out create mode 100644 modules/gdscript/tests/test_gdscript_uid.h diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 45fb531d3707..babae9eae8be 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -146,6 +146,7 @@ void ScriptTextEditor::set_edited_resource(const Ref &p_res) { ERR_FAIL_COND(p_res.is_null()); script = p_res; + script->connect_changed(callable_mp((ScriptEditorBase *)this, &ScriptEditorBase::reload_text)); code_editor->get_text_editor()->set_text(script->get_source_code()); code_editor->get_text_editor()->clear_undo_history(); diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index 933bfba5bad2..b335bf8faec7 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -627,7 +627,7 @@ [/codeblock] [b]Note:[/b] Only the script can have a custom icon. Inner classes are not supported. [b]Note:[/b] As annotations describe their subject, the [annotation @icon] annotation must be placed before the class definition and inheritance. - [b]Note:[/b] Unlike other annotations, the argument of the [annotation @icon] annotation must be a string literal (constant expressions are not supported). + [b]Note:[/b] Unlike most other annotations, the argument of the [annotation @icon] annotation must be a string literal (constant expressions are not supported). @@ -681,6 +681,14 @@ [b]Note:[/b] As annotations describe their subject, the [annotation @tool] annotation must be placed before the class definition and inheritance. + + + + + Stores information about UID of this script. This annotation is auto-generated when saving the script and must not be modified manually. Only applies to scripts saved as separate files (i.e. not built-in). + [b]Note:[/b] Unlike most other annotations, the argument of the [annotation @uid] annotation must be a string literal (constant expressions are not supported). + + diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 1f0830aa1741..c4509aa5c3d4 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -55,6 +55,7 @@ #ifdef TOOLS_ENABLED #include "editor/editor_paths.h" +#include "editor/editor_settings.h" #endif #include @@ -1076,6 +1077,36 @@ Ref GDScript::get_base() const { return base; } +String GDScript::get_raw_source_code(const String &p_path, bool *r_error) { + Ref f = FileAccess::open(p_path, FileAccess::READ); + if (f.is_null()) { + if (r_error) { + *r_error = true; + } + return String(); + } + return f->get_as_utf8_string(); +} + +Vector2i GDScript::get_uid_lines(const String &p_source) { + GDScriptParser parser; + parser.parse(p_source, "", false); + const GDScriptParser::ClassNode *c = parser.get_tree(); + if (!c) { + return Vector2i(-1, -1); + } + return c->uid_lines; +} + +String GDScript::create_uid_line(const String &p_uid_str) { +#ifdef TOOLS_ENABLED + if (EDITOR_GET("text_editor/completion/use_single_quotes")) { + return vformat(R"(@uid('%s') # %s)", p_uid_str, RTR("Generated automatically, do not modify.")); + } +#endif + return vformat(R"(@uid("%s") # %s)", p_uid_str, RTR("Generated automatically, do not modify.")); +} + bool GDScript::inherits_script(const Ref