Unfold folded sections when jumping to location (#65217)

## Summary

When jumping to code locations, which have been folded away, one needs
to manually unfold the section and do the same jump action (e.g. go to
line) again.
With this change, the required sections are unfolded automatically when
jumping, while preserving other folded sections in the same buffer.

Without automatic unfolding:

https://github.com/user-attachments/assets/89017150-80a5-4c70-ba75-5627807a02fe

With automatic unfolding

https://github.com/user-attachments/assets/373498e7-6daf-4ff1-8352-eceb422b37b5



## Testing

To test this change, take any piece of code and fold sections of the
code. Then use go-to-line or jump-to-definition to jump into one of
these sections. All sections leading up to the jumped to line should be
unfolded automatically.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Added automatic unfolding of folded sections when using one of the
following features: go to line, go to
definition/declaration/implementation/type definition, symbol
navigation, outline or project-symbol pickers, multibuffer location
results, and navigation history
This commit is contained in:
Hendrik Müller
2026-10-12 13:47:49 +00:00
committed by GitHub
parent 7eccdcecb5
commit 6802ba72f0
11 changed files with 342 additions and 19 deletions
+12 -1
View File
@@ -1370,13 +1370,14 @@ struct HoveredCursor {
/// in vim mode we never do.
///
/// Similarly, you might want to disable scrolling if you don't want the viewport to
/// move.
/// move. Explicit jumps can opt into revealing folded destinations with [`Self::unfold`].
#[derive(Clone)]
pub struct SelectionEffects {
nav_history: Option<bool>,
completions: bool,
scroll: Option<Autoscroll>,
from_search: bool,
unfold: bool,
}
impl Default for SelectionEffects {
@@ -1386,10 +1387,20 @@ impl Default for SelectionEffects {
completions: true,
scroll: Some(Autoscroll::fit()),
from_search: false,
unfold: false,
}
}
}
impl SelectionEffects {
/// Reveals the selected ranges and their folded buffers before scrolling.
/// Also applies when selections are unchanged; deferred changes reveal the final selections.
pub fn unfold(self) -> Self {
Self {
unfold: true,
..self
}
}
pub fn scroll(scroll: Autoscroll) -> Self {
Self {
scroll: Some(scroll),
+181
View File
@@ -31763,6 +31763,187 @@ async fn test_row_highlights_for_empty_conflict_side_after_edit(
});
}
#[gpui::test]
async fn test_jumps_unfold_enclosing_sections(cx: &mut TestAppContext) {
use workspace::item::Item;
init_test(cx, |_| {});
let mut cx = EditorTestContext::new(cx).await;
cx.set_state("ˇfirst\nouter\ninner\ntarget\nend\nunrelated\nend\nlast");
let outer = Point::new(1, 0)..Point::new(4, 0);
let inner = Point::new(2, 0)..Point::new(3, 6);
let unrelated = Point::new(5, 0)..Point::new(6, 3);
let target = Point::new(3, 2);
for use_history in [false, true] {
for already_selected in [false, true] {
cx.update_editor(|editor, window, cx| {
editor.unfold_ranges(&[Point::zero()..Point::new(7, 4)], false, false, cx);
let initial = if already_selected {
target
} else {
Point::zero()
};
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
selections.select_ranges([initial..initial]);
});
editor.fold_ranges(
vec![outer.clone(), inner.clone(), unrelated.clone()],
false,
window,
cx,
);
let snapshot = editor.display_snapshot(cx);
assert_eq!(
snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.count(),
3
);
if use_history {
let snapshot = editor.buffer.read(cx).snapshot(cx);
assert!(editor.navigate(
Arc::new(NavigationData {
cursor_anchor: snapshot.anchor_before(target),
cursor_position: target,
scroll_anchor: ScrollAnchor {
anchor: snapshot.anchor_before(Point::zero()),
offset: Default::default(),
},
scroll_top_row: 0,
}),
window,
cx,
));
} else {
editor.go_to_singleton_buffer_point(target, window, cx);
}
let snapshot = editor.display_snapshot(cx);
assert_eq!(editor.selections.newest::<Point>(&snapshot).head(), target);
let remaining_folds = snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.map(|fold| {
fold.range.start.to_point(snapshot.buffer_snapshot())
..fold.range.end.to_point(snapshot.buffer_snapshot())
})
.collect::<Vec<_>>();
assert_eq!(remaining_folds, vec![unrelated.clone()]);
});
}
}
cx.update_editor(|editor, window, cx| {
editor.fold_ranges(vec![outer.clone(), inner.clone()], false, window, cx);
editor.change_selections(Default::default(), window, cx, |selections| {
selections.select_ranges([target..target]);
});
let snapshot = editor.display_snapshot(cx);
assert_eq!(
snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.count(),
3
);
editor.go_to_singleton_buffer_point(outer.start, window, cx);
let snapshot = editor.display_snapshot(cx);
assert_eq!(
snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.count(),
3
);
editor.go_to_singleton_buffer_point(outer.end, window, cx);
let snapshot = editor.display_snapshot(cx);
assert_eq!(
snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.count(),
3
);
});
}
#[gpui::test]
async fn test_selection_effects_unfold_opt_in(cx: &mut TestAppContext) {
init_test(cx, |_| {});
let mut cx = EditorTestContext::new(cx).await;
cx.set_state("ˇfirst\nouter\ninner\ntarget\nend\nunrelated\nend\nlast");
let target = Point::new(3, 2);
let unrelated = Point::new(5, 0)..Point::new(6, 3);
let folds = vec![
Point::new(1, 0)..Point::new(4, 0),
Point::new(2, 0)..Point::new(3, 6),
unrelated.clone(),
];
for deferred in [false, true] {
cx.update_editor(|editor, window, cx| {
editor.unfold_ranges(&[Point::zero()..Point::new(7, 4)], false, false, cx);
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
selections.select_ranges([target..target]);
});
editor.fold_ranges(folds.clone(), false, window, cx);
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |_| {});
assert_eq!(
editor
.display_snapshot(cx)
.folds_in_range(target..target)
.count(),
2
);
if deferred {
editor.with_selection_effects_deferred(window, cx, |editor, window, cx| {
editor.change_selections(
SelectionEffects::no_scroll().unfold(),
window,
cx,
|selections| selections.select_ranges([Point::zero()..Point::zero()]),
);
editor.change_selections(
SelectionEffects::no_scroll(),
window,
cx,
|selections| {
let snapshot = selections.display_snapshot();
let anchor = snapshot.buffer_snapshot().anchor_before(target);
selections.select_anchor_ranges([anchor..anchor]);
},
);
assert_eq!(
editor
.display_snapshot(cx)
.folds_in_range(target..target)
.count(),
2
);
});
} else {
editor.change_selections(
SelectionEffects::no_scroll().unfold(),
window,
cx,
|_| {},
);
}
let snapshot = editor.display_snapshot(cx);
let remaining_folds = snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.map(|fold| {
fold.range.start.to_point(snapshot.buffer_snapshot())
..fold.range.end.to_point(snapshot.buffer_snapshot())
})
.collect::<Vec<_>>();
assert_eq!(remaining_folds, vec![unrelated.clone()]);
assert_eq!(editor.selections.newest::<Point>(&snapshot).head(), target);
});
}
}
#[gpui::test]
async fn test_go_to_singleton_buffer_point_with_expanded_deleted_hunks(
executor: BackgroundExecutor,
+5
View File
@@ -805,6 +805,11 @@ impl Editor {
let buffer_ids: HashSet<BufferId> = self
.selections
.disjoint_anchor_ranges()
.chain(
self.selections
.pending_anchor()
.map(|selection| selection.range()),
)
.flat_map(|range| snapshot.buffer_ids_for_range(range))
.collect();
for buffer_id in buffer_ids {
+14 -2
View File
@@ -664,6 +664,7 @@ impl Item for Editor {
} else {
buffer.clip_point(data.cursor_position, Bias::Left)
};
let destination_anchor = buffer.anchor_before(offset);
let mut scroll_anchor = data.scroll_anchor;
if !buffer.can_resolve(&scroll_anchor.anchor) {
@@ -674,12 +675,23 @@ impl Item for Editor {
drop(buffer);
if newest_selection.head() == offset {
let was_folded = |cx: &mut Context<Self>| {
self.display_snapshot(cx)
.folds_in_range(offset..offset)
.next()
.is_some()
};
let buffer_was_folded = |cx: &Context<Self>| {
destination_anchor
.buffer_id()
.is_some_and(|buffer_id| self.is_buffer_folded(buffer_id, cx))
};
if newest_selection.head() == offset && !was_folded(cx) && !buffer_was_folded(cx) {
false
} else {
self.set_scroll_anchor(scroll_anchor, window, cx);
self.change_selections(
SelectionEffects::default().nav_history(false),
SelectionEffects::default().nav_history(false).unfold(),
window,
cx,
|s| s.select_ranges([offset..offset]),
+21 -9
View File
@@ -1686,6 +1686,10 @@ impl Editor {
window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(buffer_id) = position.buffer_id() {
self.unfold_buffer(buffer_id, cx);
}
self.unfold_ranges(&[position..position], false, false, cx);
let snapshot = self.snapshot(window, cx).display_snapshot;
let position = position.to_point(&snapshot.buffer_snapshot());
let start = snapshot
@@ -1926,7 +1930,8 @@ impl Editor {
editor.cursor_top_offset(cx),
cx,
))
.nav_history(true),
.nav_history(true)
.unfold(),
window,
cx,
|s| s.select_anchor_ranges(target_ranges),
@@ -2013,7 +2018,8 @@ impl Editor {
SelectionEffects::scroll(Autoscroll::for_go_to_definition(
offset, cx,
))
.nav_history(true),
.nav_history(true)
.unfold(),
window,
cx,
|s| s.select_anchor_ranges(target_ranges),
@@ -2137,7 +2143,7 @@ impl Editor {
editor
.update(acx, |editor, ecx| {
editor.change_selections(
SelectionEffects::scroll(Autoscroll::newest()),
SelectionEffects::scroll(Autoscroll::newest()).unfold(),
window,
ecx,
|s| s.select_ranges(selection),
@@ -2270,7 +2276,7 @@ impl Editor {
MultibufferSelectionMode::First => {
if let Some(first_range) = ranges.first() {
editor.change_selections(
SelectionEffects::no_scroll(),
SelectionEffects::no_scroll().unfold(),
window,
cx,
|selections| {
@@ -2287,10 +2293,15 @@ impl Editor {
);
}
MultibufferSelectionMode::All => {
editor.change_selections(SelectionEffects::no_scroll(), window, cx, |selections| {
selections.clear_disjoint();
selections.select_anchor_ranges(ranges);
});
editor.change_selections(
SelectionEffects::no_scroll().unfold(),
window,
cx,
|selections| {
selections.clear_disjoint();
selections.select_anchor_ranges(ranges);
},
);
}
});
@@ -2487,7 +2498,8 @@ impl Editor {
self.cursor_top_offset(cx),
cx,
))
.nav_history(record_nav_history),
.nav_history(record_nav_history)
.unfold(),
window,
cx,
|s| s.select_anchor_ranges([start..end]),
+31
View File
@@ -1778,13 +1778,31 @@ impl Editor {
return;
}
if state.effects.unfold {
self.unfold_buffers_with_selections(cx);
let ranges = self
.selections
.disjoint_anchor_ranges()
.chain(
self.selections
.pending_anchor()
.map(|selection| selection.range()),
)
.collect::<Vec<_>>();
self.unfold_ranges(&ranges, false, false, cx);
}
if state.changed {
self.selection_history.push(state.history_entry);
}
if state.changed || state.effects.unfold {
if let Some(autoscroll) = state.effects.scroll {
self.request_autoscroll(autoscroll, cx);
}
}
if state.changed {
let old_cursor_position = &state.old_cursor_position;
self.selections_did_change(true, old_cursor_position, state.effects, window, cx);
@@ -2085,12 +2103,25 @@ impl Editor {
change: impl FnOnce(&mut MutableSelectionsCollection<'_, '_>) -> R,
) -> R {
let snapshot = self.display_snapshot(cx);
let unfold = effects.unfold
|| self
.deferred_selection_effects_state
.as_ref()
.is_some_and(|state| state.effects.unfold);
let change = |selections: &mut MutableSelectionsCollection<'_, '_>| {
if unfold {
// Folded display coordinates would otherwise move anchor-based jump destinations.
selections.preserve_buffer_positions();
}
change(selections)
};
if self.deferred_selection_effects_state.is_some() {
let (changed, result) = self.change_selections_without_effects(&snapshot, change);
if let Some(state) = &mut self.deferred_selection_effects_state {
state.effects.scroll = effects.scroll.or(state.effects.scroll);
state.effects.completions = effects.completions;
state.effects.nav_history = effects.nav_history.or(state.effects.nav_history);
state.effects.unfold |= effects.unfold;
state.changed |= changed;
}
return result;
+12 -3
View File
@@ -601,6 +601,7 @@ impl SelectionsCollection {
snapshot,
collection: self,
selections_changed: false,
preserve_buffer_positions: false,
};
let result = change(&mut mutable_collection);
@@ -690,6 +691,7 @@ pub struct MutableSelectionsCollection<'snap, 'a> {
collection: &'a mut SelectionsCollection,
snapshot: &'snap DisplaySnapshot,
selections_changed: bool,
preserve_buffer_positions: bool,
}
impl<'snap, 'a> fmt::Debug for MutableSelectionsCollection<'snap, 'a> {
@@ -702,6 +704,10 @@ impl<'snap, 'a> fmt::Debug for MutableSelectionsCollection<'snap, 'a> {
}
impl<'snap, 'a> MutableSelectionsCollection<'snap, 'a> {
pub(crate) fn preserve_buffer_positions(&mut self) {
self.preserve_buffer_positions = true;
}
pub fn display_snapshot(&self) -> DisplaySnapshot {
self.snapshot.clone()
}
@@ -903,9 +909,12 @@ impl<'snap, 'a> MutableSelectionsCollection<'snap, 'a> {
pub fn select_anchors(&mut self, selections: Vec<Selection<Anchor>>) {
let map = self.display_snapshot();
let resolved_selections =
resolve_selections_wrapping_blocks::<MultiBufferOffset, _>(&selections, &map)
.collect::<Vec<_>>();
let resolved_selections = resolve_selections::<MultiBufferOffset, _>(
&selections,
&map,
!self.preserve_buffer_positions,
)
.collect::<Vec<_>>();
self.select(resolved_selections);
}
+63 -1
View File
@@ -291,7 +291,7 @@ impl GoToLine {
return;
};
editor.change_selections(
SelectionEffects::scroll(Autoscroll::center()),
SelectionEffects::scroll(Autoscroll::center()).unfold(),
window,
cx,
|s| s.select_anchor_ranges([start..start]),
@@ -474,6 +474,52 @@ mod tests {
assert_single_caret_at_row(&editor, expected_highlighted_row, cx);
}
#[gpui::test]
async fn test_go_to_line_unfolds_enclosing_sections(cx: &mut TestAppContext) {
init_test(cx);
let cx = cx.add_empty_window();
let buffer = cx
.new(|cx| Buffer::local("first\nouter\ninner\ntarget\nend\nunrelated\nend\nlast", cx));
let editor =
cx.new_window_entity(|window, cx| Editor::for_buffer(buffer.clone(), None, window, cx));
editor.update_in(cx, |editor, window, cx| {
editor.fold_ranges(
vec![
Point::new(1, 0)..Point::new(4, 0),
Point::new(2, 0)..Point::new(3, 6),
Point::new(5, 0)..Point::new(6, 3),
],
false,
window,
cx,
);
});
let go_to_line_view = cx.new_window_entity(|window, cx| {
GoToLine::new(editor.clone(), buffer.clone(), window, cx)
});
go_to_line_view.update_in(cx, |go_to_line_view, window, cx| {
go_to_line_view.line_editor.update(cx, |line_editor, cx| {
line_editor.set_text("4:3", window, cx);
});
go_to_line_view.confirm(&menu::Confirm, window, cx);
});
editor.update(cx, |editor, cx| {
let snapshot = editor.display_snapshot(cx);
assert_eq!(
editor.selections.newest::<Point>(&snapshot).head(),
Point::new(3, 2)
);
let remaining_folds = snapshot
.folds_in_range(Point::zero()..Point::new(7, 4))
.map(|fold| {
fold.range.start.to_point(snapshot.buffer_snapshot())
..fold.range.end.to_point(snapshot.buffer_snapshot())
})
.collect::<Vec<_>>();
assert_eq!(remaining_folds, vec![Point::new(5, 0)..Point::new(6, 3)]);
});
}
#[gpui::test]
async fn test_go_to_line_uses_buffer_rows_in_multibuffers(cx: &mut TestAppContext) {
init_test(cx);
@@ -504,6 +550,17 @@ mod tests {
GoToLine::new(editor.clone(), buffer.clone(), window, cx)
});
editor.update_in(cx, |editor, window, cx| {
let multibuffer = editor.buffer().read(cx);
let start = multibuffer
.buffer_point_to_anchor(&buffer, Point::new(50, 0), cx)
.expect("target excerpt start");
let end = multibuffer
.buffer_point_to_anchor(&buffer, Point::new(52, 6), cx)
.expect("target excerpt end");
editor.fold_ranges(vec![start..end], false, window, cx);
editor.fold_buffer(buffer.read(cx).remote_id(), cx);
});
go_to_line_view.update_in(cx, |go_to_line_view, window, cx| {
go_to_line_view.line_editor.update(cx, |line_editor, cx| {
line_editor.set_text("52", window, cx);
@@ -511,6 +568,11 @@ mod tests {
go_to_line_view.confirm(&menu::Confirm, window, cx);
});
assert_single_caret_at_buffer_row(&editor, 51, cx);
editor.update(cx, |editor, cx| {
assert!(!editor.is_buffer_folded(buffer.read(cx).remote_id(), cx));
let snapshot = editor.display_snapshot(cx);
assert!(!snapshot.intersects_fold(editor.selections.newest_anchor().head()));
});
go_to_line_view.update_in(cx, |go_to_line_view, window, cx| {
go_to_line_view.line_editor.update(cx, |line_editor, cx| {
+1 -1
View File
@@ -414,7 +414,7 @@ impl PickerDelegate for OutlineViewDelegate {
.next();
if let Some((rows, _)) = highlight {
active_editor.change_selections(
SelectionEffects::scroll(Autoscroll::center()),
SelectionEffects::scroll(Autoscroll::center()).unfold(),
window,
cx,
|s| s.select_ranges([rows.start..rows.start]),
+1 -1
View File
@@ -1538,7 +1538,7 @@ impl OutlinePanel {
if change_selection {
active_editor.update(cx, |editor, cx| {
editor.change_selections(
SelectionEffects::scroll(Autoscroll::center()),
SelectionEffects::scroll(Autoscroll::center()).unfold(),
window,
cx,
|s| s.select_ranges(Some(anchor..anchor)),
@@ -162,7 +162,7 @@ impl PickerDelegate for ProjectSymbolsDelegate {
return;
};
editor.change_selections(
SelectionEffects::scroll(Autoscroll::center()),
SelectionEffects::scroll(Autoscroll::center()).unfold(),
window,
cx,
|s| s.select_ranges([anchor..anchor]),