Skip to content

Commit e3714ec

Browse files
authored
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
1 parent a66faad commit e3714ec

File tree

8 files changed

+88
-63
lines changed

8 files changed

+88
-63
lines changed

editor/editor_tools.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ struct JavaScriptAlphCompare {
2424
};
2525

2626
static Error dump_to_file(const String &p_path, const String &p_content) {
27-
Ref<FileAccess> tsconfig = FileAccess::open(p_path, FileAccess::WRITE);
28-
if (tsconfig.is_valid() && tsconfig->is_open()) {
29-
tsconfig->store_string(p_content);
27+
const Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE);
28+
if (file.is_valid() && file->is_open()) {
29+
file->store_string(p_content);
3030
return OK;
3131
}
3232
return FAILED;
@@ -43,8 +43,8 @@ void JavaScriptPlugin::_notification(int p_what) {
4343
case MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN: {
4444
const HashSet<Ref<JavaScript>> &scripts = JavaScriptLanguage::get_singleton()->get_scripts();
4545
for (const Ref<JavaScript> &s : scripts) {
46-
uint64_t last_time = s->get_last_modified_time();
47-
uint64_t time = FileAccess::get_modified_time(s->get_script_path());
46+
const uint64_t last_time = s->get_last_modified_time();
47+
const uint64_t time = FileAccess::get_modified_time(s->get_script_path());
4848
if (last_time != time) {
4949
JavaScriptLanguage::get_singleton()->reload_tool_script(s, true);
5050
}
@@ -58,9 +58,6 @@ void JavaScriptPlugin::_on_menu_item_pressed(int item) {
5858
case MenuItem::ITEM_GEN_DECLARE_FILE:
5959
declaration_file_dialog->popup_centered_ratio();
6060
break;
61-
case MenuItem::ITEM_GEN_TYPESCRIPT_PROJECT:
62-
_generate_typescript_project();
63-
break;
6461
case MenuItem::ITEM_GEN_ENUM_BINDING_SCRIPT:
6562
enumberation_file_dialog->popup_centered_ratio();
6663
break;
@@ -72,7 +69,6 @@ JavaScriptPlugin::JavaScriptPlugin(EditorNode *p_node) {
7269
add_tool_submenu_item(TTR("JavaScript"), menu);
7370
menu->add_item(TTR("Generate TypeScript Declaration File"), ITEM_GEN_DECLARE_FILE);
7471
menu->add_item(TTR("Generate Enumeration Binding Script"), ITEM_GEN_ENUM_BINDING_SCRIPT);
75-
menu->add_item(TTR("Generate TypeScript Project"), ITEM_GEN_TYPESCRIPT_PROJECT);
7672
menu->connect("id_pressed", callable_mp(this, &JavaScriptPlugin::_on_menu_item_pressed));
7773

7874
declaration_file_dialog = memnew(EditorFileDialog);
@@ -184,13 +180,6 @@ void JavaScriptPlugin::_export_enumeration_binding_file(const String &p_path) {
184180
dump_to_file(p_path, file_content);
185181
}
186182

187-
void JavaScriptPlugin::_generate_typescript_project() {
188-
_export_typescript_declare_file("res://godot.d.ts");
189-
dump_to_file("res://tsconfig.json", TSCONFIG_CONTENT);
190-
dump_to_file("res://decorators.ts", TS_DECORATORS_CONTENT);
191-
dump_to_file("res://package.json", PACKAGE_JSON_CONTENT);
192-
}
193-
194183
// The following functions are used to generate a godot.d.ts file out of the docs folder from godot
195184
#pragma region TS declare file
196185

editor/editor_tools.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class JavaScriptPlugin : public EditorPlugin {
1313

1414
enum MenuItem {
1515
ITEM_GEN_DECLARE_FILE,
16-
ITEM_GEN_TYPESCRIPT_PROJECT,
1716
ITEM_GEN_ENUM_BINDING_SCRIPT,
1817
};
1918

@@ -24,9 +23,6 @@ class JavaScriptPlugin : public EditorPlugin {
2423
protected:
2524
/* Strings will be generated by ./SCsub from misc directory */
2625
static String BUILTIN_DECLARATION_TEXT;
27-
static String TSCONFIG_CONTENT;
28-
static String TS_DECORATORS_CONTENT;
29-
static String PACKAGE_JSON_CONTENT;
3026

3127
/* Missing declarations, ignoring*/
3228
static Dictionary DECLARATION_CONSTRUCTORS;
@@ -39,7 +35,6 @@ class JavaScriptPlugin : public EditorPlugin {
3935
void _on_menu_item_pressed(int item);
4036
void _export_typescript_declare_file(const String &p_path);
4137
void _export_enumeration_binding_file(const String &p_path);
42-
void _generate_typescript_project();
4338

4439
public:
4540
virtual String get_name() const override { return "JavaScriptPlugin"; }
File renamed without changes.

javascript.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define EXT_TSCLASS "ts"
1515
#define EXT_JSMODULE "js"
1616
#define EXT_JSON "json"
17+
#define EXT_GENERATE "//generatedPath="
1718

1819
class JavaScript : public Script {
1920
GDCLASS(JavaScript, Script);

misc/generate/editor_tools.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,5 @@ def get_editor_tools_files():
1212
"editor/godot.d.ts.gen.cpp": (
1313
"BUILTIN_DECLARATION_TEXT",
1414
"misc/typescript/godot.d.ts",
15-
),
16-
"editor/tsconfig.json.gen.cpp": (
17-
"TSCONFIG_CONTENT",
18-
"misc/typescript/tsconfig.json",
19-
),
20-
"editor/decorators.ts.gen.cpp": (
21-
"TS_DECORATORS_CONTENT",
22-
"misc/typescript/decorators.ts",
23-
),
24-
"editor/package.json.gen.cpp": (
25-
"PACKAGE_JSON_CONTENT",
26-
"misc/typescript/package.json",
27-
),
15+
)
2816
}

misc/typescript/package.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

misc/typescript/tsconfig.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/language/javascript_todo_texteditor.cpp

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,92 @@
22

33
#include "javascript_language.h"
44

5+
#include <core/config/project_settings.h>
6+
#include <core/io/json.h>
7+
8+
#ifdef TOOLS_ENABLED
9+
#include <editor/editor_settings.h>
10+
#endif
11+
512
int JavaScriptLanguage::find_function(const String &p_function, const String &p_code) const { return -1; }
613
String JavaScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const { return ""; }
714
void JavaScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {}
815

916
bool JavaScriptLanguage::supports_documentation() const { return false; }
1017
bool JavaScriptLanguage::can_inherit_from_file() const { return false; }
11-
Error JavaScriptLanguage::open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; }
12-
bool JavaScriptLanguage::overrides_external_editor() { return false; }
1318
Error JavaScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force, String &r_call_hint) { return ERR_UNAVAILABLE; }
1419
Error JavaScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, ScriptLanguage::LookupResult &r_result) { return ERR_UNAVAILABLE; }
20+
21+
#ifdef TOOLS_ENABLED
22+
Error JavaScriptLanguage::open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) {
23+
const Ref<JavaScript> s = p_script;
24+
const String origin_script_path = s->get_script_path();
25+
if (origin_script_path.ends_with(EXT_JSCLASS)) {
26+
const String path = EditorSettings::get_singleton()->get("text_editor/external/exec_path");
27+
String flags = EditorSettings::get_singleton()->get("text_editor/external/exec_flags");
28+
29+
List<String> args;
30+
bool has_file_flag = false;
31+
32+
String resolved_path = origin_script_path;
33+
const String source_code = s->get_source_code();
34+
if (source_code.begins_with(EXT_GENERATE)) {
35+
const Vector<String> found_path = source_code.split("\n", false, 0);
36+
if (found_path.size() > 0) {
37+
resolved_path = found_path[0].replacen(EXT_GENERATE, "");
38+
}
39+
}
40+
41+
const String script_path = ProjectSettings::get_singleton()->globalize_path(resolved_path);
42+
if (flags.size()) {
43+
const String project_path = ProjectSettings::get_singleton()->get_resource_path();
44+
flags = flags.replacen("{line}", itos(p_line > 0 ? p_line : 0));
45+
flags = flags.replacen("{col}", itos(p_col));
46+
flags = flags.strip_edges().replace("\\\\", "\\");
47+
int from = 0;
48+
int num_chars = 0;
49+
bool inside_quotes = false;
50+
for (int i = 0; i < flags.size(); i++) {
51+
if (flags[i] == '"' && (!i || flags[i - 1] != '\\')) {
52+
if (!inside_quotes) {
53+
from++;
54+
}
55+
inside_quotes = !inside_quotes;
56+
} else if (flags[i] == '\0' || (!inside_quotes && flags[i] == ' ')) {
57+
String arg = flags.substr(from, num_chars);
58+
if (arg.find("{file}") != -1) {
59+
has_file_flag = true;
60+
}
61+
// do path replacement here, else there will be issues with spaces and quotes
62+
arg = arg.replacen("{project}", project_path);
63+
arg = arg.replacen("{file}", script_path);
64+
args.push_back(arg);
65+
from = i + 1;
66+
num_chars = 0;
67+
} else {
68+
num_chars++;
69+
}
70+
}
71+
}
72+
// Default to passing script path if no {file} flag is specified.
73+
if (!has_file_flag) {
74+
args.push_back(script_path);
75+
}
76+
const Error err = OS::get_singleton()->execute(path, args);
77+
if (err != OK) {
78+
WARN_PRINT("Couldn't open external text editor, using internal");
79+
}
80+
81+
return err;
82+
}
83+
84+
return OK;
85+
}
86+
87+
bool JavaScriptLanguage::overrides_external_editor() {
88+
return EditorSettings::get_singleton()->get("text_editor/external/use_external_editor");
89+
}
90+
#else
91+
Error JavaScriptLanguage::open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) { return ERR_UNAVAILABLE; }
92+
bool JavaScriptLanguage::overrides_external_editor() { return false; }
93+
#endif

0 commit comments

Comments
 (0)