vim: Add vim counts and vim shortcuts to project_panel (#36653)

Closes #10930 
Closes #11353

Release Notes:

- Adds commands to project_panel
  - `ctrl-u` scrolls the project_panel up half of the visible entries
  - `ctrl-d` scrolls the project_panel down half of the visible entries
  - `z z` scrolls current selection to center of window
  - `z t`  scrolls current selection to top of window
  - `z b` scrolls current selection to bottom of window
  - `{num} j` and `{num} k` now move up and  down with a count
This commit is contained in:
AidanV
2025-09-29 15:53:59 -06:00
committed by GitHub
parent fbb4dcf2b1
commit 9c60bc3837
6 changed files with 183 additions and 18 deletions
+65 -1
View File
@@ -85,6 +85,7 @@ pub struct ProjectPanel {
// An update loop that keeps incrementing/decrementing scroll offset while there is a dragged entry that's
// hovered over the start/end of a list.
hover_scroll_task: Option<Task<()>>,
rendered_entries_len: usize,
visible_entries: Vec<VisibleEntriesForWorktree>,
/// Maps from leaf project entry ID to the currently selected ancestor.
/// Relevant only for auto-fold dirs, where a single project panel entry may actually consist of several
@@ -278,6 +279,16 @@ actions!(
UnfoldDirectory,
/// Folds the selected directory.
FoldDirectory,
/// Scroll half a page upwards
ScrollUp,
/// Scroll half a page downwards
ScrollDown,
/// Scroll until the cursor displays at the center
ScrollCursorCenter,
/// Scroll until the cursor displays at the top
ScrollCursorTop,
/// Scroll until the cursor displays at the bottom
ScrollCursorBottom,
/// Selects the parent directory.
SelectParent,
/// Selects the next entry with git changes.
@@ -634,6 +645,7 @@ impl ProjectPanel {
hover_scroll_task: None,
fs: workspace.app_state().fs.clone(),
focus_handle,
rendered_entries_len: 0,
visible_entries: Default::default(),
ancestors: Default::default(),
folded_directory_drag_target: None,
@@ -2080,6 +2092,52 @@ impl ProjectPanel {
}
}
fn scroll_up(&mut self, _: &ScrollUp, window: &mut Window, cx: &mut Context<Self>) {
for _ in 0..self.rendered_entries_len / 2 {
window.dispatch_action(SelectPrevious.boxed_clone(), cx);
}
}
fn scroll_down(&mut self, _: &ScrollDown, window: &mut Window, cx: &mut Context<Self>) {
for _ in 0..self.rendered_entries_len / 2 {
window.dispatch_action(SelectNext.boxed_clone(), cx);
}
}
fn scroll_cursor_center(
&mut self,
_: &ScrollCursorCenter,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
self.scroll_handle
.scroll_to_item_strict(index, ScrollStrategy::Center);
cx.notify();
}
}
fn scroll_cursor_top(&mut self, _: &ScrollCursorTop, _: &mut Window, cx: &mut Context<Self>) {
if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
self.scroll_handle
.scroll_to_item_strict(index, ScrollStrategy::Top);
cx.notify();
}
}
fn scroll_cursor_bottom(
&mut self,
_: &ScrollCursorBottom,
_: &mut Window,
cx: &mut Context<Self>,
) {
if let Some((_, _, index)) = self.selection.and_then(|s| self.index_for_selection(s)) {
self.scroll_handle
.scroll_to_item_strict(index, ScrollStrategy::Bottom);
cx.notify();
}
}
fn select_next(&mut self, _: &SelectNext, window: &mut Window, cx: &mut Context<Self>) {
if let Some(edit_state) = &self.edit_state
&& edit_state.processing_filename.is_none()
@@ -5198,6 +5256,11 @@ impl Render for ProjectPanel {
},
))
.key_context(self.dispatch_context(window, cx))
.on_action(cx.listener(Self::scroll_up))
.on_action(cx.listener(Self::scroll_down))
.on_action(cx.listener(Self::scroll_cursor_center))
.on_action(cx.listener(Self::scroll_cursor_top))
.on_action(cx.listener(Self::scroll_cursor_bottom))
.on_action(cx.listener(Self::select_next))
.on_action(cx.listener(Self::select_previous))
.on_action(cx.listener(Self::select_first))
@@ -5272,7 +5335,8 @@ impl Render for ProjectPanel {
.child(
uniform_list("entries", item_count, {
cx.processor(|this, range: Range<usize>, window, cx| {
let mut items = Vec::with_capacity(range.end - range.start);
this.rendered_entries_len = range.end - range.start;
let mut items = Vec::with_capacity(this.rendered_entries_len);
this.for_each_visible_entry(
range,
window,