From 14435ebcee2b0e6a90f9303771246fb2f9cf8777 Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 23 Sep 2022 14:46:28 +0200 Subject: Split script navigation state and edit state --- editor/code_editor.cpp | 37 ++++++++++++++++++++------------- editor/code_editor.h | 1 + editor/plugins/script_editor_plugin.cpp | 15 ++++++------- editor/plugins/script_editor_plugin.h | 1 + editor/plugins/script_text_editor.cpp | 4 ++++ editor/plugins/script_text_editor.h | 1 + editor/plugins/text_editor.cpp | 4 ++++ editor/plugins/text_editor.h | 1 + 8 files changed, 43 insertions(+), 21 deletions(-) diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 11a6912aa5..25556482bc 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1526,19 +1526,7 @@ void CodeTextEditor::clear_executing_line() { Variant CodeTextEditor::get_edit_state() { Dictionary state; - - state["scroll_position"] = text_editor->get_v_scroll(); - state["h_scroll_position"] = text_editor->get_h_scroll(); - state["column"] = text_editor->get_caret_column(); - state["row"] = text_editor->get_caret_line(); - - state["selection"] = get_text_editor()->has_selection(); - if (get_text_editor()->has_selection()) { - state["selection_from_line"] = text_editor->get_selection_from_line(); - state["selection_from_column"] = text_editor->get_selection_from_column(); - state["selection_to_line"] = text_editor->get_selection_to_line(); - state["selection_to_column"] = text_editor->get_selection_to_column(); - } + state.merge(get_navigation_state()); state["folded_lines"] = text_editor->get_folded_lines(); state["breakpoints"] = text_editor->get_breakpointed_lines(); @@ -1559,8 +1547,10 @@ void CodeTextEditor::set_edit_state(const Variant &p_state) { text_editor->set_v_scroll(state["scroll_position"]); text_editor->set_h_scroll(state["h_scroll_position"]); - if (state.has("selection")) { + if (state.get("selection", false)) { text_editor->select(state["selection_from_line"], state["selection_from_column"], state["selection_to_line"], state["selection_to_column"]); + } else { + text_editor->deselect(); } if (state.has("folded_lines")) { @@ -1585,6 +1575,25 @@ void CodeTextEditor::set_edit_state(const Variant &p_state) { } } +Variant CodeTextEditor::get_navigation_state() { + Dictionary state; + + state["scroll_position"] = text_editor->get_v_scroll(); + state["h_scroll_position"] = text_editor->get_h_scroll(); + state["column"] = text_editor->get_caret_column(); + state["row"] = text_editor->get_caret_line(); + + state["selection"] = get_text_editor()->has_selection(); + if (get_text_editor()->has_selection()) { + state["selection_from_line"] = text_editor->get_selection_from_line(); + state["selection_from_column"] = text_editor->get_selection_from_column(); + state["selection_to_line"] = text_editor->get_selection_to_line(); + state["selection_to_column"] = text_editor->get_selection_to_column(); + } + + return state; +} + void CodeTextEditor::set_error(const String &p_error) { error->set_text(p_error); if (!p_error.is_empty()) { diff --git a/editor/code_editor.h b/editor/code_editor.h index 49679cc700..775708954d 100644 --- a/editor/code_editor.h +++ b/editor/code_editor.h @@ -246,6 +246,7 @@ public: Variant get_edit_state(); void set_edit_state(const Variant &p_state); + Variant get_navigation_state(); void set_error_count(int p_error_count); void set_warning_count(int p_warning_count); diff --git a/editor/plugins/script_editor_plugin.cpp b/editor/plugins/script_editor_plugin.cpp index 941fcbac2a..6801e29877 100644 --- a/editor/plugins/script_editor_plugin.cpp +++ b/editor/plugins/script_editor_plugin.cpp @@ -565,7 +565,7 @@ void ScriptEditor::_save_history() { Node *n = tab_container->get_current_tab_control(); if (Object::cast_to(n)) { - history.write[history_pos].state = Object::cast_to(n)->get_edit_state(); + history.write[history_pos].state = Object::cast_to(n)->get_navigation_state(); } if (Object::cast_to(n)) { history.write[history_pos].state = Object::cast_to(n)->get_scroll(); @@ -600,7 +600,7 @@ void ScriptEditor::_go_to_tab(int p_idx) { Node *n = tab_container->get_current_tab_control(); if (Object::cast_to(n)) { - history.write[history_pos].state = Object::cast_to(n)->get_edit_state(); + history.write[history_pos].state = Object::cast_to(n)->get_navigation_state(); } if (Object::cast_to(n)) { history.write[history_pos].state = Object::cast_to(n)->get_scroll(); @@ -3374,7 +3374,7 @@ void ScriptEditor::_update_history_pos(int p_new_pos) { Node *n = tab_container->get_current_tab_control(); if (Object::cast_to(n)) { - history.write[history_pos].state = Object::cast_to(n)->get_edit_state(); + history.write[history_pos].state = Object::cast_to(n)->get_navigation_state(); } if (Object::cast_to(n)) { history.write[history_pos].state = Object::cast_to(n)->get_scroll(); @@ -3385,11 +3385,12 @@ void ScriptEditor::_update_history_pos(int p_new_pos) { n = history[history_pos].control; - if (Object::cast_to(n)) { - Object::cast_to(n)->set_edit_state(history[history_pos].state); - Object::cast_to(n)->ensure_focus(); + ScriptEditorBase *seb = Object::cast_to(n); + if (seb) { + seb->set_edit_state(history[history_pos].state); + seb->ensure_focus(); - Ref