Skip to content

Commit

Permalink
Save the current script when adding a new method via signal connection
Browse files Browse the repository at this point in the history
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 godotengine#41283.
  • Loading branch information
Calinou committed May 5, 2021
1 parent 758bccf commit 0ade686
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 50 deletions.
108 changes: 58 additions & 50 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
// Do not try to save internal scripts, but prompt to save in-memory
// scripts which are not saved to disk yet (have empty path).
if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
_menu_option(FILE_SAVE);
_save_current_script();
}
}
if (script.is_valid()) {
Expand Down Expand Up @@ -1096,6 +1096,59 @@ 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<TextFile> text_file = resource;
Ref<Script> script = resource;

if (text_file != nullptr) {
current->apply_code();
_save_text_file(text_file, text_file->get_path());
return;
}

if (script != nullptr) {
const Vector<DocData::ClassDoc> &documentations = script->get_documentation();
for (int j = 0; j < documentations.size(); j++) {
const DocData::ClassDoc &doc = documentations.get(j);
if (EditorHelp::get_doc_data()->has_doc(doc.name)) {
EditorHelp::get_doc_data()->remove_doc(doc.name);
}
}
}

editor->save_resource(resource);

if (script != nullptr) {
const Vector<DocData::ClassDoc> &documentations = script->get_documentation();
for (int j = 0; j < documentations.size(); j++) {
const DocData::ClassDoc &doc = documentations.get(j);
EditorHelp::get_doc_data()->add_doc(doc);
update_doc(doc.name);
}
}
}

void ScriptEditor::_menu_option(int p_option) {
ScriptEditorBase *current = _get_current_editor();
switch (p_option) {
Expand Down Expand Up @@ -1224,55 +1277,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<TextFile> text_file = resource;
Ref<Script> script = resource;

if (text_file != nullptr) {
current->apply_code();
_save_text_file(text_file, text_file->get_path());
break;
}

if (script != nullptr) {
const Vector<DocData::ClassDoc> &documentations = script->get_documentation();
for (int j = 0; j < documentations.size(); j++) {
const DocData::ClassDoc &doc = documentations.get(j);
if (EditorHelp::get_doc_data()->has_doc(doc.name)) {
EditorHelp::get_doc_data()->remove_doc(doc.name);
}
}
}

editor->save_resource(resource);

if (script != nullptr) {
const Vector<DocData::ClassDoc> &documentations = script->get_documentation();
for (int j = 0; j < documentations.size(); j++) {
const DocData::ClassDoc &doc = documentations.get(j);
EditorHelp::get_doc_data()->add_doc(doc);
update_doc(doc.name);
}
}

_save_current_script();
} break;
case FILE_SAVE_AS: {
if (trim_trailing_whitespace_on_save) {
Expand Down Expand Up @@ -2439,6 +2444,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;
}
}
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,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();

Expand Down

0 comments on commit 0ade686

Please sign in to comment.