Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_ui: Keep the open commit highlighted in the History tab (#66226)
Smit Barmase422b53cff836
diff --git a/crates/git_ui/src/commit_view.rs b/crates/git_ui/src/commit_view.rsindex fb3a0f1de9f..42f733aae98 100644--- a/crates/git_ui/src/commit_view.rs+++ b/crates/git_ui/src/commit_view.rs@@ -201,6 +201,14 @@ impl CommitView {)}+ pub(crate) fn commit_sha(&self) -> &SharedString {+ &self.commit.sha+ }++ pub(crate) fn repository(&self) -> &Entity<Repository> {+ &self.repository+ }+fn open_with_options(commit_sha: String,repo: WeakEntity<Repository>,diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rsindex 2fa9bd1fd72..2d40e4e0b19 100644--- a/crates/git_ui/src/git_panel.rs+++ b/crates/git_ui/src/git_panel.rs@@ -1228,6 +1228,7 @@ pub struct GitPanel {commit_history_scroll_handle: UniformListScrollHandle,commit_history: CommitHistory,focused_history_entry: Option<usize>,+ active_history_sha: Option<Oid>,history_keyboard_nav: bool,_commit_message_buffer_subscription: Option<Subscription>,_repo_subscriptions: Vec<Subscription>,@@ -1371,11 +1372,19 @@ impl GitPanel {let pending_commit_message_restores = serialized_panel.map(|panel| panel.commit_messages).unwrap_or_default();+ let workspace_entity = cx.entity();cx.new(|cx| {let focus_handle = cx.focus_handle();cx.on_focus(&focus_handle, window, Self::focus_in).detach();+ cx.subscribe(&workspace_entity, |this, _, event, cx| {+ if let workspace::Event::ActiveItemChanged = event {+ this.update_active_history_sha(cx);+ }+ })+ .detach();+let mut was_sort_by = GitPanelSettings::get_global(cx).sort_by;let mut was_group_by = GitPanelSettings::get_global(cx).group_by;let mut was_tree_view = GitPanelSettings::get_global(cx).tree_view;@@ -1565,6 +1574,7 @@ impl GitPanel {commit_history_scroll_handle: UniformListScrollHandle::new(),commit_history: CommitHistory::Loading,focused_history_entry: None,+ active_history_sha: None,history_keyboard_nav: false,_commit_message_buffer_subscription: None,_repo_subscriptions: Vec::new(),@@ -2146,7 +2156,12 @@ impl GitPanel {if !self.focus_handle.contains_focused(window, cx) {cx.emit(Event::Focus);}- if self.active_tab == GitPanelTab::History && self.focused_history_entry.is_some() {+ // Clicking into the panel also focuses it, and that shouldn't show the+ // keyboard focus border.+ if self.active_tab == GitPanelTab::History+ && self.focused_history_entry.is_some()+ && window.last_input_was_keyboard()+ {self.history_keyboard_nav = true;cx.notify();}@@ -5564,6 +5579,9 @@ impl GitPanel {}}self.active_repository = new_active_repository;+ if active_repository_changed {+ self.update_active_history_sha(cx);+ }self.reopen_commit_buffer(window, cx);self.preload_commit_history(cx);if self.active_tab == GitPanelTab::History {@@ -7510,6 +7528,37 @@ impl GitPanel {}}+ fn active_history_entry_index(&self) -> Option<usize> {+ let active_history_sha = self.active_history_sha?;+ self.commit_history_entries()+ .iter()+ .position(|entry| entry.sha == active_history_sha)+ }++ fn update_active_history_sha(&mut self, cx: &mut Context<Self>) {+ let active_history_sha = maybe!({+ let repository = self.active_repository.as_ref()?;+ let commit_view = self+ .workspace+ .upgrade()?+ .read(cx)+ .active_item_as::<CommitView>(cx)?;+ let commit_view = commit_view.read(cx);+ if commit_view.repository() != repository {+ return None;+ }+ commit_view.commit_sha().parse().ok()+ });+ if self.active_history_sha == active_history_sha {+ return;+ }+ self.active_history_sha = active_history_sha;+ if let Some(index) = self.active_history_entry_index() {+ self.focused_history_entry = Some(index);+ }+ cx.notify();+ }+fn select_next_history_entry(&mut self, cx: &mut Context<Self>) {let count = self.commit_history_entries().len();if count == 0 {@@ -7703,10 +7752,14 @@ impl GitPanel {fn set_commit_history(&mut self, commit_history: CommitHistory, cx: &mut Context<Self>) {let changed = self.commit_history != commit_history;self.commit_history = commit_history;- // Keep the focused entry within range as the history grows or clears.+ // Keep the focused entry within range as the history grows or clears,+ // starting from the active commit when nothing is focused yet.let count = self.commit_history_entries().len();- let focused = self.focused_history_entry.unwrap_or(0);- self.focused_history_entry = (count > 0).then(|| focused.min(count - 1));+ let focused = self+ .focused_history_entry+ .or_else(|| self.active_history_entry_index());+ self.focused_history_entry =+ focused.and_then(|focused| (count > 0).then(|| focused.min(count - 1)));if changed {cx.notify();}@@ -7754,6 +7807,7 @@ impl GitPanel {let remote = self.git_remote(cx);let focused_history_entry = self.focused_history_entry;+ let active_history_sha = self.active_history_sha;let is_panel_focused = self.focus_handle.is_focused(window);let show_focus_border = self.history_keyboard_nav;let has_context_menu = self.context_menu.is_some();@@ -7853,6 +7907,7 @@ impl GitPanel {let is_unpushed = index < ahead_count;let is_focused = focused_history_entry == Some(index);+ let is_active = active_history_sha == Some(entry.sha);let is_context_menu_target =context_menu_target_index == Some(index);let workspace = workspace.clone();@@ -7884,10 +7939,18 @@ impl GitPanel {)},)- .hover(|s| s.bg(cx.theme().colors().element_hover))- .when(is_context_menu_target, |this| {- this.bg(cx.theme().colors().element_hover)- })+ .when_else(+ is_active,+ |this| this.bg(cx.theme().colors().element_selected),+ |this| {+ this.hover(|s| {+ s.bg(cx.theme().colors().element_hover)+ })+ .when(is_context_menu_target, |this| {+ this.bg(cx.theme().colors().element_hover)+ })+ },+ ).child(h_flex().gap_1()@@ -11025,17 +11088,23 @@ mod tests {);}+ struct HistoryPanelTest {+ workspace: Entity<Workspace>,+ panel: Entity<GitPanel>,+ cx: VisualTestContext,+ }+async fn history_panel_for_project(fs: Arc<FakeFs>,cx: &mut TestAppContext,- ) -> Entity<GitPanel> {+ ) -> HistoryPanelTest {let project = Project::test(fs, [Path::new(path!("/root/project"))], cx).await;let window_handle =cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));let workspace = window_handle.read_with(cx, |mw, _| mw.workspace().clone()).unwrap();- let cx = &mut VisualTestContext::from_window(window_handle.into(), cx);+ let mut cx = VisualTestContext::from_window(window_handle.into(), cx);cx.read(|cx| {project@@ -11051,12 +11120,16 @@ mod tests {.await;cx.executor().run_until_parked();- let panel = workspace.update_in(cx, GitPanel::new);- panel.update_in(cx, |panel, window, cx| {+ let panel = workspace.update_in(&mut cx, GitPanel::new);+ panel.update_in(&mut cx, |panel, window, cx| {panel.activate_history_tab(&ActivateHistoryTab, window, cx);});cx.run_until_parked();- panel+ HistoryPanelTest {+ workspace,+ panel,+ cx,+ }}async fn wait_for_commit_history_to_settle(panel: &Entity<GitPanel>, cx: &mut TestAppContext) {@@ -11081,7 +11154,7 @@ mod tests {}).unwrap();- let panel = history_panel_for_project(fs.clone(), cx).await;+ let panel = history_panel_for_project(fs.clone(), cx).await.panel;wait_for_commit_history_to_settle(&panel, cx).await;panel.read_with(cx, |panel, _| {@@ -11110,7 +11183,7 @@ mod tests {}).unwrap();- let panel = history_panel_for_project(fs.clone(), cx).await;+ let panel = history_panel_for_project(fs.clone(), cx).await.panel;wait_for_commit_history_to_settle(&panel, cx).await;panel.read_with(cx, |panel, _| {@@ -11146,7 +11219,7 @@ mod tests {.unwrap();fs.set_graph_error(dot_git, Some("simulated git log failure".into()));- let panel = history_panel_for_project(fs.clone(), cx).await;+ let panel = history_panel_for_project(fs.clone(), cx).await.panel;wait_for_commit_history_to_settle(&panel, cx).await;panel.read_with(cx, |panel, _| {@@ -11161,7 +11234,7 @@ mod tests {let fs = FakeFs::new(cx.background_executor.clone());fs.insert_tree("/root", json!({ "project": {} })).await;- let panel = history_panel_for_project(fs.clone(), cx).await;+ let panel = history_panel_for_project(fs.clone(), cx).await.panel;panel.read_with(cx, |panel, _| {assert_eq!(panel.commit_history, CommitHistory::Loading);@@ -11199,6 +11272,213 @@ mod tests {);}+ /// Opens a History tab over three commits, returned newest first.+ async fn history_panel_with_commits(cx: &mut TestAppContext) -> (HistoryPanelTest, [Oid; 3]) {+ let fs = FakeFs::new(cx.background_executor.clone());+ fs.insert_tree("/root", json!({ "project": { ".git": {} } }))+ .await;++ let dot_git = Path::new(path!("/root/project/.git"));+ let shas = [3, 2, 1].map(|byte| Oid::from_bytes(&[byte; 20]).unwrap());+ fs.with_git_state(dot_git, false, |state| {+ state.refs.insert("HEAD".into(), shas[0].to_string());+ state.graph_commits = shas+ .into_iter()+ .map(|sha| {+ Arc::new(git::repository::InitialGraphCommitData {+ sha,+ parents: SmallVec::new(),+ ref_names: Vec::new(),+ })+ })+ .collect();+ })+ .unwrap();++ let mut test = history_panel_for_project(fs, cx).await;+ wait_for_commit_history_to_settle(&test.panel, &mut test.cx).await;++ (test, shas)+ }++ impl HistoryPanelTest {+ fn history_state(&self) -> (Option<Oid>, Option<usize>) {+ self.panel.read_with(&self.cx, |panel, _| {+ (panel.active_history_sha, panel.focused_history_entry)+ })+ }++ fn open_commit(&mut self, sha: Oid) {+ let repository = self.panel.read_with(&self.cx, |panel, _| {+ panel.active_repository.clone().unwrap()+ });+ let workspace = self.workspace.downgrade();+ self.cx.update(|window, cx| {+ CommitView::open(+ sha.to_string(),+ repository.downgrade(),+ workspace,+ None,+ None,+ window,+ cx,+ );+ });+ self.cx.run_until_parked();+ }+ }++ #[gpui::test]+ async fn test_history_tab_highlights_opened_commit_view(cx: &mut TestAppContext) {+ init_test(cx);++ let (mut test, [_, middle_sha, _]) = history_panel_with_commits(cx).await;++ assert_eq!(+ test.history_state(),+ (None, None),+ "no commit should be highlighted or focused before one is opened"+ );++ test.open_commit(middle_sha);+ assert_eq!(test.history_state(), (Some(middle_sha), Some(1)));+ }++ #[gpui::test]+ async fn test_history_tab_clears_highlight_for_non_commit_tab(cx: &mut TestAppContext) {+ init_test(cx);++ let (mut test, [_, middle_sha, _]) = history_panel_with_commits(cx).await;+ test.open_commit(middle_sha);+ let commit_view = test+ .workspace+ .read_with(&test.cx, |workspace, cx| {+ workspace.active_item_as::<CommitView>(cx)+ })+ .unwrap();++ test.workspace+ .update_in(&mut test.cx, |workspace, window, cx| {+ let test_item = cx.new(TestItem::new);+ workspace.add_item_to_active_pane(Box::new(test_item), None, true, window, cx);+ });+ test.cx.run_until_parked();+ assert_eq!(+ test.history_state(),+ (None, Some(1)),+ "switching to a non-commit tab should clear the highlight but keep the cursor"+ );++ test.workspace+ .update_in(&mut test.cx, |workspace, window, cx| {+ workspace.active_pane().update(cx, |pane, cx| {+ let index = pane.index_for_item(&commit_view).unwrap();+ pane.activate_item(index, true, true, window, cx);+ });+ });+ test.cx.run_until_parked();+ assert_eq!(test.history_state(), (Some(middle_sha), Some(1)));+ }++ #[gpui::test]+ async fn test_history_tab_focuses_active_commit_after_reload(cx: &mut TestAppContext) {+ init_test(cx);++ let (mut test, [_, _, oldest_sha]) = history_panel_with_commits(cx).await;+ test.open_commit(oldest_sha);++ test.panel.update_in(&mut test.cx, |panel, window, cx| {+ panel.activate_changes_tab(&ActivateChangesTab, window, cx);+ });+ assert_eq!(test.history_state(), (Some(oldest_sha), None));++ test.panel.update_in(&mut test.cx, |panel, window, cx| {+ panel.activate_history_tab(&ActivateHistoryTab, window, cx);+ });+ wait_for_commit_history_to_settle(&test.panel, &mut test.cx).await;+ assert_eq!(+ test.history_state(),+ (Some(oldest_sha), Some(2)),+ "reloading the history should place the cursor on the active commit"+ );+ }++ #[gpui::test]+ async fn test_history_tab_highlights_next_commit_view_after_close(cx: &mut TestAppContext) {+ init_test(cx);++ let (mut test, [_, middle_sha, oldest_sha]) = history_panel_with_commits(cx).await;+ test.open_commit(middle_sha);+ test.open_commit(oldest_sha);+ assert_eq!(test.history_state(), (Some(oldest_sha), Some(2)));++ test.workspace+ .update_in(&mut test.cx, |workspace, window, cx| {+ workspace.active_pane().update(cx, |pane, cx| {+ let item_id = pane.active_item().unwrap().item_id();+ pane.close_item_by_id(item_id, workspace::SaveIntent::Skip, window, cx)+ })+ })+ .await+ .unwrap();+ test.cx.run_until_parked();+ assert_eq!(+ test.history_state(),+ (Some(middle_sha), Some(1)),+ "closing the active commit view should highlight the commit view activated next"+ );+ }++ #[gpui::test]+ async fn test_history_tab_focus_border_only_for_keyboard_focus(cx: &mut TestAppContext) {+ init_test(cx);++ let (mut test, [_, middle_sha, _]) = history_panel_with_commits(cx).await;+ test.workspace+ .update_in(&mut test.cx, |workspace, window, cx| {+ workspace.add_panel(test.panel.clone(), window, cx);+ workspace.open_panel::<GitPanel>(window, cx);+ });+ test.open_commit(middle_sha);+ let commit_view = test+ .workspace+ .read_with(&test.cx, |workspace, cx| {+ workspace.active_item_as::<CommitView>(cx)+ })+ .unwrap();+ let focus_panel = |cx: &mut VisualTestContext| {+ test.panel+ .update_in(cx, |panel, window, cx| panel.focus_handle.focus(window, cx));+ cx.run_until_parked();+ };+ let focus_commit_view = |cx: &mut VisualTestContext| {+ commit_view.update_in(cx, |commit_view, window, cx| {+ commit_view.focus_handle(cx).focus(window, cx)+ });+ cx.run_until_parked();+ };++ focus_commit_view(&mut test.cx);+ test.cx+ .simulate_mouse_move(gpui::point(px(1.), px(1.)), None, Modifiers::none());+ focus_panel(&mut test.cx);+ assert!(+ !test+ .panel+ .read_with(&test.cx, |panel, _| panel.history_keyboard_nav),+ "focusing the panel after mouse input shouldn't show the focus border"+ );++ focus_commit_view(&mut test.cx);+ test.cx.simulate_keystrokes("f19");+ focus_panel(&mut test.cx);+ assert!(+ test.panel+ .read_with(&test.cx, |panel, _| panel.history_keyboard_nav),+ "focusing the panel after keyboard input should show the focus border"+ );+ }+#[test]fn test_commit_history_from_response() {let sha: Oid = "0123456789012345678901234567890123456789".parse().unwrap();