Preserve script editor state through tab closes

This commit is contained in:
Paulb23 2021-10-07 19:44:23 +01:00
parent 6877cccfb1
commit bec8ae7ba6
2 changed files with 65 additions and 5 deletions

View file

@ -756,6 +756,7 @@ void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
int idx = tab_container->get_current_tab();
if (current) {
current->clear_edit_menu();
_save_editor_state(current);
}
memdelete(tselected);
if (idx >= tab_container->get_child_count()) {
@ -1485,6 +1486,7 @@ void ScriptEditor::_notification(int p_what) {
editor->connect("stop_pressed", callable_mp(this, &ScriptEditor::_editor_stop));
editor->connect("script_add_function_request", callable_mp(this, &ScriptEditor::_add_callback));
editor->connect("resource_saved", callable_mp(this, &ScriptEditor::_res_saved_callback));
editor->get_filesystem_dock()->connect("files_moved", callable_mp(this, &ScriptEditor::_files_moved));
editor->get_filesystem_dock()->connect("file_removed", callable_mp(this, &ScriptEditor::_file_removed));
script_list->connect("item_selected", callable_mp(this, &ScriptEditor::_script_selected));
@ -2275,6 +2277,10 @@ bool ScriptEditor::edit(const RES &p_resource, int p_line, int p_col, bool p_gra
_add_recent_script(p_resource->get_path());
}
if (script_editor_cache->has_section(p_resource->get_path())) {
se->set_edit_state(script_editor_cache->get_value(p_resource->get_path(), "state"));
}
_sort_list_on_update = true;
_update_script_names();
_save_layout();
@ -2510,6 +2516,20 @@ void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const
}
}
void ScriptEditor::_save_editor_state(ScriptEditorBase *p_editor) {
if (restoring_layout) {
return;
}
const String &path = p_editor->get_edited_resource()->get_path();
if (!path.is_resource_file()) {
return;
}
script_editor_cache->set_value(path, "state", p_editor->get_edit_state());
// This is saved later when we save the editor layout.
}
void ScriptEditor::_save_layout() {
if (restoring_layout) {
return;
@ -2555,6 +2575,16 @@ void ScriptEditor::_filesystem_changed() {
_update_script_names();
}
void ScriptEditor::_files_moved(const String &p_old_file, const String &p_new_file) {
if (!script_editor_cache->has_section(p_old_file)) {
return;
}
Variant state = script_editor_cache->get_value(p_old_file, "state");
script_editor_cache->erase_section(p_old_file);
script_editor_cache->set_value(p_new_file, "state", state);
// This is saved later when we save the editor layout.
}
void ScriptEditor::_file_removed(const String &p_removed_file) {
for (int i = 0; i < tab_container->get_child_count(); i++) {
ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_child(i));
@ -2566,6 +2596,11 @@ void ScriptEditor::_file_removed(const String &p_removed_file) {
_close_tab(i, false, false);
}
}
// Check un-opened.
if (script_editor_cache->has_section(p_removed_file)) {
script_editor_cache->erase_section(p_removed_file);
}
}
void ScriptEditor::_update_find_replace_bar() {
@ -2916,6 +2951,7 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
restoring_layout = true;
Set<String> loaded_scripts;
List<String> extensions;
ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
@ -2928,8 +2964,12 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
}
if (!FileAccess::exists(path)) {
if (script_editor_cache->has_section(path)) {
script_editor_cache->erase_section(path);
}
continue;
}
loaded_scripts.insert(path);
if (extensions.find(path.get_extension())) {
Ref<Script> scr = ResourceLoader::load(path);
@ -2974,6 +3014,20 @@ void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
script_split->set_split_offset(p_layout->get_value("ScriptEditor", "split_offset"));
}
// Remove any deleted editors that have been removed between launches.
// and if a Script, register breakpoints with the debugger.
List<String> cached_editors;
script_editor_cache->get_sections(&cached_editors);
for (const String &E : cached_editors) {
if (loaded_scripts.has(E)) {
continue;
}
if (!FileAccess::exists(E)) {
script_editor_cache->erase_section(E);
}
}
restoring_layout = false;
_update_script_names();
@ -2991,11 +3045,8 @@ void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
continue;
}
Dictionary script_info;
script_info["path"] = path;
script_info["state"] = se->get_edit_state();
scripts.push_back(script_info);
_save_editor_state(se);
scripts.push_back(path);
}
EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_child(i));
@ -3008,6 +3059,9 @@ void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
p_layout->set_value("ScriptEditor", "open_scripts", scripts);
p_layout->set_value("ScriptEditor", "open_help", helps);
p_layout->set_value("ScriptEditor", "split_offset", script_split->get_split_offset());
// Save the cache.
script_editor_cache->save(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("script_editor_cache.cfg"));
}
void ScriptEditor::_help_class_open(const String &p_class) {
@ -3374,6 +3428,9 @@ void ScriptEditor::_bind_methods() {
ScriptEditor::ScriptEditor(EditorNode *p_editor) {
current_theme = "";
script_editor_cache.instantiate();
script_editor_cache->load(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("script_editor_cache.cfg"));
completion_cache = memnew(EditorScriptCodeCompletionCache);
restoring_layout = false;
waiting_update_names = false;

View file

@ -386,9 +386,12 @@ class ScriptEditor : public PanelContainer {
ScriptEditorBase *_get_current_editor() const;
Array _get_open_script_editors() const;
Ref<ConfigFile> script_editor_cache;
void _save_editor_state(ScriptEditorBase *p_editor);
void _save_layout();
void _editor_settings_changed();
void _filesystem_changed();
void _files_moved(const String &p_old_file, const String &p_new_file);
void _file_removed(const String &p_file);
void _autosave_scripts();
void _update_autosave_timer();