Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_graph: Improve navigation in Vim mode (#55506)

aprogramq8c753ddba728
diff --git a/assets/keymaps/vim.json b/assets/keymaps/vim.json
index 6e52ac3ce63..88e8e352040 100644
--- a/assets/keymaps/vim.json
+++ b/assets/keymaps/vim.json
@@ -1052,6 +1052,8 @@
"bindings": {
"j": "vim::MenuSelectNext",
"k": "vim::MenuSelectPrevious",
+ "ctrl-d": "git_graph::ScrollDown",
+ "ctrl-u": "git_graph::ScrollUp",
"shift-g": "menu::SelectLast",
"g g": "menu::SelectFirst"
}
diff --git a/crates/git_graph/src/git_graph.rs b/crates/git_graph/src/git_graph.rs
index 1f7d4b88661..5a7e7a30294 100644
--- a/crates/git_graph/src/git_graph.rs
+++ b/crates/git_graph/src/git_graph.rs
@@ -286,6 +286,10 @@ actions!(
FocusNextTabStop,
/// Focuses the previous git graph tab stop.
FocusPreviousTabStop,
+ /// Selects a commit half a page above the current selection.
+ ScrollUp,
+ /// Selects a commit half a page below the current selection.
+ ScrollDown,
]
);
@@ -1025,6 +1029,20 @@ impl GitGraph {
(raw * scale).round() / scale
}
+ fn visible_row_count(&self, window: &Window, cx: &App) -> usize {
+ let row_height = Self::row_height(window, cx);
+ let viewport_height = self
+ .table_interaction_state
+ .read(cx)
+ .scroll_handle
+ .0
+ .borrow()
+ .last_item_size
+ .map_or(window.viewport_size().height, |size| size.item.height);
+
+ ((viewport_height / row_height).ceil() as usize).min(self.graph_data.commits.len())
+ }
+
fn graph_canvas_content_width(&self) -> Pixels {
(LANE_WIDTH * self.graph_data.max_lanes.max(6) as f32) + LEFT_PADDING * 2.0
}
@@ -1534,6 +1552,28 @@ impl GitGraph {
);
}
+ fn scroll_up(&mut self, _: &ScrollUp, window: &mut Window, cx: &mut Context<Self>) {
+ let step = (self.visible_row_count(window, cx) / 2).max(1);
+ let target_idx = self.selected_entry_idx.unwrap_or(0).saturating_sub(step);
+
+ self.select_entry(target_idx, ScrollStrategy::Nearest, cx);
+ }
+
+ fn scroll_down(&mut self, _: &ScrollDown, window: &mut Window, cx: &mut Context<Self>) {
+ let Some(last_entry_idx) = self.graph_data.commits.len().checked_sub(1) else {
+ return;
+ };
+
+ let step = (self.visible_row_count(window, cx) / 2).max(1);
+ let target_idx = self
+ .selected_entry_idx
+ .unwrap_or(0)
+ .saturating_add(step)
+ .min(last_entry_idx);
+
+ self.select_entry(target_idx, ScrollStrategy::Nearest, cx);
+ }
+
fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
self.open_selected_commit_view(window, cx);
}
@@ -1649,7 +1689,12 @@ impl GitGraph {
scroll_strategy: ScrollStrategy,
cx: &mut Context<Self>,
) {
- if self.selected_entry_idx == Some(idx) {
+ if self.selected_entry_idx == Some(idx) || idx >= self.graph_data.commits.len() {
+ debug_assert!(
+ idx < self.graph_data.commits.len(),
+ "attempted to select out of bounds index: {idx}, commits.len: {}",
+ self.graph_data.commits.len()
+ );
return;
}
@@ -2345,6 +2390,7 @@ impl GitGraph {
fn render_graph_canvas(&self, window: &Window, cx: &mut Context<GitGraph>) -> impl IntoElement {
let row_height = Self::row_height(window, cx);
+ let visible_row_count = self.visible_row_count(window, cx);
let table_state = self.table_interaction_state.read(cx);
let viewport_height = table_state
.scroll_handle
@@ -2368,8 +2414,7 @@ impl GitGraph {
} else {
graph_viewport_width
};
- let last_visible_row =
- first_visible_row + (viewport_height / row_height).ceil() as usize + 1;
+ let last_visible_row = first_visible_row + visible_row_count + 1;
let viewport_range = first_visible_row.min(loaded_commit_count.saturating_sub(1))
..(last_visible_row).min(loaded_commit_count);
@@ -3023,6 +3068,8 @@ impl Render for GitGraph {
.on_action(cx.listener(Self::select_prev))
.on_action(cx.listener(Self::select_next))
.on_action(cx.listener(Self::select_last))
+ .on_action(cx.listener(Self::scroll_up))
+ .on_action(cx.listener(Self::scroll_down))
.on_action(cx.listener(Self::confirm))
.on_action(cx.listener(Self::focus_next_tab_stop))
.on_action(cx.listener(Self::focus_previous_tab_stop))
@@ -5173,8 +5220,8 @@ mod tests {
workspace::MultiWorkspace::test_new(project.clone(), window, cx)
});
- let workspace_weak =
- multi_workspace.read_with(&*cx, |multi, _| multi.workspace().downgrade());
+ let workspace = multi_workspace.read_with(&*cx, |multi, _| multi.workspace().clone());
+ let workspace_weak = workspace.downgrade();
let git_graph = cx.new_window_entity(|window, cx| {
GitGraph::new(
@@ -5188,6 +5235,11 @@ mod tests {
});
cx.run_until_parked();
+ workspace.update_in(cx, |workspace, window, cx| {
+ workspace.add_item_to_active_pane(Box::new(git_graph.clone()), None, true, window, cx);
+ });
+ cx.run_until_parked();
+
git_graph.update_in(cx, |graph, window, cx| {
graph.focus_handle(cx).focus(window, cx);
});
@@ -5196,10 +5248,15 @@ mod tests {
cx.draw(
point(px(0.), px(0.)),
gpui::size(px(1200.), px(800.)),
- |_, _| git_graph.clone().into_any_element(),
+ |_, _| multi_workspace.clone().into_any_element(),
);
cx.run_until_parked();
+ git_graph.update_in(cx, |graph, window, cx| {
+ graph.focus_handle(cx).focus(window, cx);
+ });
+ cx.run_until_parked();
+
git_graph.read_with(&*cx, |graph, _| {
assert_eq!(graph.graph_data.commits.len(), 10);
});
@@ -5215,6 +5272,22 @@ mod tests {
assert_eq!(graph.selected_entry_idx, Some(0));
});
+ let scroll_step = git_graph.update_in(cx, |graph, window, cx| {
+ (graph.visible_row_count(window, cx) / 2).max(1)
+ });
+
+ cx.dispatch_action(ScrollDown);
+ cx.run_until_parked();
+ git_graph.read_with(&*cx, |graph, _| {
+ assert_eq!(graph.selected_entry_idx, Some(scroll_step));
+ });
+
+ cx.dispatch_action(ScrollUp);
+ cx.run_until_parked();
+ git_graph.read_with(&*cx, |graph, _| {
+ assert_eq!(graph.selected_entry_idx, Some(0));
+ });
+
git_graph.update_in(cx, |graph, window, cx| {
graph.select_next(&menu::SelectNext, window, cx);
});
@@ -5239,6 +5312,12 @@ mod tests {
assert_eq!(graph.selected_entry_idx, Some(9));
});
+ cx.dispatch_action(ScrollDown);
+ cx.run_until_parked();
+ git_graph.read_with(&*cx, |graph, _| {
+ assert_eq!(graph.selected_entry_idx, Some(9));
+ });
+
git_graph.update_in(cx, |graph, window, cx| {
graph.select_next(&menu::SelectNext, window, cx);
});