From 9505d6cdcfcafd935de9467367f08fc3ed6199fe Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 20 Jan 2022 21:33:16 -0700 Subject: [PATCH] Disable the nav history when selecting a definition in a different buffer When jumping between different buffers, we don't care about the cursor's previous location. When navigating backward, we want to jump directly to the site of the jump. --- crates/editor/src/editor.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index dc1df79b9165658a2adf41868f398cc243af7f88..0c393d93ab68420585ec9da62b81f00f59031271 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -3000,7 +3000,7 @@ impl Editor { cx: &mut ViewContext, ) { let active_item = workspace.active_item(cx); - let editor = if let Some(editor) = active_item + let editor_handle = if let Some(editor) = active_item .as_ref() .and_then(|item| item.act_as::(cx)) { @@ -3009,7 +3009,7 @@ impl Editor { return; }; - let editor = editor.read(cx); + let editor = editor_handle.read(cx); let buffer = editor.buffer.read(cx); let head = editor.newest_selection::(&buffer.read(cx)).head(); let (buffer, head) = editor.buffer.read(cx).text_anchor_for_position(head, cx); @@ -3023,12 +3023,23 @@ impl Editor { let range = definition .target_range .to_offset(definition.target_buffer.read(cx)); - let target_editor = workspace + let target_editor_handle = workspace .open_item(BufferItemHandle(definition.target_buffer), cx) .downcast::() .unwrap(); - target_editor.update(cx, |target_editor, cx| { + + target_editor_handle.update(cx, |target_editor, cx| { + // When selecting a definition in a different buffer, disable the nav history + // to avoid creating a history entry at the previous cursor location. + let disabled_history = if editor_handle == target_editor_handle { + None + } else { + target_editor.nav_history.take() + }; target_editor.select_ranges([range], Some(Autoscroll::Center), cx); + if disabled_history.is_some() { + target_editor.nav_history = disabled_history; + } }); } });