fix symbol jump history navigation issue.

This commit is contained in:
jinyangcruise 2026-02-18 00:17:00 +08:00
parent 8a4df69e22
commit e4e31abeaa
13 changed files with 183 additions and 32 deletions

View file

@ -201,12 +201,6 @@ void ScriptEditor::_goto_script_line(Ref<RefCounted> p_script, int p_line) {
if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) {
if (edit(p_script, p_line, 0)) {
EditorNode::get_singleton()->push_item(p_script.ptr());
if (TextEditorBase *current = Object::cast_to<TextEditorBase>(_get_current_editor())) {
current->goto_line_centered(p_line);
}
_save_history();
}
}
}
@ -314,7 +308,9 @@ void ScriptEditor::_save_history() {
Node *n = tab_container->get_current_tab_control();
if (Object::cast_to<TextEditorBase>(n)) {
history.write[history_pos].state = Object::cast_to<TextEditorBase>(n)->get_navigation_state();
Dictionary nav_state = Object::cast_to<TextEditorBase>(n)->get_navigation_state();
nav_state["ensure_caret_visible"] = true;
history.write[history_pos].state = nav_state;
}
if (Object::cast_to<EditorHelp>(n)) {
history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
@ -373,7 +369,9 @@ void ScriptEditor::_go_to_tab(int p_idx) {
Node *n = tab_container->get_current_tab_control();
if (Object::cast_to<TextEditorBase>(n)) {
history.write[history_pos].state = Object::cast_to<TextEditorBase>(n)->get_navigation_state();
Dictionary nav_state = Object::cast_to<TextEditorBase>(n)->get_navigation_state();
nav_state["ensure_caret_visible"] = true;
history.write[history_pos].state = nav_state;
}
if (Object::cast_to<EditorHelp>(n)) {
history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
@ -2213,7 +2211,7 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
}
if (p_line >= 0) {
teb->goto_line(p_line, p_col);
teb->goto_line_centered(p_line, p_col);
}
} else if (tab_container->get_current_tab() != i) {
_go_to_tab(i);
@ -2340,7 +2338,7 @@ bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col,
if (TextEditorBase *teb = Object::cast_to<TextEditorBase>(seb)) {
if (p_line >= 0) {
teb->goto_line(p_line, p_col);
teb->goto_line_centered(p_line, p_col);
}
}
@ -3416,7 +3414,9 @@ void ScriptEditor::_update_history_pos(int p_new_pos) {
Node *n = tab_container->get_current_tab_control();
if (Object::cast_to<TextEditorBase>(n)) {
history.write[history_pos].state = Object::cast_to<TextEditorBase>(n)->get_navigation_state();
Dictionary nav_state = Object::cast_to<TextEditorBase>(n)->get_navigation_state();
nav_state["ensure_caret_visible"] = true;
history.write[history_pos].state = nav_state;
}
if (Object::cast_to<EditorHelp>(n)) {
history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();

View file

@ -1159,11 +1159,18 @@ void ScriptTextEditor::_on_caret_moved() {
if (code_editor->is_previewing_navigation_change()) {
return;
}
if (is_layout_pending_in_tree()) {
call_on_all_layout_pending_finished(callable_mp(this, &ScriptTextEditor::_on_caret_moved));
return;
}
// When previous_line < 0, it means the user has just switched to this editor from a different one
// (which already saved a state in the history). In this case, we should not save this editor's previous state.
int current_line = code_editor->get_text_editor()->get_caret_line();
if (Math::abs(current_line - previous_line) >= 10) {
if (previous_line >= 0 && Math::abs(current_line - previous_line) >= 10) {
Dictionary nav_state = get_navigation_state();
nav_state["row"] = previous_line;
nav_state["scroll_position"] = -1;
nav_state["ensure_caret_visible"] = true;
emit_signal(SNAME("request_save_previous_state"), nav_state);
store_previous_state();
}
@ -1886,6 +1893,11 @@ void ScriptTextEditor::_notification(int p_what) {
case NOTIFICATION_DRAG_END: {
drag_info_label->hide();
} break;
case NOTIFICATION_VISIBILITY_CHANGED: {
if (!is_visible()) {
previous_line = -1;
}
} break;
}
}

View file

@ -94,7 +94,7 @@ class ScriptTextEditor : public CodeEditorBase {
Color marked_line_color = Color(1, 1, 1);
Color warning_line_color = Color(1, 1, 1);
Color folded_code_region_color = Color(1, 1, 1);
int previous_line = 0;
int previous_line = -1; // Previous caret line number when user continuously operates in this editor. Affects history state. Reset to -1 on editor switch.
PopupPanel *color_panel = nullptr;
ColorPicker *color_picker = nullptr;