Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
git_ui: Prevent crash when selecting collapsed sections (#63164)
Cole Miller507a1b994c88
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rsindex 661ba221696..77aabce3bf6 100644--- a/crates/git_ui/src/git_panel.rs+++ b/crates/git_ui/src/git_panel.rs@@ -1764,18 +1764,21 @@ impl GitPanel {_window: &mut Window,cx: &mut Context<Self>,) {+ let is_selectable = |index| {+ self.entries+ .get(index)+ .is_some_and(GitListEntry::is_selectable)+ };let first_entry = match &self.view_mode {GitPanelViewMode::Flat => self- .entries+ .visible_flat_entry_indices()+ .into_iter()+ .find(|&index| is_selectable(index)),+ GitPanelViewMode::Tree(state) => state+ .logical_indices.iter()- .position(|entry| entry.status_entry().is_some()),- GitPanelViewMode::Tree(state) => {- let index = self.entries.iter().position(|entry| {- entry.status_entry().is_some() || entry.directory_entry().is_some()- });-- index.map(|index| state.logical_indices[index])- }+ .copied()+ .find(|&index| is_selectable(index)),};if let Some(first_entry) = first_entry {@@ -12389,6 +12392,48 @@ mod tests {});}+ #[gpui::test]+ async fn test_tree_view_select_first_skips_collapsed_section(cx: &mut TestAppContext) {+ init_test(cx);+ cx.update(|cx| {+ SettingsStore::update_global(cx, |store, cx| {+ store.update_user_settings(cx, |settings| {+ settings.git_panel.get_or_insert_default().tree_view = Some(true);+ })+ });+ });++ let (_, _, _, panel, mut cx) = setup_git_panel_with_changes(+ cx,+ json!({+ ".git": {},+ "modified.rs": "fn main() {}",+ }),+ &[("modified.rs", StatusCode::Modified)],+ )+ .await;++ panel.update_in(&mut cx, |panel, window, cx| {+ panel.selected_entry = None;+ panel.toggle_section_collapsed(Section::Tracked, window, cx);++ let state = panel+ .view_mode+ .tree_state()+ .expect("tree view state should exist");+ assert_eq!(state.logical_indices, [0]);+ assert!(panel.selected_entry.is_none());++ panel.toggle_section_collapsed(Section::Tracked, window, cx);++ let selected_entry = panel+ .get_selected_entry()+ .and_then(GitListEntry::status_entry)+ .expect("the first visible file should be selected");+ assert_eq!(selected_entry.repo_path, repo_path("modified.rs"));+ });+ }+fn assert_entry_paths(entries: &[GitListEntry], expected_paths: &[Option<&str>]) {assert_eq!(entries.len(), expected_paths.len());for (entry, expected_path) in entries.iter().zip(expected_paths) {