branch picker: Always show HEAD first (#11552)
I think this is a lot less confusing than another branch being selected by default when there's no query. Release Notes: - Changed the branch picker to always show the current branch as the default selected entry. https://github.com/zed-industries/zed/assets/1185253/18b50656-f6ac-4138-b4e0-9024072e1555
This commit is contained in:
parent
9d681bda8d
commit
ca680f07f7
2 changed files with 16 additions and 7 deletions
|
@ -19,6 +19,7 @@ pub use git2::Repository as LibGitRepository;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Hash, PartialEq)]
|
#[derive(Clone, Debug, Hash, PartialEq)]
|
||||||
pub struct Branch {
|
pub struct Branch {
|
||||||
|
pub is_head: bool,
|
||||||
pub name: Box<str>,
|
pub name: Box<str>,
|
||||||
/// Timestamp of most recent commit, normalized to Unix Epoch format.
|
/// Timestamp of most recent commit, normalized to Unix Epoch format.
|
||||||
pub unix_timestamp: Option<i64>,
|
pub unix_timestamp: Option<i64>,
|
||||||
|
@ -202,6 +203,7 @@ impl GitRepository for RealGitRepository {
|
||||||
let valid_branches = local_branches
|
let valid_branches = local_branches
|
||||||
.filter_map(|branch| {
|
.filter_map(|branch| {
|
||||||
branch.ok().and_then(|(branch, _)| {
|
branch.ok().and_then(|(branch, _)| {
|
||||||
|
let is_head = branch.is_head();
|
||||||
let name = branch.name().ok().flatten().map(Box::from)?;
|
let name = branch.name().ok().flatten().map(Box::from)?;
|
||||||
let timestamp = branch.get().peel_to_commit().ok()?.time();
|
let timestamp = branch.get().peel_to_commit().ok()?.time();
|
||||||
let unix_timestamp = timestamp.seconds();
|
let unix_timestamp = timestamp.seconds();
|
||||||
|
@ -211,6 +213,7 @@ impl GitRepository for RealGitRepository {
|
||||||
let unix_timestamp =
|
let unix_timestamp =
|
||||||
time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?;
|
time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?;
|
||||||
Some(Branch {
|
Some(Branch {
|
||||||
|
is_head,
|
||||||
name,
|
name,
|
||||||
unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()),
|
unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()),
|
||||||
})
|
})
|
||||||
|
|
|
@ -159,14 +159,20 @@ impl PickerDelegate for BranchListDelegate {
|
||||||
let candidates = picker.update(&mut cx, |view, _| {
|
let candidates = picker.update(&mut cx, |view, _| {
|
||||||
const RECENT_BRANCHES_COUNT: usize = 10;
|
const RECENT_BRANCHES_COUNT: usize = 10;
|
||||||
let mut branches = view.delegate.all_branches.clone();
|
let mut branches = view.delegate.all_branches.clone();
|
||||||
if query.is_empty() && branches.len() > RECENT_BRANCHES_COUNT {
|
if query.is_empty() {
|
||||||
// Truncate list of recent branches
|
if branches.len() > RECENT_BRANCHES_COUNT {
|
||||||
// Do a partial sort to show recent-ish branches first.
|
// Truncate list of recent branches
|
||||||
branches.select_nth_unstable_by(RECENT_BRANCHES_COUNT - 1, |lhs, rhs| {
|
// Do a partial sort to show recent-ish branches first.
|
||||||
rhs.unix_timestamp.cmp(&lhs.unix_timestamp)
|
branches.select_nth_unstable_by(RECENT_BRANCHES_COUNT - 1, |lhs, rhs| {
|
||||||
|
rhs.is_head
|
||||||
|
.cmp(&lhs.is_head)
|
||||||
|
.then(rhs.unix_timestamp.cmp(&lhs.unix_timestamp))
|
||||||
|
});
|
||||||
|
branches.truncate(RECENT_BRANCHES_COUNT);
|
||||||
|
}
|
||||||
|
branches.sort_unstable_by(|lhs, rhs| {
|
||||||
|
rhs.is_head.cmp(&lhs.is_head).then(lhs.name.cmp(&rhs.name))
|
||||||
});
|
});
|
||||||
branches.truncate(RECENT_BRANCHES_COUNT);
|
|
||||||
branches.sort_unstable_by(|lhs, rhs| lhs.name.cmp(&rhs.name));
|
|
||||||
}
|
}
|
||||||
branches
|
branches
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue