[WIP] editor: Implement next/prev reference (#41078)

Co-authored-by: Cole <cole@zed.dev>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
This commit is contained in:
Cameron Mcloughlin
2025-10-28 20:41:44 +00:00
committed by GitHub
co-authored by Cole Conrad Irwin
parent b75736568b
commit 5e7927f628
7 changed files with 313 additions and 0 deletions
+133
View File
@@ -16686,6 +16686,139 @@ impl Editor {
})
}
fn go_to_next_reference(
&mut self,
_: &GoToNextReference,
window: &mut Window,
cx: &mut Context<Self>,
) {
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<Self>,
) {
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<Self>,
) -> Option<Task<Result<()>>> {
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::<Vec<_>>();
// 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,