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
27 changes: 26 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,28 @@ 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 a_line_scrolled_to_has_bounds_inside_the_viewport_and_names_the_top_file(
cx: &mut TestAppContext,
) {
let (state, cx) = long_patch(cx);
let target = DiffLinePosition::new("long.txt", DiffSide::Modified, 200);
assert!(
cx.update(|_, cx| state.read(cx).line_bounds(&target))
.is_none(),
"not laid out before it's scrolled to"
);
state.update(cx, |state, cx| state.scroll_to_line(target.clone(), cx));
draw(cx);
let (line, viewport) = cx.update(|_, cx| {
let state = state.read(cx);
(state.line_bounds(&target), state.viewport_bounds())
});
let line = line.expect("laid out once scrolled to");
assert!(viewport.contains(&line.origin));
assert_eq!(
cx.update(|_, cx| state.read(cx).top_file()).as_deref(),
Some("long.txt")
);
}
34 changes: 30 additions & 4 deletions crates/component/src/diff/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::{
};

use gpui::{
App, AppContext as _, Context, ElementId, EventEmitter, FocusHandle, Focusable, HighlightStyle,
ListAlignment, ListOffset, ListState, Pixels, ScrollHandle, SharedString, Size, Subscription,
Task, Window, WindowId, point, px,
App, AppContext as _, Bounds, Context, ElementId, EventEmitter, FocusHandle, Focusable,
HighlightStyle, ListAlignment, ListOffset, ListState, Pixels, ScrollHandle, SharedString, Size,
Subscription, Task, Window, WindowId, point, px,
};
use gpui_base::{TextSelection, TextSelectionContentKey, TextSelectionEvent, TextSelectionHandle};

Expand Down Expand Up @@ -340,6 +340,13 @@ impl DiffState {
cx,
);
let files = files.into_iter().collect::<Vec<_>>();
// Observers are told when the diff scrolls, so content an application
// places beside its lines can follow them.
let list = ListState::new(0, ListAlignment::Top, px(0.));
let scrolled = cx.entity().downgrade();
list.set_scroll_handler(move |_, _, cx| {
let _ = scrolled.update(cx, |_, cx| cx.notify());
});
let mut state = Self {
expanded: vec![Vec::new(); files.len()],
presentations: vec![None; files.len()],
Expand All @@ -355,7 +362,7 @@ impl DiffState {
header_rows: Vec::new(),
rows: Rc::default(),
change_rows: Vec::new(),
list: ListState::new(0, ListAlignment::Top, px(0.)),
list,
horizontal_scroll: ScrollHandle::new(),
focus,
selection,
Expand Down Expand Up @@ -864,6 +871,25 @@ impl DiffState {
.to_owned()
}

/// Where a source line is laid out now, in window coordinates, for
/// placing application content beside it. `None` when it isn't laid out:
/// outside the rendered range, folded, or in a collapsed file.
pub fn line_bounds(&self, position: &DiffLinePosition) -> Option<Bounds<Pixels>> {
self.list.bounds_for_item(self.source_row(position)?)
}

/// The part of the diff on screen, in window coordinates, to keep placed
/// content inside it.
pub fn viewport_bounds(&self) -> Bounds<Pixels> {
self.list.viewport_bounds()
}

/// The path of the file at the top of the viewport: the one being read.
pub fn top_file(&self) -> Option<SharedString> {
let row = self.rows.get(self.list.logical_scroll_top().item_ix)?;
Some(self.files.get(row.file())?.path().clone())
}

/// Reveals and scrolls to a source line, expanding its file and hidden
/// context if needed.
pub fn scroll_to_line(&mut self, position: DiffLinePosition, cx: &mut Context<Self>) {
Expand Down