From e3714ecce1dfc20b9f48f8a30d05e0c8f07a548f Mon Sep 17 00:00:00 2001 From: Nicolas Merget <104347736+nmerget@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:20:32 +0200 Subject: [PATCH] feat: open .mjs files generated from .ts file inside external editor (#211) * feat: open .mjs files generated from .ts file inside external editor * fix: issue with external textedtior just used if TOOLS_ENABLED * chore: removed generating typescript project * fix: issue with OS::get_singleton()->execute --- editor/editor_tools.cpp | 21 ++--- editor/editor_tools.h | 5 -- ...criptInternal.svg => JavaScriptModule.svg} | 0 javascript.h | 1 + misc/generate/editor_tools.py | 14 +--- misc/typescript/package.json | 10 --- misc/typescript/tsconfig.json | 17 ---- src/language/javascript_todo_texteditor.cpp | 83 ++++++++++++++++++- 8 files changed, 88 insertions(+), 63 deletions(-) rename icons/{JavaScriptInternal.svg => JavaScriptModule.svg} (100%) delete mode 100644 misc/typescript/package.json delete mode 100644 misc/typescript/tsconfig.json diff --git a/editor/editor_tools.cpp b/editor/editor_tools.cpp index 5741eb56..1c0940a2 100644 --- a/editor/editor_tools.cpp +++ b/editor/editor_tools.cpp @@ -24,9 +24,9 @@ struct JavaScriptAlphCompare { }; static Error dump_to_file(const String &p_path, const String &p_content) { - Ref tsconfig = FileAccess::open(p_path, FileAccess::WRITE); - if (tsconfig.is_valid() && tsconfig->is_open()) { - tsconfig->store_string(p_content); + const Ref file = FileAccess::open(p_path, FileAccess::WRITE); + if (file.is_valid() && file->is_open()) { + file->store_string(p_content); return OK; } return FAILED; @@ -43,8 +43,8 @@ void JavaScriptPlugin::_notification(int p_what) { case MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN: { const HashSet> &scripts = JavaScriptLanguage::get_singleton()->get_scripts(); for (const Ref &s : scripts) { - uint64_t last_time = s->get_last_modified_time(); - uint64_t time = FileAccess::get_modified_time(s->get_script_path()); + const uint64_t last_time = s->get_last_modified_time(); + const uint64_t time = FileAccess::get_modified_time(s->get_script_path()); if (last_time != time) { JavaScriptLanguage::get_singleton()->reload_tool_script(s, true); } @@ -58,9 +58,6 @@ void JavaScriptPlugin::_on_menu_item_pressed(int item) { case MenuItem::ITEM_GEN_DECLARE_FILE: declaration_file_dialog->popup_centered_ratio(); break; - case MenuItem::ITEM_GEN_TYPESCRIPT_PROJECT: - _generate_typescript_project(); - break; case MenuItem::ITEM_GEN_ENUM_BINDING_SCRIPT: enumberation_file_dialog->popup_centered_ratio(); break; @@ -72,7 +69,6 @@ JavaScriptPlugin::JavaScriptPlugin(EditorNode *p_node) { add_tool_submenu_item(TTR("JavaScript"), menu); menu->add_item(TTR("Generate TypeScript Declaration File"), ITEM_GEN_DECLARE_FILE); menu->add_item(TTR("Generate Enumeration Binding Script"), ITEM_GEN_ENUM_BINDING_SCRIPT); - menu->add_item(TTR("Generate TypeScript Project"), ITEM_GEN_TYPESCRIPT_PROJECT); menu->connect("id_pressed", callable_mp(this, &JavaScriptPlugin::_on_menu_item_pressed)); declaration_file_dialog = memnew(EditorFileDialog); @@ -184,13 +180,6 @@ void JavaScriptPlugin::_export_enumeration_binding_file(const String &p_path) { dump_to_file(p_path, file_content); } -void JavaScriptPlugin::_generate_typescript_project() { - _export_typescript_declare_file("res://godot.d.ts"); - dump_to_file("res://tsconfig.json", TSCONFIG_CONTENT); - dump_to_file("res://decorators.ts", TS_DECORATORS_CONTENT); - dump_to_file("res://package.json", PACKAGE_JSON_CONTENT); -} - // The following functions are used to generate a godot.d.ts file out of the docs folder from godot #pragma region TS declare file diff --git a/editor/editor_tools.h b/editor/editor_tools.h index 7180abcf..6fd6efac 100644 --- a/editor/editor_tools.h +++ b/editor/editor_tools.h @@ -13,7 +13,6 @@ class JavaScriptPlugin : public EditorPlugin { enum MenuItem { ITEM_GEN_DECLARE_FILE, - ITEM_GEN_TYPESCRIPT_PROJECT, ITEM_GEN_ENUM_BINDING_SCRIPT, }; @@ -24,9 +23,6 @@ class JavaScriptPlugin : public EditorPlugin { protected: /* Strings will be generated by ./SCsub from misc directory */ static String BUILTIN_DECLARATION_TEXT; - static String TSCONFIG_CONTENT; - static String TS_DECORATORS_CONTENT; - static String PACKAGE_JSON_CONTENT; /* Missing declarations, ignoring*/ static Dictionary DECLARATION_CONSTRUCTORS; @@ -39,7 +35,6 @@ class JavaScriptPlugin : public EditorPlugin { void _on_menu_item_pressed(int item); void _export_typescript_declare_file(const String &p_path); void _export_enumeration_binding_file(const String &p_path); - void _generate_typescript_project(); public: virtual String get_name() const override { return "JavaScriptPlugin"; } diff --git a/icons/JavaScriptInternal.svg b/icons/JavaScriptModule.svg similarity index 100% rename from icons/JavaScriptInternal.svg rename to icons/JavaScriptModule.svg diff --git a/javascript.h b/javascript.h index 98bfc4eb..68c7eb30 100644 --- a/javascript.h +++ b/javascript.h @@ -14,6 +14,7 @@ #define EXT_TSCLASS "ts" #define EXT_JSMODULE "js" #define EXT_JSON "json" +#define EXT_GENERATE "//generatedPath=" class JavaScript : public Script { GDCLASS(JavaScript, Script); diff --git a/misc/generate/editor_tools.py b/misc/generate/editor_tools.py index 63014b8f..2703d304 100644 --- a/misc/generate/editor_tools.py +++ b/misc/generate/editor_tools.py @@ -12,17 +12,5 @@ def get_editor_tools_files(): "editor/godot.d.ts.gen.cpp": ( "BUILTIN_DECLARATION_TEXT", "misc/typescript/godot.d.ts", - ), - "editor/tsconfig.json.gen.cpp": ( - "TSCONFIG_CONTENT", - "misc/typescript/tsconfig.json", - ), - "editor/decorators.ts.gen.cpp": ( - "TS_DECORATORS_CONTENT", - "misc/typescript/decorators.ts", - ), - "editor/package.json.gen.cpp": ( - "PACKAGE_JSON_CONTENT", - "misc/typescript/package.json", - ), + ) } diff --git a/misc/typescript/package.json b/misc/typescript/package.json deleted file mode 100644 index 88c2e7d0..00000000 --- a/misc/typescript/package.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "scripts": { - "watch": "tsc -w -p .", - "compile": "tsc -p ." - }, - "devDependencies": { - "typescript": "^3.9.7" - }, - "types": "godot.d.ts" -} diff --git a/misc/typescript/tsconfig.json b/misc/typescript/tsconfig.json deleted file mode 100644 index 5d48ca9c..00000000 --- a/misc/typescript/tsconfig.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "include": ["."], - "exclude": [".import"], - "compilerOptions": { - "rootDir": ".", - "target": "ESNext", - "module": "ESNext", - "lib": ["ESNext"], - "experimentalDecorators": true, - "jsx": "preserve", - "sourceMap": true, - "sourceRoot": ".", - "paths": { - "decorators": ["./decorators"], - } - } -} diff --git a/src/language/javascript_todo_texteditor.cpp b/src/language/javascript_todo_texteditor.cpp index c8d2dec1..93bf07eb 100644 --- a/src/language/javascript_todo_texteditor.cpp +++ b/src/language/javascript_todo_texteditor.cpp @@ -2,13 +2,92 @@ #include "javascript_language.h" +#include +#include + +#ifdef TOOLS_ENABLED +#include +#endif + int JavaScriptLanguage::find_function(const String &p_function, const String &p_code) const { return -1; } String JavaScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const { return ""; } void JavaScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {} bool JavaScriptLanguage::supports_documentation() const { return false; } bool JavaScriptLanguage::can_inherit_from_file() const { return false; } -Error JavaScriptLanguage::open_in_external_editor(const Ref