diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 3839a9bb685..dc7cdad5f87 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -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, completions: bool, scroll: Option, 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), diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 5e255697cfc..94fe9821d0b 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -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::(&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::>(); + 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::>(); + assert_eq!(remaining_folds, vec![unrelated.clone()]); + assert_eq!(editor.selections.newest::(&snapshot).head(), target); + }); + } +} + #[gpui::test] async fn test_go_to_singleton_buffer_point_with_expanded_deleted_hunks( executor: BackgroundExecutor, diff --git a/crates/editor/src/fold.rs b/crates/editor/src/fold.rs index cd7437d01e5..35b6be912de 100644 --- a/crates/editor/src/fold.rs +++ b/crates/editor/src/fold.rs @@ -805,6 +805,11 @@ impl Editor { let buffer_ids: HashSet = 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 { diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 3c6d585d17f..6dff2b30ae8 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -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.display_snapshot(cx) + .folds_in_range(offset..offset) + .next() + .is_some() + }; + let buffer_was_folded = |cx: &Context| { + 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]), diff --git a/crates/editor/src/navigation.rs b/crates/editor/src/navigation.rs index a84ce2b0fe1..ebc3df466e0 100644 --- a/crates/editor/src/navigation.rs +++ b/crates/editor/src/navigation.rs @@ -1686,6 +1686,10 @@ impl Editor { window: &mut Window, cx: &mut Context, ) { + 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]), diff --git a/crates/editor/src/selection.rs b/crates/editor/src/selection.rs index 97ca4b49a8e..7e8990ab719 100644 --- a/crates/editor/src/selection.rs +++ b/crates/editor/src/selection.rs @@ -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::>(); + 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; diff --git a/crates/editor/src/selections_collection.rs b/crates/editor/src/selections_collection.rs index f358bb6b781..2c1b0ad2613 100644 --- a/crates/editor/src/selections_collection.rs +++ b/crates/editor/src/selections_collection.rs @@ -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>) { let map = self.display_snapshot(); - let resolved_selections = - resolve_selections_wrapping_blocks::(&selections, &map) - .collect::>(); + let resolved_selections = resolve_selections::( + &selections, + &map, + !self.preserve_buffer_positions, + ) + .collect::>(); self.select(resolved_selections); } diff --git a/crates/go_to_line/src/go_to_line.rs b/crates/go_to_line/src/go_to_line.rs index 48017a6e6a6..439929d8fb6 100644 --- a/crates/go_to_line/src/go_to_line.rs +++ b/crates/go_to_line/src/go_to_line.rs @@ -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::(&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::>(); + 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| { diff --git a/crates/outline/src/outline.rs b/crates/outline/src/outline.rs index d11a710892d..4151fb555a4 100644 --- a/crates/outline/src/outline.rs +++ b/crates/outline/src/outline.rs @@ -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]), diff --git a/crates/outline_panel/src/outline_panel.rs b/crates/outline_panel/src/outline_panel.rs index 861d506da94..fb0936c5544 100644 --- a/crates/outline_panel/src/outline_panel.rs +++ b/crates/outline_panel/src/outline_panel.rs @@ -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)), diff --git a/crates/project_symbols/src/project_symbols.rs b/crates/project_symbols/src/project_symbols.rs index 9d5416c1b8c..e835a9b819f 100644 --- a/crates/project_symbols/src/project_symbols.rs +++ b/crates/project_symbols/src/project_symbols.rs @@ -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]),