From 5e7927f6284833fa2a2be98376754d5385d33bdd Mon Sep 17 00:00:00 2001 From: Cameron Mcloughlin Date: Tue, 28 Oct 2025 20:41:44 +0000 Subject: [PATCH] [WIP] editor: Implement next/prev reference (#41078) Co-authored-by: Cole Co-authored-by: Conrad Irwin --- assets/keymaps/vim.json | 2 + crates/editor/src/actions.rs | 4 + crates/editor/src/editor.rs | 133 ++++++++++++++++++++++++ crates/editor/src/editor_tests.rs | 120 +++++++++++++++++++++ crates/editor/src/element.rs | 2 + crates/multi_buffer/src/multi_buffer.rs | 18 ++++ crates/vim/src/normal.rs | 34 ++++++ 7 files changed, 313 insertions(+) diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json index 8382fd0653aec7e80c722c3c759588860fab6c9f..d6bdff1cd02fcd0bfb31fb48d2c47a321c54de2c 100644 --- a/assets/keymaps/vim.json +++ b/assets/keymaps/vim.json @@ -220,6 +220,8 @@ "[ {": ["vim::UnmatchedBackward", { "char": "{" }], "] )": ["vim::UnmatchedForward", { "char": ")" }], "[ (": ["vim::UnmatchedBackward", { "char": "(" }], + "[ r": "vim::GoToPreviousReference", + "] r": "vim::GoToNextReference", // tree-sitter related commands "[ x": "vim::SelectLargerSyntaxNode", "] x": "vim::SelectSmallerSyntaxNode" diff --git a/crates/editor/src/actions.rs b/crates/editor/src/actions.rs index 810b84efcd40de6e507dfe12b1a1a7f89d2ec4cf..38ae42c3814fa09e50a92dcc20f0a34bad82ea40 100644 --- a/crates/editor/src/actions.rs +++ b/crates/editor/src/actions.rs @@ -539,6 +539,10 @@ actions!( GoToParentModule, /// Goes to the previous change in the file. GoToPreviousChange, + /// Goes to the next reference to the symbol under the cursor. + GoToNextReference, + /// Goes to the previous reference to the symbol under the cursor. + GoToPreviousReference, /// Goes to the type definition of the symbol at cursor. GoToTypeDefinition, /// Goes to type definition in a split pane. diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 26249db9dcdd196136147c63c4b83bfc4a703192..77fadacfb12f08732d63f652164dd709724dc59b 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -16686,6 +16686,139 @@ impl Editor { }) } + fn go_to_next_reference( + &mut self, + _: &GoToNextReference, + window: &mut Window, + cx: &mut Context, + ) { + let task = self.go_to_reference_before_or_after_position(Direction::Next, 1, window, cx); + if let Some(task) = task { + task.detach(); + }; + } + + fn go_to_prev_reference( + &mut self, + _: &GoToPreviousReference, + window: &mut Window, + cx: &mut Context, + ) { + let task = self.go_to_reference_before_or_after_position(Direction::Prev, 1, window, cx); + if let Some(task) = task { + task.detach(); + }; + } + + pub fn go_to_reference_before_or_after_position( + &mut self, + direction: Direction, + count: usize, + window: &mut Window, + cx: &mut Context, + ) -> Option>> { + let selection = self.selections.newest_anchor(); + let head = selection.head(); + + let multi_buffer = self.buffer.read(cx); + + let (buffer, text_head) = multi_buffer.text_anchor_for_position(head, cx)?; + let workspace = self.workspace()?; + let project = workspace.read(cx).project().clone(); + let references = + project.update(cx, |project, cx| project.references(&buffer, text_head, cx)); + Some(cx.spawn_in(window, async move |editor, cx| -> Result<()> { + let Some(locations) = references.await? else { + return Ok(()); + }; + + if locations.is_empty() { + // totally normal - the cursor may be on something which is not + // a symbol (e.g. a keyword) + log::info!("no references found under cursor"); + return Ok(()); + } + + let multi_buffer = editor.read_with(cx, |editor, _| editor.buffer().clone())?; + + let multi_buffer_snapshot = + multi_buffer.read_with(cx, |multi_buffer, cx| multi_buffer.snapshot(cx))?; + + let (locations, current_location_index) = + multi_buffer.update(cx, |multi_buffer, cx| { + let mut locations = locations + .into_iter() + .filter_map(|loc| { + let start = multi_buffer.buffer_anchor_to_anchor( + &loc.buffer, + loc.range.start, + cx, + )?; + let end = multi_buffer.buffer_anchor_to_anchor( + &loc.buffer, + loc.range.end, + cx, + )?; + Some(start..end) + }) + .collect::>(); + + // There is an O(n) implementation, but given this list will be + // small (usually <100 items), the extra O(log(n)) factor isn't + // worth the (surprisingly large amount of) extra complexity. + locations + .sort_unstable_by(|l, r| l.start.cmp(&r.start, &multi_buffer_snapshot)); + + let head_offset = head.to_offset(&multi_buffer_snapshot); + + let current_location_index = locations.iter().position(|loc| { + loc.start.to_offset(&multi_buffer_snapshot) <= head_offset + && loc.end.to_offset(&multi_buffer_snapshot) >= head_offset + }); + + (locations, current_location_index) + })?; + + let Some(current_location_index) = current_location_index else { + // This indicates something has gone wrong, because we already + // handle the "no references" case above + log::error!( + "failed to find current reference under cursor. Total references: {}", + locations.len() + ); + return Ok(()); + }; + + let destination_location_index = match direction { + Direction::Next => (current_location_index + count) % locations.len(), + Direction::Prev => { + (current_location_index + locations.len() - count % locations.len()) + % locations.len() + } + }; + + // TODO(cameron): is this needed? + // the thinking is to avoid "jumping to the current location" (avoid + // polluting "jumplist" in vim terms) + if current_location_index == destination_location_index { + return Ok(()); + } + + let Range { start, end } = locations[destination_location_index]; + + editor.update_in(cx, |editor, window, cx| { + let effects = SelectionEffects::default(); + + editor.unfold_ranges(&[start..end], false, false, cx); + editor.change_selections(effects, window, cx, |s| { + s.select_ranges([start..start]); + }); + })?; + + Ok(()) + })) + } + pub fn find_all_references( &mut self, _: &FindAllReferences, diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index ddb9cbd3b35bfde6a68ba7884ef626e2c84d9436..1d277b8b99b5f60f02b450dcc06997b15cd37184 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -26859,3 +26859,123 @@ async fn test_end_of_editor_context(cx: &mut TestAppContext) { assert!(!e.key_context(window, cx).contains("end_of_input")); }); } + +#[gpui::test] +async fn test_next_prev_reference(cx: &mut TestAppContext) { + const CYCLE_POSITIONS: &[&'static str] = &[ + indoc! {" + fn foo() { + let ˇabc = 123; + let x = abc + 1; + let y = abc + 2; + let z = abc + 2; + } + "}, + indoc! {" + fn foo() { + let abc = 123; + let x = ˇabc + 1; + let y = abc + 2; + let z = abc + 2; + } + "}, + indoc! {" + fn foo() { + let abc = 123; + let x = abc + 1; + let y = ˇabc + 2; + let z = abc + 2; + } + "}, + indoc! {" + fn foo() { + let abc = 123; + let x = abc + 1; + let y = abc + 2; + let z = ˇabc + 2; + } + "}, + ]; + + init_test(cx, |_| {}); + + let mut cx = EditorLspTestContext::new_rust( + lsp::ServerCapabilities { + references_provider: Some(lsp::OneOf::Left(true)), + ..Default::default() + }, + cx, + ) + .await; + + // importantly, the cursor is in the middle + cx.set_state(indoc! {" + fn foo() { + let aˇbc = 123; + let x = abc + 1; + let y = abc + 2; + let z = abc + 2; + } + "}); + + let reference_ranges = [ + lsp::Position::new(1, 8), + lsp::Position::new(2, 12), + lsp::Position::new(3, 12), + lsp::Position::new(4, 12), + ] + .map(|start| lsp::Range::new(start, lsp::Position::new(start.line, start.character + 3))); + + cx.lsp + .set_request_handler::(move |params, _cx| async move { + Ok(Some( + reference_ranges + .map(|range| lsp::Location { + uri: params.text_document_position.text_document.uri.clone(), + range, + }) + .to_vec(), + )) + }); + + let _move = async |direction, count, cx: &mut EditorLspTestContext| { + cx.update_editor(|editor, window, cx| { + editor.go_to_reference_before_or_after_position(direction, count, window, cx) + }) + .unwrap() + .await + .unwrap() + }; + + _move(Direction::Next, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[1]); + + _move(Direction::Next, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[2]); + + _move(Direction::Next, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[3]); + + // loops back to the start + _move(Direction::Next, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[0]); + + // loops back to the end + _move(Direction::Prev, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[3]); + + _move(Direction::Prev, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[2]); + + _move(Direction::Prev, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[1]); + + _move(Direction::Prev, 1, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[0]); + + _move(Direction::Next, 3, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[3]); + + _move(Direction::Prev, 2, &mut cx).await; + cx.assert_editor_state(CYCLE_POSITIONS[1]); +} diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index fd4795f40ff3857e68a6c0a71c138bc737c4f90f..9cade5dc671a4cb52ef7c7f1cacc7da7dcb79109 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -495,6 +495,8 @@ impl EditorElement { register_action(editor, window, Editor::collapse_all_diff_hunks); register_action(editor, window, Editor::go_to_previous_change); register_action(editor, window, Editor::go_to_next_change); + register_action(editor, window, Editor::go_to_prev_reference); + register_action(editor, window, Editor::go_to_next_reference); register_action(editor, window, |editor, action, window, cx| { if let Some(task) = editor.format(action, window, cx) { diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index 8bd9ca8e78b468965c943e631742e57720ae7b20..90f1bcbe39468fcfa390ce8175414451ddb3b2c7 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -1541,6 +1541,24 @@ impl MultiBuffer { }) } + pub fn buffer_anchor_to_anchor( + &self, + buffer: &Entity, + anchor: text::Anchor, + cx: &App, + ) -> Option { + let snapshot = buffer.read(cx).snapshot(); + for (excerpt_id, range) in self.excerpts_for_buffer(snapshot.remote_id(), cx) { + if range.context.start.cmp(&anchor, &snapshot).is_le() + && range.context.end.cmp(&anchor, &snapshot).is_ge() + { + return Some(Anchor::in_buffer(excerpt_id, snapshot.remote_id(), anchor)); + } + } + + None + } + pub fn remove_excerpts( &mut self, excerpt_ids: impl IntoIterator, diff --git a/crates/vim/src/normal.rs b/crates/vim/src/normal.rs index f80f9be38edbb7fafb0864437c8de2bda4740154..739b40124181044326144c85897cf7e1d7536d5c 100644 --- a/crates/vim/src/normal.rs +++ b/crates/vim/src/normal.rs @@ -100,6 +100,10 @@ actions!( GoToTab, /// Go to previous tab page (with count support). GoToPreviousTab, + /// Go to tab page (with count support). + GoToPreviousReference, + /// Go to previous tab page (with count support). + GoToNextReference, ] ); @@ -202,6 +206,36 @@ pub(crate) fn register(editor: &mut Editor, cx: &mut Context) { vim.join_lines_impl(false, window, cx); }); + Vim::action(editor, cx, |vim, _: &GoToPreviousReference, window, cx| { + let count = Vim::take_count(cx); + vim.update_editor(cx, |_, editor, cx| { + let task = editor.go_to_reference_before_or_after_position( + editor::Direction::Prev, + count.unwrap_or(1), + window, + cx, + ); + if let Some(task) = task { + task.detach_and_log_err(cx); + }; + }); + }); + + Vim::action(editor, cx, |vim, _: &GoToNextReference, window, cx| { + let count = Vim::take_count(cx); + vim.update_editor(cx, |_, editor, cx| { + let task = editor.go_to_reference_before_or_after_position( + editor::Direction::Next, + count.unwrap_or(1), + window, + cx, + ); + if let Some(task) = task { + task.detach_and_log_err(cx); + }; + }); + }); + Vim::action(editor, cx, |vim, _: &Undo, window, cx| { let times = Vim::take_count(cx); Vim::take_forced_motion(cx);