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+
512int JavaScriptLanguage::find_function (const String &p_function, const String &p_code) const { return -1 ; }
613String JavaScriptLanguage::make_function (const String &p_class, const String &p_name, const PackedStringArray &p_args) const { return " " ; }
714void JavaScriptLanguage::auto_indent_code (String &p_code, int p_from_line, int p_to_line) const {}
815
916bool JavaScriptLanguage::supports_documentation () const { return false ; }
1017bool 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 ; }
1318Error 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; }
1419Error 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