1 | /**************************************************************************/ |
2 | /* script_editor_plugin.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "script_editor_plugin.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/input/input.h" |
35 | #include "core/io/file_access.h" |
36 | #include "core/io/json.h" |
37 | #include "core/io/resource_loader.h" |
38 | #include "core/os/keyboard.h" |
39 | #include "core/os/os.h" |
40 | #include "core/version.h" |
41 | #include "editor/debugger/editor_debugger_node.h" |
42 | #include "editor/debugger/script_editor_debugger.h" |
43 | #include "editor/editor_command_palette.h" |
44 | #include "editor/editor_help_search.h" |
45 | #include "editor/editor_interface.h" |
46 | #include "editor/editor_node.h" |
47 | #include "editor/editor_paths.h" |
48 | #include "editor/editor_scale.h" |
49 | #include "editor/editor_script.h" |
50 | #include "editor/editor_settings.h" |
51 | #include "editor/editor_string_names.h" |
52 | #include "editor/filesystem_dock.h" |
53 | #include "editor/find_in_files.h" |
54 | #include "editor/gui/editor_file_dialog.h" |
55 | #include "editor/gui/editor_run_bar.h" |
56 | #include "editor/gui/editor_toaster.h" |
57 | #include "editor/inspector_dock.h" |
58 | #include "editor/node_dock.h" |
59 | #include "editor/plugins/shader_editor_plugin.h" |
60 | #include "editor/plugins/text_shader_editor.h" |
61 | #include "editor/window_wrapper.h" |
62 | #include "scene/main/node.h" |
63 | #include "scene/main/window.h" |
64 | #include "scene/scene_string_names.h" |
65 | #include "script_text_editor.h" |
66 | #include "servers/display_server.h" |
67 | #include "text_editor.h" |
68 | |
69 | /*** SYNTAX HIGHLIGHTER ****/ |
70 | |
71 | String EditorSyntaxHighlighter::_get_name() const { |
72 | String ret = "Unnamed" ; |
73 | GDVIRTUAL_CALL(_get_name, ret); |
74 | return ret; |
75 | } |
76 | |
77 | PackedStringArray EditorSyntaxHighlighter::_get_supported_languages() const { |
78 | PackedStringArray ret; |
79 | GDVIRTUAL_CALL(_get_supported_languages, ret); |
80 | return ret; |
81 | } |
82 | |
83 | Ref<EditorSyntaxHighlighter> EditorSyntaxHighlighter::_create() const { |
84 | Ref<EditorSyntaxHighlighter> syntax_highlighter; |
85 | syntax_highlighter.instantiate(); |
86 | if (get_script_instance()) { |
87 | syntax_highlighter->set_script(get_script_instance()->get_script()); |
88 | } |
89 | return syntax_highlighter; |
90 | } |
91 | |
92 | void EditorSyntaxHighlighter::_bind_methods() { |
93 | ClassDB::bind_method(D_METHOD("_get_edited_resource" ), &EditorSyntaxHighlighter::_get_edited_resource); |
94 | |
95 | GDVIRTUAL_BIND(_get_name) |
96 | GDVIRTUAL_BIND(_get_supported_languages) |
97 | } |
98 | |
99 | //// |
100 | |
101 | void EditorStandardSyntaxHighlighter::_update_cache() { |
102 | highlighter->set_text_edit(text_edit); |
103 | highlighter->clear_keyword_colors(); |
104 | highlighter->clear_member_keyword_colors(); |
105 | highlighter->clear_color_regions(); |
106 | |
107 | highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color" )); |
108 | highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color" )); |
109 | highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color" )); |
110 | highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color" )); |
111 | |
112 | /* Engine types. */ |
113 | const Color type_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color" ); |
114 | List<StringName> types; |
115 | ClassDB::get_class_list(&types); |
116 | for (const StringName &E : types) { |
117 | highlighter->add_keyword_color(E, type_color); |
118 | } |
119 | |
120 | /* User types. */ |
121 | const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color" ); |
122 | List<StringName> global_classes; |
123 | ScriptServer::get_global_class_list(&global_classes); |
124 | for (const StringName &E : global_classes) { |
125 | highlighter->add_keyword_color(E, usertype_color); |
126 | } |
127 | |
128 | /* Autoloads. */ |
129 | HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list(); |
130 | for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) { |
131 | const ProjectSettings::AutoloadInfo &info = E.value; |
132 | if (info.is_singleton) { |
133 | highlighter->add_keyword_color(info.name, usertype_color); |
134 | } |
135 | } |
136 | |
137 | const Ref<Script> scr = _get_edited_resource(); |
138 | if (scr.is_valid()) { |
139 | /* Core types. */ |
140 | const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color" ); |
141 | List<String> core_types; |
142 | scr->get_language()->get_core_type_words(&core_types); |
143 | for (const String &E : core_types) { |
144 | highlighter->add_keyword_color(E, basetype_color); |
145 | } |
146 | |
147 | /* Reserved words. */ |
148 | const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color" ); |
149 | const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color" ); |
150 | List<String> keywords; |
151 | scr->get_language()->get_reserved_words(&keywords); |
152 | for (const String &E : keywords) { |
153 | if (scr->get_language()->is_control_flow_keyword(E)) { |
154 | highlighter->add_keyword_color(E, control_flow_keyword_color); |
155 | } else { |
156 | highlighter->add_keyword_color(E, keyword_color); |
157 | } |
158 | } |
159 | |
160 | /* Member types. */ |
161 | const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color" ); |
162 | StringName instance_base = scr->get_instance_base_type(); |
163 | if (instance_base != StringName()) { |
164 | List<PropertyInfo> plist; |
165 | ClassDB::get_property_list(instance_base, &plist); |
166 | for (const PropertyInfo &E : plist) { |
167 | String prop_name = E.name; |
168 | if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) { |
169 | continue; |
170 | } |
171 | if (prop_name.contains("/" )) { |
172 | continue; |
173 | } |
174 | highlighter->add_member_keyword_color(prop_name, member_variable_color); |
175 | } |
176 | |
177 | List<String> clist; |
178 | ClassDB::get_integer_constant_list(instance_base, &clist); |
179 | for (const String &E : clist) { |
180 | highlighter->add_member_keyword_color(E, member_variable_color); |
181 | } |
182 | } |
183 | |
184 | /* Comments */ |
185 | const Color = EDITOR_GET("text_editor/theme/highlighting/comment_color" ); |
186 | List<String> ; |
187 | scr->get_language()->get_comment_delimiters(&comments); |
188 | for (const String & : comments) { |
189 | String beg = comment.get_slice(" " , 0); |
190 | String end = comment.get_slice_count(" " ) > 1 ? comment.get_slice(" " , 1) : String(); |
191 | highlighter->add_color_region(beg, end, comment_color, end.is_empty()); |
192 | } |
193 | |
194 | /* Strings */ |
195 | const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color" ); |
196 | List<String> strings; |
197 | scr->get_language()->get_string_delimiters(&strings); |
198 | for (const String &string : strings) { |
199 | String beg = string.get_slice(" " , 0); |
200 | String end = string.get_slice_count(" " ) > 1 ? string.get_slice(" " , 1) : String(); |
201 | highlighter->add_color_region(beg, end, string_color, end.is_empty()); |
202 | } |
203 | } |
204 | } |
205 | |
206 | Ref<EditorSyntaxHighlighter> EditorStandardSyntaxHighlighter::_create() const { |
207 | Ref<EditorStandardSyntaxHighlighter> syntax_highlighter; |
208 | syntax_highlighter.instantiate(); |
209 | return syntax_highlighter; |
210 | } |
211 | |
212 | //// |
213 | |
214 | Ref<EditorSyntaxHighlighter> EditorPlainTextSyntaxHighlighter::_create() const { |
215 | Ref<EditorPlainTextSyntaxHighlighter> syntax_highlighter; |
216 | syntax_highlighter.instantiate(); |
217 | return syntax_highlighter; |
218 | } |
219 | |
220 | //// |
221 | |
222 | void EditorJSONSyntaxHighlighter::_update_cache() { |
223 | highlighter->set_text_edit(text_edit); |
224 | highlighter->clear_keyword_colors(); |
225 | highlighter->clear_member_keyword_colors(); |
226 | highlighter->clear_color_regions(); |
227 | |
228 | highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color" )); |
229 | highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color" )); |
230 | |
231 | const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color" ); |
232 | highlighter->add_color_region("\"" , "\"" , string_color); |
233 | } |
234 | |
235 | Ref<EditorSyntaxHighlighter> EditorJSONSyntaxHighlighter::_create() const { |
236 | Ref<EditorJSONSyntaxHighlighter> syntax_highlighter; |
237 | syntax_highlighter.instantiate(); |
238 | return syntax_highlighter; |
239 | } |
240 | |
241 | //////////////////////////////////////////////////////////////////////////////// |
242 | |
243 | /*** SCRIPT EDITOR ****/ |
244 | |
245 | void ScriptEditorBase::_bind_methods() { |
246 | ClassDB::bind_method(D_METHOD("get_base_editor" ), &ScriptEditorBase::get_base_editor); |
247 | ClassDB::bind_method(D_METHOD("add_syntax_highlighter" , "highlighter" ), &ScriptEditorBase::add_syntax_highlighter); |
248 | |
249 | ADD_SIGNAL(MethodInfo("name_changed" )); |
250 | ADD_SIGNAL(MethodInfo("edited_script_changed" )); |
251 | ADD_SIGNAL(MethodInfo("request_help" , PropertyInfo(Variant::STRING, "topic" ))); |
252 | ADD_SIGNAL(MethodInfo("request_open_script_at_line" , PropertyInfo(Variant::OBJECT, "script" ), PropertyInfo(Variant::INT, "line" ))); |
253 | ADD_SIGNAL(MethodInfo("request_save_history" )); |
254 | ADD_SIGNAL(MethodInfo("go_to_help" , PropertyInfo(Variant::STRING, "what" ))); |
255 | ADD_SIGNAL(MethodInfo("search_in_files_requested" , PropertyInfo(Variant::STRING, "text" ))); |
256 | ADD_SIGNAL(MethodInfo("replace_in_files_requested" , PropertyInfo(Variant::STRING, "text" ))); |
257 | ADD_SIGNAL(MethodInfo("go_to_method" , PropertyInfo(Variant::OBJECT, "script" ), PropertyInfo(Variant::STRING, "method" ))); |
258 | } |
259 | |
260 | class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache { |
261 | struct Cache { |
262 | uint64_t time_loaded = 0; |
263 | Ref<Resource> cache; |
264 | }; |
265 | |
266 | HashMap<String, Cache> cached; |
267 | |
268 | public: |
269 | uint64_t max_time_cache = 5 * 60 * 1000; //minutes, five |
270 | uint32_t max_cache_size = 128; |
271 | |
272 | void cleanup() { |
273 | List<String> to_clean; |
274 | |
275 | HashMap<String, Cache>::Iterator I = cached.begin(); |
276 | while (I) { |
277 | if ((OS::get_singleton()->get_ticks_msec() - I->value.time_loaded) > max_time_cache) { |
278 | to_clean.push_back(I->key); |
279 | } |
280 | ++I; |
281 | } |
282 | |
283 | while (to_clean.front()) { |
284 | cached.erase(to_clean.front()->get()); |
285 | to_clean.pop_front(); |
286 | } |
287 | } |
288 | |
289 | virtual Ref<Resource> get_cached_resource(const String &p_path) { |
290 | HashMap<String, Cache>::Iterator E = cached.find(p_path); |
291 | if (!E) { |
292 | Cache c; |
293 | c.cache = ResourceLoader::load(p_path); |
294 | E = cached.insert(p_path, c); |
295 | } |
296 | |
297 | E->value.time_loaded = OS::get_singleton()->get_ticks_msec(); |
298 | |
299 | if (cached.size() > max_cache_size) { |
300 | uint64_t older; |
301 | HashMap<String, Cache>::Iterator O = cached.begin(); |
302 | older = O->value.time_loaded; |
303 | HashMap<String, Cache>::Iterator I = O; |
304 | while (I) { |
305 | if (I->value.time_loaded < older) { |
306 | older = I->value.time_loaded; |
307 | O = I; |
308 | } |
309 | ++I; |
310 | } |
311 | |
312 | if (O != E) { //should never happen.. |
313 | cached.remove(O); |
314 | } |
315 | } |
316 | |
317 | return E->value.cache; |
318 | } |
319 | |
320 | virtual ~EditorScriptCodeCompletionCache() {} |
321 | }; |
322 | |
323 | void ScriptEditorQuickOpen::(const Vector<String> &p_functions, bool p_dontclear) { |
324 | popup_centered_ratio(0.6); |
325 | if (p_dontclear) { |
326 | search_box->select_all(); |
327 | } else { |
328 | search_box->clear(); |
329 | } |
330 | search_box->grab_focus(); |
331 | functions = p_functions; |
332 | _update_search(); |
333 | } |
334 | |
335 | void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) { |
336 | _update_search(); |
337 | } |
338 | |
339 | void ScriptEditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) { |
340 | Ref<InputEventKey> k = p_ie; |
341 | |
342 | if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) { |
343 | search_options->gui_input(k); |
344 | search_box->accept_event(); |
345 | } |
346 | } |
347 | |
348 | void ScriptEditorQuickOpen::_update_search() { |
349 | search_options->clear(); |
350 | TreeItem *root = search_options->create_item(); |
351 | |
352 | for (int i = 0; i < functions.size(); i++) { |
353 | String file = functions[i]; |
354 | if ((search_box->get_text().is_empty() || file.findn(search_box->get_text()) != -1)) { |
355 | TreeItem *ti = search_options->create_item(root); |
356 | ti->set_text(0, file); |
357 | if (root->get_first_child() == ti) { |
358 | ti->select(0); |
359 | } |
360 | } |
361 | } |
362 | |
363 | get_ok_button()->set_disabled(root->get_first_child() == nullptr); |
364 | } |
365 | |
366 | void ScriptEditorQuickOpen::_confirmed() { |
367 | TreeItem *ti = search_options->get_selected(); |
368 | if (!ti) { |
369 | return; |
370 | } |
371 | int line = ti->get_text(0).get_slice(":" , 1).to_int(); |
372 | |
373 | emit_signal(SNAME("goto_line" ), line - 1); |
374 | hide(); |
375 | } |
376 | |
377 | void ScriptEditorQuickOpen::_notification(int p_what) { |
378 | switch (p_what) { |
379 | case NOTIFICATION_ENTER_TREE: { |
380 | connect("confirmed" , callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); |
381 | |
382 | search_box->set_clear_button_enabled(true); |
383 | [[fallthrough]]; |
384 | } |
385 | case NOTIFICATION_VISIBILITY_CHANGED: { |
386 | search_box->set_right_icon(search_options->get_editor_theme_icon(SNAME("Search" ))); |
387 | } break; |
388 | |
389 | case NOTIFICATION_EXIT_TREE: { |
390 | disconnect("confirmed" , callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); |
391 | } break; |
392 | } |
393 | } |
394 | |
395 | void ScriptEditorQuickOpen::_bind_methods() { |
396 | ADD_SIGNAL(MethodInfo("goto_line" , PropertyInfo(Variant::INT, "line" ))); |
397 | } |
398 | |
399 | ScriptEditorQuickOpen::ScriptEditorQuickOpen() { |
400 | VBoxContainer *vbc = memnew(VBoxContainer); |
401 | add_child(vbc); |
402 | search_box = memnew(LineEdit); |
403 | vbc->add_margin_child(TTR("Search:" ), search_box); |
404 | search_box->connect("text_changed" , callable_mp(this, &ScriptEditorQuickOpen::_text_changed)); |
405 | search_box->connect("gui_input" , callable_mp(this, &ScriptEditorQuickOpen::_sbox_input)); |
406 | search_options = memnew(Tree); |
407 | vbc->add_margin_child(TTR("Matches:" ), search_options, true); |
408 | set_ok_button_text(TTR("Open" )); |
409 | get_ok_button()->set_disabled(true); |
410 | register_text_enter(search_box); |
411 | set_hide_on_ok(false); |
412 | search_options->connect("item_activated" , callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); |
413 | search_options->set_hide_root(true); |
414 | search_options->set_hide_folding(true); |
415 | search_options->add_theme_constant_override("draw_guides" , 1); |
416 | } |
417 | |
418 | ///////////////////////////////// |
419 | |
420 | ScriptEditor *ScriptEditor::script_editor = nullptr; |
421 | |
422 | /*** SCRIPT EDITOR ******/ |
423 | |
424 | String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_se) { |
425 | String val = EditorDebuggerNode::get_singleton()->get_var_value(p_text); |
426 | if (!val.is_empty()) { |
427 | return p_text + ": " + val; |
428 | } else { |
429 | return String(); |
430 | } |
431 | } |
432 | |
433 | void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) { |
434 | if (bool(EDITOR_GET("text_editor/external/use_external_editor" ))) { |
435 | return; |
436 | } |
437 | |
438 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
439 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
440 | if (!se) { |
441 | continue; |
442 | } |
443 | |
444 | se->set_debugger_active(p_breaked); |
445 | } |
446 | } |
447 | |
448 | void ScriptEditor::_script_created(Ref<Script> p_script) { |
449 | EditorNode::get_singleton()->push_item(p_script.operator->()); |
450 | } |
451 | |
452 | void ScriptEditor::_goto_script_line2(int p_line) { |
453 | ScriptEditorBase *current = _get_current_editor(); |
454 | if (current) { |
455 | current->goto_line(p_line); |
456 | } |
457 | } |
458 | |
459 | void ScriptEditor::_goto_script_line(Ref<RefCounted> p_script, int p_line) { |
460 | Ref<Script> scr = Object::cast_to<Script>(*p_script); |
461 | if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) { |
462 | if (edit(p_script, p_line, 0)) { |
463 | EditorNode::get_singleton()->push_item(p_script.ptr()); |
464 | |
465 | ScriptEditorBase *current = _get_current_editor(); |
466 | if (ScriptTextEditor *script_text_editor = Object::cast_to<ScriptTextEditor>(current)) { |
467 | script_text_editor->goto_line_centered(p_line); |
468 | } else if (current) { |
469 | current->goto_line(p_line, true); |
470 | } |
471 | |
472 | _save_history(); |
473 | } |
474 | } |
475 | } |
476 | |
477 | void ScriptEditor::_set_execution(Ref<RefCounted> p_script, int p_line) { |
478 | Ref<Script> scr = Object::cast_to<Script>(*p_script); |
479 | if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) { |
480 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
481 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
482 | if (!se) { |
483 | continue; |
484 | } |
485 | |
486 | if ((scr != nullptr && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) { |
487 | se->set_executing_line(p_line); |
488 | } |
489 | } |
490 | } |
491 | } |
492 | |
493 | void ScriptEditor::_clear_execution(Ref<RefCounted> p_script) { |
494 | Ref<Script> scr = Object::cast_to<Script>(*p_script); |
495 | if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) { |
496 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
497 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
498 | if (!se) { |
499 | continue; |
500 | } |
501 | |
502 | if ((scr != nullptr && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) { |
503 | se->clear_executing_line(); |
504 | } |
505 | } |
506 | } |
507 | } |
508 | |
509 | void ScriptEditor::_set_breakpoint(Ref<RefCounted> p_script, int p_line, bool p_enabled) { |
510 | Ref<Script> scr = Object::cast_to<Script>(*p_script); |
511 | if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) { |
512 | // Update if open. |
513 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
514 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
515 | if (se && se->get_edited_resource()->get_path() == scr->get_path()) { |
516 | se->set_breakpoint(p_line, p_enabled); |
517 | return; |
518 | } |
519 | } |
520 | |
521 | // Handle closed. |
522 | Dictionary state = script_editor_cache->get_value(scr->get_path(), "state" ); |
523 | Array breakpoints; |
524 | if (state.has("breakpoints" )) { |
525 | breakpoints = state["breakpoints" ]; |
526 | } |
527 | |
528 | if (breakpoints.has(p_line)) { |
529 | if (!p_enabled) { |
530 | breakpoints.erase(p_line); |
531 | } |
532 | } else if (p_enabled) { |
533 | breakpoints.push_back(p_line); |
534 | } |
535 | state["breakpoints" ] = breakpoints; |
536 | script_editor_cache->set_value(scr->get_path(), "state" , state); |
537 | EditorDebuggerNode::get_singleton()->set_breakpoint(scr->get_path(), p_line + 1, false); |
538 | } |
539 | } |
540 | |
541 | void ScriptEditor::_clear_breakpoints() { |
542 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
543 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
544 | if (se) { |
545 | se->clear_breakpoints(); |
546 | } |
547 | } |
548 | |
549 | // Clear from closed scripts. |
550 | List<String> cached_editors; |
551 | script_editor_cache->get_sections(&cached_editors); |
552 | for (const String &E : cached_editors) { |
553 | Array breakpoints = _get_cached_breakpoints_for_script(E); |
554 | for (int i = 0; i < breakpoints.size(); i++) { |
555 | EditorDebuggerNode::get_singleton()->set_breakpoint(E, (int)breakpoints[i] + 1, false); |
556 | } |
557 | |
558 | if (breakpoints.size() > 0) { |
559 | Dictionary state = script_editor_cache->get_value(E, "state" ); |
560 | state["breakpoints" ] = Array(); |
561 | script_editor_cache->set_value(E, "state" , state); |
562 | } |
563 | } |
564 | } |
565 | |
566 | Array ScriptEditor::_get_cached_breakpoints_for_script(const String &p_path) const { |
567 | if (!ResourceLoader::exists(p_path, "Script" ) || p_path.begins_with("local://" ) || !script_editor_cache->has_section_key(p_path, "state" )) { |
568 | return Array(); |
569 | } |
570 | |
571 | Dictionary state = script_editor_cache->get_value(p_path, "state" ); |
572 | if (!state.has("breakpoints" )) { |
573 | return Array(); |
574 | } |
575 | return state["breakpoints" ]; |
576 | } |
577 | |
578 | ScriptEditorBase *ScriptEditor::_get_current_editor() const { |
579 | int selected = tab_container->get_current_tab(); |
580 | if (selected < 0 || selected >= tab_container->get_tab_count()) { |
581 | return nullptr; |
582 | } |
583 | |
584 | return Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(selected)); |
585 | } |
586 | |
587 | void ScriptEditor::_update_history_arrows() { |
588 | script_back->set_disabled(history_pos <= 0); |
589 | script_forward->set_disabled(history_pos >= history.size() - 1); |
590 | } |
591 | |
592 | void ScriptEditor::_save_history() { |
593 | if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) { |
594 | Node *n = tab_container->get_current_tab_control(); |
595 | |
596 | if (Object::cast_to<ScriptEditorBase>(n)) { |
597 | history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_navigation_state(); |
598 | } |
599 | if (Object::cast_to<EditorHelp>(n)) { |
600 | history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll(); |
601 | } |
602 | } |
603 | |
604 | history.resize(history_pos + 1); |
605 | ScriptHistory sh; |
606 | sh.control = tab_container->get_current_tab_control(); |
607 | sh.state = Variant(); |
608 | |
609 | history.push_back(sh); |
610 | history_pos++; |
611 | |
612 | _update_history_arrows(); |
613 | } |
614 | |
615 | void ScriptEditor::_go_to_tab(int p_idx) { |
616 | ScriptEditorBase *current = _get_current_editor(); |
617 | if (current) { |
618 | if (current->is_unsaved()) { |
619 | current->apply_code(); |
620 | } |
621 | } |
622 | |
623 | Control *c = tab_container->get_tab_control(p_idx); |
624 | if (!c) { |
625 | return; |
626 | } |
627 | |
628 | if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) { |
629 | Node *n = tab_container->get_current_tab_control(); |
630 | |
631 | if (Object::cast_to<ScriptEditorBase>(n)) { |
632 | history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_navigation_state(); |
633 | } |
634 | if (Object::cast_to<EditorHelp>(n)) { |
635 | history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll(); |
636 | } |
637 | } |
638 | |
639 | history.resize(history_pos + 1); |
640 | ScriptHistory sh; |
641 | sh.control = c; |
642 | sh.state = Variant(); |
643 | |
644 | history.push_back(sh); |
645 | history_pos++; |
646 | |
647 | tab_container->set_current_tab(p_idx); |
648 | |
649 | c = tab_container->get_current_tab_control(); |
650 | |
651 | if (Object::cast_to<ScriptEditorBase>(c)) { |
652 | script_name_label->set_text(Object::cast_to<ScriptEditorBase>(c)->get_name()); |
653 | script_icon->set_texture(Object::cast_to<ScriptEditorBase>(c)->get_theme_icon()); |
654 | if (is_visible_in_tree()) { |
655 | Object::cast_to<ScriptEditorBase>(c)->ensure_focus(); |
656 | } |
657 | |
658 | Ref<Script> scr = Object::cast_to<ScriptEditorBase>(c)->get_edited_resource(); |
659 | if (scr != nullptr) { |
660 | notify_script_changed(scr); |
661 | } |
662 | |
663 | Object::cast_to<ScriptEditorBase>(c)->validate(); |
664 | } |
665 | if (Object::cast_to<EditorHelp>(c)) { |
666 | script_name_label->set_text(Object::cast_to<EditorHelp>(c)->get_class()); |
667 | script_icon->set_texture(get_editor_theme_icon(SNAME("Help" ))); |
668 | if (is_visible_in_tree()) { |
669 | Object::cast_to<EditorHelp>(c)->set_focused(); |
670 | } |
671 | } |
672 | |
673 | c->set_meta("__editor_pass" , ++edit_pass); |
674 | _update_history_arrows(); |
675 | _update_script_colors(); |
676 | _update_members_overview(); |
677 | _update_help_overview(); |
678 | _update_selected_editor_menu(); |
679 | _update_members_overview_visibility(); |
680 | _update_help_overview_visibility(); |
681 | } |
682 | |
683 | void ScriptEditor::_add_recent_script(String p_path) { |
684 | if (p_path.is_empty()) { |
685 | return; |
686 | } |
687 | |
688 | Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files" , "scripts" , Array()); |
689 | if (rc.find(p_path) != -1) { |
690 | rc.erase(p_path); |
691 | } |
692 | rc.push_front(p_path); |
693 | if (rc.size() > 10) { |
694 | rc.resize(10); |
695 | } |
696 | |
697 | EditorSettings::get_singleton()->set_project_metadata("recent_files" , "scripts" , rc); |
698 | _update_recent_scripts(); |
699 | } |
700 | |
701 | void ScriptEditor::_update_recent_scripts() { |
702 | Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files" , "scripts" , Array()); |
703 | recent_scripts->clear(); |
704 | |
705 | String path; |
706 | for (int i = 0; i < rc.size(); i++) { |
707 | path = rc[i]; |
708 | recent_scripts->add_item(path.replace("res://" , "" )); |
709 | } |
710 | |
711 | recent_scripts->add_separator(); |
712 | recent_scripts->add_shortcut(ED_SHORTCUT("script_editor/clear_recent" , TTR("Clear Recent Files" ))); |
713 | recent_scripts->set_item_disabled(recent_scripts->get_item_id(recent_scripts->get_item_count() - 1), rc.is_empty()); |
714 | |
715 | recent_scripts->reset_size(); |
716 | } |
717 | |
718 | void ScriptEditor::_open_recent_script(int p_idx) { |
719 | // clear button |
720 | if (p_idx == recent_scripts->get_item_count() - 1) { |
721 | EditorSettings::get_singleton()->set_project_metadata("recent_files" , "scripts" , Array()); |
722 | call_deferred(SNAME("_update_recent_scripts" )); |
723 | return; |
724 | } |
725 | |
726 | Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files" , "scripts" , Array()); |
727 | ERR_FAIL_INDEX(p_idx, rc.size()); |
728 | |
729 | String path = rc[p_idx]; |
730 | // if its not on disk its a help file or deleted |
731 | if (FileAccess::exists(path)) { |
732 | List<String> extensions; |
733 | ResourceLoader::get_recognized_extensions_for_type("Script" , &extensions); |
734 | ResourceLoader::get_recognized_extensions_for_type("JSON" , &extensions); |
735 | |
736 | if (extensions.find(path.get_extension())) { |
737 | Ref<Resource> scr = ResourceLoader::load(path); |
738 | if (scr.is_valid()) { |
739 | edit(scr, true); |
740 | return; |
741 | } |
742 | } |
743 | |
744 | Error err; |
745 | Ref<TextFile> text_file = _load_text_file(path, &err); |
746 | if (text_file.is_valid()) { |
747 | edit(text_file, true); |
748 | return; |
749 | } |
750 | // if it's a path then it's most likely a deleted file not help |
751 | } else if (path.contains("::" )) { |
752 | // built-in script |
753 | String res_path = path.get_slice("::" , 0); |
754 | if (ResourceLoader::get_resource_type(res_path) == "PackedScene" ) { |
755 | if (!EditorNode::get_singleton()->is_scene_open(res_path)) { |
756 | EditorNode::get_singleton()->load_scene(res_path); |
757 | } |
758 | } else { |
759 | EditorNode::get_singleton()->load_resource(res_path); |
760 | } |
761 | Ref<Script> scr = ResourceLoader::load(path); |
762 | if (scr.is_valid()) { |
763 | edit(scr, true); |
764 | return; |
765 | } |
766 | } else if (!path.is_resource_file()) { |
767 | _help_class_open(path); |
768 | return; |
769 | } |
770 | |
771 | rc.remove_at(p_idx); |
772 | EditorSettings::get_singleton()->set_project_metadata("recent_files" , "scripts" , rc); |
773 | _update_recent_scripts(); |
774 | _show_error_dialog(path); |
775 | } |
776 | |
777 | void ScriptEditor::_show_error_dialog(String p_path) { |
778 | error_dialog->set_text(vformat(TTR("Can't open '%s'. The file could have been moved or deleted." ), p_path)); |
779 | error_dialog->popup_centered(); |
780 | } |
781 | |
782 | void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) { |
783 | int selected = p_idx; |
784 | if (selected < 0 || selected >= tab_container->get_tab_count()) { |
785 | return; |
786 | } |
787 | |
788 | Node *tselected = tab_container->get_tab_control(selected); |
789 | |
790 | ScriptEditorBase *current = Object::cast_to<ScriptEditorBase>(tselected); |
791 | if (current) { |
792 | Ref<Resource> file = current->get_edited_resource(); |
793 | if (p_save && file.is_valid()) { |
794 | // Do not try to save internal scripts, but prompt to save in-memory |
795 | // scripts which are not saved to disk yet (have empty path). |
796 | if (!file->is_built_in()) { |
797 | save_current_script(); |
798 | } |
799 | } |
800 | if (file.is_valid()) { |
801 | if (!file->get_path().is_empty()) { |
802 | // Only saved scripts can be restored. |
803 | previous_scripts.push_back(file->get_path()); |
804 | } |
805 | |
806 | Ref<Script> scr = file; |
807 | if (scr.is_valid()) { |
808 | notify_script_close(scr); |
809 | } |
810 | } |
811 | } |
812 | |
813 | // roll back to previous tab |
814 | if (p_history_back) { |
815 | _history_back(); |
816 | } |
817 | |
818 | //remove from history |
819 | history.resize(history_pos + 1); |
820 | |
821 | for (int i = 0; i < history.size(); i++) { |
822 | if (history[i].control == tselected) { |
823 | history.remove_at(i); |
824 | i--; |
825 | history_pos--; |
826 | } |
827 | } |
828 | |
829 | if (history_pos >= history.size()) { |
830 | history_pos = history.size() - 1; |
831 | } |
832 | |
833 | int idx = tab_container->get_current_tab(); |
834 | if (current) { |
835 | current->clear_edit_menu(); |
836 | _save_editor_state(current); |
837 | } |
838 | memdelete(tselected); |
839 | if (idx >= tab_container->get_tab_count()) { |
840 | idx = tab_container->get_tab_count() - 1; |
841 | } |
842 | if (idx >= 0) { |
843 | if (history_pos >= 0) { |
844 | idx = tab_container->get_tab_idx_from_control(history[history_pos].control); |
845 | } |
846 | _go_to_tab(idx); |
847 | } else { |
848 | _update_selected_editor_menu(); |
849 | } |
850 | |
851 | if (script_close_queue.is_empty()) { |
852 | _update_history_arrows(); |
853 | _update_script_names(); |
854 | _update_members_overview_visibility(); |
855 | _update_help_overview_visibility(); |
856 | _save_layout(); |
857 | _update_find_replace_bar(); |
858 | } |
859 | } |
860 | |
861 | void ScriptEditor::_close_current_tab(bool p_save) { |
862 | _close_tab(tab_container->get_current_tab(), p_save); |
863 | } |
864 | |
865 | void ScriptEditor::_close_discard_current_tab(const String &p_str) { |
866 | _close_tab(tab_container->get_current_tab(), false); |
867 | erase_tab_confirm->hide(); |
868 | } |
869 | |
870 | void ScriptEditor::_close_docs_tab() { |
871 | int child_count = tab_container->get_tab_count(); |
872 | for (int i = child_count - 1; i >= 0; i--) { |
873 | EditorHelp *se = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i)); |
874 | |
875 | if (se) { |
876 | _close_tab(i, true, false); |
877 | } |
878 | } |
879 | } |
880 | |
881 | void ScriptEditor::_copy_script_path() { |
882 | ScriptEditorBase *se = _get_current_editor(); |
883 | if (se) { |
884 | Ref<Resource> scr = se->get_edited_resource(); |
885 | DisplayServer::get_singleton()->clipboard_set(scr->get_path()); |
886 | } |
887 | } |
888 | |
889 | void ScriptEditor::_close_other_tabs() { |
890 | int current_idx = tab_container->get_current_tab(); |
891 | for (int i = tab_container->get_tab_count() - 1; i >= 0; i--) { |
892 | if (i != current_idx) { |
893 | script_close_queue.push_back(i); |
894 | } |
895 | } |
896 | _queue_close_tabs(); |
897 | } |
898 | |
899 | void ScriptEditor::_close_all_tabs() { |
900 | for (int i = tab_container->get_tab_count() - 1; i >= 0; i--) { |
901 | script_close_queue.push_back(i); |
902 | } |
903 | _queue_close_tabs(); |
904 | } |
905 | |
906 | void ScriptEditor::_queue_close_tabs() { |
907 | while (!script_close_queue.is_empty()) { |
908 | int idx = script_close_queue.front()->get(); |
909 | script_close_queue.pop_front(); |
910 | |
911 | tab_container->set_current_tab(idx); |
912 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(idx)); |
913 | if (se) { |
914 | // Maybe there are unsaved changes. |
915 | if (se->is_unsaved()) { |
916 | _ask_close_current_unsaved_tab(se); |
917 | erase_tab_confirm->connect(SceneStringNames::get_singleton()->visibility_changed, callable_mp(this, &ScriptEditor::_queue_close_tabs), CONNECT_ONE_SHOT); |
918 | break; |
919 | } |
920 | } |
921 | |
922 | _close_current_tab(false); |
923 | } |
924 | _update_find_replace_bar(); |
925 | } |
926 | |
927 | void ScriptEditor::_ask_close_current_unsaved_tab(ScriptEditorBase *current) { |
928 | erase_tab_confirm->set_text(TTR("Close and save changes?" ) + "\n\"" + current->get_name() + "\"" ); |
929 | erase_tab_confirm->popup_centered(); |
930 | } |
931 | |
932 | void ScriptEditor::_resave_scripts(const String &p_str) { |
933 | apply_scripts(); |
934 | |
935 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
936 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
937 | if (!se) { |
938 | continue; |
939 | } |
940 | |
941 | Ref<Resource> scr = se->get_edited_resource(); |
942 | |
943 | if (scr->is_built_in()) { |
944 | continue; //internal script, who cares |
945 | } |
946 | |
947 | if (trim_trailing_whitespace_on_save) { |
948 | se->trim_trailing_whitespace(); |
949 | } |
950 | |
951 | se->insert_final_newline(); |
952 | |
953 | if (convert_indent_on_save) { |
954 | se->convert_indent(); |
955 | } |
956 | |
957 | Ref<TextFile> text_file = scr; |
958 | if (text_file != nullptr) { |
959 | se->apply_code(); |
960 | _save_text_file(text_file, text_file->get_path()); |
961 | break; |
962 | } else { |
963 | EditorNode::get_singleton()->save_resource(scr); |
964 | } |
965 | se->tag_saved_version(); |
966 | } |
967 | |
968 | disk_changed->hide(); |
969 | } |
970 | |
971 | void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) { |
972 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
973 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
974 | if (!se) { |
975 | continue; |
976 | } |
977 | |
978 | Ref<Resource> scr = se->get_edited_resource(); |
979 | |
980 | if (scr == p_res) { |
981 | se->tag_saved_version(); |
982 | } |
983 | } |
984 | |
985 | _update_script_names(); |
986 | _trigger_live_script_reload(); |
987 | } |
988 | |
989 | void ScriptEditor::_scene_saved_callback(const String &p_path) { |
990 | // If scene was saved, mark all built-in scripts from that scene as saved. |
991 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
992 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
993 | if (!se) { |
994 | continue; |
995 | } |
996 | |
997 | Ref<Resource> edited_res = se->get_edited_resource(); |
998 | |
999 | if (!edited_res->is_built_in()) { |
1000 | continue; // External script, who cares. |
1001 | } |
1002 | |
1003 | if (edited_res->get_path().get_slice("::" , 0) == p_path) { |
1004 | se->tag_saved_version(); |
1005 | } |
1006 | |
1007 | Ref<Script> scr = edited_res; |
1008 | if (scr.is_valid() && scr->is_tool()) { |
1009 | scr->reload(true); |
1010 | } |
1011 | } |
1012 | } |
1013 | |
1014 | void ScriptEditor::_trigger_live_script_reload() { |
1015 | if (!pending_auto_reload && auto_reload_running_scripts) { |
1016 | call_deferred(SNAME("_live_auto_reload_running_scripts" )); |
1017 | pending_auto_reload = true; |
1018 | } |
1019 | } |
1020 | |
1021 | void ScriptEditor::_live_auto_reload_running_scripts() { |
1022 | pending_auto_reload = false; |
1023 | EditorDebuggerNode::get_singleton()->reload_scripts(); |
1024 | } |
1025 | |
1026 | bool ScriptEditor::_test_script_times_on_disk(Ref<Resource> p_for_script) { |
1027 | disk_changed_list->clear(); |
1028 | TreeItem *r = disk_changed_list->create_item(); |
1029 | disk_changed_list->set_hide_root(true); |
1030 | |
1031 | bool need_ask = false; |
1032 | bool need_reload = false; |
1033 | bool use_autoreload = bool(EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change" )); |
1034 | |
1035 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
1036 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
1037 | if (se) { |
1038 | Ref<Resource> edited_res = se->get_edited_resource(); |
1039 | if (p_for_script.is_valid() && edited_res.is_valid() && p_for_script != edited_res) { |
1040 | continue; |
1041 | } |
1042 | |
1043 | if (edited_res->is_built_in()) { |
1044 | continue; //internal script, who cares |
1045 | } |
1046 | |
1047 | uint64_t last_date = edited_res->get_last_modified_time(); |
1048 | uint64_t date = FileAccess::get_modified_time(edited_res->get_path()); |
1049 | |
1050 | if (last_date != date) { |
1051 | TreeItem *ti = disk_changed_list->create_item(r); |
1052 | ti->set_text(0, edited_res->get_path().get_file()); |
1053 | |
1054 | if (!use_autoreload || se->is_unsaved()) { |
1055 | need_ask = true; |
1056 | } |
1057 | need_reload = true; |
1058 | } |
1059 | } |
1060 | } |
1061 | |
1062 | if (need_reload) { |
1063 | if (!need_ask) { |
1064 | script_editor->reload_scripts(); |
1065 | need_reload = false; |
1066 | } else { |
1067 | disk_changed->call_deferred(SNAME("popup_centered_ratio" ), 0.3); |
1068 | } |
1069 | } |
1070 | |
1071 | return need_reload; |
1072 | } |
1073 | |
1074 | void ScriptEditor::_file_dialog_action(String p_file) { |
1075 | switch (file_dialog_option) { |
1076 | case FILE_NEW_TEXTFILE: { |
1077 | Error err; |
1078 | { |
1079 | Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err); |
1080 | if (err) { |
1081 | EditorNode::get_singleton()->show_warning(TTR("Error writing TextFile:" ) + "\n" + p_file, TTR("Error!" )); |
1082 | break; |
1083 | } |
1084 | } |
1085 | |
1086 | if (EditorFileSystem::get_singleton()) { |
1087 | if (textfile_extensions.has(p_file.get_extension())) { |
1088 | EditorFileSystem::get_singleton()->update_file(p_file); |
1089 | } |
1090 | } |
1091 | |
1092 | if (!open_textfile_after_create) { |
1093 | return; |
1094 | } |
1095 | [[fallthrough]]; |
1096 | } |
1097 | case FILE_OPEN: { |
1098 | open_file(p_file); |
1099 | file_dialog_option = -1; |
1100 | } break; |
1101 | case FILE_SAVE_AS: { |
1102 | ScriptEditorBase *current = _get_current_editor(); |
1103 | if (current) { |
1104 | Ref<Resource> resource = current->get_edited_resource(); |
1105 | String path = ProjectSettings::get_singleton()->localize_path(p_file); |
1106 | Error err = _save_text_file(resource, path); |
1107 | |
1108 | if (err != OK) { |
1109 | EditorNode::get_singleton()->show_accept(TTR("Error saving file!" ), TTR("OK" )); |
1110 | return; |
1111 | } |
1112 | |
1113 | resource->set_path(path); |
1114 | _update_script_names(); |
1115 | } |
1116 | } break; |
1117 | case THEME_SAVE_AS: { |
1118 | if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) { |
1119 | EditorNode::get_singleton()->show_warning(TTR("Error while saving theme." ), TTR("Error Saving" )); |
1120 | } |
1121 | } break; |
1122 | case THEME_IMPORT: { |
1123 | if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) { |
1124 | EditorNode::get_singleton()->show_warning(TTR("Error importing theme." ), TTR("Error Importing" )); |
1125 | } |
1126 | } break; |
1127 | } |
1128 | file_dialog_option = -1; |
1129 | } |
1130 | |
1131 | Ref<Script> ScriptEditor::_get_current_script() { |
1132 | ScriptEditorBase *current = _get_current_editor(); |
1133 | |
1134 | if (current) { |
1135 | Ref<Script> scr = current->get_edited_resource(); |
1136 | return scr != nullptr ? scr : nullptr; |
1137 | } else { |
1138 | return nullptr; |
1139 | } |
1140 | } |
1141 | |
1142 | TypedArray<Script> ScriptEditor::_get_open_scripts() const { |
1143 | TypedArray<Script> ret; |
1144 | Vector<Ref<Script>> scripts = get_open_scripts(); |
1145 | int scrits_amount = scripts.size(); |
1146 | for (int idx_script = 0; idx_script < scrits_amount; idx_script++) { |
1147 | ret.push_back(scripts[idx_script]); |
1148 | } |
1149 | return ret; |
1150 | } |
1151 | |
1152 | bool ScriptEditor::toggle_scripts_panel() { |
1153 | list_split->set_visible(!list_split->is_visible()); |
1154 | EditorSettings::get_singleton()->set_project_metadata("scripts_panel" , "show_scripts_panel" , list_split->is_visible()); |
1155 | return list_split->is_visible(); |
1156 | } |
1157 | |
1158 | bool ScriptEditor::is_scripts_panel_toggled() { |
1159 | return list_split->is_visible(); |
1160 | } |
1161 | |
1162 | void ScriptEditor::(int p_option) { |
1163 | ScriptEditorBase *current = _get_current_editor(); |
1164 | switch (p_option) { |
1165 | case FILE_NEW: { |
1166 | script_create_dialog->config("Node" , "new_script" , false, false); |
1167 | script_create_dialog->popup_centered(); |
1168 | } break; |
1169 | case FILE_NEW_TEXTFILE: { |
1170 | file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); |
1171 | file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
1172 | file_dialog_option = FILE_NEW_TEXTFILE; |
1173 | |
1174 | file_dialog->clear_filters(); |
1175 | for (const String &E : textfile_extensions) { |
1176 | file_dialog->add_filter("*." + E, E.to_upper()); |
1177 | } |
1178 | file_dialog->popup_file_dialog(); |
1179 | file_dialog->set_title(TTR("New Text File..." )); |
1180 | open_textfile_after_create = true; |
1181 | } break; |
1182 | case FILE_OPEN: { |
1183 | file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
1184 | file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
1185 | file_dialog_option = FILE_OPEN; |
1186 | |
1187 | List<String> extensions; |
1188 | ResourceLoader::get_recognized_extensions_for_type("Script" , &extensions); |
1189 | file_dialog->clear_filters(); |
1190 | for (int i = 0; i < extensions.size(); i++) { |
1191 | file_dialog->add_filter("*." + extensions[i], extensions[i].to_upper()); |
1192 | } |
1193 | |
1194 | for (const String &E : textfile_extensions) { |
1195 | file_dialog->add_filter("*." + E, E.to_upper()); |
1196 | } |
1197 | |
1198 | file_dialog->popup_file_dialog(); |
1199 | file_dialog->set_title(TTR("Open File" )); |
1200 | return; |
1201 | } break; |
1202 | case FILE_REOPEN_CLOSED: { |
1203 | if (previous_scripts.is_empty()) { |
1204 | return; |
1205 | } |
1206 | |
1207 | String path = previous_scripts.back()->get(); |
1208 | previous_scripts.pop_back(); |
1209 | |
1210 | List<String> extensions; |
1211 | ResourceLoader::get_recognized_extensions_for_type("Script" , &extensions); |
1212 | ResourceLoader::get_recognized_extensions_for_type("JSON" , &extensions); |
1213 | bool built_in = !path.is_resource_file(); |
1214 | |
1215 | if (extensions.find(path.get_extension()) || built_in) { |
1216 | if (built_in) { |
1217 | String res_path = path.get_slice("::" , 0); |
1218 | if (ResourceLoader::get_resource_type(res_path) == "PackedScene" ) { |
1219 | if (!EditorNode::get_singleton()->is_scene_open(res_path)) { |
1220 | EditorNode::get_singleton()->load_scene(res_path); |
1221 | } |
1222 | } else { |
1223 | EditorNode::get_singleton()->load_resource(res_path); |
1224 | } |
1225 | } |
1226 | |
1227 | Ref<Resource> scr = ResourceLoader::load(path); |
1228 | if (!scr.is_valid()) { |
1229 | EditorNode::get_singleton()->show_warning(TTR("Could not load file at:" ) + "\n\n" + path, TTR("Error!" )); |
1230 | file_dialog_option = -1; |
1231 | return; |
1232 | } |
1233 | |
1234 | edit(scr); |
1235 | file_dialog_option = -1; |
1236 | } else { |
1237 | Error error; |
1238 | Ref<TextFile> text_file = _load_text_file(path, &error); |
1239 | if (error != OK) { |
1240 | EditorNode::get_singleton()->show_warning(TTR("Could not load file at:" ) + "\n\n" + path, TTR("Error!" )); |
1241 | } |
1242 | |
1243 | if (text_file.is_valid()) { |
1244 | edit(text_file); |
1245 | file_dialog_option = -1; |
1246 | } |
1247 | } |
1248 | } break; |
1249 | case FILE_SAVE_ALL: { |
1250 | if (_test_script_times_on_disk()) { |
1251 | return; |
1252 | } |
1253 | |
1254 | save_all_scripts(); |
1255 | } break; |
1256 | case SEARCH_IN_FILES: { |
1257 | _on_find_in_files_requested("" ); |
1258 | } break; |
1259 | case REPLACE_IN_FILES: { |
1260 | _on_replace_in_files_requested("" ); |
1261 | } break; |
1262 | case SEARCH_HELP: { |
1263 | help_search_dialog->popup_dialog(); |
1264 | } break; |
1265 | case SEARCH_WEBSITE: { |
1266 | OS::get_singleton()->shell_open(VERSION_DOCS_URL "/" ); |
1267 | } break; |
1268 | case WINDOW_NEXT: { |
1269 | _history_forward(); |
1270 | } break; |
1271 | case WINDOW_PREV: { |
1272 | _history_back(); |
1273 | } break; |
1274 | case WINDOW_SORT: { |
1275 | _sort_list_on_update = true; |
1276 | _update_script_names(); |
1277 | } break; |
1278 | case TOGGLE_SCRIPTS_PANEL: { |
1279 | toggle_scripts_panel(); |
1280 | if (current) { |
1281 | current->update_toggle_scripts_button(); |
1282 | } else { |
1283 | Control *tab = tab_container->get_current_tab_control(); |
1284 | EditorHelp *editor_help = Object::cast_to<EditorHelp>(tab); |
1285 | if (editor_help) { |
1286 | editor_help->update_toggle_scripts_button(); |
1287 | } |
1288 | } |
1289 | } |
1290 | } |
1291 | |
1292 | if (current) { |
1293 | switch (p_option) { |
1294 | case FILE_SAVE: { |
1295 | save_current_script(); |
1296 | } break; |
1297 | case FILE_SAVE_AS: { |
1298 | if (trim_trailing_whitespace_on_save) { |
1299 | current->trim_trailing_whitespace(); |
1300 | } |
1301 | |
1302 | current->insert_final_newline(); |
1303 | |
1304 | if (convert_indent_on_save) { |
1305 | current->convert_indent(); |
1306 | } |
1307 | |
1308 | Ref<Resource> resource = current->get_edited_resource(); |
1309 | Ref<TextFile> text_file = resource; |
1310 | Ref<Script> scr = resource; |
1311 | |
1312 | if (text_file != nullptr) { |
1313 | file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); |
1314 | file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
1315 | file_dialog_option = FILE_SAVE_AS; |
1316 | |
1317 | List<String> extensions; |
1318 | ResourceLoader::get_recognized_extensions_for_type("Script" , &extensions); |
1319 | file_dialog->clear_filters(); |
1320 | file_dialog->set_current_dir(text_file->get_path().get_base_dir()); |
1321 | file_dialog->set_current_file(text_file->get_path().get_file()); |
1322 | file_dialog->popup_file_dialog(); |
1323 | file_dialog->set_title(TTR("Save File As..." )); |
1324 | break; |
1325 | } |
1326 | |
1327 | if (scr.is_valid()) { |
1328 | clear_docs_from_script(scr); |
1329 | } |
1330 | |
1331 | EditorNode::get_singleton()->push_item(resource.ptr()); |
1332 | EditorNode::get_singleton()->save_resource_as(resource); |
1333 | |
1334 | if (scr.is_valid()) { |
1335 | update_docs_from_script(scr); |
1336 | } |
1337 | } break; |
1338 | |
1339 | case FILE_TOOL_RELOAD_SOFT: { |
1340 | Ref<Script> scr = current->get_edited_resource(); |
1341 | if (scr == nullptr || scr.is_null()) { |
1342 | EditorNode::get_singleton()->show_warning(TTR("Can't obtain the script for reloading." )); |
1343 | break; |
1344 | } |
1345 | if (!scr->is_tool()) { |
1346 | EditorNode::get_singleton()->show_warning(TTR("Reload only takes effect on tool scripts." )); |
1347 | return; |
1348 | } |
1349 | scr->reload(true); |
1350 | |
1351 | } break; |
1352 | |
1353 | case FILE_RUN: { |
1354 | Ref<Script> scr = current->get_edited_resource(); |
1355 | if (scr == nullptr || scr.is_null()) { |
1356 | EditorToaster::get_singleton()->popup_str(TTR("Cannot run the edited file because it's not a script." ), EditorToaster::SEVERITY_WARNING); |
1357 | break; |
1358 | } |
1359 | |
1360 | current->apply_code(); |
1361 | |
1362 | Error err = scr->reload(false); // Always hard reload the script before running. |
1363 | if (err != OK || !scr->is_valid()) { |
1364 | EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log." ), EditorToaster::SEVERITY_WARNING); |
1365 | return; |
1366 | } |
1367 | |
1368 | // Perform additional checks on the script to evaluate if it's runnable. |
1369 | |
1370 | bool is_runnable = true; |
1371 | if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript" )) { |
1372 | is_runnable = false; |
1373 | |
1374 | EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript." ), EditorToaster::SEVERITY_WARNING); |
1375 | } |
1376 | if (!scr->is_tool()) { |
1377 | is_runnable = false; |
1378 | |
1379 | if (scr->get_class() == "GDScript" ) { |
1380 | EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script (add the @tool annotation at the top)." ), EditorToaster::SEVERITY_WARNING); |
1381 | } else { |
1382 | EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script." ), EditorToaster::SEVERITY_WARNING); |
1383 | } |
1384 | } |
1385 | if (!is_runnable) { |
1386 | return; |
1387 | } |
1388 | |
1389 | Ref<EditorScript> es = memnew(EditorScript); |
1390 | es->set_script(scr); |
1391 | es->run(); |
1392 | } break; |
1393 | |
1394 | case FILE_CLOSE: { |
1395 | if (current->is_unsaved()) { |
1396 | _ask_close_current_unsaved_tab(current); |
1397 | } else { |
1398 | _close_current_tab(false); |
1399 | } |
1400 | } break; |
1401 | case FILE_COPY_PATH: { |
1402 | _copy_script_path(); |
1403 | } break; |
1404 | case SHOW_IN_FILE_SYSTEM: { |
1405 | const Ref<Resource> scr = current->get_edited_resource(); |
1406 | String path = scr->get_path(); |
1407 | if (!path.is_empty()) { |
1408 | if (scr->is_built_in()) { |
1409 | path = path.get_slice("::" , 0); // Show the scene instead. |
1410 | } |
1411 | |
1412 | FileSystemDock *file_system_dock = FileSystemDock::get_singleton(); |
1413 | file_system_dock->navigate_to_path(path); |
1414 | // Ensure that the FileSystem dock is visible. |
1415 | TabContainer *dock_tab_container = (TabContainer *)file_system_dock->get_parent_control(); |
1416 | dock_tab_container->set_current_tab(dock_tab_container->get_tab_idx_from_control(file_system_dock)); |
1417 | } |
1418 | } break; |
1419 | case CLOSE_DOCS: { |
1420 | _close_docs_tab(); |
1421 | } break; |
1422 | case CLOSE_OTHER_TABS: { |
1423 | _close_other_tabs(); |
1424 | } break; |
1425 | case CLOSE_ALL: { |
1426 | _close_all_tabs(); |
1427 | } break; |
1428 | case WINDOW_MOVE_UP: { |
1429 | if (tab_container->get_current_tab() > 0) { |
1430 | tab_container->move_child(current, tab_container->get_current_tab() - 1); |
1431 | tab_container->set_current_tab(tab_container->get_current_tab()); |
1432 | _update_script_names(); |
1433 | } |
1434 | } break; |
1435 | case WINDOW_MOVE_DOWN: { |
1436 | if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) { |
1437 | tab_container->move_child(current, tab_container->get_current_tab() + 1); |
1438 | tab_container->set_current_tab(tab_container->get_current_tab()); |
1439 | _update_script_names(); |
1440 | } |
1441 | } break; |
1442 | default: { |
1443 | if (p_option >= WINDOW_SELECT_BASE) { |
1444 | _go_to_tab(p_option - WINDOW_SELECT_BASE); |
1445 | _update_script_names(); |
1446 | } |
1447 | } |
1448 | } |
1449 | } else { |
1450 | EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control()); |
1451 | if (help) { |
1452 | switch (p_option) { |
1453 | case HELP_SEARCH_FIND: { |
1454 | help->popup_search(); |
1455 | } break; |
1456 | case HELP_SEARCH_FIND_NEXT: { |
1457 | help->search_again(); |
1458 | } break; |
1459 | case HELP_SEARCH_FIND_PREVIOUS: { |
1460 | help->search_again(true); |
1461 | } break; |
1462 | case FILE_CLOSE: { |
1463 | _close_current_tab(); |
1464 | } break; |
1465 | case CLOSE_DOCS: { |
1466 | _close_docs_tab(); |
1467 | } break; |
1468 | case CLOSE_OTHER_TABS: { |
1469 | _close_other_tabs(); |
1470 | } break; |
1471 | case CLOSE_ALL: { |
1472 | _close_all_tabs(); |
1473 | } break; |
1474 | case WINDOW_MOVE_UP: { |
1475 | if (tab_container->get_current_tab() > 0) { |
1476 | tab_container->move_child(help, tab_container->get_current_tab() - 1); |
1477 | tab_container->set_current_tab(tab_container->get_current_tab()); |
1478 | _update_script_names(); |
1479 | } |
1480 | } break; |
1481 | case WINDOW_MOVE_DOWN: { |
1482 | if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) { |
1483 | tab_container->move_child(help, tab_container->get_current_tab() + 1); |
1484 | tab_container->set_current_tab(tab_container->get_current_tab()); |
1485 | _update_script_names(); |
1486 | } |
1487 | } break; |
1488 | } |
1489 | } |
1490 | } |
1491 | } |
1492 | |
1493 | void ScriptEditor::_theme_option(int p_option) { |
1494 | switch (p_option) { |
1495 | case THEME_IMPORT: { |
1496 | file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
1497 | file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
1498 | file_dialog_option = THEME_IMPORT; |
1499 | file_dialog->clear_filters(); |
1500 | file_dialog->add_filter("*.tet" ); |
1501 | file_dialog->popup_file_dialog(); |
1502 | file_dialog->set_title(TTR("Import Theme" )); |
1503 | } break; |
1504 | case THEME_RELOAD: { |
1505 | EditorSettings::get_singleton()->load_text_editor_theme(); |
1506 | } break; |
1507 | case THEME_SAVE: { |
1508 | if (EditorSettings::get_singleton()->is_default_text_editor_theme()) { |
1509 | ScriptEditor::_show_save_theme_as_dialog(); |
1510 | } else if (!EditorSettings::get_singleton()->save_text_editor_theme()) { |
1511 | EditorNode::get_singleton()->show_warning(TTR("Error while saving theme" ), TTR("Error saving" )); |
1512 | } |
1513 | } break; |
1514 | case THEME_SAVE_AS: { |
1515 | ScriptEditor::_show_save_theme_as_dialog(); |
1516 | } break; |
1517 | } |
1518 | } |
1519 | |
1520 | void ScriptEditor::_show_save_theme_as_dialog() { |
1521 | file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); |
1522 | file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
1523 | file_dialog_option = THEME_SAVE_AS; |
1524 | file_dialog->clear_filters(); |
1525 | file_dialog->add_filter("*.tet" ); |
1526 | file_dialog->set_current_path(EditorPaths::get_singleton()->get_text_editor_themes_dir().path_join(EDITOR_GET("text_editor/theme/color_theme" ))); |
1527 | file_dialog->popup_file_dialog(); |
1528 | file_dialog->set_title(TTR("Save Theme As..." )); |
1529 | } |
1530 | |
1531 | bool ScriptEditor::_has_docs_tab() const { |
1532 | const int child_count = tab_container->get_tab_count(); |
1533 | for (int i = 0; i < child_count; i++) { |
1534 | if (Object::cast_to<EditorHelp>(tab_container->get_tab_control(i))) { |
1535 | return true; |
1536 | } |
1537 | } |
1538 | return false; |
1539 | } |
1540 | |
1541 | bool ScriptEditor::_has_script_tab() const { |
1542 | const int child_count = tab_container->get_tab_count(); |
1543 | for (int i = 0; i < child_count; i++) { |
1544 | if (Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i))) { |
1545 | return true; |
1546 | } |
1547 | } |
1548 | return false; |
1549 | } |
1550 | |
1551 | void ScriptEditor::() { |
1552 | PopupMenu * = file_menu->get_popup(); |
1553 | const bool current_is_doc = _get_current_editor() == nullptr; |
1554 | |
1555 | menu->set_item_disabled(menu->get_item_index(FILE_REOPEN_CLOSED), previous_scripts.is_empty()); |
1556 | |
1557 | menu->set_item_disabled(menu->get_item_index(FILE_SAVE), current_is_doc); |
1558 | menu->set_item_disabled(menu->get_item_index(FILE_SAVE_AS), current_is_doc); |
1559 | menu->set_item_disabled(menu->get_item_index(FILE_SAVE_ALL), !_has_script_tab()); |
1560 | |
1561 | menu->set_item_disabled(menu->get_item_index(FILE_TOOL_RELOAD_SOFT), current_is_doc); |
1562 | menu->set_item_disabled(menu->get_item_index(FILE_COPY_PATH), current_is_doc); |
1563 | menu->set_item_disabled(menu->get_item_index(SHOW_IN_FILE_SYSTEM), current_is_doc); |
1564 | |
1565 | menu->set_item_disabled(menu->get_item_index(WINDOW_PREV), history_pos <= 0); |
1566 | menu->set_item_disabled(menu->get_item_index(WINDOW_NEXT), history_pos >= history.size() - 1); |
1567 | |
1568 | menu->set_item_disabled(menu->get_item_index(FILE_CLOSE), tab_container->get_tab_count() < 1); |
1569 | menu->set_item_disabled(menu->get_item_index(CLOSE_ALL), tab_container->get_tab_count() < 1); |
1570 | menu->set_item_disabled(menu->get_item_index(CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1); |
1571 | menu->set_item_disabled(menu->get_item_index(CLOSE_DOCS), !_has_docs_tab()); |
1572 | |
1573 | menu->set_item_disabled(menu->get_item_index(FILE_RUN), current_is_doc); |
1574 | } |
1575 | |
1576 | void ScriptEditor::() { |
1577 | PopupMenu * = file_menu->get_popup(); |
1578 | |
1579 | menu->set_item_disabled(menu->get_item_index(FILE_REOPEN_CLOSED), false); |
1580 | |
1581 | menu->set_item_disabled(menu->get_item_index(FILE_SAVE), false); |
1582 | menu->set_item_disabled(menu->get_item_index(FILE_SAVE_AS), false); |
1583 | menu->set_item_disabled(menu->get_item_index(FILE_SAVE_ALL), false); |
1584 | |
1585 | menu->set_item_disabled(menu->get_item_index(FILE_TOOL_RELOAD_SOFT), false); |
1586 | menu->set_item_disabled(menu->get_item_index(FILE_COPY_PATH), false); |
1587 | menu->set_item_disabled(menu->get_item_index(SHOW_IN_FILE_SYSTEM), false); |
1588 | |
1589 | menu->set_item_disabled(menu->get_item_index(WINDOW_PREV), false); |
1590 | menu->set_item_disabled(menu->get_item_index(WINDOW_NEXT), false); |
1591 | |
1592 | menu->set_item_disabled(menu->get_item_index(FILE_CLOSE), false); |
1593 | menu->set_item_disabled(menu->get_item_index(CLOSE_ALL), false); |
1594 | menu->set_item_disabled(menu->get_item_index(CLOSE_OTHER_TABS), false); |
1595 | menu->set_item_disabled(menu->get_item_index(CLOSE_DOCS), false); |
1596 | |
1597 | menu->set_item_disabled(menu->get_item_index(FILE_RUN), false); |
1598 | } |
1599 | |
1600 | void ScriptEditor::_tab_changed(int p_which) { |
1601 | ensure_select_current(); |
1602 | } |
1603 | |
1604 | void ScriptEditor::_notification(int p_what) { |
1605 | switch (p_what) { |
1606 | case NOTIFICATION_ENTER_TREE: { |
1607 | EditorRunBar::get_singleton()->connect("stop_pressed" , callable_mp(this, &ScriptEditor::_editor_stop)); |
1608 | _editor_settings_changed(); |
1609 | [[fallthrough]]; |
1610 | } |
1611 | |
1612 | case NOTIFICATION_TRANSLATION_CHANGED: |
1613 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
1614 | case NOTIFICATION_THEME_CHANGED: { |
1615 | tab_container->add_theme_style_override("panel" , get_theme_stylebox(SNAME("ScriptEditor" ), EditorStringName(EditorStyles))); |
1616 | |
1617 | help_search->set_icon(get_editor_theme_icon(SNAME("HelpSearch" ))); |
1618 | site_search->set_icon(get_editor_theme_icon(SNAME("ExternalLink" ))); |
1619 | |
1620 | if (is_layout_rtl()) { |
1621 | script_forward->set_icon(get_editor_theme_icon(SNAME("Back" ))); |
1622 | script_back->set_icon(get_editor_theme_icon(SNAME("Forward" ))); |
1623 | } else { |
1624 | script_forward->set_icon(get_editor_theme_icon(SNAME("Forward" ))); |
1625 | script_back->set_icon(get_editor_theme_icon(SNAME("Back" ))); |
1626 | } |
1627 | |
1628 | members_overview_alphabeta_sort_button->set_icon(get_editor_theme_icon(SNAME("Sort" ))); |
1629 | |
1630 | filter_scripts->set_right_icon(get_editor_theme_icon(SNAME("Search" ))); |
1631 | filter_methods->set_right_icon(get_editor_theme_icon(SNAME("Search" ))); |
1632 | |
1633 | filename->add_theme_style_override("normal" , get_theme_stylebox(SNAME("normal" ), SNAME("LineEdit" ))); |
1634 | |
1635 | recent_scripts->reset_size(); |
1636 | |
1637 | if (is_inside_tree()) { |
1638 | _update_script_colors(); |
1639 | _update_script_names(); |
1640 | } |
1641 | } break; |
1642 | |
1643 | case NOTIFICATION_READY: { |
1644 | // Can't set own styles in NOTIFICATION_THEME_CHANGED, so for now this will do. |
1645 | add_theme_style_override("panel" , get_theme_stylebox(SNAME("ScriptEditorPanel" ), SNAME("EditorStyles" ))); |
1646 | |
1647 | get_tree()->connect("tree_changed" , callable_mp(this, &ScriptEditor::_tree_changed)); |
1648 | InspectorDock::get_singleton()->connect("request_help" , callable_mp(this, &ScriptEditor::_help_class_open)); |
1649 | EditorNode::get_singleton()->connect("request_help_search" , callable_mp(this, &ScriptEditor::_help_search)); |
1650 | EditorNode::get_singleton()->connect("scene_closed" , callable_mp(this, &ScriptEditor::_close_builtin_scripts_from_scene)); |
1651 | EditorNode::get_singleton()->connect("script_add_function_request" , callable_mp(this, &ScriptEditor::_add_callback)); |
1652 | EditorNode::get_singleton()->connect("resource_saved" , callable_mp(this, &ScriptEditor::_res_saved_callback)); |
1653 | EditorNode::get_singleton()->connect("scene_saved" , callable_mp(this, &ScriptEditor::_scene_saved_callback)); |
1654 | FileSystemDock::get_singleton()->connect("files_moved" , callable_mp(this, &ScriptEditor::_files_moved)); |
1655 | FileSystemDock::get_singleton()->connect("file_removed" , callable_mp(this, &ScriptEditor::_file_removed)); |
1656 | script_list->connect("item_selected" , callable_mp(this, &ScriptEditor::_script_selected)); |
1657 | |
1658 | members_overview->connect("item_selected" , callable_mp(this, &ScriptEditor::_members_overview_selected)); |
1659 | help_overview->connect("item_selected" , callable_mp(this, &ScriptEditor::_help_overview_selected)); |
1660 | script_split->connect("dragged" , callable_mp(this, &ScriptEditor::_split_dragged)); |
1661 | list_split->connect("dragged" , callable_mp(this, &ScriptEditor::_split_dragged)); |
1662 | |
1663 | EditorSettings::get_singleton()->connect("settings_changed" , callable_mp(this, &ScriptEditor::_editor_settings_changed)); |
1664 | EditorFileSystem::get_singleton()->connect("filesystem_changed" , callable_mp(this, &ScriptEditor::_filesystem_changed)); |
1665 | } break; |
1666 | |
1667 | case NOTIFICATION_EXIT_TREE: { |
1668 | EditorRunBar::get_singleton()->disconnect("stop_pressed" , callable_mp(this, &ScriptEditor::_editor_stop)); |
1669 | } break; |
1670 | |
1671 | case NOTIFICATION_APPLICATION_FOCUS_IN: { |
1672 | _test_script_times_on_disk(); |
1673 | _update_modified_scripts_for_external_editor(); |
1674 | } break; |
1675 | |
1676 | case CanvasItem::NOTIFICATION_VISIBILITY_CHANGED: { |
1677 | if (is_visible()) { |
1678 | find_in_files_button->show(); |
1679 | } else { |
1680 | if (find_in_files->is_visible_in_tree()) { |
1681 | EditorNode::get_singleton()->hide_bottom_panel(); |
1682 | } |
1683 | find_in_files_button->hide(); |
1684 | } |
1685 | |
1686 | } break; |
1687 | } |
1688 | } |
1689 | |
1690 | bool ScriptEditor::can_take_away_focus() const { |
1691 | ScriptEditorBase *current = _get_current_editor(); |
1692 | if (current) { |
1693 | return current->can_lose_focus_on_node_selection(); |
1694 | } else { |
1695 | return true; |
1696 | } |
1697 | } |
1698 | |
1699 | void ScriptEditor::_close_builtin_scripts_from_scene(const String &p_scene) { |
1700 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
1701 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
1702 | |
1703 | if (se) { |
1704 | Ref<Script> scr = se->get_edited_resource(); |
1705 | if (scr == nullptr || !scr.is_valid()) { |
1706 | continue; |
1707 | } |
1708 | |
1709 | if (scr->is_built_in() && scr->get_path().begins_with(p_scene)) { // Is an internal script and belongs to scene being closed. |
1710 | _close_tab(i, false); |
1711 | i--; |
1712 | } |
1713 | } |
1714 | } |
1715 | } |
1716 | |
1717 | void ScriptEditor::edited_scene_changed() { |
1718 | _update_modified_scripts_for_external_editor(); |
1719 | } |
1720 | |
1721 | void ScriptEditor::notify_script_close(const Ref<Script> &p_script) { |
1722 | emit_signal(SNAME("script_close" ), p_script); |
1723 | } |
1724 | |
1725 | void ScriptEditor::notify_script_changed(const Ref<Script> &p_script) { |
1726 | emit_signal(SNAME("editor_script_changed" ), p_script); |
1727 | } |
1728 | |
1729 | void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) { |
1730 | HashSet<String> loaded_scripts; |
1731 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
1732 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
1733 | if (!se) { |
1734 | continue; |
1735 | } |
1736 | |
1737 | Ref<Script> scr = se->get_edited_resource(); |
1738 | if (scr == nullptr) { |
1739 | continue; |
1740 | } |
1741 | |
1742 | String base = scr->get_path(); |
1743 | loaded_scripts.insert(base); |
1744 | if (base.begins_with("local://" ) || base.is_empty()) { |
1745 | continue; |
1746 | } |
1747 | |
1748 | PackedInt32Array bpoints = se->get_breakpoints(); |
1749 | for (int j = 0; j < bpoints.size(); j++) { |
1750 | p_breakpoints->push_back(base + ":" + itos((int)bpoints[j] + 1)); |
1751 | } |
1752 | } |
1753 | |
1754 | // Load breakpoints that are in closed scripts. |
1755 | List<String> cached_editors; |
1756 | script_editor_cache->get_sections(&cached_editors); |
1757 | for (const String &E : cached_editors) { |
1758 | if (loaded_scripts.has(E)) { |
1759 | continue; |
1760 | } |
1761 | |
1762 | Array breakpoints = _get_cached_breakpoints_for_script(E); |
1763 | for (int i = 0; i < breakpoints.size(); i++) { |
1764 | p_breakpoints->push_back(E + ":" + itos((int)breakpoints[i] + 1)); |
1765 | } |
1766 | } |
1767 | } |
1768 | |
1769 | void ScriptEditor::_members_overview_selected(int p_idx) { |
1770 | ScriptEditorBase *se = _get_current_editor(); |
1771 | if (!se) { |
1772 | return; |
1773 | } |
1774 | // Go to the member's line and reset the cursor column. We can't change scroll_position |
1775 | // directly until we have gone to the line first, since code might be folded. |
1776 | se->goto_line(members_overview->get_item_metadata(p_idx)); |
1777 | Dictionary state = se->get_edit_state(); |
1778 | state["column" ] = 0; |
1779 | state["scroll_position" ] = members_overview->get_item_metadata(p_idx); |
1780 | se->set_edit_state(state); |
1781 | } |
1782 | |
1783 | void ScriptEditor::_help_overview_selected(int p_idx) { |
1784 | Node *current = tab_container->get_tab_control(tab_container->get_current_tab()); |
1785 | EditorHelp *se = Object::cast_to<EditorHelp>(current); |
1786 | if (!se) { |
1787 | return; |
1788 | } |
1789 | se->scroll_to_section(help_overview->get_item_metadata(p_idx)); |
1790 | } |
1791 | |
1792 | void ScriptEditor::_script_selected(int p_idx) { |
1793 | grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT); //amazing hack, simply amazing |
1794 | |
1795 | _go_to_tab(script_list->get_item_metadata(p_idx)); |
1796 | grab_focus_block = false; |
1797 | } |
1798 | |
1799 | void ScriptEditor::ensure_select_current() { |
1800 | if (tab_container->get_tab_count() && tab_container->get_current_tab() >= 0) { |
1801 | ScriptEditorBase *se = _get_current_editor(); |
1802 | if (se) { |
1803 | se->enable_editor(this); |
1804 | |
1805 | if (!grab_focus_block && is_visible_in_tree()) { |
1806 | se->ensure_focus(); |
1807 | } |
1808 | } |
1809 | } |
1810 | _update_find_replace_bar(); |
1811 | |
1812 | _update_selected_editor_menu(); |
1813 | } |
1814 | |
1815 | bool ScriptEditor::is_editor_floating() { |
1816 | return is_floating; |
1817 | } |
1818 | |
1819 | void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, HashSet<Ref<Script>> &used) { |
1820 | if (p_current != p_base && p_current->get_owner() != p_base) { |
1821 | return; |
1822 | } |
1823 | |
1824 | if (p_current->get_script_instance()) { |
1825 | Ref<Script> scr = p_current->get_script(); |
1826 | if (scr.is_valid()) { |
1827 | used.insert(scr); |
1828 | } |
1829 | } |
1830 | |
1831 | for (int i = 0; i < p_current->get_child_count(); i++) { |
1832 | _find_scripts(p_base, p_current->get_child(i), used); |
1833 | } |
1834 | } |
1835 | |
1836 | struct _ScriptEditorItemData { |
1837 | String name; |
1838 | String sort_key; |
1839 | Ref<Texture2D> icon; |
1840 | bool tool = false; |
1841 | int index = 0; |
1842 | String tooltip; |
1843 | bool used = false; |
1844 | int category = 0; |
1845 | Node *ref = nullptr; |
1846 | |
1847 | bool operator<(const _ScriptEditorItemData &id) const { |
1848 | if (category == id.category) { |
1849 | if (sort_key == id.sort_key) { |
1850 | return index < id.index; |
1851 | } else { |
1852 | return sort_key.naturalnocasecmp_to(id.sort_key) < 0; |
1853 | } |
1854 | } else { |
1855 | return category < id.category; |
1856 | } |
1857 | } |
1858 | }; |
1859 | |
1860 | void ScriptEditor::_update_members_overview_visibility() { |
1861 | ScriptEditorBase *se = _get_current_editor(); |
1862 | if (!se) { |
1863 | members_overview_alphabeta_sort_button->set_visible(false); |
1864 | members_overview->set_visible(false); |
1865 | overview_vbox->set_visible(false); |
1866 | return; |
1867 | } |
1868 | |
1869 | if (members_overview_enabled && se->show_members_overview()) { |
1870 | members_overview_alphabeta_sort_button->set_visible(true); |
1871 | filter_methods->set_visible(true); |
1872 | members_overview->set_visible(true); |
1873 | overview_vbox->set_visible(true); |
1874 | } else { |
1875 | members_overview_alphabeta_sort_button->set_visible(false); |
1876 | filter_methods->set_visible(false); |
1877 | members_overview->set_visible(false); |
1878 | overview_vbox->set_visible(false); |
1879 | } |
1880 | } |
1881 | |
1882 | void ScriptEditor::_toggle_members_overview_alpha_sort(bool p_alphabetic_sort) { |
1883 | EditorSettings::get_singleton()->set("text_editor/script_list/sort_members_outline_alphabetically" , p_alphabetic_sort); |
1884 | _update_members_overview(); |
1885 | } |
1886 | |
1887 | void ScriptEditor::_update_members_overview() { |
1888 | members_overview->clear(); |
1889 | |
1890 | ScriptEditorBase *se = _get_current_editor(); |
1891 | if (!se) { |
1892 | return; |
1893 | } |
1894 | |
1895 | Vector<String> functions = se->get_functions(); |
1896 | if (EDITOR_GET("text_editor/script_list/sort_members_outline_alphabetically" )) { |
1897 | functions.sort(); |
1898 | } |
1899 | |
1900 | for (int i = 0; i < functions.size(); i++) { |
1901 | String filter = filter_methods->get_text(); |
1902 | String name = functions[i].get_slice(":" , 0); |
1903 | if (filter.is_empty() || filter.is_subsequence_ofn(name)) { |
1904 | members_overview->add_item(name); |
1905 | members_overview->set_item_metadata(-1, functions[i].get_slice(":" , 1).to_int() - 1); |
1906 | } |
1907 | } |
1908 | |
1909 | String path = se->get_edited_resource()->get_path(); |
1910 | bool built_in = !path.is_resource_file(); |
1911 | String name = built_in ? path.get_file() : se->get_name(); |
1912 | filename->set_text(name); |
1913 | } |
1914 | |
1915 | void ScriptEditor::_update_help_overview_visibility() { |
1916 | int selected = tab_container->get_current_tab(); |
1917 | if (selected < 0 || selected >= tab_container->get_tab_count()) { |
1918 | help_overview->set_visible(false); |
1919 | return; |
1920 | } |
1921 | |
1922 | Node *current = tab_container->get_tab_control(tab_container->get_current_tab()); |
1923 | EditorHelp *se = Object::cast_to<EditorHelp>(current); |
1924 | if (!se) { |
1925 | help_overview->set_visible(false); |
1926 | return; |
1927 | } |
1928 | |
1929 | if (help_overview_enabled) { |
1930 | members_overview_alphabeta_sort_button->set_visible(false); |
1931 | filter_methods->set_visible(false); |
1932 | help_overview->set_visible(true); |
1933 | overview_vbox->set_visible(true); |
1934 | filename->set_text(se->get_name()); |
1935 | } else { |
1936 | help_overview->set_visible(false); |
1937 | overview_vbox->set_visible(false); |
1938 | } |
1939 | } |
1940 | |
1941 | void ScriptEditor::_update_help_overview() { |
1942 | help_overview->clear(); |
1943 | |
1944 | int selected = tab_container->get_current_tab(); |
1945 | if (selected < 0 || selected >= tab_container->get_tab_count()) { |
1946 | return; |
1947 | } |
1948 | |
1949 | Node *current = tab_container->get_tab_control(tab_container->get_current_tab()); |
1950 | EditorHelp *se = Object::cast_to<EditorHelp>(current); |
1951 | if (!se) { |
1952 | return; |
1953 | } |
1954 | |
1955 | Vector<Pair<String, int>> sections = se->get_sections(); |
1956 | for (int i = 0; i < sections.size(); i++) { |
1957 | help_overview->add_item(sections[i].first); |
1958 | help_overview->set_item_metadata(i, sections[i].second); |
1959 | } |
1960 | } |
1961 | |
1962 | void ScriptEditor::_update_script_colors() { |
1963 | bool script_temperature_enabled = EDITOR_GET("text_editor/script_list/script_temperature_enabled" ); |
1964 | |
1965 | int hist_size = EDITOR_GET("text_editor/script_list/script_temperature_history_size" ); |
1966 | Color hot_color = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
1967 | hot_color.set_s(hot_color.get_s() * 0.9); |
1968 | Color cold_color = get_theme_color(SNAME("font_color" ), EditorStringName(Editor)); |
1969 | |
1970 | for (int i = 0; i < script_list->get_item_count(); i++) { |
1971 | int c = script_list->get_item_metadata(i); |
1972 | Node *n = tab_container->get_tab_control(c); |
1973 | if (!n) { |
1974 | continue; |
1975 | } |
1976 | |
1977 | script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0)); |
1978 | |
1979 | if (script_temperature_enabled) { |
1980 | int pass = n->get_meta("__editor_pass" , -1); |
1981 | if (pass < 0) { |
1982 | continue; |
1983 | } |
1984 | |
1985 | int h = edit_pass - pass; |
1986 | if (h > hist_size) { |
1987 | continue; |
1988 | } |
1989 | int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size; |
1990 | float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4); |
1991 | |
1992 | script_list->set_item_custom_fg_color(i, hot_color.lerp(cold_color, v)); |
1993 | } |
1994 | } |
1995 | } |
1996 | |
1997 | void ScriptEditor::_update_script_names() { |
1998 | if (restoring_layout) { |
1999 | return; |
2000 | } |
2001 | |
2002 | HashSet<Ref<Script>> used; |
2003 | Node *edited = EditorNode::get_singleton()->get_edited_scene(); |
2004 | if (edited) { |
2005 | _find_scripts(edited, edited, used); |
2006 | } |
2007 | |
2008 | script_list->clear(); |
2009 | bool split_script_help = EDITOR_GET("text_editor/script_list/group_help_pages" ); |
2010 | ScriptSortBy sort_by = (ScriptSortBy)(int)EDITOR_GET("text_editor/script_list/sort_scripts_by" ); |
2011 | ScriptListName display_as = (ScriptListName)(int)EDITOR_GET("text_editor/script_list/list_script_names_as" ); |
2012 | |
2013 | Vector<_ScriptEditorItemData> sedata; |
2014 | |
2015 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2016 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2017 | if (se) { |
2018 | Ref<Texture2D> icon = se->get_theme_icon(); |
2019 | String path = se->get_edited_resource()->get_path(); |
2020 | bool saved = !path.is_empty(); |
2021 | if (saved) { |
2022 | // The script might be deleted, moved, or renamed, so make sure |
2023 | // to update original path to previously edited resource. |
2024 | se->set_meta("_edit_res_path" , path); |
2025 | } |
2026 | String name = se->get_name(); |
2027 | Ref<Script> scr = se->get_edited_resource(); |
2028 | |
2029 | _ScriptEditorItemData sd; |
2030 | sd.icon = icon; |
2031 | sd.name = name; |
2032 | sd.tooltip = saved ? path : TTR("Unsaved file." ); |
2033 | sd.index = i; |
2034 | sd.used = used.has(se->get_edited_resource()); |
2035 | sd.category = 0; |
2036 | sd.ref = se; |
2037 | if (scr.is_valid()) { |
2038 | sd.tool = scr->is_tool(); |
2039 | } |
2040 | |
2041 | switch (sort_by) { |
2042 | case SORT_BY_NAME: { |
2043 | sd.sort_key = name.to_lower(); |
2044 | } break; |
2045 | case SORT_BY_PATH: { |
2046 | sd.sort_key = path; |
2047 | } break; |
2048 | case SORT_BY_NONE: { |
2049 | sd.sort_key = "" ; |
2050 | } break; |
2051 | } |
2052 | |
2053 | switch (display_as) { |
2054 | case DISPLAY_NAME: { |
2055 | sd.name = name; |
2056 | } break; |
2057 | case DISPLAY_DIR_AND_NAME: { |
2058 | if (!path.get_base_dir().get_file().is_empty()) { |
2059 | sd.name = path.get_base_dir().get_file().path_join(name); |
2060 | } else { |
2061 | sd.name = name; |
2062 | } |
2063 | } break; |
2064 | case DISPLAY_FULL_PATH: { |
2065 | sd.name = path; |
2066 | } break; |
2067 | } |
2068 | if (!saved) { |
2069 | sd.name = se->get_name(); |
2070 | } |
2071 | |
2072 | sedata.push_back(sd); |
2073 | } |
2074 | |
2075 | Vector<String> disambiguated_script_names; |
2076 | Vector<String> full_script_paths; |
2077 | for (int j = 0; j < sedata.size(); j++) { |
2078 | String name = sedata[j].name.replace("(*)" , "" ); |
2079 | ScriptListName script_display = (ScriptListName)(int)EDITOR_GET("text_editor/script_list/list_script_names_as" ); |
2080 | switch (script_display) { |
2081 | case DISPLAY_NAME: { |
2082 | name = name.get_file(); |
2083 | } break; |
2084 | case DISPLAY_DIR_AND_NAME: { |
2085 | name = name.get_base_dir().get_file().path_join(name.get_file()); |
2086 | } break; |
2087 | default: |
2088 | break; |
2089 | } |
2090 | |
2091 | disambiguated_script_names.append(name); |
2092 | full_script_paths.append(sedata[j].tooltip); |
2093 | } |
2094 | |
2095 | EditorNode::disambiguate_filenames(full_script_paths, disambiguated_script_names); |
2096 | |
2097 | for (int j = 0; j < sedata.size(); j++) { |
2098 | if (sedata[j].name.ends_with("(*)" )) { |
2099 | sedata.write[j].name = disambiguated_script_names[j] + "(*)" ; |
2100 | } else { |
2101 | sedata.write[j].name = disambiguated_script_names[j]; |
2102 | } |
2103 | } |
2104 | |
2105 | EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i)); |
2106 | if (eh) { |
2107 | String name = eh->get_class(); |
2108 | Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Help" )); |
2109 | String tooltip = vformat(TTR("%s Class Reference" ), name); |
2110 | |
2111 | _ScriptEditorItemData sd; |
2112 | sd.icon = icon; |
2113 | sd.name = name; |
2114 | sd.sort_key = name.to_lower(); |
2115 | sd.tooltip = tooltip; |
2116 | sd.index = i; |
2117 | sd.used = false; |
2118 | sd.category = split_script_help ? 1 : 0; |
2119 | sd.ref = eh; |
2120 | |
2121 | sedata.push_back(sd); |
2122 | } |
2123 | } |
2124 | |
2125 | if (_sort_list_on_update && !sedata.is_empty()) { |
2126 | sedata.sort(); |
2127 | |
2128 | // change actual order of tab_container so that the order can be rearranged by user |
2129 | int cur_tab = tab_container->get_current_tab(); |
2130 | int prev_tab = tab_container->get_previous_tab(); |
2131 | int new_cur_tab = -1; |
2132 | int new_prev_tab = -1; |
2133 | for (int i = 0; i < sedata.size(); i++) { |
2134 | tab_container->move_child(sedata[i].ref, i); |
2135 | if (new_prev_tab == -1 && sedata[i].index == prev_tab) { |
2136 | new_prev_tab = i; |
2137 | } |
2138 | if (new_cur_tab == -1 && sedata[i].index == cur_tab) { |
2139 | new_cur_tab = i; |
2140 | } |
2141 | // Update index of sd entries for sorted order |
2142 | _ScriptEditorItemData sd = sedata[i]; |
2143 | sd.index = i; |
2144 | sedata.set(i, sd); |
2145 | } |
2146 | _go_to_tab(new_prev_tab); |
2147 | _go_to_tab(new_cur_tab); |
2148 | _sort_list_on_update = false; |
2149 | } |
2150 | |
2151 | Vector<_ScriptEditorItemData> sedata_filtered; |
2152 | for (int i = 0; i < sedata.size(); i++) { |
2153 | String filter = filter_scripts->get_text(); |
2154 | if (filter.is_empty() || filter.is_subsequence_ofn(sedata[i].name)) { |
2155 | sedata_filtered.push_back(sedata[i]); |
2156 | } |
2157 | } |
2158 | |
2159 | Color tool_color = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
2160 | tool_color.set_s(tool_color.get_s() * 1.5); |
2161 | for (int i = 0; i < sedata_filtered.size(); i++) { |
2162 | script_list->add_item(sedata_filtered[i].name, sedata_filtered[i].icon); |
2163 | if (sedata_filtered[i].tool) { |
2164 | script_list->set_item_icon_modulate(-1, tool_color); |
2165 | } |
2166 | |
2167 | int index = script_list->get_item_count() - 1; |
2168 | script_list->set_item_tooltip(index, sedata_filtered[i].tooltip); |
2169 | script_list->set_item_metadata(index, sedata_filtered[i].index); /* Saving as metadata the script's index in the tab container and not the filtered one */ |
2170 | if (sedata_filtered[i].used) { |
2171 | script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0)); |
2172 | } |
2173 | if (tab_container->get_current_tab() == sedata_filtered[i].index) { |
2174 | script_list->select(index); |
2175 | |
2176 | script_name_label->set_text(sedata_filtered[i].name); |
2177 | script_icon->set_texture(sedata_filtered[i].icon); |
2178 | |
2179 | ScriptEditorBase *se = _get_current_editor(); |
2180 | if (se) { |
2181 | se->enable_editor(this); |
2182 | _update_selected_editor_menu(); |
2183 | } |
2184 | } |
2185 | } |
2186 | |
2187 | if (!waiting_update_names) { |
2188 | _update_members_overview(); |
2189 | _update_help_overview(); |
2190 | } else { |
2191 | waiting_update_names = false; |
2192 | } |
2193 | _update_members_overview_visibility(); |
2194 | _update_help_overview_visibility(); |
2195 | _update_script_colors(); |
2196 | } |
2197 | |
2198 | Ref<TextFile> ScriptEditor::_load_text_file(const String &p_path, Error *r_error) const { |
2199 | if (r_error) { |
2200 | *r_error = ERR_FILE_CANT_OPEN; |
2201 | } |
2202 | |
2203 | String local_path = ProjectSettings::get_singleton()->localize_path(p_path); |
2204 | String path = ResourceLoader::path_remap(local_path); |
2205 | |
2206 | TextFile *text_file = memnew(TextFile); |
2207 | Ref<TextFile> text_res(text_file); |
2208 | Error err = text_file->load_text(path); |
2209 | |
2210 | ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot load text file '" + path + "'." ); |
2211 | |
2212 | text_file->set_file_path(local_path); |
2213 | text_file->set_path(local_path, true); |
2214 | |
2215 | if (ResourceLoader::get_timestamp_on_load()) { |
2216 | text_file->set_last_modified_time(FileAccess::get_modified_time(path)); |
2217 | } |
2218 | |
2219 | if (r_error) { |
2220 | *r_error = OK; |
2221 | } |
2222 | |
2223 | return text_res; |
2224 | } |
2225 | |
2226 | Error ScriptEditor::_save_text_file(Ref<TextFile> p_text_file, const String &p_path) { |
2227 | Ref<TextFile> sqscr = p_text_file; |
2228 | ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER); |
2229 | |
2230 | String source = sqscr->get_text(); |
2231 | |
2232 | Error err; |
2233 | { |
2234 | Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err); |
2235 | |
2236 | ERR_FAIL_COND_V_MSG(err, err, "Cannot save text file '" + p_path + "'." ); |
2237 | |
2238 | file->store_string(source); |
2239 | if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { |
2240 | return ERR_CANT_CREATE; |
2241 | } |
2242 | } |
2243 | |
2244 | if (ResourceSaver::get_timestamp_on_save()) { |
2245 | p_text_file->set_last_modified_time(FileAccess::get_modified_time(p_path)); |
2246 | } |
2247 | |
2248 | EditorFileSystem::get_singleton()->update_file(p_path); |
2249 | |
2250 | _res_saved_callback(sqscr); |
2251 | return OK; |
2252 | } |
2253 | |
2254 | bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col, bool p_grab_focus) { |
2255 | if (p_resource.is_null()) { |
2256 | return false; |
2257 | } |
2258 | |
2259 | Ref<Script> scr = p_resource; |
2260 | |
2261 | // Don't open dominant script if using an external editor. |
2262 | bool use_external_editor = |
2263 | EDITOR_GET("text_editor/external/use_external_editor" ) || |
2264 | (scr.is_valid() && scr->get_language()->overrides_external_editor()); |
2265 | use_external_editor = use_external_editor && !(scr.is_valid() && scr->is_built_in()); // Ignore external editor for built-in scripts. |
2266 | const bool open_dominant = EDITOR_GET("text_editor/behavior/files/open_dominant_script_on_scene_change" ); |
2267 | |
2268 | const bool should_open = (open_dominant && !use_external_editor) || !EditorNode::get_singleton()->is_changing_scene(); |
2269 | |
2270 | if (scr.is_valid() && scr->get_language()->overrides_external_editor()) { |
2271 | if (should_open) { |
2272 | Error err = scr->get_language()->open_in_external_editor(scr, p_line >= 0 ? p_line : 0, p_col); |
2273 | if (err != OK) { |
2274 | ERR_PRINT("Couldn't open script in the overridden external text editor" ); |
2275 | } |
2276 | } |
2277 | return false; |
2278 | } |
2279 | |
2280 | if (use_external_editor && |
2281 | (EditorDebuggerNode::get_singleton()->get_dump_stack_script() != p_resource || EditorDebuggerNode::get_singleton()->get_debug_with_external_editor()) && |
2282 | p_resource->get_path().is_resource_file()) { |
2283 | String path = EDITOR_GET("text_editor/external/exec_path" ); |
2284 | String flags = EDITOR_GET("text_editor/external/exec_flags" ); |
2285 | |
2286 | List<String> args; |
2287 | bool has_file_flag = false; |
2288 | String script_path = ProjectSettings::get_singleton()->globalize_path(p_resource->get_path()); |
2289 | |
2290 | if (flags.size()) { |
2291 | String project_path = ProjectSettings::get_singleton()->get_resource_path(); |
2292 | |
2293 | flags = flags.replacen("{line}" , itos(p_line > 0 ? p_line : 0)); |
2294 | flags = flags.replacen("{col}" , itos(p_col)); |
2295 | flags = flags.strip_edges().replace("\\\\" , "\\" ); |
2296 | |
2297 | int from = 0; |
2298 | int num_chars = 0; |
2299 | bool inside_quotes = false; |
2300 | |
2301 | for (int i = 0; i < flags.size(); i++) { |
2302 | if (flags[i] == '"' && (!i || flags[i - 1] != '\\')) { |
2303 | if (!inside_quotes) { |
2304 | from++; |
2305 | } |
2306 | inside_quotes = !inside_quotes; |
2307 | |
2308 | } else if (flags[i] == '\0' || (!inside_quotes && flags[i] == ' ')) { |
2309 | String arg = flags.substr(from, num_chars); |
2310 | if (arg.contains("{file}" )) { |
2311 | has_file_flag = true; |
2312 | } |
2313 | |
2314 | // do path replacement here, else there will be issues with spaces and quotes |
2315 | arg = arg.replacen("{project}" , project_path); |
2316 | arg = arg.replacen("{file}" , script_path); |
2317 | args.push_back(arg); |
2318 | |
2319 | from = i + 1; |
2320 | num_chars = 0; |
2321 | } else { |
2322 | num_chars++; |
2323 | } |
2324 | } |
2325 | } |
2326 | |
2327 | // Default to passing script path if no {file} flag is specified. |
2328 | if (!has_file_flag) { |
2329 | args.push_back(script_path); |
2330 | } |
2331 | |
2332 | if (!path.is_empty()) { |
2333 | Error err = OS::get_singleton()->create_process(path, args); |
2334 | if (err == OK) { |
2335 | return false; |
2336 | } |
2337 | } |
2338 | |
2339 | ERR_PRINT("Couldn't open external text editor, falling back to the internal editor. Review your `text_editor/external/` editor settings." ); |
2340 | } |
2341 | |
2342 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2343 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2344 | if (!se) { |
2345 | continue; |
2346 | } |
2347 | |
2348 | if ((scr != nullptr && se->get_edited_resource() == p_resource) || se->get_edited_resource()->get_path() == p_resource->get_path()) { |
2349 | if (should_open) { |
2350 | se->enable_editor(this); |
2351 | |
2352 | if (tab_container->get_current_tab() != i) { |
2353 | _go_to_tab(i); |
2354 | } |
2355 | if (is_visible_in_tree()) { |
2356 | se->ensure_focus(); |
2357 | } |
2358 | |
2359 | if (p_line > 0) { |
2360 | se->goto_line(p_line); |
2361 | } |
2362 | } |
2363 | _update_script_names(); |
2364 | script_list->ensure_current_is_visible(); |
2365 | return true; |
2366 | } |
2367 | } |
2368 | |
2369 | // doesn't have it, make a new one |
2370 | |
2371 | ScriptEditorBase *se = nullptr; |
2372 | |
2373 | for (int i = script_editor_func_count - 1; i >= 0; i--) { |
2374 | se = script_editor_funcs[i](p_resource); |
2375 | if (se) { |
2376 | break; |
2377 | } |
2378 | } |
2379 | ERR_FAIL_NULL_V(se, false); |
2380 | |
2381 | se->set_edited_resource(p_resource); |
2382 | |
2383 | // Syntax highlighting. |
2384 | bool highlighter_set = false; |
2385 | for (int i = 0; i < syntax_highlighters.size(); i++) { |
2386 | Ref<EditorSyntaxHighlighter> highlighter = syntax_highlighters[i]->_create(); |
2387 | if (highlighter.is_null()) { |
2388 | continue; |
2389 | } |
2390 | se->add_syntax_highlighter(highlighter); |
2391 | |
2392 | if (highlighter_set) { |
2393 | continue; |
2394 | } |
2395 | |
2396 | PackedStringArray languages = highlighter->_get_supported_languages(); |
2397 | // If script try language, else use extension. |
2398 | if (scr != nullptr) { |
2399 | if (languages.has(scr->get_language()->get_name())) { |
2400 | se->set_syntax_highlighter(highlighter); |
2401 | highlighter_set = true; |
2402 | } |
2403 | continue; |
2404 | } |
2405 | |
2406 | if (languages.has(p_resource->get_path().get_extension())) { |
2407 | se->set_syntax_highlighter(highlighter); |
2408 | highlighter_set = true; |
2409 | } |
2410 | } |
2411 | |
2412 | tab_container->add_child(se); |
2413 | |
2414 | if (p_grab_focus) { |
2415 | se->enable_editor(this); |
2416 | } |
2417 | |
2418 | // If we delete a script within the filesystem, the original resource path |
2419 | // is lost, so keep it as metadata to figure out the exact tab to delete. |
2420 | se->set_meta("_edit_res_path" , p_resource->get_path()); |
2421 | se->set_tooltip_request_func(callable_mp(this, &ScriptEditor::_get_debug_tooltip)); |
2422 | if (se->get_edit_menu()) { |
2423 | se->get_edit_menu()->hide(); |
2424 | menu_hb->add_child(se->get_edit_menu()); |
2425 | menu_hb->move_child(se->get_edit_menu(), 1); |
2426 | } |
2427 | |
2428 | if (p_grab_focus) { |
2429 | _go_to_tab(tab_container->get_tab_count() - 1); |
2430 | _add_recent_script(p_resource->get_path()); |
2431 | } |
2432 | |
2433 | if (script_editor_cache->has_section(p_resource->get_path())) { |
2434 | se->set_edit_state(script_editor_cache->get_value(p_resource->get_path(), "state" )); |
2435 | } |
2436 | |
2437 | _sort_list_on_update = true; |
2438 | _update_script_names(); |
2439 | _save_layout(); |
2440 | se->connect("name_changed" , callable_mp(this, &ScriptEditor::_update_script_names)); |
2441 | se->connect("edited_script_changed" , callable_mp(this, &ScriptEditor::_script_changed)); |
2442 | se->connect("request_help" , callable_mp(this, &ScriptEditor::_help_search)); |
2443 | se->connect("request_open_script_at_line" , callable_mp(this, &ScriptEditor::_goto_script_line)); |
2444 | se->connect("go_to_help" , callable_mp(this, &ScriptEditor::_help_class_goto)); |
2445 | se->connect("request_save_history" , callable_mp(this, &ScriptEditor::_save_history)); |
2446 | se->connect("search_in_files_requested" , callable_mp(this, &ScriptEditor::_on_find_in_files_requested)); |
2447 | se->connect("replace_in_files_requested" , callable_mp(this, &ScriptEditor::_on_replace_in_files_requested)); |
2448 | se->connect("go_to_method" , callable_mp(this, &ScriptEditor::script_goto_method)); |
2449 | |
2450 | //test for modification, maybe the script was not edited but was loaded |
2451 | |
2452 | _test_script_times_on_disk(p_resource); |
2453 | _update_modified_scripts_for_external_editor(p_resource); |
2454 | |
2455 | if (p_line >= 0) { |
2456 | se->goto_line(p_line); |
2457 | } |
2458 | |
2459 | notify_script_changed(p_resource); |
2460 | return true; |
2461 | } |
2462 | |
2463 | PackedStringArray ScriptEditor::get_unsaved_scripts() const { |
2464 | PackedStringArray unsaved_list; |
2465 | |
2466 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2467 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2468 | if (se && se->is_unsaved()) { |
2469 | unsaved_list.append(se->get_name()); |
2470 | } |
2471 | } |
2472 | return unsaved_list; |
2473 | } |
2474 | |
2475 | void ScriptEditor::save_current_script() { |
2476 | ScriptEditorBase *current = _get_current_editor(); |
2477 | if (!current || _test_script_times_on_disk()) { |
2478 | return; |
2479 | } |
2480 | |
2481 | if (trim_trailing_whitespace_on_save) { |
2482 | current->trim_trailing_whitespace(); |
2483 | } |
2484 | |
2485 | current->insert_final_newline(); |
2486 | |
2487 | if (convert_indent_on_save) { |
2488 | current->convert_indent(); |
2489 | } |
2490 | |
2491 | Ref<Resource> resource = current->get_edited_resource(); |
2492 | Ref<TextFile> text_file = resource; |
2493 | Ref<Script> scr = resource; |
2494 | |
2495 | if (text_file != nullptr) { |
2496 | current->apply_code(); |
2497 | _save_text_file(text_file, text_file->get_path()); |
2498 | return; |
2499 | } |
2500 | |
2501 | if (scr.is_valid()) { |
2502 | clear_docs_from_script(scr); |
2503 | } |
2504 | |
2505 | if (resource->is_built_in()) { |
2506 | // If built-in script, save the scene instead. |
2507 | const String scene_path = resource->get_path().get_slice("::" , 0); |
2508 | if (!scene_path.is_empty()) { |
2509 | Vector<String> scene_to_save; |
2510 | scene_to_save.push_back(scene_path); |
2511 | EditorNode::get_singleton()->save_scene_list(scene_to_save); |
2512 | } |
2513 | } else { |
2514 | EditorNode::get_singleton()->save_resource(resource); |
2515 | } |
2516 | |
2517 | if (scr.is_valid()) { |
2518 | update_docs_from_script(scr); |
2519 | } |
2520 | } |
2521 | |
2522 | void ScriptEditor::save_all_scripts() { |
2523 | Vector<String> scenes_to_save; |
2524 | |
2525 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2526 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2527 | if (!se) { |
2528 | continue; |
2529 | } |
2530 | |
2531 | if (convert_indent_on_save) { |
2532 | se->convert_indent(); |
2533 | } |
2534 | |
2535 | if (trim_trailing_whitespace_on_save) { |
2536 | se->trim_trailing_whitespace(); |
2537 | } |
2538 | |
2539 | se->insert_final_newline(); |
2540 | |
2541 | if (!se->is_unsaved()) { |
2542 | continue; |
2543 | } |
2544 | |
2545 | Ref<Resource> edited_res = se->get_edited_resource(); |
2546 | if (edited_res.is_valid()) { |
2547 | se->apply_code(); |
2548 | } |
2549 | |
2550 | if (!edited_res->is_built_in()) { |
2551 | Ref<TextFile> text_file = edited_res; |
2552 | Ref<Script> scr = edited_res; |
2553 | |
2554 | if (text_file != nullptr) { |
2555 | _save_text_file(text_file, text_file->get_path()); |
2556 | continue; |
2557 | } |
2558 | |
2559 | if (scr.is_valid()) { |
2560 | clear_docs_from_script(scr); |
2561 | } |
2562 | |
2563 | EditorNode::get_singleton()->save_resource(edited_res); //external script, save it |
2564 | |
2565 | if (scr.is_valid()) { |
2566 | update_docs_from_script(scr); |
2567 | } |
2568 | } else { |
2569 | // For built-in scripts, save their scenes instead. |
2570 | const String scene_path = edited_res->get_path().get_slice("::" , 0); |
2571 | if (!scene_path.is_empty() && !scenes_to_save.has(scene_path)) { |
2572 | scenes_to_save.push_back(scene_path); |
2573 | } |
2574 | } |
2575 | } |
2576 | |
2577 | if (!scenes_to_save.is_empty()) { |
2578 | EditorNode::get_singleton()->save_scene_list(scenes_to_save); |
2579 | } |
2580 | |
2581 | _update_script_names(); |
2582 | } |
2583 | |
2584 | void ScriptEditor::apply_scripts() const { |
2585 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2586 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2587 | if (!se) { |
2588 | continue; |
2589 | } |
2590 | se->apply_code(); |
2591 | } |
2592 | } |
2593 | |
2594 | void ScriptEditor::reload_scripts(bool p_refresh_only) { |
2595 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2596 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2597 | if (!se) { |
2598 | continue; |
2599 | } |
2600 | |
2601 | Ref<Resource> edited_res = se->get_edited_resource(); |
2602 | |
2603 | if (edited_res->is_built_in()) { |
2604 | continue; //internal script, who cares |
2605 | } |
2606 | |
2607 | if (!p_refresh_only) { |
2608 | uint64_t last_date = edited_res->get_last_modified_time(); |
2609 | uint64_t date = FileAccess::get_modified_time(edited_res->get_path()); |
2610 | |
2611 | if (last_date == date) { |
2612 | continue; |
2613 | } |
2614 | |
2615 | Ref<Script> scr = edited_res; |
2616 | if (scr.is_valid()) { |
2617 | Ref<Script> rel_scr = ResourceLoader::load(scr->get_path(), scr->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); |
2618 | ERR_CONTINUE(!rel_scr.is_valid()); |
2619 | scr->set_source_code(rel_scr->get_source_code()); |
2620 | scr->set_last_modified_time(rel_scr->get_last_modified_time()); |
2621 | scr->reload(true); |
2622 | } |
2623 | |
2624 | Ref<JSON> json = edited_res; |
2625 | if (json != nullptr) { |
2626 | Ref<JSON> rel_json = ResourceLoader::load(json->get_path(), json->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); |
2627 | ERR_CONTINUE(!rel_json.is_valid()); |
2628 | json->parse(rel_json->get_parsed_text(), true); |
2629 | json->set_last_modified_time(rel_json->get_last_modified_time()); |
2630 | } |
2631 | |
2632 | Ref<TextFile> text_file = edited_res; |
2633 | if (text_file.is_valid()) { |
2634 | text_file->reload_from_file(); |
2635 | } |
2636 | } |
2637 | |
2638 | se->reload_text(); |
2639 | } |
2640 | |
2641 | disk_changed->hide(); |
2642 | _update_script_names(); |
2643 | } |
2644 | |
2645 | void ScriptEditor::open_script_create_dialog(const String &p_base_name, const String &p_base_path) { |
2646 | _menu_option(FILE_NEW); |
2647 | script_create_dialog->config(p_base_name, p_base_path); |
2648 | } |
2649 | |
2650 | void ScriptEditor::open_text_file_create_dialog(const String &p_base_path, const String &p_base_name) { |
2651 | _menu_option(FILE_NEW_TEXTFILE); |
2652 | file_dialog->set_current_dir(p_base_path); |
2653 | file_dialog->set_current_file(p_base_name); |
2654 | open_textfile_after_create = false; |
2655 | } |
2656 | |
2657 | Ref<Resource> ScriptEditor::open_file(const String &p_file) { |
2658 | List<String> extensions; |
2659 | ResourceLoader::get_recognized_extensions_for_type("Script" , &extensions); |
2660 | ResourceLoader::get_recognized_extensions_for_type("JSON" , &extensions); |
2661 | if (extensions.find(p_file.get_extension())) { |
2662 | Ref<Resource> scr = ResourceLoader::load(p_file); |
2663 | if (!scr.is_valid()) { |
2664 | EditorNode::get_singleton()->show_warning(TTR("Could not load file at:" ) + "\n\n" + p_file, TTR("Error!" )); |
2665 | return Ref<Resource>(); |
2666 | } |
2667 | |
2668 | edit(scr); |
2669 | return scr; |
2670 | } |
2671 | |
2672 | Error error; |
2673 | Ref<TextFile> text_file = _load_text_file(p_file, &error); |
2674 | if (error != OK) { |
2675 | EditorNode::get_singleton()->show_warning(TTR("Could not load file at:" ) + "\n\n" + p_file, TTR("Error!" )); |
2676 | return Ref<Resource>(); |
2677 | } |
2678 | |
2679 | if (text_file.is_valid()) { |
2680 | edit(text_file); |
2681 | return text_file; |
2682 | } |
2683 | return Ref<Resource>(); |
2684 | } |
2685 | |
2686 | void ScriptEditor::_editor_stop() { |
2687 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2688 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2689 | if (!se) { |
2690 | continue; |
2691 | } |
2692 | |
2693 | se->set_debugger_active(false); |
2694 | } |
2695 | } |
2696 | |
2697 | void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args) { |
2698 | ERR_FAIL_NULL(p_obj); |
2699 | Ref<Script> scr = p_obj->get_script(); |
2700 | ERR_FAIL_COND(!scr.is_valid()); |
2701 | |
2702 | EditorNode::get_singleton()->push_item(scr.ptr()); |
2703 | |
2704 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2705 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2706 | if (!se) { |
2707 | continue; |
2708 | } |
2709 | if (se->get_edited_resource() != scr) { |
2710 | continue; |
2711 | } |
2712 | |
2713 | se->add_callback(p_function, p_args); |
2714 | |
2715 | _go_to_tab(i); |
2716 | |
2717 | script_list->select(script_list->find_metadata(i)); |
2718 | |
2719 | // Save the current script so the changes can be picked up by an external editor. |
2720 | if (!scr.ptr()->is_built_in()) { // But only if it's not built-in script. |
2721 | save_current_script(); |
2722 | } |
2723 | |
2724 | break; |
2725 | } |
2726 | } |
2727 | |
2728 | void ScriptEditor::_save_editor_state(ScriptEditorBase *p_editor) { |
2729 | if (restoring_layout) { |
2730 | return; |
2731 | } |
2732 | |
2733 | const String &path = p_editor->get_edited_resource()->get_path(); |
2734 | if (!path.is_resource_file()) { |
2735 | return; |
2736 | } |
2737 | |
2738 | script_editor_cache->set_value(path, "state" , p_editor->get_edit_state()); |
2739 | // This is saved later when we save the editor layout. |
2740 | } |
2741 | |
2742 | void ScriptEditor::_save_layout() { |
2743 | if (restoring_layout) { |
2744 | return; |
2745 | } |
2746 | |
2747 | EditorNode::get_singleton()->save_editor_layout_delayed(); |
2748 | } |
2749 | |
2750 | void ScriptEditor::_editor_settings_changed() { |
2751 | textfile_extensions.clear(); |
2752 | const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions" ))).split("," , false); |
2753 | for (const String &E : textfile_ext) { |
2754 | textfile_extensions.insert(E); |
2755 | } |
2756 | |
2757 | trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save" ); |
2758 | convert_indent_on_save = EDITOR_GET("text_editor/behavior/files/convert_indent_on_save" ); |
2759 | |
2760 | members_overview_enabled = EDITOR_GET("text_editor/script_list/show_members_overview" ); |
2761 | help_overview_enabled = EDITOR_GET("text_editor/help/show_help_index" ); |
2762 | _update_members_overview_visibility(); |
2763 | _update_help_overview_visibility(); |
2764 | |
2765 | _update_autosave_timer(); |
2766 | |
2767 | if (current_theme.is_empty()) { |
2768 | current_theme = EDITOR_GET("text_editor/theme/color_theme" ); |
2769 | } else if (current_theme != String(EDITOR_GET("text_editor/theme/color_theme" ))) { |
2770 | current_theme = EDITOR_GET("text_editor/theme/color_theme" ); |
2771 | EditorSettings::get_singleton()->load_text_editor_theme(); |
2772 | } |
2773 | |
2774 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2775 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2776 | if (!se) { |
2777 | continue; |
2778 | } |
2779 | |
2780 | se->update_settings(); |
2781 | } |
2782 | _update_script_colors(); |
2783 | _update_script_names(); |
2784 | |
2785 | ScriptServer::set_reload_scripts_on_save(EDITOR_GET("text_editor/behavior/files/auto_reload_and_parse_scripts_on_save" )); |
2786 | } |
2787 | |
2788 | void ScriptEditor::_filesystem_changed() { |
2789 | _update_script_names(); |
2790 | } |
2791 | |
2792 | void ScriptEditor::_files_moved(const String &p_old_file, const String &p_new_file) { |
2793 | if (!script_editor_cache->has_section(p_old_file)) { |
2794 | return; |
2795 | } |
2796 | Variant state = script_editor_cache->get_value(p_old_file, "state" ); |
2797 | script_editor_cache->erase_section(p_old_file); |
2798 | script_editor_cache->set_value(p_new_file, "state" , state); |
2799 | |
2800 | // If Script, update breakpoints with debugger. |
2801 | Array breakpoints = _get_cached_breakpoints_for_script(p_new_file); |
2802 | for (int i = 0; i < breakpoints.size(); i++) { |
2803 | int line = (int)breakpoints[i] + 1; |
2804 | EditorDebuggerNode::get_singleton()->set_breakpoint(p_old_file, line, false); |
2805 | if (!p_new_file.begins_with("local://" ) && ResourceLoader::exists(p_new_file, "Script" )) { |
2806 | EditorDebuggerNode::get_singleton()->set_breakpoint(p_new_file, line, true); |
2807 | } |
2808 | } |
2809 | // This is saved later when we save the editor layout. |
2810 | } |
2811 | |
2812 | void ScriptEditor::_file_removed(const String &p_removed_file) { |
2813 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
2814 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
2815 | if (!se) { |
2816 | continue; |
2817 | } |
2818 | if (se->get_meta("_edit_res_path" ) == p_removed_file) { |
2819 | // The script is deleted with no undo, so just close the tab. |
2820 | _close_tab(i, false, false); |
2821 | } |
2822 | } |
2823 | |
2824 | // Check closed. |
2825 | if (script_editor_cache->has_section(p_removed_file)) { |
2826 | Array breakpoints = _get_cached_breakpoints_for_script(p_removed_file); |
2827 | for (int i = 0; i < breakpoints.size(); i++) { |
2828 | EditorDebuggerNode::get_singleton()->set_breakpoint(p_removed_file, (int)breakpoints[i] + 1, false); |
2829 | } |
2830 | script_editor_cache->erase_section(p_removed_file); |
2831 | } |
2832 | } |
2833 | |
2834 | void ScriptEditor::_update_find_replace_bar() { |
2835 | ScriptEditorBase *se = _get_current_editor(); |
2836 | if (se) { |
2837 | se->set_find_replace_bar(find_replace_bar); |
2838 | } else { |
2839 | find_replace_bar->set_text_edit(nullptr); |
2840 | find_replace_bar->hide(); |
2841 | } |
2842 | } |
2843 | |
2844 | void ScriptEditor::_autosave_scripts() { |
2845 | save_all_scripts(); |
2846 | } |
2847 | |
2848 | void ScriptEditor::_update_autosave_timer() { |
2849 | if (!autosave_timer->is_inside_tree()) { |
2850 | return; |
2851 | } |
2852 | |
2853 | float autosave_time = EDITOR_GET("text_editor/behavior/files/autosave_interval_secs" ); |
2854 | if (autosave_time > 0) { |
2855 | autosave_timer->set_wait_time(autosave_time); |
2856 | autosave_timer->start(); |
2857 | } else { |
2858 | autosave_timer->stop(); |
2859 | } |
2860 | } |
2861 | |
2862 | void ScriptEditor::_tree_changed() { |
2863 | if (waiting_update_names) { |
2864 | return; |
2865 | } |
2866 | |
2867 | waiting_update_names = true; |
2868 | call_deferred(SNAME("_update_script_names" )); |
2869 | } |
2870 | |
2871 | void ScriptEditor::_split_dragged(float) { |
2872 | _save_layout(); |
2873 | } |
2874 | |
2875 | Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) { |
2876 | if (tab_container->get_tab_count() == 0) { |
2877 | return Variant(); |
2878 | } |
2879 | |
2880 | Node *cur_node = tab_container->get_tab_control(tab_container->get_current_tab()); |
2881 | |
2882 | HBoxContainer *drag_preview = memnew(HBoxContainer); |
2883 | String preview_name = "" ; |
2884 | Ref<Texture2D> preview_icon; |
2885 | |
2886 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(cur_node); |
2887 | if (se) { |
2888 | preview_name = se->get_name(); |
2889 | preview_icon = se->get_theme_icon(); |
2890 | } |
2891 | EditorHelp *eh = Object::cast_to<EditorHelp>(cur_node); |
2892 | if (eh) { |
2893 | preview_name = eh->get_class(); |
2894 | preview_icon = get_editor_theme_icon(SNAME("Help" )); |
2895 | } |
2896 | |
2897 | if (!preview_icon.is_null()) { |
2898 | TextureRect *tf = memnew(TextureRect); |
2899 | tf->set_texture(preview_icon); |
2900 | tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED); |
2901 | drag_preview->add_child(tf); |
2902 | } |
2903 | Label *label = memnew(Label(preview_name)); |
2904 | drag_preview->add_child(label); |
2905 | set_drag_preview(drag_preview); |
2906 | |
2907 | Dictionary drag_data; |
2908 | drag_data["type" ] = "script_list_element" ; // using a custom type because node caused problems when dragging to scene tree |
2909 | drag_data["script_list_element" ] = cur_node; |
2910 | |
2911 | return drag_data; |
2912 | } |
2913 | |
2914 | bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { |
2915 | Dictionary d = p_data; |
2916 | if (!d.has("type" )) { |
2917 | return false; |
2918 | } |
2919 | |
2920 | if (String(d["type" ]) == "script_list_element" ) { |
2921 | Node *node = Object::cast_to<Node>(d["script_list_element" ]); |
2922 | |
2923 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node); |
2924 | if (se) { |
2925 | return true; |
2926 | } |
2927 | EditorHelp *eh = Object::cast_to<EditorHelp>(node); |
2928 | if (eh) { |
2929 | return true; |
2930 | } |
2931 | } |
2932 | |
2933 | if (String(d["type" ]) == "nodes" ) { |
2934 | Array nodes = d["nodes" ]; |
2935 | if (nodes.size() == 0) { |
2936 | return false; |
2937 | } |
2938 | Node *node = get_node((nodes[0])); |
2939 | |
2940 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node); |
2941 | if (se) { |
2942 | return true; |
2943 | } |
2944 | EditorHelp *eh = Object::cast_to<EditorHelp>(node); |
2945 | if (eh) { |
2946 | return true; |
2947 | } |
2948 | } |
2949 | |
2950 | if (String(d["type" ]) == "files" ) { |
2951 | Vector<String> files = d["files" ]; |
2952 | |
2953 | if (files.size() == 0) { |
2954 | return false; //weird |
2955 | } |
2956 | |
2957 | for (int i = 0; i < files.size(); i++) { |
2958 | String file = files[i]; |
2959 | if (file.is_empty() || !FileAccess::exists(file)) { |
2960 | continue; |
2961 | } |
2962 | if (ResourceLoader::exists(file, "Script" ) || ResourceLoader::exists(file, "JSON" )) { |
2963 | Ref<Resource> scr = ResourceLoader::load(file); |
2964 | if (scr.is_valid()) { |
2965 | return true; |
2966 | } |
2967 | } |
2968 | |
2969 | if (textfile_extensions.has(file.get_extension())) { |
2970 | Error err; |
2971 | Ref<TextFile> text_file = _load_text_file(file, &err); |
2972 | if (text_file.is_valid() && err == OK) { |
2973 | return true; |
2974 | } |
2975 | } |
2976 | } |
2977 | return false; |
2978 | } |
2979 | |
2980 | return false; |
2981 | } |
2982 | |
2983 | void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
2984 | if (!can_drop_data_fw(p_point, p_data, p_from)) { |
2985 | return; |
2986 | } |
2987 | |
2988 | Dictionary d = p_data; |
2989 | if (!d.has("type" )) { |
2990 | return; |
2991 | } |
2992 | |
2993 | if (String(d["type" ]) == "script_list_element" ) { |
2994 | Node *node = Object::cast_to<Node>(d["script_list_element" ]); |
2995 | |
2996 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node); |
2997 | EditorHelp *eh = Object::cast_to<EditorHelp>(node); |
2998 | if (se || eh) { |
2999 | int new_index = 0; |
3000 | if (script_list->get_item_count() > 0) { |
3001 | new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point)); |
3002 | } |
3003 | tab_container->move_child(node, new_index); |
3004 | tab_container->set_current_tab(new_index); |
3005 | _update_script_names(); |
3006 | } |
3007 | } |
3008 | |
3009 | if (String(d["type" ]) == "nodes" ) { |
3010 | Array nodes = d["nodes" ]; |
3011 | if (nodes.size() == 0) { |
3012 | return; |
3013 | } |
3014 | Node *node = get_node(nodes[0]); |
3015 | |
3016 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node); |
3017 | EditorHelp *eh = Object::cast_to<EditorHelp>(node); |
3018 | if (se || eh) { |
3019 | int new_index = 0; |
3020 | if (script_list->get_item_count() > 0) { |
3021 | new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point)); |
3022 | } |
3023 | tab_container->move_child(node, new_index); |
3024 | tab_container->set_current_tab(new_index); |
3025 | _update_script_names(); |
3026 | } |
3027 | } |
3028 | |
3029 | if (String(d["type" ]) == "files" ) { |
3030 | Vector<String> files = d["files" ]; |
3031 | |
3032 | int new_index = 0; |
3033 | if (script_list->get_item_count() > 0) { |
3034 | new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point)); |
3035 | } |
3036 | int num_tabs_before = tab_container->get_tab_count(); |
3037 | for (int i = 0; i < files.size(); i++) { |
3038 | String file = files[i]; |
3039 | if (file.is_empty() || !FileAccess::exists(file)) { |
3040 | continue; |
3041 | } |
3042 | |
3043 | if (!ResourceLoader::exists(file, "Script" ) && !ResourceLoader::exists(file, "JSON" ) && !textfile_extensions.has(file.get_extension())) { |
3044 | continue; |
3045 | } |
3046 | |
3047 | Ref<Resource> res = open_file(file); |
3048 | if (res.is_valid()) { |
3049 | const int num_tabs = tab_container->get_tab_count(); |
3050 | if (num_tabs > num_tabs_before) { |
3051 | tab_container->move_child(tab_container->get_tab_control(tab_container->get_tab_count() - 1), new_index); |
3052 | num_tabs_before = num_tabs; |
3053 | } else if (num_tabs > 0) { /* Maybe script was already open */ |
3054 | tab_container->move_child(tab_container->get_tab_control(tab_container->get_current_tab()), new_index); |
3055 | } |
3056 | } |
3057 | } |
3058 | if (tab_container->get_tab_count() > 0) { |
3059 | tab_container->set_current_tab(new_index); |
3060 | } |
3061 | _update_script_names(); |
3062 | } |
3063 | } |
3064 | |
3065 | void ScriptEditor::input(const Ref<InputEvent> &p_event) { |
3066 | // This is implemented in `input()` rather than `unhandled_input()` to allow |
3067 | // the shortcut to be used regardless of the click location. |
3068 | // This feature can be disabled to avoid interfering with other uses of the additional |
3069 | // mouse buttons, such as push-to-talk in a VoIP program. |
3070 | if (EDITOR_GET("interface/editor/mouse_extra_buttons_navigate_history" )) { |
3071 | const Ref<InputEventMouseButton> mb = p_event; |
3072 | |
3073 | // Navigate the script history using additional mouse buttons present on some mice. |
3074 | // This must be hardcoded as the editor shortcuts dialog doesn't allow assigning |
3075 | // more than one shortcut per action. |
3076 | if (mb.is_valid() && mb->is_pressed() && is_visible_in_tree()) { |
3077 | if (mb->get_button_index() == MouseButton::MB_XBUTTON1) { |
3078 | _history_back(); |
3079 | } |
3080 | |
3081 | if (mb->get_button_index() == MouseButton::MB_XBUTTON2) { |
3082 | _history_forward(); |
3083 | } |
3084 | } |
3085 | } |
3086 | } |
3087 | |
3088 | void ScriptEditor::shortcut_input(const Ref<InputEvent> &p_event) { |
3089 | ERR_FAIL_COND(p_event.is_null()); |
3090 | |
3091 | if (!is_visible_in_tree() || !p_event->is_pressed() || p_event->is_echo()) { |
3092 | return; |
3093 | } |
3094 | if (ED_IS_SHORTCUT("script_editor/next_script" , p_event)) { |
3095 | if (script_list->get_item_count() > 1) { |
3096 | int next_tab = script_list->get_current() + 1; |
3097 | next_tab %= script_list->get_item_count(); |
3098 | _go_to_tab(script_list->get_item_metadata(next_tab)); |
3099 | _update_script_names(); |
3100 | } |
3101 | accept_event(); |
3102 | } |
3103 | if (ED_IS_SHORTCUT("script_editor/prev_script" , p_event)) { |
3104 | if (script_list->get_item_count() > 1) { |
3105 | int next_tab = script_list->get_current() - 1; |
3106 | next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1; |
3107 | _go_to_tab(script_list->get_item_metadata(next_tab)); |
3108 | _update_script_names(); |
3109 | } |
3110 | accept_event(); |
3111 | } |
3112 | if (ED_IS_SHORTCUT("script_editor/window_move_up" , p_event)) { |
3113 | _menu_option(WINDOW_MOVE_UP); |
3114 | accept_event(); |
3115 | } |
3116 | if (ED_IS_SHORTCUT("script_editor/window_move_down" , p_event)) { |
3117 | _menu_option(WINDOW_MOVE_DOWN); |
3118 | accept_event(); |
3119 | } |
3120 | } |
3121 | |
3122 | void ScriptEditor::_script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) { |
3123 | if (p_mouse_button_index == MouseButton::MIDDLE) { |
3124 | script_list->select(p_item); |
3125 | _script_selected(p_item); |
3126 | _menu_option(FILE_CLOSE); |
3127 | } |
3128 | |
3129 | if (p_mouse_button_index == MouseButton::RIGHT) { |
3130 | _make_script_list_context_menu(); |
3131 | } |
3132 | } |
3133 | |
3134 | void ScriptEditor::() { |
3135 | context_menu->clear(); |
3136 | |
3137 | int selected = tab_container->get_current_tab(); |
3138 | if (selected < 0 || selected >= tab_container->get_tab_count()) { |
3139 | return; |
3140 | } |
3141 | |
3142 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(selected)); |
3143 | if (se) { |
3144 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save" ), FILE_SAVE); |
3145 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as" ), FILE_SAVE_AS); |
3146 | } |
3147 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file" ), FILE_CLOSE); |
3148 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all" ), CLOSE_ALL); |
3149 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs" ), CLOSE_OTHER_TABS); |
3150 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_docs" ), CLOSE_DOCS); |
3151 | context_menu->add_separator(); |
3152 | if (se) { |
3153 | Ref<Script> scr = se->get_edited_resource(); |
3154 | if (scr != nullptr) { |
3155 | if (!scr.is_null() && scr->is_tool()) { |
3156 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft" ), FILE_TOOL_RELOAD_SOFT); |
3157 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file" ), FILE_RUN); |
3158 | context_menu->add_separator(); |
3159 | } |
3160 | } |
3161 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path" ), FILE_COPY_PATH); |
3162 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system" ), SHOW_IN_FILE_SYSTEM); |
3163 | context_menu->add_separator(); |
3164 | } |
3165 | |
3166 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up" ), WINDOW_MOVE_UP); |
3167 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down" ), WINDOW_MOVE_DOWN); |
3168 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort" ), WINDOW_SORT); |
3169 | context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_scripts_panel" ), TOGGLE_SCRIPTS_PANEL); |
3170 | |
3171 | context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_ALL), tab_container->get_tab_count() <= 0); |
3172 | context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1); |
3173 | context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_DOCS), !_has_docs_tab()); |
3174 | context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_MOVE_UP), tab_container->get_current_tab() <= 0); |
3175 | context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_MOVE_DOWN), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1); |
3176 | context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_SORT), tab_container->get_tab_count() <= 1); |
3177 | |
3178 | context_menu->set_position(get_screen_position() + get_local_mouse_position()); |
3179 | context_menu->reset_size(); |
3180 | context_menu->popup(); |
3181 | } |
3182 | |
3183 | void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) { |
3184 | if (!bool(EDITOR_GET("text_editor/behavior/files/restore_scripts_on_load" ))) { |
3185 | return; |
3186 | } |
3187 | |
3188 | if (!p_layout->has_section_key("ScriptEditor" , "open_scripts" ) && !p_layout->has_section_key("ScriptEditor" , "open_help" )) { |
3189 | return; |
3190 | } |
3191 | |
3192 | Array scripts = p_layout->get_value("ScriptEditor" , "open_scripts" ); |
3193 | Array helps; |
3194 | if (p_layout->has_section_key("ScriptEditor" , "open_help" )) { |
3195 | helps = p_layout->get_value("ScriptEditor" , "open_help" ); |
3196 | } |
3197 | |
3198 | restoring_layout = true; |
3199 | |
3200 | HashSet<String> loaded_scripts; |
3201 | List<String> extensions; |
3202 | ResourceLoader::get_recognized_extensions_for_type("Script" , &extensions); |
3203 | ResourceLoader::get_recognized_extensions_for_type("JSON" , &extensions); |
3204 | |
3205 | for (int i = 0; i < scripts.size(); i++) { |
3206 | String path = scripts[i]; |
3207 | |
3208 | Dictionary script_info = scripts[i]; |
3209 | if (!script_info.is_empty()) { |
3210 | path = script_info["path" ]; |
3211 | } |
3212 | |
3213 | if (!FileAccess::exists(path)) { |
3214 | if (script_editor_cache->has_section(path)) { |
3215 | script_editor_cache->erase_section(path); |
3216 | } |
3217 | continue; |
3218 | } |
3219 | loaded_scripts.insert(path); |
3220 | |
3221 | if (extensions.find(path.get_extension())) { |
3222 | Ref<Resource> scr = ResourceLoader::load(path); |
3223 | if (!scr.is_valid()) { |
3224 | continue; |
3225 | } |
3226 | if (!edit(scr, false)) { |
3227 | continue; |
3228 | } |
3229 | } else { |
3230 | Error error; |
3231 | Ref<TextFile> text_file = _load_text_file(path, &error); |
3232 | if (error != OK || !text_file.is_valid()) { |
3233 | continue; |
3234 | } |
3235 | if (!edit(text_file, false)) { |
3236 | continue; |
3237 | } |
3238 | } |
3239 | |
3240 | if (!script_info.is_empty()) { |
3241 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(tab_container->get_tab_count() - 1)); |
3242 | if (se) { |
3243 | se->set_edit_state(script_info["state" ]); |
3244 | } |
3245 | } |
3246 | } |
3247 | |
3248 | for (int i = 0; i < helps.size(); i++) { |
3249 | String path = helps[i]; |
3250 | if (path.is_empty()) { // invalid, skip |
3251 | continue; |
3252 | } |
3253 | _help_class_open(path); |
3254 | } |
3255 | |
3256 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3257 | tab_container->get_tab_control(i)->set_meta("__editor_pass" , Variant()); |
3258 | } |
3259 | |
3260 | if (p_layout->has_section_key("ScriptEditor" , "script_split_offset" )) { |
3261 | script_split->set_split_offset(p_layout->get_value("ScriptEditor" , "script_split_offset" )); |
3262 | } |
3263 | |
3264 | if (p_layout->has_section_key("ScriptEditor" , "list_split_offset" )) { |
3265 | list_split->set_split_offset(p_layout->get_value("ScriptEditor" , "list_split_offset" )); |
3266 | } |
3267 | |
3268 | // Remove any deleted editors that have been removed between launches. |
3269 | // and if a Script, register breakpoints with the debugger. |
3270 | List<String> cached_editors; |
3271 | script_editor_cache->get_sections(&cached_editors); |
3272 | for (const String &E : cached_editors) { |
3273 | if (loaded_scripts.has(E)) { |
3274 | continue; |
3275 | } |
3276 | |
3277 | if (!FileAccess::exists(E)) { |
3278 | script_editor_cache->erase_section(E); |
3279 | continue; |
3280 | } |
3281 | |
3282 | Array breakpoints = _get_cached_breakpoints_for_script(E); |
3283 | for (int i = 0; i < breakpoints.size(); i++) { |
3284 | EditorDebuggerNode::get_singleton()->set_breakpoint(E, (int)breakpoints[i] + 1, true); |
3285 | } |
3286 | } |
3287 | |
3288 | restoring_layout = false; |
3289 | |
3290 | _update_script_names(); |
3291 | |
3292 | if (p_layout->has_section_key("ScriptEditor" , "selected_script" )) { |
3293 | String selected_script = p_layout->get_value("ScriptEditor" , "selected_script" ); |
3294 | // If the selected script is not in the list of open scripts, select nothing. |
3295 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3296 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
3297 | if (se && se->get_edited_resource()->get_path() == selected_script) { |
3298 | _go_to_tab(i); |
3299 | break; |
3300 | } |
3301 | } |
3302 | } |
3303 | } |
3304 | |
3305 | void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) { |
3306 | Array scripts; |
3307 | Array helps; |
3308 | String selected_script; |
3309 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3310 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
3311 | if (se) { |
3312 | String path = se->get_edited_resource()->get_path(); |
3313 | if (!path.is_resource_file()) { |
3314 | continue; |
3315 | } |
3316 | |
3317 | if (tab_container->get_current_tab_control() == tab_container->get_tab_control(i)) { |
3318 | selected_script = path; |
3319 | } |
3320 | |
3321 | _save_editor_state(se); |
3322 | scripts.push_back(path); |
3323 | } |
3324 | |
3325 | EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i)); |
3326 | |
3327 | if (eh) { |
3328 | helps.push_back(eh->get_class()); |
3329 | } |
3330 | } |
3331 | |
3332 | p_layout->set_value("ScriptEditor" , "open_scripts" , scripts); |
3333 | p_layout->set_value("ScriptEditor" , "selected_script" , selected_script); |
3334 | p_layout->set_value("ScriptEditor" , "open_help" , helps); |
3335 | p_layout->set_value("ScriptEditor" , "script_split_offset" , script_split->get_split_offset()); |
3336 | p_layout->set_value("ScriptEditor" , "list_split_offset" , list_split->get_split_offset()); |
3337 | |
3338 | // Save the cache. |
3339 | script_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg" )); |
3340 | } |
3341 | |
3342 | void ScriptEditor::_help_class_open(const String &p_class) { |
3343 | if (p_class.is_empty()) { |
3344 | return; |
3345 | } |
3346 | |
3347 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3348 | EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i)); |
3349 | |
3350 | if (eh && eh->get_class() == p_class) { |
3351 | _go_to_tab(i); |
3352 | _update_script_names(); |
3353 | return; |
3354 | } |
3355 | } |
3356 | |
3357 | EditorHelp *eh = memnew(EditorHelp); |
3358 | |
3359 | eh->set_name(p_class); |
3360 | tab_container->add_child(eh); |
3361 | _go_to_tab(tab_container->get_tab_count() - 1); |
3362 | eh->go_to_class(p_class); |
3363 | eh->connect("go_to_help" , callable_mp(this, &ScriptEditor::_help_class_goto)); |
3364 | _add_recent_script(p_class); |
3365 | _sort_list_on_update = true; |
3366 | _update_script_names(); |
3367 | _save_layout(); |
3368 | } |
3369 | |
3370 | void ScriptEditor::_help_class_goto(const String &p_desc) { |
3371 | String cname = p_desc.get_slice(":" , 1); |
3372 | |
3373 | if (_help_tab_goto(cname, p_desc)) { |
3374 | return; |
3375 | } |
3376 | |
3377 | EditorHelp *eh = memnew(EditorHelp); |
3378 | |
3379 | eh->set_name(cname); |
3380 | tab_container->add_child(eh); |
3381 | _go_to_tab(tab_container->get_tab_count() - 1); |
3382 | eh->go_to_help(p_desc); |
3383 | eh->connect("go_to_help" , callable_mp(this, &ScriptEditor::_help_class_goto)); |
3384 | _add_recent_script(eh->get_class()); |
3385 | _sort_list_on_update = true; |
3386 | _update_script_names(); |
3387 | _save_layout(); |
3388 | |
3389 | call_deferred("_help_tab_goto" , cname, p_desc); |
3390 | } |
3391 | |
3392 | bool ScriptEditor::_help_tab_goto(const String &p_name, const String &p_desc) { |
3393 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3394 | EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i)); |
3395 | |
3396 | if (eh && eh->get_class() == p_name) { |
3397 | _go_to_tab(i); |
3398 | eh->go_to_help(p_desc); |
3399 | _update_script_names(); |
3400 | return true; |
3401 | } |
3402 | } |
3403 | return false; |
3404 | } |
3405 | |
3406 | void ScriptEditor::update_doc(const String &p_name) { |
3407 | ERR_FAIL_COND(!EditorHelp::get_doc_data()->has_doc(p_name)); |
3408 | |
3409 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3410 | EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i)); |
3411 | if (eh && eh->get_class() == p_name) { |
3412 | eh->update_doc(); |
3413 | return; |
3414 | } |
3415 | } |
3416 | } |
3417 | |
3418 | void ScriptEditor::clear_docs_from_script(const Ref<Script> &p_script) { |
3419 | ERR_FAIL_COND(p_script.is_null()); |
3420 | |
3421 | Vector<DocData::ClassDoc> documentations = p_script->get_documentation(); |
3422 | for (int j = 0; j < documentations.size(); j++) { |
3423 | const DocData::ClassDoc &doc = documentations.get(j); |
3424 | if (EditorHelp::get_doc_data()->has_doc(doc.name)) { |
3425 | EditorHelp::get_doc_data()->remove_doc(doc.name); |
3426 | } |
3427 | } |
3428 | } |
3429 | |
3430 | void ScriptEditor::update_docs_from_script(const Ref<Script> &p_script) { |
3431 | ERR_FAIL_COND(p_script.is_null()); |
3432 | |
3433 | Vector<DocData::ClassDoc> documentations = p_script->get_documentation(); |
3434 | for (int j = 0; j < documentations.size(); j++) { |
3435 | const DocData::ClassDoc &doc = documentations.get(j); |
3436 | EditorHelp::get_doc_data()->add_doc(doc); |
3437 | update_doc(doc.name); |
3438 | } |
3439 | } |
3440 | |
3441 | void ScriptEditor::() { |
3442 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3443 | bool current = tab_container->get_current_tab() == i; |
3444 | |
3445 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
3446 | if (se && se->get_edit_menu()) { |
3447 | if (current) { |
3448 | se->get_edit_menu()->show(); |
3449 | } else { |
3450 | se->get_edit_menu()->hide(); |
3451 | } |
3452 | } |
3453 | } |
3454 | |
3455 | EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control()); |
3456 | script_search_menu->get_popup()->clear(); |
3457 | if (eh) { |
3458 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find" , TTR("Find..." ), KeyModifierMask::CMD_OR_CTRL | Key::F), HELP_SEARCH_FIND); |
3459 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next" , TTR("Find Next" ), Key::F3), HELP_SEARCH_FIND_NEXT); |
3460 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous" , TTR("Find Previous" ), KeyModifierMask::SHIFT | Key::F3), HELP_SEARCH_FIND_PREVIOUS); |
3461 | script_search_menu->get_popup()->add_separator(); |
3462 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_in_files" , TTR("Find in Files" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F), SEARCH_IN_FILES); |
3463 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace_in_files" , TTR("Replace in Files" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R), REPLACE_IN_FILES); |
3464 | script_search_menu->show(); |
3465 | } else { |
3466 | if (tab_container->get_tab_count() == 0) { |
3467 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_in_files" , TTR("Find in Files" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F), SEARCH_IN_FILES); |
3468 | script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace_in_files" , TTR("Replace in Files" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R), REPLACE_IN_FILES); |
3469 | script_search_menu->show(); |
3470 | } else { |
3471 | script_search_menu->hide(); |
3472 | } |
3473 | } |
3474 | } |
3475 | |
3476 | void ScriptEditor::_update_history_pos(int p_new_pos) { |
3477 | Node *n = tab_container->get_current_tab_control(); |
3478 | |
3479 | if (Object::cast_to<ScriptEditorBase>(n)) { |
3480 | history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_navigation_state(); |
3481 | } |
3482 | if (Object::cast_to<EditorHelp>(n)) { |
3483 | history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll(); |
3484 | } |
3485 | |
3486 | history_pos = p_new_pos; |
3487 | tab_container->set_current_tab(tab_container->get_tab_idx_from_control(history[history_pos].control)); |
3488 | |
3489 | n = history[history_pos].control; |
3490 | |
3491 | ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(n); |
3492 | if (seb) { |
3493 | seb->set_edit_state(history[history_pos].state); |
3494 | seb->ensure_focus(); |
3495 | |
3496 | Ref<Script> scr = seb->get_edited_resource(); |
3497 | if (scr != nullptr) { |
3498 | notify_script_changed(scr); |
3499 | } |
3500 | } |
3501 | |
3502 | if (Object::cast_to<EditorHelp>(n)) { |
3503 | Object::cast_to<EditorHelp>(n)->set_scroll(history[history_pos].state); |
3504 | Object::cast_to<EditorHelp>(n)->set_focused(); |
3505 | } |
3506 | |
3507 | n->set_meta("__editor_pass" , ++edit_pass); |
3508 | _update_script_names(); |
3509 | _update_history_arrows(); |
3510 | _update_selected_editor_menu(); |
3511 | } |
3512 | |
3513 | void ScriptEditor::_history_forward() { |
3514 | if (history_pos < history.size() - 1) { |
3515 | _update_history_pos(history_pos + 1); |
3516 | } |
3517 | } |
3518 | |
3519 | void ScriptEditor::_history_back() { |
3520 | if (history_pos > 0) { |
3521 | _update_history_pos(history_pos - 1); |
3522 | } |
3523 | } |
3524 | |
3525 | Vector<Ref<Script>> ScriptEditor::get_open_scripts() const { |
3526 | Vector<Ref<Script>> out_scripts = Vector<Ref<Script>>(); |
3527 | |
3528 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3529 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
3530 | if (!se) { |
3531 | continue; |
3532 | } |
3533 | |
3534 | Ref<Script> scr = se->get_edited_resource(); |
3535 | if (scr != nullptr) { |
3536 | out_scripts.push_back(scr); |
3537 | } |
3538 | } |
3539 | |
3540 | return out_scripts; |
3541 | } |
3542 | |
3543 | TypedArray<ScriptEditorBase> ScriptEditor::_get_open_script_editors() const { |
3544 | TypedArray<ScriptEditorBase> script_editors; |
3545 | for (int i = 0; i < tab_container->get_tab_count(); i++) { |
3546 | ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i)); |
3547 | if (!se) { |
3548 | continue; |
3549 | } |
3550 | script_editors.push_back(se); |
3551 | } |
3552 | return script_editors; |
3553 | } |
3554 | |
3555 | void ScriptEditor::set_scene_root_script(Ref<Script> p_script) { |
3556 | // Don't open dominant script if using an external editor. |
3557 | bool use_external_editor = |
3558 | EDITOR_GET("text_editor/external/use_external_editor" ) || |
3559 | (p_script.is_valid() && p_script->get_language()->overrides_external_editor()); |
3560 | use_external_editor = use_external_editor && !(p_script.is_valid() && p_script->is_built_in()); // Ignore external editor for built-in scripts. |
3561 | const bool open_dominant = EDITOR_GET("text_editor/behavior/files/open_dominant_script_on_scene_change" ); |
3562 | |
3563 | if (open_dominant && !use_external_editor && p_script.is_valid()) { |
3564 | edit(p_script); |
3565 | } |
3566 | } |
3567 | |
3568 | bool ScriptEditor::script_goto_method(Ref<Script> p_script, const String &p_method) { |
3569 | int line = p_script->get_member_line(p_method); |
3570 | |
3571 | if (line == -1) { |
3572 | return false; |
3573 | } |
3574 | |
3575 | return edit(p_script, line, 0); |
3576 | } |
3577 | |
3578 | void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) { |
3579 | auto_reload_running_scripts = p_enabled; |
3580 | } |
3581 | |
3582 | void ScriptEditor::_help_search(String p_text) { |
3583 | help_search_dialog->popup_dialog(p_text); |
3584 | } |
3585 | |
3586 | void ScriptEditor::_open_script_request(const String &p_path) { |
3587 | Ref<Script> scr = ResourceLoader::load(p_path); |
3588 | if (scr.is_valid()) { |
3589 | script_editor->edit(scr, false); |
3590 | return; |
3591 | } |
3592 | |
3593 | Ref<JSON> json = ResourceLoader::load(p_path); |
3594 | if (json.is_valid()) { |
3595 | script_editor->edit(json, false); |
3596 | return; |
3597 | } |
3598 | |
3599 | Error err; |
3600 | Ref<TextFile> text_file = script_editor->_load_text_file(p_path, &err); |
3601 | if (text_file.is_valid()) { |
3602 | script_editor->edit(text_file, false); |
3603 | return; |
3604 | } |
3605 | } |
3606 | |
3607 | void ScriptEditor::register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) { |
3608 | ERR_FAIL_COND(p_syntax_highlighter.is_null()); |
3609 | |
3610 | if (!syntax_highlighters.has(p_syntax_highlighter)) { |
3611 | syntax_highlighters.push_back(p_syntax_highlighter); |
3612 | } |
3613 | } |
3614 | |
3615 | void ScriptEditor::unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) { |
3616 | ERR_FAIL_COND(p_syntax_highlighter.is_null()); |
3617 | |
3618 | syntax_highlighters.erase(p_syntax_highlighter); |
3619 | } |
3620 | |
3621 | int ScriptEditor::script_editor_func_count = 0; |
3622 | CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX]; |
3623 | |
3624 | void ScriptEditor::register_create_script_editor_function(CreateScriptEditorFunc p_func) { |
3625 | ERR_FAIL_COND(script_editor_func_count == SCRIPT_EDITOR_FUNC_MAX); |
3626 | script_editor_funcs[script_editor_func_count++] = p_func; |
3627 | } |
3628 | |
3629 | void ScriptEditor::_script_changed() { |
3630 | NodeDock::get_singleton()->update_lists(); |
3631 | } |
3632 | |
3633 | void ScriptEditor::_on_find_in_files_requested(String text) { |
3634 | find_in_files_dialog->set_find_in_files_mode(FindInFilesDialog::SEARCH_MODE); |
3635 | find_in_files_dialog->set_search_text(text); |
3636 | find_in_files_dialog->popup_centered(); |
3637 | } |
3638 | |
3639 | void ScriptEditor::_on_replace_in_files_requested(String text) { |
3640 | find_in_files_dialog->set_find_in_files_mode(FindInFilesDialog::REPLACE_MODE); |
3641 | find_in_files_dialog->set_search_text(text); |
3642 | find_in_files_dialog->set_replace_text("" ); |
3643 | find_in_files_dialog->popup_centered(); |
3644 | } |
3645 | |
3646 | void ScriptEditor::_on_find_in_files_result_selected(String fpath, int line_number, int begin, int end) { |
3647 | if (ResourceLoader::exists(fpath)) { |
3648 | Ref<Resource> res = ResourceLoader::load(fpath); |
3649 | |
3650 | if (fpath.get_extension() == "gdshader" ) { |
3651 | ShaderEditorPlugin *shader_editor = Object::cast_to<ShaderEditorPlugin>(EditorNode::get_editor_data().get_editor_by_name("Shader" )); |
3652 | shader_editor->edit(res.ptr()); |
3653 | shader_editor->make_visible(true); |
3654 | shader_editor->get_shader_editor(res)->goto_line_selection(line_number - 1, begin, end); |
3655 | return; |
3656 | } else if (fpath.get_extension() == "tscn" ) { |
3657 | Ref<FileAccess> f = FileAccess::open(fpath, FileAccess::READ); |
3658 | bool is_script_found = false; |
3659 | |
3660 | // Starting from top of the tscn file. |
3661 | int scr_start_line = 1; |
3662 | |
3663 | String = "[sub_resource type=\"GDScript\" id=\"" ; |
3664 | String scr_id = "" ; |
3665 | String line = "" ; |
3666 | |
3667 | int l = 0; |
3668 | |
3669 | while (!f->eof_reached()) { |
3670 | line = f->get_line(); |
3671 | l++; |
3672 | |
3673 | if (!line.begins_with(scr_header)) { |
3674 | continue; |
3675 | } |
3676 | |
3677 | // Found the end of the script. |
3678 | scr_id = line.get_slice(scr_header, 1); |
3679 | scr_id = scr_id.get_slice("\"" , 0); |
3680 | |
3681 | scr_start_line = l + 1; |
3682 | int scr_line_count = 0; |
3683 | |
3684 | do { |
3685 | line = f->get_line(); |
3686 | l++; |
3687 | String strline = line.strip_edges(); |
3688 | |
3689 | if (strline.ends_with("\"" ) && !strline.ends_with("\\\"" )) { |
3690 | // Found the end of script. |
3691 | break; |
3692 | } |
3693 | scr_line_count++; |
3694 | |
3695 | } while (!f->eof_reached()); |
3696 | |
3697 | if (line_number > scr_start_line + scr_line_count) { |
3698 | // Find in another built-in GDScript. |
3699 | continue; |
3700 | } |
3701 | |
3702 | // Real line number of the built-in script. |
3703 | line_number = line_number - scr_start_line; |
3704 | |
3705 | is_script_found = true; |
3706 | break; |
3707 | } |
3708 | |
3709 | EditorNode::get_singleton()->load_scene(fpath); |
3710 | |
3711 | if (is_script_found && !scr_id.is_empty()) { |
3712 | Ref<Script> scr = ResourceLoader::load(fpath + "::" + scr_id, "Script" ); |
3713 | if (scr.is_valid()) { |
3714 | edit(scr); |
3715 | ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(_get_current_editor()); |
3716 | |
3717 | if (ste) { |
3718 | ste->goto_line_selection(line_number, begin, end); |
3719 | } |
3720 | } |
3721 | } |
3722 | |
3723 | return; |
3724 | } else { |
3725 | Ref<Script> scr = res; |
3726 | Ref<JSON> json = res; |
3727 | if (scr.is_valid() || json.is_valid()) { |
3728 | edit(scr); |
3729 | |
3730 | ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(_get_current_editor()); |
3731 | if (ste) { |
3732 | ste->goto_line_selection(line_number - 1, begin, end); |
3733 | } |
3734 | return; |
3735 | } |
3736 | } |
3737 | } |
3738 | |
3739 | // If the file is not a valid resource/script, load it as a text file. |
3740 | Error err; |
3741 | Ref<TextFile> text_file = _load_text_file(fpath, &err); |
3742 | if (text_file.is_valid()) { |
3743 | edit(text_file); |
3744 | |
3745 | TextEditor *te = Object::cast_to<TextEditor>(_get_current_editor()); |
3746 | if (te) { |
3747 | te->goto_line_selection(line_number - 1, begin, end); |
3748 | } |
3749 | } |
3750 | } |
3751 | |
3752 | void ScriptEditor::_start_find_in_files(bool with_replace) { |
3753 | FindInFiles *f = find_in_files->get_finder(); |
3754 | |
3755 | f->set_search_text(find_in_files_dialog->get_search_text()); |
3756 | f->set_match_case(find_in_files_dialog->is_match_case()); |
3757 | f->set_whole_words(find_in_files_dialog->is_whole_words()); |
3758 | f->set_folder(find_in_files_dialog->get_folder()); |
3759 | f->set_filter(find_in_files_dialog->get_filter()); |
3760 | |
3761 | find_in_files->set_with_replace(with_replace); |
3762 | find_in_files->set_replace_text(find_in_files_dialog->get_replace_text()); |
3763 | find_in_files->start_search(); |
3764 | |
3765 | EditorNode::get_singleton()->make_bottom_panel_item_visible(find_in_files); |
3766 | } |
3767 | |
3768 | void ScriptEditor::_on_find_in_files_modified_files(PackedStringArray paths) { |
3769 | _test_script_times_on_disk(); |
3770 | _update_modified_scripts_for_external_editor(); |
3771 | } |
3772 | |
3773 | void ScriptEditor::_window_changed(bool p_visible) { |
3774 | make_floating->set_visible(!p_visible); |
3775 | is_floating = p_visible; |
3776 | } |
3777 | |
3778 | void ScriptEditor::_filter_scripts_text_changed(const String &p_newtext) { |
3779 | _update_script_names(); |
3780 | } |
3781 | |
3782 | void ScriptEditor::_filter_methods_text_changed(const String &p_newtext) { |
3783 | _update_members_overview(); |
3784 | } |
3785 | |
3786 | void ScriptEditor::_bind_methods() { |
3787 | ClassDB::bind_method("_close_docs_tab" , &ScriptEditor::_close_docs_tab); |
3788 | ClassDB::bind_method("_close_all_tabs" , &ScriptEditor::_close_all_tabs); |
3789 | ClassDB::bind_method("_close_other_tabs" , &ScriptEditor::_close_other_tabs); |
3790 | ClassDB::bind_method("_goto_script_line2" , &ScriptEditor::_goto_script_line2); |
3791 | ClassDB::bind_method("_copy_script_path" , &ScriptEditor::_copy_script_path); |
3792 | |
3793 | ClassDB::bind_method("_help_class_open" , &ScriptEditor::_help_class_open); |
3794 | ClassDB::bind_method("_help_tab_goto" , &ScriptEditor::_help_tab_goto); |
3795 | ClassDB::bind_method("_live_auto_reload_running_scripts" , &ScriptEditor::_live_auto_reload_running_scripts); |
3796 | ClassDB::bind_method("_update_members_overview" , &ScriptEditor::_update_members_overview); |
3797 | ClassDB::bind_method("_update_recent_scripts" , &ScriptEditor::_update_recent_scripts); |
3798 | |
3799 | ClassDB::bind_method("get_current_editor" , &ScriptEditor::_get_current_editor); |
3800 | ClassDB::bind_method("get_open_script_editors" , &ScriptEditor::_get_open_script_editors); |
3801 | |
3802 | ClassDB::bind_method(D_METHOD("register_syntax_highlighter" , "syntax_highlighter" ), &ScriptEditor::register_syntax_highlighter); |
3803 | ClassDB::bind_method(D_METHOD("unregister_syntax_highlighter" , "syntax_highlighter" ), &ScriptEditor::unregister_syntax_highlighter); |
3804 | |
3805 | ClassDB::bind_method(D_METHOD("goto_line" , "line_number" ), &ScriptEditor::_goto_script_line2); |
3806 | ClassDB::bind_method(D_METHOD("get_current_script" ), &ScriptEditor::_get_current_script); |
3807 | ClassDB::bind_method(D_METHOD("get_open_scripts" ), &ScriptEditor::_get_open_scripts); |
3808 | ClassDB::bind_method(D_METHOD("open_script_create_dialog" , "base_name" , "base_path" ), &ScriptEditor::open_script_create_dialog); |
3809 | |
3810 | ADD_SIGNAL(MethodInfo("editor_script_changed" , PropertyInfo(Variant::OBJECT, "script" , PROPERTY_HINT_RESOURCE_TYPE, "Script" ))); |
3811 | ADD_SIGNAL(MethodInfo("script_close" , PropertyInfo(Variant::OBJECT, "script" , PROPERTY_HINT_RESOURCE_TYPE, "Script" ))); |
3812 | } |
3813 | |
3814 | ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) { |
3815 | window_wrapper = p_wrapper; |
3816 | current_theme = "" ; |
3817 | |
3818 | script_editor_cache.instantiate(); |
3819 | script_editor_cache->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg" )); |
3820 | |
3821 | completion_cache = memnew(EditorScriptCodeCompletionCache); |
3822 | restoring_layout = false; |
3823 | waiting_update_names = false; |
3824 | pending_auto_reload = false; |
3825 | auto_reload_running_scripts = true; |
3826 | members_overview_enabled = EDITOR_GET("text_editor/script_list/show_members_overview" ); |
3827 | help_overview_enabled = EDITOR_GET("text_editor/help/show_help_index" ); |
3828 | |
3829 | VBoxContainer *main_container = memnew(VBoxContainer); |
3830 | add_child(main_container); |
3831 | |
3832 | menu_hb = memnew(HBoxContainer); |
3833 | main_container->add_child(menu_hb); |
3834 | |
3835 | script_split = memnew(HSplitContainer); |
3836 | main_container->add_child(script_split); |
3837 | script_split->set_v_size_flags(SIZE_EXPAND_FILL); |
3838 | |
3839 | list_split = memnew(VSplitContainer); |
3840 | script_split->add_child(list_split); |
3841 | list_split->set_v_size_flags(SIZE_EXPAND_FILL); |
3842 | |
3843 | scripts_vbox = memnew(VBoxContainer); |
3844 | scripts_vbox->set_v_size_flags(SIZE_EXPAND_FILL); |
3845 | list_split->add_child(scripts_vbox); |
3846 | |
3847 | filter_scripts = memnew(LineEdit); |
3848 | filter_scripts->set_placeholder(TTR("Filter Scripts" )); |
3849 | filter_scripts->set_clear_button_enabled(true); |
3850 | filter_scripts->connect("text_changed" , callable_mp(this, &ScriptEditor::_filter_scripts_text_changed)); |
3851 | scripts_vbox->add_child(filter_scripts); |
3852 | |
3853 | script_list = memnew(ItemList); |
3854 | scripts_vbox->add_child(script_list); |
3855 | script_list->set_custom_minimum_size(Size2(150, 60) * EDSCALE); //need to give a bit of limit to avoid it from disappearing |
3856 | script_list->set_v_size_flags(SIZE_EXPAND_FILL); |
3857 | script_split->set_split_offset(70 * EDSCALE); |
3858 | _sort_list_on_update = true; |
3859 | script_list->connect("item_clicked" , callable_mp(this, &ScriptEditor::_script_list_clicked), CONNECT_DEFERRED); |
3860 | script_list->set_allow_rmb_select(true); |
3861 | SET_DRAG_FORWARDING_GCD(script_list, ScriptEditor); |
3862 | |
3863 | context_menu = memnew(PopupMenu); |
3864 | add_child(context_menu); |
3865 | context_menu->connect("id_pressed" , callable_mp(this, &ScriptEditor::_menu_option)); |
3866 | |
3867 | overview_vbox = memnew(VBoxContainer); |
3868 | overview_vbox->set_custom_minimum_size(Size2(0, 90)); |
3869 | overview_vbox->set_v_size_flags(SIZE_EXPAND_FILL); |
3870 | |
3871 | list_split->add_child(overview_vbox); |
3872 | list_split->set_visible(EditorSettings::get_singleton()->get_project_metadata("scripts_panel" , "show_scripts_panel" , true)); |
3873 | buttons_hbox = memnew(HBoxContainer); |
3874 | overview_vbox->add_child(buttons_hbox); |
3875 | |
3876 | filename = memnew(Label); |
3877 | filename->set_clip_text(true); |
3878 | filename->set_h_size_flags(SIZE_EXPAND_FILL); |
3879 | filename->add_theme_style_override("normal" , EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("normal" ), SNAME("LineEdit" ))); |
3880 | buttons_hbox->add_child(filename); |
3881 | |
3882 | members_overview_alphabeta_sort_button = memnew(Button); |
3883 | members_overview_alphabeta_sort_button->set_flat(true); |
3884 | members_overview_alphabeta_sort_button->set_tooltip_text(TTR("Toggle alphabetical sorting of the method list." )); |
3885 | members_overview_alphabeta_sort_button->set_toggle_mode(true); |
3886 | members_overview_alphabeta_sort_button->set_pressed(EDITOR_GET("text_editor/script_list/sort_members_outline_alphabetically" )); |
3887 | members_overview_alphabeta_sort_button->connect("toggled" , callable_mp(this, &ScriptEditor::_toggle_members_overview_alpha_sort)); |
3888 | |
3889 | buttons_hbox->add_child(members_overview_alphabeta_sort_button); |
3890 | |
3891 | filter_methods = memnew(LineEdit); |
3892 | filter_methods->set_placeholder(TTR("Filter Methods" )); |
3893 | filter_methods->set_clear_button_enabled(true); |
3894 | filter_methods->connect("text_changed" , callable_mp(this, &ScriptEditor::_filter_methods_text_changed)); |
3895 | overview_vbox->add_child(filter_methods); |
3896 | |
3897 | members_overview = memnew(ItemList); |
3898 | overview_vbox->add_child(members_overview); |
3899 | |
3900 | members_overview->set_allow_reselect(true); |
3901 | members_overview->set_custom_minimum_size(Size2(0, 60) * EDSCALE); //need to give a bit of limit to avoid it from disappearing |
3902 | members_overview->set_v_size_flags(SIZE_EXPAND_FILL); |
3903 | members_overview->set_allow_rmb_select(true); |
3904 | |
3905 | help_overview = memnew(ItemList); |
3906 | overview_vbox->add_child(help_overview); |
3907 | help_overview->set_allow_reselect(true); |
3908 | help_overview->set_custom_minimum_size(Size2(0, 60) * EDSCALE); //need to give a bit of limit to avoid it from disappearing |
3909 | help_overview->set_v_size_flags(SIZE_EXPAND_FILL); |
3910 | |
3911 | VBoxContainer *code_editor_container = memnew(VBoxContainer); |
3912 | script_split->add_child(code_editor_container); |
3913 | |
3914 | tab_container = memnew(TabContainer); |
3915 | tab_container->set_tabs_visible(false); |
3916 | tab_container->set_custom_minimum_size(Size2(200, 0) * EDSCALE); |
3917 | code_editor_container->add_child(tab_container); |
3918 | tab_container->set_h_size_flags(SIZE_EXPAND_FILL); |
3919 | tab_container->set_v_size_flags(SIZE_EXPAND_FILL); |
3920 | |
3921 | find_replace_bar = memnew(FindReplaceBar); |
3922 | code_editor_container->add_child(find_replace_bar); |
3923 | find_replace_bar->hide(); |
3924 | |
3925 | ED_SHORTCUT("script_editor/window_sort" , TTR("Sort" )); |
3926 | ED_SHORTCUT("script_editor/window_move_up" , TTR("Move Up" ), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::UP); |
3927 | ED_SHORTCUT("script_editor/window_move_down" , TTR("Move Down" ), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::DOWN); |
3928 | // FIXME: These should be `Key::GREATER` and `Key::LESS` but those don't work. |
3929 | ED_SHORTCUT("script_editor/next_script" , TTR("Next Script" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::PERIOD); |
3930 | ED_SHORTCUT("script_editor/prev_script" , TTR("Previous Script" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::COMMA); |
3931 | set_process_input(true); |
3932 | set_process_shortcut_input(true); |
3933 | |
3934 | file_menu = memnew(MenuButton); |
3935 | file_menu->set_text(TTR("File" )); |
3936 | file_menu->set_switch_on_hover(true); |
3937 | file_menu->set_shortcut_context(this); |
3938 | menu_hb->add_child(file_menu); |
3939 | |
3940 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new" , TTR("New Script..." ), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_NEW); |
3941 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new_textfile" , TTR("New Text File..." ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_NEW_TEXTFILE); |
3942 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open" , TTR("Open..." )), FILE_OPEN); |
3943 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reopen_closed_script" , TTR("Reopen Closed Script" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::T), FILE_REOPEN_CLOSED); |
3944 | file_menu->get_popup()->add_submenu_item(TTR("Open Recent" ), "RecentScripts" , FILE_OPEN_RECENT); |
3945 | |
3946 | recent_scripts = memnew(PopupMenu); |
3947 | recent_scripts->set_name("RecentScripts" ); |
3948 | file_menu->get_popup()->add_child(recent_scripts); |
3949 | recent_scripts->connect("id_pressed" , callable_mp(this, &ScriptEditor::_open_recent_script)); |
3950 | _update_recent_scripts(); |
3951 | |
3952 | file_menu->get_popup()->add_separator(); |
3953 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save" , TTR("Save" ), KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_SAVE); |
3954 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as" , TTR("Save As..." ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::S), FILE_SAVE_AS); |
3955 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all" , TTR("Save All" ), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::S), FILE_SAVE_ALL); |
3956 | ED_SHORTCUT_OVERRIDE("script_editor/save_all" , "macos" , KeyModifierMask::META | KeyModifierMask::CTRL | Key::S); |
3957 | file_menu->get_popup()->add_separator(); |
3958 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft" , TTR("Soft Reload Tool Script" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::R), FILE_TOOL_RELOAD_SOFT); |
3959 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_path" , TTR("Copy Script Path" )), FILE_COPY_PATH); |
3960 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/show_in_file_system" , TTR("Show in FileSystem" )), SHOW_IN_FILE_SYSTEM); |
3961 | file_menu->get_popup()->add_separator(); |
3962 | |
3963 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous" , TTR("History Previous" ), KeyModifierMask::ALT | Key::LEFT), WINDOW_PREV); |
3964 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next" , TTR("History Next" ), KeyModifierMask::ALT | Key::RIGHT), WINDOW_NEXT); |
3965 | file_menu->get_popup()->add_separator(); |
3966 | |
3967 | file_menu->get_popup()->add_submenu_item(TTR("Theme" ), "Theme" , FILE_THEME); |
3968 | |
3969 | theme_submenu = memnew(PopupMenu); |
3970 | theme_submenu->set_name("Theme" ); |
3971 | file_menu->get_popup()->add_child(theme_submenu); |
3972 | theme_submenu->connect("id_pressed" , callable_mp(this, &ScriptEditor::_theme_option)); |
3973 | theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/import_theme" , TTR("Import Theme..." )), THEME_IMPORT); |
3974 | theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/reload_theme" , TTR("Reload Theme" )), THEME_RELOAD); |
3975 | |
3976 | theme_submenu->add_separator(); |
3977 | theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme" , TTR("Save Theme" )), THEME_SAVE); |
3978 | theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as" , TTR("Save Theme As..." )), THEME_SAVE_AS); |
3979 | |
3980 | file_menu->get_popup()->add_separator(); |
3981 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file" , TTR("Close" ), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE); |
3982 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all" , TTR("Close All" )), CLOSE_ALL); |
3983 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_other_tabs" , TTR("Close Other Tabs" )), CLOSE_OTHER_TABS); |
3984 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs" , TTR("Close Docs" )), CLOSE_DOCS); |
3985 | |
3986 | file_menu->get_popup()->add_separator(); |
3987 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/run_file" , TTR("Run" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::X), FILE_RUN); |
3988 | |
3989 | file_menu->get_popup()->add_separator(); |
3990 | file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_scripts_panel" , TTR("Toggle Scripts Panel" ), KeyModifierMask::CMD_OR_CTRL | Key::BACKSLASH), TOGGLE_SCRIPTS_PANEL); |
3991 | file_menu->get_popup()->connect("id_pressed" , callable_mp(this, &ScriptEditor::_menu_option)); |
3992 | file_menu->get_popup()->connect("about_to_popup" , callable_mp(this, &ScriptEditor::_prepare_file_menu)); |
3993 | file_menu->get_popup()->connect("popup_hide" , callable_mp(this, &ScriptEditor::_file_menu_closed)); |
3994 | |
3995 | script_search_menu = memnew(MenuButton); |
3996 | script_search_menu->set_text(TTR("Search" )); |
3997 | script_search_menu->set_switch_on_hover(true); |
3998 | script_search_menu->set_shortcut_context(this); |
3999 | script_search_menu->get_popup()->connect("id_pressed" , callable_mp(this, &ScriptEditor::_menu_option)); |
4000 | menu_hb->add_child(script_search_menu); |
4001 | |
4002 | MenuButton * = memnew(MenuButton); |
4003 | menu_hb->add_child(debug_menu_btn); |
4004 | debug_menu_btn->hide(); // Handled by EditorDebuggerNode below. |
4005 | |
4006 | EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton(); |
4007 | debugger->set_script_debug_button(debug_menu_btn); |
4008 | debugger->connect("goto_script_line" , callable_mp(this, &ScriptEditor::_goto_script_line)); |
4009 | debugger->connect("set_execution" , callable_mp(this, &ScriptEditor::_set_execution)); |
4010 | debugger->connect("clear_execution" , callable_mp(this, &ScriptEditor::_clear_execution)); |
4011 | debugger->connect("breaked" , callable_mp(this, &ScriptEditor::_breaked)); |
4012 | debugger->get_default_debugger()->connect("set_breakpoint" , callable_mp(this, &ScriptEditor::_set_breakpoint)); |
4013 | debugger->get_default_debugger()->connect("clear_breakpoints" , callable_mp(this, &ScriptEditor::_clear_breakpoints)); |
4014 | |
4015 | menu_hb->add_spacer(); |
4016 | |
4017 | script_icon = memnew(TextureRect); |
4018 | menu_hb->add_child(script_icon); |
4019 | script_name_label = memnew(Label); |
4020 | menu_hb->add_child(script_name_label); |
4021 | |
4022 | script_icon->hide(); |
4023 | script_name_label->hide(); |
4024 | |
4025 | menu_hb->add_spacer(); |
4026 | |
4027 | site_search = memnew(Button); |
4028 | site_search->set_flat(true); |
4029 | site_search->set_text(TTR("Online Docs" )); |
4030 | site_search->connect("pressed" , callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_WEBSITE)); |
4031 | menu_hb->add_child(site_search); |
4032 | site_search->set_tooltip_text(TTR("Open Godot online documentation." )); |
4033 | |
4034 | help_search = memnew(Button); |
4035 | help_search->set_flat(true); |
4036 | help_search->set_text(TTR("Search Help" )); |
4037 | help_search->connect("pressed" , callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_HELP)); |
4038 | menu_hb->add_child(help_search); |
4039 | help_search->set_tooltip_text(TTR("Search the reference documentation." )); |
4040 | |
4041 | menu_hb->add_child(memnew(VSeparator)); |
4042 | |
4043 | script_back = memnew(Button); |
4044 | script_back->set_flat(true); |
4045 | script_back->connect("pressed" , callable_mp(this, &ScriptEditor::_history_back)); |
4046 | menu_hb->add_child(script_back); |
4047 | script_back->set_disabled(true); |
4048 | script_back->set_tooltip_text(TTR("Go to previous edited document." )); |
4049 | |
4050 | script_forward = memnew(Button); |
4051 | script_forward->set_flat(true); |
4052 | script_forward->connect("pressed" , callable_mp(this, &ScriptEditor::_history_forward)); |
4053 | menu_hb->add_child(script_forward); |
4054 | script_forward->set_disabled(true); |
4055 | script_forward->set_tooltip_text(TTR("Go to next edited document." )); |
4056 | |
4057 | if (p_wrapper->is_window_available()) { |
4058 | menu_hb->add_child(memnew(VSeparator)); |
4059 | |
4060 | make_floating = memnew(ScreenSelect); |
4061 | make_floating->set_flat(true); |
4062 | make_floating->set_tooltip_text(TTR("Make the script editor floating." )); |
4063 | make_floating->connect("request_open_in_screen" , callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true)); |
4064 | |
4065 | menu_hb->add_child(make_floating); |
4066 | p_wrapper->connect("window_visibility_changed" , callable_mp(this, &ScriptEditor::_window_changed)); |
4067 | } |
4068 | |
4069 | tab_container->connect("tab_changed" , callable_mp(this, &ScriptEditor::_tab_changed)); |
4070 | |
4071 | erase_tab_confirm = memnew(ConfirmationDialog); |
4072 | erase_tab_confirm->set_ok_button_text(TTR("Save" )); |
4073 | erase_tab_confirm->add_button(TTR("Discard" ), DisplayServer::get_singleton()->get_swap_cancel_ok(), "discard" ); |
4074 | erase_tab_confirm->connect("confirmed" , callable_mp(this, &ScriptEditor::_close_current_tab).bind(true)); |
4075 | erase_tab_confirm->connect("custom_action" , callable_mp(this, &ScriptEditor::_close_discard_current_tab)); |
4076 | add_child(erase_tab_confirm); |
4077 | |
4078 | script_create_dialog = memnew(ScriptCreateDialog); |
4079 | script_create_dialog->set_title(TTR("Create Script" )); |
4080 | add_child(script_create_dialog); |
4081 | script_create_dialog->connect("script_created" , callable_mp(this, &ScriptEditor::_script_created)); |
4082 | |
4083 | file_dialog_option = -1; |
4084 | file_dialog = memnew(EditorFileDialog); |
4085 | add_child(file_dialog); |
4086 | file_dialog->connect("file_selected" , callable_mp(this, &ScriptEditor::_file_dialog_action)); |
4087 | |
4088 | error_dialog = memnew(AcceptDialog); |
4089 | add_child(error_dialog); |
4090 | |
4091 | disk_changed = memnew(ConfirmationDialog); |
4092 | { |
4093 | VBoxContainer *vbc = memnew(VBoxContainer); |
4094 | disk_changed->add_child(vbc); |
4095 | |
4096 | Label *dl = memnew(Label); |
4097 | dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:" )); |
4098 | vbc->add_child(dl); |
4099 | |
4100 | disk_changed_list = memnew(Tree); |
4101 | vbc->add_child(disk_changed_list); |
4102 | disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL); |
4103 | |
4104 | disk_changed->connect("confirmed" , callable_mp(this, &ScriptEditor::reload_scripts).bind(false)); |
4105 | disk_changed->set_ok_button_text(TTR("Reload" )); |
4106 | |
4107 | disk_changed->add_button(TTR("Resave" ), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave" ); |
4108 | disk_changed->connect("custom_action" , callable_mp(this, &ScriptEditor::_resave_scripts)); |
4109 | } |
4110 | |
4111 | add_child(disk_changed); |
4112 | |
4113 | script_editor = this; |
4114 | |
4115 | autosave_timer = memnew(Timer); |
4116 | autosave_timer->set_one_shot(false); |
4117 | autosave_timer->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &ScriptEditor::_update_autosave_timer)); |
4118 | autosave_timer->connect("timeout" , callable_mp(this, &ScriptEditor::_autosave_scripts)); |
4119 | add_child(autosave_timer); |
4120 | |
4121 | grab_focus_block = false; |
4122 | |
4123 | help_search_dialog = memnew(EditorHelpSearch); |
4124 | add_child(help_search_dialog); |
4125 | help_search_dialog->connect("go_to_help" , callable_mp(this, &ScriptEditor::_help_class_goto)); |
4126 | |
4127 | find_in_files_dialog = memnew(FindInFilesDialog); |
4128 | find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_FIND_REQUESTED, callable_mp(this, &ScriptEditor::_start_find_in_files).bind(false)); |
4129 | find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_REPLACE_REQUESTED, callable_mp(this, &ScriptEditor::_start_find_in_files).bind(true)); |
4130 | add_child(find_in_files_dialog); |
4131 | find_in_files = memnew(FindInFilesPanel); |
4132 | find_in_files_button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Search Results" ), find_in_files); |
4133 | find_in_files->set_custom_minimum_size(Size2(0, 200) * EDSCALE); |
4134 | find_in_files->connect(FindInFilesPanel::SIGNAL_RESULT_SELECTED, callable_mp(this, &ScriptEditor::_on_find_in_files_result_selected)); |
4135 | find_in_files->connect(FindInFilesPanel::SIGNAL_FILES_MODIFIED, callable_mp(this, &ScriptEditor::_on_find_in_files_modified_files)); |
4136 | find_in_files->hide(); |
4137 | find_in_files_button->hide(); |
4138 | |
4139 | history_pos = -1; |
4140 | |
4141 | edit_pass = 0; |
4142 | trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save" ); |
4143 | convert_indent_on_save = EDITOR_GET("text_editor/behavior/files/convert_indent_on_save" ); |
4144 | |
4145 | ScriptServer::edit_request_func = _open_script_request; |
4146 | |
4147 | Ref<EditorJSONSyntaxHighlighter> json_syntax_highlighter; |
4148 | json_syntax_highlighter.instantiate(); |
4149 | register_syntax_highlighter(json_syntax_highlighter); |
4150 | } |
4151 | |
4152 | ScriptEditor::~ScriptEditor() { |
4153 | memdelete(completion_cache); |
4154 | } |
4155 | |
4156 | void ScriptEditorPlugin::_focus_another_editor() { |
4157 | if (window_wrapper->get_window_enabled()) { |
4158 | ERR_FAIL_COND(last_editor.is_empty()); |
4159 | EditorInterface::get_singleton()->set_main_screen_editor(last_editor); |
4160 | } |
4161 | } |
4162 | |
4163 | void ScriptEditorPlugin::_save_last_editor(String p_editor) { |
4164 | if (p_editor != get_name()) { |
4165 | last_editor = p_editor; |
4166 | } |
4167 | } |
4168 | |
4169 | void ScriptEditorPlugin::_window_visibility_changed(bool p_visible) { |
4170 | _focus_another_editor(); |
4171 | if (p_visible) { |
4172 | script_editor->add_theme_style_override("panel" , script_editor->get_theme_stylebox("ScriptEditorPanelFloating" , EditorStringName(EditorStyles))); |
4173 | } else { |
4174 | script_editor->add_theme_style_override("panel" , script_editor->get_theme_stylebox("ScriptEditorPanel" , EditorStringName(EditorStyles))); |
4175 | } |
4176 | } |
4177 | |
4178 | void ScriptEditorPlugin::_notification(int p_what) { |
4179 | switch (p_what) { |
4180 | case NOTIFICATION_ENTER_TREE: { |
4181 | connect("main_screen_changed" , callable_mp(this, &ScriptEditorPlugin::_save_last_editor)); |
4182 | } break; |
4183 | case NOTIFICATION_EXIT_TREE: { |
4184 | disconnect("main_screen_changed" , callable_mp(this, &ScriptEditorPlugin::_save_last_editor)); |
4185 | } break; |
4186 | } |
4187 | } |
4188 | |
4189 | void ScriptEditorPlugin::edit(Object *p_object) { |
4190 | if (Object::cast_to<Script>(p_object)) { |
4191 | Script *p_script = Object::cast_to<Script>(p_object); |
4192 | String res_path = p_script->get_path().get_slice("::" , 0); |
4193 | |
4194 | if (p_script->is_built_in() && !res_path.is_empty()) { |
4195 | if (ResourceLoader::get_resource_type(res_path) == "PackedScene" ) { |
4196 | if (!EditorNode::get_singleton()->is_scene_open(res_path)) { |
4197 | EditorNode::get_singleton()->load_scene(res_path); |
4198 | } |
4199 | } else { |
4200 | EditorNode::get_singleton()->load_resource(res_path); |
4201 | } |
4202 | } |
4203 | script_editor->edit(p_script); |
4204 | } else if (Object::cast_to<JSON>(p_object)) { |
4205 | script_editor->edit(Object::cast_to<JSON>(p_object)); |
4206 | } else if (Object::cast_to<TextFile>(p_object)) { |
4207 | script_editor->edit(Object::cast_to<TextFile>(p_object)); |
4208 | } |
4209 | } |
4210 | |
4211 | bool ScriptEditorPlugin::handles(Object *p_object) const { |
4212 | if (Object::cast_to<TextFile>(p_object)) { |
4213 | return true; |
4214 | } |
4215 | |
4216 | if (Object::cast_to<Script>(p_object)) { |
4217 | return true; |
4218 | } |
4219 | |
4220 | if (Object::cast_to<JSON>(p_object)) { |
4221 | return true; |
4222 | } |
4223 | |
4224 | return p_object->is_class("Script" ); |
4225 | } |
4226 | |
4227 | void ScriptEditorPlugin::make_visible(bool p_visible) { |
4228 | if (p_visible) { |
4229 | window_wrapper->show(); |
4230 | script_editor->set_process(true); |
4231 | script_editor->ensure_select_current(); |
4232 | } else { |
4233 | window_wrapper->hide(); |
4234 | if (!window_wrapper->get_window_enabled()) { |
4235 | script_editor->set_process(false); |
4236 | } |
4237 | } |
4238 | } |
4239 | |
4240 | void ScriptEditorPlugin::selected_notify() { |
4241 | script_editor->ensure_select_current(); |
4242 | _focus_another_editor(); |
4243 | } |
4244 | |
4245 | String ScriptEditorPlugin::get_unsaved_status(const String &p_for_scene) const { |
4246 | const PackedStringArray unsaved_scripts = script_editor->get_unsaved_scripts(); |
4247 | if (unsaved_scripts.is_empty()) { |
4248 | return String(); |
4249 | } |
4250 | |
4251 | PackedStringArray message; |
4252 | if (!p_for_scene.is_empty()) { |
4253 | PackedStringArray unsaved_built_in_scripts; |
4254 | |
4255 | const String scene_file = p_for_scene.get_file(); |
4256 | for (const String &E : unsaved_scripts) { |
4257 | if (!E.is_resource_file() && E.contains(scene_file)) { |
4258 | unsaved_built_in_scripts.append(E); |
4259 | } |
4260 | } |
4261 | |
4262 | if (unsaved_built_in_scripts.is_empty()) { |
4263 | return String(); |
4264 | } else { |
4265 | message.resize(unsaved_built_in_scripts.size() + 1); |
4266 | message.write[0] = TTR("There are unsaved changes in the following built-in script(s):" ); |
4267 | |
4268 | int i = 1; |
4269 | for (const String &E : unsaved_built_in_scripts) { |
4270 | message.write[i] = E.trim_suffix("(*)" ); |
4271 | i++; |
4272 | } |
4273 | return String("\n" ).join(message); |
4274 | } |
4275 | } |
4276 | |
4277 | message.resize(unsaved_scripts.size() + 1); |
4278 | message.write[0] = TTR("Save changes to the following script(s) before quitting?" ); |
4279 | |
4280 | int i = 1; |
4281 | for (const String &E : unsaved_scripts) { |
4282 | message.write[i] = E.trim_suffix("(*)" ); |
4283 | i++; |
4284 | } |
4285 | return String("\n" ).join(message); |
4286 | } |
4287 | |
4288 | void ScriptEditorPlugin::save_external_data() { |
4289 | if (!EditorNode::get_singleton()->is_exiting()) { |
4290 | script_editor->save_all_scripts(); |
4291 | } |
4292 | } |
4293 | |
4294 | void ScriptEditorPlugin::apply_changes() { |
4295 | script_editor->apply_scripts(); |
4296 | } |
4297 | |
4298 | void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) { |
4299 | script_editor->set_window_layout(p_layout); |
4300 | |
4301 | if (EDITOR_GET("interface/multi_window/restore_windows_on_load" ) && window_wrapper->is_window_available() && p_layout->has_section_key("ScriptEditor" , "window_rect" )) { |
4302 | window_wrapper->restore_window_from_saved_position( |
4303 | p_layout->get_value("ScriptEditor" , "window_rect" , Rect2i()), |
4304 | p_layout->get_value("ScriptEditor" , "window_screen" , -1), |
4305 | p_layout->get_value("ScriptEditor" , "window_screen_rect" , Rect2i())); |
4306 | } else { |
4307 | window_wrapper->set_window_enabled(false); |
4308 | } |
4309 | } |
4310 | |
4311 | void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) { |
4312 | script_editor->get_window_layout(p_layout); |
4313 | |
4314 | if (window_wrapper->get_window_enabled()) { |
4315 | p_layout->set_value("ScriptEditor" , "window_rect" , window_wrapper->get_window_rect()); |
4316 | int screen = window_wrapper->get_window_screen(); |
4317 | p_layout->set_value("ScriptEditor" , "window_screen" , screen); |
4318 | p_layout->set_value("ScriptEditor" , "window_screen_rect" , DisplayServer::get_singleton()->screen_get_usable_rect(screen)); |
4319 | |
4320 | } else { |
4321 | if (p_layout->has_section_key("ScriptEditor" , "window_rect" )) { |
4322 | p_layout->erase_section_key("ScriptEditor" , "window_rect" ); |
4323 | } |
4324 | if (p_layout->has_section_key("ScriptEditor" , "window_screen" )) { |
4325 | p_layout->erase_section_key("ScriptEditor" , "window_screen" ); |
4326 | } |
4327 | if (p_layout->has_section_key("ScriptEditor" , "window_screen_rect" )) { |
4328 | p_layout->erase_section_key("ScriptEditor" , "window_screen_rect" ); |
4329 | } |
4330 | } |
4331 | } |
4332 | |
4333 | void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) { |
4334 | script_editor->get_breakpoints(p_breakpoints); |
4335 | } |
4336 | |
4337 | void ScriptEditorPlugin::edited_scene_changed() { |
4338 | script_editor->edited_scene_changed(); |
4339 | } |
4340 | |
4341 | ScriptEditorPlugin::ScriptEditorPlugin() { |
4342 | window_wrapper = memnew(WindowWrapper); |
4343 | window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine" ), TTR("Script Editor" ))); |
4344 | window_wrapper->set_margins_enabled(true); |
4345 | |
4346 | script_editor = memnew(ScriptEditor(window_wrapper)); |
4347 | Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("script_editor/make_floating" , TTR("Make Floating" )); |
4348 | window_wrapper->set_wrapped_control(script_editor, make_floating_shortcut); |
4349 | |
4350 | EditorNode::get_singleton()->get_main_screen_control()->add_child(window_wrapper); |
4351 | window_wrapper->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
4352 | window_wrapper->hide(); |
4353 | window_wrapper->connect("window_visibility_changed" , callable_mp(this, &ScriptEditorPlugin::_window_visibility_changed)); |
4354 | |
4355 | EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change" ); |
4356 | ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/behavior/files/auto_reload_and_parse_scripts_on_save" , true)); |
4357 | EDITOR_DEF("text_editor/behavior/files/open_dominant_script_on_scene_change" , true); |
4358 | EDITOR_DEF("text_editor/external/use_external_editor" , false); |
4359 | EDITOR_DEF("text_editor/external/exec_path" , "" ); |
4360 | EDITOR_DEF("text_editor/script_list/script_temperature_enabled" , true); |
4361 | EDITOR_DEF("text_editor/script_list/script_temperature_history_size" , 15); |
4362 | EDITOR_DEF("text_editor/script_list/group_help_pages" , true); |
4363 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/script_list/sort_scripts_by" , PROPERTY_HINT_ENUM, "Name,Path,None" )); |
4364 | EDITOR_DEF("text_editor/script_list/sort_scripts_by" , 0); |
4365 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/script_list/list_script_names_as" , PROPERTY_HINT_ENUM, "Name,Parent Directory And Name,Full Path" )); |
4366 | EDITOR_DEF("text_editor/script_list/list_script_names_as" , 0); |
4367 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_path" , PROPERTY_HINT_GLOBAL_FILE)); |
4368 | EDITOR_DEF("text_editor/external/exec_flags" , "{file}" ); |
4369 | EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_flags" , PROPERTY_HINT_PLACEHOLDER_TEXT, "Call flags with placeholders: {project}, {file}, {col}, {line}." )); |
4370 | |
4371 | ED_SHORTCUT("script_editor/reopen_closed_script" , TTR("Reopen Closed Script" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::T); |
4372 | ED_SHORTCUT("script_editor/clear_recent" , TTR("Clear Recent Scripts" )); |
4373 | } |
4374 | |
4375 | ScriptEditorPlugin::~ScriptEditorPlugin() { |
4376 | } |
4377 | |