Add path/status sorting toggle to Git panel menu

The changes add a new menu option to toggle between sorting Git entries
by path or status, including unit tests and settings integration.

rem unneccessary tests

rem test

rem test
This commit is contained in:
nick-kilian 2025-08-05 21:03:25 +01:00
parent 7217439c97
commit a523b4f941

View file

@ -93,6 +93,8 @@ actions!(
FocusChanges,
/// Toggles automatic co-author suggestions.
ToggleFillCoAuthors,
/// Toggles sorting entries by path vs status.
ToggleSortByPath,
]
);
@ -121,6 +123,7 @@ struct GitMenuState {
has_staged_changes: bool,
has_unstaged_changes: bool,
has_new_changes: bool,
sort_by_path: bool,
}
fn git_panel_context_menu(
@ -162,6 +165,16 @@ fn git_panel_context_menu(
"Trash Untracked Files",
TrashUntrackedFiles.boxed_clone(),
)
.separator()
.entry(
if state.sort_by_path {
"Sort by Status"
} else {
"Sort by Path"
},
Some(Box::new(ToggleSortByPath)),
move |window, cx| window.dispatch_action(Box::new(ToggleSortByPath), cx),
)
})
}
@ -2450,6 +2463,24 @@ impl GitPanel {
cx.notify();
}
fn toggle_sort_by_path(
&mut self,
_: &ToggleSortByPath,
_: &mut Window,
cx: &mut Context<Self>,
) {
let current_setting = GitPanelSettings::get_global(cx).sort_by_path;
if let Some(workspace) = self.workspace.upgrade() {
let workspace = workspace.read(cx);
let fs = workspace.app_state().fs.clone();
cx.update_global::<SettingsStore, _>(|store, _cx| {
store.update_settings_file::<GitPanelSettings>(fs, move |settings, _cx| {
settings.sort_by_path = Some(!current_setting);
});
});
}
}
fn fill_co_authors(&mut self, message: &mut String, cx: &mut Context<Self>) {
const CO_AUTHOR_PREFIX: &str = "Co-authored-by: ";
@ -2986,6 +3017,7 @@ impl GitPanel {
has_staged_changes,
has_unstaged_changes,
has_new_changes,
sort_by_path: GitPanelSettings::get_global(cx).sort_by_path,
},
window,
cx,
@ -4035,6 +4067,7 @@ impl GitPanel {
has_staged_changes: self.has_staged_changes(),
has_unstaged_changes: self.has_unstaged_changes(),
has_new_changes: self.new_count > 0,
sort_by_path: GitPanelSettings::get_global(cx).sort_by_path,
},
window,
cx,
@ -4446,6 +4479,7 @@ impl Render for GitPanel {
.when(has_write_access && has_co_authors, |git_panel| {
git_panel.on_action(cx.listener(Self::toggle_fill_co_authors))
})
.on_action(cx.listener(Self::toggle_sort_by_path))
.on_hover(cx.listener(move |this, hovered, window, cx| {
if *hovered {
this.horizontal_scrollbar.show(cx);