Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion crates/component/src/diff/scroll_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use gpui::{
ScrollEvent, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
};

use super::{Diff, DiffState, document::fixture};
use super::{Diff, DiffLinePosition, DiffSide, DiffState, document::fixture};

/// Every line changed, so the unified patch is two rows per line.
const LINES: usize = 300;
Expand Down Expand Up @@ -246,3 +246,29 @@ fn file_headers_stay_in_the_viewport_under_horizontal_scrolling(cx: &mut TestApp
assert_eq!(header(cx), before, "{mode:?}: the header stays in place");
}
}

#[gpui::test]
fn replacing_files_in_place_keeps_the_line_at_the_top(cx: &mut TestAppContext) {
let (state, cx) = long_patch(cx);
let target = DiffLinePosition::new("long.txt", DiffSide::Modified, 200);
state.update(cx, |state, cx| state.scroll_to_line(target, cx));
draw(cx);
let top = cx.update(|_, cx| state.read(cx).list().logical_scroll_top().item_ix);
assert!(top > 0, "scrolled down to the line");

let original = (0..LINES)
.map(|ix| format!("old {ix}\n"))
.collect::<String>();
let modified = (0..LINES)
.map(|ix| format!("new {ix}\n"))
.collect::<String>();
state.update(cx, |state, cx| {
state.replace_files_in_place([fixture::modified("long.txt", &original, &modified)], cx)
});
draw(cx);
assert_eq!(
cx.update(|_, cx| state.read(cx).list().logical_scroll_top().item_ix),
top,
"the same line is at the top"
);
}
16 changes: 16 additions & 0 deletions crates/component/src/diff/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,22 @@ impl DiffState {
cx.notify();
}

/// Replaces the files like [`Self::set_files`], then scrolls back to the
/// source line that was at the top of the viewport. For the same changes
/// presented differently (more context, whitespace ignored), so the reader
/// keeps their place.
pub fn replace_files_in_place(
&mut self,
files: impl IntoIterator<Item = DiffFile>,
cx: &mut Context<Self>,
) {
let anchor = self.row_position(self.list.logical_scroll_top().item_ix);
self.set_files(files, cx);
if let Some(anchor) = anchor {
self.reveal_line(&anchor);
}
}

/// Changes the display mode while preserving source-line selection and a
/// nearby source row.
pub fn set_mode(&mut self, mode: DiffMode, cx: &mut Context<Self>) {
Expand Down