From dc42ef2df91a5ce224f2def2e90a6ff3d282db03 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Tue, 29 Sep 2020 23:00:34 +0200 Subject: [PATCH] Save the current script when adding a new method via signal connection This makes it possible for external editors to pick up the changes. Most modern editors should reload the file automatically, but some older/lightweight editors may ask the user instead (or only warn after trying to save in the external editor). This closes #41283. (cherry picked from commit 0ade6866014b9d4bf8469b4fe06e6a7ef9e82592) --- editor/plugins/script_editor_plugin.cpp | 66 ++++++++++++++----------- editor/plugins/script_editor_plugin.h | 1 + 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index b03e3b69a80..e3c16d47604 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -551,7 +551,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) { if (p_save) { // Do not try to save internal scripts if (!script.is_valid() || !(script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1)) { - _menu_option(FILE_SAVE); + _save_current_script(); } } @@ -948,6 +948,39 @@ bool ScriptEditor::is_scripts_panel_toggled() { return list_split->is_visible(); } +void ScriptEditor::_save_current_script() { + ScriptEditorBase *current = _get_current_editor(); + + if (_test_script_times_on_disk()) { + return; + } + + if (trim_trailing_whitespace_on_save) { + current->trim_trailing_whitespace(); + } + + current->insert_final_newline(); + + if (convert_indent_on_save) { + if (use_space_indentation) { + current->convert_indent_to_spaces(); + } else { + current->convert_indent_to_tabs(); + } + } + + RES resource = current->get_edited_resource(); + Ref text_file = resource; + + if (text_file != nullptr) { + current->apply_code(); + _save_text_file(text_file, text_file->get_path()); + return; + } + + editor->save_resource(resource); +} + void ScriptEditor::_menu_option(int p_option) { ScriptEditorBase *current = _get_current_editor(); switch (p_option) { @@ -1089,33 +1122,7 @@ void ScriptEditor::_menu_option(int p_option) { if (current) { switch (p_option) { case FILE_SAVE: { - if (_test_script_times_on_disk()) { - return; - } - - if (trim_trailing_whitespace_on_save) { - current->trim_trailing_whitespace(); - } - - current->insert_final_newline(); - - if (convert_indent_on_save) { - if (use_space_indentation) { - current->convert_indent_to_spaces(); - } else { - current->convert_indent_to_tabs(); - } - } - - RES resource = current->get_edited_resource(); - Ref text_file = resource; - if (text_file != nullptr) { - current->apply_code(); - _save_text_file(text_file, text_file->get_path()); - break; - } - editor->save_resource(resource); - + _save_current_script(); } break; case FILE_SAVE_AS: { if (trim_trailing_whitespace_on_save) { @@ -2284,6 +2291,9 @@ void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const script_list->select(script_list->find_metadata(i)); + // Save the current script so the changes can be picked up by an external editor. + _save_current_script(); + break; } } diff --git a/editor/plugins/script_editor_plugin.h b/editor/plugins/script_editor_plugin.h index 0faf6773156..54c310424b8 100644 --- a/editor/plugins/script_editor_plugin.h +++ b/editor/plugins/script_editor_plugin.h @@ -276,6 +276,7 @@ class ScriptEditor : public PanelContainer { String _get_debug_tooltip(const String &p_text, Node *_se); + void _save_current_script(); void _resave_scripts(const String &p_str); void _reload_scripts();