Repository: zed-industries/zed
File: crates/git_ui/src/git_panel.rs (Rust)

Code before:
                let Some(current_logical_index) = state
                    .logical_indices
                    .iter()
                    .position(|&i| i == selected_entry)
                else {
                    return;
                };

                let Some(new_index) = state
                    .logical_indices
                    .get(current_logical_index.saturating_add(1))
                    .copied()
                else {
                    return;
                };

                new_index
            }
        };

        if matches!(self.entries.get(new_index), Some(GitListEntry::Header(..))) {
            let Some(new_selected_entry) = new_index.checked_add(1).filter(|ix| *ix < item_count)
            else {
                return;
            };
            self.selected_entry = Some(new_selected_entry);
        } else {
            self.selected_entry = Some(new_index);
        }

        self.scroll_to_selected_entry(cx);
    }

    fn select_last(&mut self, _: &menu::SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
        if self.entries.last().is_some() {
            self.selected_entry = Some(self.entries.len() - 1);
            self.scroll_to_selected_entry(cx);
        }
    }

    /// Show diff view at selected entry, only if the diff view is open
    fn move_diff_to_entry(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        maybe!({
            let workspace = self.workspace.upgrade()?;

            if let Some(project_diff) = workspace.read(cx).item_of_type::<ProjectDiff>(cx) {
                let entry = self.entries.get(self.selected_entry?)?.status_entry()?;

                project_diff.update(cx, |project_diff, cx| {
                    project_diff.move_to_entry(entry.clone(), window, cx);
                });

Review comment (suggestion):
This check seems redundant:
* in `GitPanelViewMode::Flat`, we already check for this
* in `GitPanelViewMode::Tree`, next entry is bound by `logical_indices` precludes this case
Lemme know if I missed something though! Also, your test does not account for this which strengthens my belief that this check is redundant.

Code after:
                let Some(current_logical_index) = state
                    .logical_indices
                    .iter()
                    .position(|&i| i == selected_entry)
                else {
                    return;
                };

                let Some(new_index) = state
                    .logical_indices
                    .get(current_logical_index.saturating_add(1))
                    .copied()
                else {
                    return;
                };

                new_index
            }
        };

        if matches!(self.entries.get(new_index), Some(GitListEntry::Header(..))) {
            self.selected_entry = Some(new_index.saturating_add(1));
        } else {
            self.selected_entry = Some(new_index);
        }

        self.scroll_to_selected_entry(cx);
    }

    fn select_last(&mut self, _: &menu::SelectLast, _window: &mut Window, cx: &mut Context<Self>) {
        if self.entries.last().is_some() {
            self.selected_entry = Some(self.entries.len() - 1);
            self.scroll_to_selected_entry(cx);
        }
    }

    /// Show diff view at selected entry, only if the diff view is open
    fn move_diff_to_entry(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        maybe!({
            let workspace = self.workspace.upgrade()?;

            if let Some(project_diff) = workspace.read(cx).item_of_type::<ProjectDiff>(cx) {
                let entry = self.entries.get(self.selected_entry?)?.status_entry()?;

                project_diff.update(cx, |project_diff, cx| {
                    project_diff.move_to_entry(entry.clone(), window, cx);
                });
            }

            Some(())
        });