Visual Studio Code, rebuilt in Rust
Rust
fix(explorer): reveal the opened file below the sticky rows
rokarturfebf6341d255
diff --git a/native/crates/app/src/list_tree.rs b/native/crates/app/src/list_tree.rsindex 238fdc1de55..95bf82f4091 100644--- a/native/crates/app/src/list_tree.rs+++ b/native/crates/app/src/list_tree.rs@@ -46,6 +46,19 @@ pub fn reveal_scroll_top(list: ListScroll, index: usize, center: bool) -> OptionSome(scroll_top.clamp(0., list.max_scroll_top()))}+/// listWidget.ts `getRelativeTop` is not null: the whole row shows below the top `padding_top` of the view.+pub fn shows_row(list: ListScroll, index: usize, padding_top: f32) -> bool {+ let top = list.row_height * index as f32;+ top >= list.scroll_top + padding_top && top + list.row_height <= list.scroll_top + list.viewport+}++/// listWidget.ts `reveal` with `relativeTop` 0.5: the scroll top that puts the row in the middle of the view below+/// its top `padding_top`.+pub fn center_scroll_top(list: ListScroll, index: usize, padding_top: f32) -> f32 {+ let m = list.row_height - list.viewport + padding_top;+ (m * 0.5 + list.row_height * index as f32 - padding_top).clamp(0., list.max_scroll_top())+}+/// listWidget.ts `focusNextPage` and `focusPreviousPage`: the last (first) row of the page, else one page on. Gives/// the row to focus and the new scroll top, none when the list cannot move.pub fn page_focus(list: ListScroll, down: bool, focus: Option<usize>) -> Option<(usize, f32)> {diff --git a/native/crates/app/src/workbench.rs b/native/crates/app/src/workbench.rsindex d372338d96a..caaf76668d1 100644--- a/native/crates/app/src/workbench.rs+++ b/native/crates/app/src/workbench.rs@@ -1864,6 +1864,15 @@ impl Workbench {// abstractTree.ts `AbstractFindController`: its 30px paddingTop keeps the sticky rows away while find is open.let sticky = if self.explorer.find.is_open() { Vec::new() } else { self.tree_sticky_rows(list_scroll::ListId::Explorer, &tree_rows.iter().map(|row| row.depth).collect::<Vec<_>>()) };let sticky_rows: Vec<_> = sticky.iter().map(|sticky| (sticky.index, tree_rows[sticky.index].clone())).collect();+ if self.explorer.reveal_after_layout {+ // Runs after this draw laid the tree out.+ cx.defer_in(window, |workbench, _, cx| {+ if workbench.explorer.scroll.bounds().size.height > px(0.) && std::mem::take(&mut workbench.explorer.reveal_after_layout) {+ workbench.reveal_explorer_focus(true);+ cx.notify();+ }+ });+ }// abstractTree.ts `StickyScrollWidget` toggles only `focused` on its rows, never `selected`.let mut render_row = |(index, row): (usize, crate::explorer::Row), sticky: bool| {let row_guides = guides.row(index, 0.);diff --git a/native/crates/app/src/workbench/explorer_actions.rs b/native/crates/app/src/workbench/explorer_actions.rsindex 69a58c4ad79..edcd2792312 100644--- a/native/crates/app/src/workbench/explorer_actions.rs+++ b/native/crates/app/src/workbench/explorer_actions.rs@@ -17,6 +17,7 @@ use super::Workbench;use super::explorer_find::ExplorerFind;use super::hover::HoverOptions;use super::input_box_view::{InputId, InputLook, InputView};+use super::list_scroll::ListId;use super::search_view::SideBarView;use crate::commands::Outcome;use crate::dialog;@@ -49,6 +50,8 @@ pub struct ExplorerState {pub root_drop: Option<RootDrop>,expand_timer: Option<Task<()>>,pub scroll: ScrollHandle,+ /// A centered reveal asked for before the tree's first layout, which waits for its height.+ pub reveal_after_layout: bool,pub find: ExplorerFind,pub type_navigation: super::type_navigation::TypeNavigation,}@@ -73,7 +76,7 @@ pub struct RootDrop {impl ExplorerState {pub fn new(cx: &mut gpui::App) -> Self {- ExplorerState { focus: cx.focus_handle().tab_stop(true), input: InputView::default(), input_focus: cx.focus_handle(), edit: None, drop_target: None, root_drop: None, expand_timer: None, scroll: ScrollHandle::new(), find: ExplorerFind::new(cx), type_navigation: Default::default() }+ ExplorerState { focus: cx.focus_handle().tab_stop(true), input: InputView::default(), input_focus: cx.focus_handle(), edit: None, drop_target: None, root_drop: None, expand_timer: None, scroll: ScrollHandle::new(), reveal_after_layout: false, find: ExplorerFind::new(cx), type_navigation: Default::default() }}}@@ -444,7 +447,20 @@ impl Workbench {let rows = tree.rows();let Some(index) = rows.iter().position(|row| tree.focused.as_ref() == Some(&row.path)) else { return };let list = crate::list_tree::ListScroll { count: rows.len(), row_height: 22., viewport: f32::from(self.explorer.scroll.bounds().size.height), scroll_top: -f32::from(self.explorer.scroll.offset().y) };- if let Some(scroll_top) = crate::list_tree::reveal_scroll_top(list, index, center) {+ if center {+ // explorerView.ts reveals in a tree `layoutBody` gave its height.+ if list.viewport == 0. {+ self.explorer.reveal_after_layout = true;+ return;+ }+ let (covered, padding_top) = self.tree_sticky_padding(ListId::Explorer, index);+ if !crate::list_tree::shows_row(list, index, covered) {+ let scroll_top = crate::list_tree::center_scroll_top(list, index, padding_top);+ self.explorer.scroll.set_offset(gpui::point(px(0.), px(-scroll_top)));+ }+ return;+ }+ if let Some(scroll_top) = crate::list_tree::reveal_scroll_top(list, index, false) {self.explorer.scroll.set_offset(gpui::point(px(0.), px(-scroll_top)));}}diff --git a/native/crates/app/src/workbench/explorer_actions/tests.rs b/native/crates/app/src/workbench/explorer_actions/tests.rsindex 406973cdbef..0cb93fb7cfd 100644--- a/native/crates/app/src/workbench/explorer_actions/tests.rs+++ b/native/crates/app/src/workbench/explorer_actions/tests.rs@@ -609,6 +609,74 @@ fn opening_a_file_reveals_it_unless_auto_reveal_is_off_or_excludes_it(cx: &mut Tstd::fs::remove_dir_all(&user).unwrap();}+/// explorerView.ts `selectResource` reveals a row the sticky rows hide (abstractTree.ts `getRelativeTop`), and+/// abstractTree.ts `reveal` centers it below its ancestors' sticky rows (`nodePositionTopBelowWidget` as `paddingTop`).+#[gpui::test]+fn auto_reveal_centers_a_deep_file_below_the_sticky_rows(cx: &mut TestAppContext) {+ use crate::workbench::list_scroll::ListId;+ let (dir, workbench, cx) = explorer("explorer-reveal-sticky", cx);+ // A file beside each folder keeps `explorer.compactFolders` from joining them into one row.+ let deep = dir.join("sub/d1/d2/d3/d4/d5");+ std::fs::create_dir_all(&deep).unwrap();+ for folder in deep.ancestors().skip(1).take(5) {+ std::fs::write(folder.join("x.txt"), "").unwrap();+ }+ for index in 0..150 {+ std::fs::write(deep.join(format!("f{index:03}.txt")), "").unwrap();+ }+ cx.simulate_resize(gpui::size(px(1024.), px(400.)));+ cx.run_until_parked();+ workbench.update_in(cx, |workbench, window, cx| workbench.open_file(deep.join("f000.txt"), true, window, cx));+ cx.run_until_parked();+ workbench.update(cx, |workbench, _| {+ let index = workbench.folder.as_ref().unwrap().rows().iter().position(|row| row.path == deep.join("f100.txt")).unwrap();+ workbench.explorer.scroll.set_offset(gpui::point(px(0.), px(22. - index as f32 * 22.)));+ });+ cx.run_until_parked();+ let covered = workbench.read_with(cx, |workbench, _| workbench.tree_sticky_rows(ListId::Explorer, &workbench.tree_depths(ListId::Explorer)).len());+ assert!(covered > 1, "the sticky rows cover f100.txt, the second row in view");+ workbench.update_in(cx, |workbench, window, cx| workbench.open_file(deep.join("f100.txt"), true, window, cx));+ cx.run_until_parked();++ let (row_top, sticky_bottom, viewport) = workbench.read_with(cx, |workbench, _| {+ let tree = workbench.folder.as_ref().unwrap();+ let index = tree.rows().iter().position(|row| tree.focused.as_ref() == Some(&row.path)).unwrap();+ let sticky = workbench.tree_sticky_rows(ListId::Explorer, &workbench.tree_depths(ListId::Explorer));+ let scroll_top = -f32::from(workbench.explorer.scroll.offset().y);+ (index as f32 * 22. - scroll_top, sticky.last().unwrap().position + 22., f32::from(workbench.explorer.scroll.bounds().size.height))+ });+ assert!(row_top >= sticky_bottom && row_top + 22. <= viewport, "row at {row_top}, sticky rows to {sticky_bottom}, view {viewport}");+ assert_eq!(row_top, 132. + (viewport - 132. - 22.) / 2., "centered below the six sticky rows of sub/ and d1/ to d5/");+ std::fs::remove_dir_all(&dir).unwrap();+}++/// explorerView.ts `selectActiveFile` runs on a tree `layoutBody` gave its height: a file opened before the+/// Explorer's first layout is centered below the sticky `sub` row once the tree has its height.+#[gpui::test]+fn a_file_opened_before_the_first_layout_reveals_once_the_tree_has_its_height(cx: &mut TestAppContext) {+ let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();+ let theme = load_theme(&[scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap()], "Dark Modern").unwrap();+ let dir = std::env::temp_dir().join(format!("code-gpui-explorer-reveal-layout-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(dir.join("sub")).unwrap();+ for index in 0..150 {+ std::fs::write(dir.join(format!("sub/f{index:03}.txt")), "").unwrap();+ }+ let folder = dir.clone();+ let (workbench, cx) = cx.add_window_view(|window, cx| {+ let mut workbench = Workbench::new(theme, app_root.clone(), Some(Workspace::folder(folder.clone())), Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);+ workbench.open_file(folder.join("sub/f100.txt"), true, window, cx);+ workbench+ });+ cx.run_until_parked();+ workbench.read_with(cx, |workbench, _| {+ let viewport = f32::from(workbench.explorer.scroll.bounds().size.height);+ let row_top = 101. * 22. + f32::from(workbench.explorer.scroll.offset().y);+ assert_eq!(row_top, 22. + (viewport - 22. - 22.) / 2., "view {viewport}");+ });+ std::fs::remove_dir_all(&dir).unwrap();+}+/// explorerView.ts `selectActiveFile` reveals `getCanonicalUri(activeEditor, PRIMARY)`: the modified side of a diff,/// whichever side has the focus, and nothing for a multi diff editor.#[gpui::test]diff --git a/native/crates/app/src/workbench/tree_guides.rs b/native/crates/app/src/workbench/tree_guides.rsindex 82f10a2a30a..2cf1b2679d2 100644--- a/native/crates/app/src/workbench/tree_guides.rs+++ b/native/crates/app/src/workbench/tree_guides.rs@@ -113,6 +113,19 @@ impl Workbench {sticky_rows(depths, ROW_HEIGHT, scroller.position().y, height, self.sticky_scroll_max_items())}+ /// abstractTree.ts: the top of the view the sticky rows cover for `getRelativeTop` (the row's own sticky position,+ /// else the widget's height) and the `nodePositionTopBelowWidget` `reveal` scrolls the row below; none with+ /// sticky scroll off.+ pub(super) fn tree_sticky_padding(&self, list: ListId, index: usize) -> (f32, f32) {+ if self.setting("workbench.tree.enableStickyScroll") == json!(false) {+ return (0., 0.);+ }+ let depths = self.tree_depths(list);+ let sticky = self.tree_sticky_rows(list, &depths);+ let covered = sticky.iter().find(|row| row.index == index).map(|row| row.position).or_else(|| sticky.last().map(|row| row.position + ROW_HEIGHT)).unwrap_or(0.);+ (covered, position_below_widget(&depths, index, ROW_HEIGHT, self.sticky_scroll_max_items()))+ }+fn sticky_scroll_max_items(&self) -> usize {self.setting("workbench.tree.stickyScrollMaxItemCount").as_u64().unwrap_or(7) as usize}