Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions crates/project_panel/src/project_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6679,8 +6679,89 @@ fn cmp_files_first(a: &Entry, b: &Entry) -> cmp::Ordering {
util::paths::compare_rel_paths_files_first((&a.path, a.is_file()), (&b.path, b.is_file()))
}

#[inline]
fn is_entry_dir(kind: &EntryKind) -> bool {
matches!(
kind,
EntryKind::UnloadedDir | EntryKind::PendingDir | EntryKind::Dir
)
}

#[inline]
fn sort_mode_rank(is_dir: bool, mode: &settings::ProjectPanelSortMode) -> Option<u8> {
match mode {
settings::ProjectPanelSortMode::Mixed => None,
settings::ProjectPanelSortMode::DirectoriesFirst => Some(if is_dir { 0 } else { 1 }),
settings::ProjectPanelSortMode::FilesFirst => Some(if is_dir { 1 } else { 0 }),
}
}

#[inline]
fn ordering_for_first_entry(first_is_new: bool, new_before_other: bool) -> cmp::Ordering {
match (first_is_new, new_before_other) {
(true, true) => cmp::Ordering::Less,
(true, false) => cmp::Ordering::Greater,
(false, true) => cmp::Ordering::Greater,
(false, false) => cmp::Ordering::Less,
}
}

/// Compare two entries when creating a new entry.
///
/// ## Calculation
/// | Mode | Creating | Placement In Parent |
/// |------------------|----------|---------------------------|
/// | DirectoriesFirst | dir | top |
/// | DirectoriesFirst | file | top of files (after dirs) |
/// | Mixed | dir | top |
/// | Mixed | file | top |
/// | FilesFirst | dir | top of dirs (after files) |
/// | FilesFirst | file | top |
#[inline]
fn cmp_new_entry(
a: &Entry,
b: &Entry,
mode: &settings::ProjectPanelSortMode,
) -> Option<cmp::Ordering> {
let (new_entry, other, first_is_new) = if a.id == NEW_ENTRY_ID {
(a, b, true)
} else {
(b, a, false)
};

if other.path.parent()? != new_entry.path.parent()? {
return None;
}

let new_is_dir = is_entry_dir(&new_entry.kind);
let other_is_dir = is_entry_dir(&other.kind);

if let (Some(new_rank), Some(other_rank)) = (
sort_mode_rank(new_is_dir, mode),
sort_mode_rank(other_is_dir, mode),
) {
match new_rank.cmp(&other_rank) {
cmp::Ordering::Less => return Some(ordering_for_first_entry(first_is_new, true)),
cmp::Ordering::Greater => return Some(ordering_for_first_entry(first_is_new, false)),
_ => {}
}
} else {
if new_is_dir != other_is_dir {
return None;
}
}

Some(ordering_for_first_entry(first_is_new, true))
}

#[inline]
fn cmp_with_mode(a: &Entry, b: &Entry, mode: &settings::ProjectPanelSortMode) -> cmp::Ordering {
if a.id == NEW_ENTRY_ID || b.id == NEW_ENTRY_ID {
if let Some(ordering) = cmp_new_entry(a, b, mode) {
return ordering;
}
}

match mode {
settings::ProjectPanelSortMode::DirectoriesFirst => cmp_directories_first(a, b),
settings::ProjectPanelSortMode::Mixed => cmp_mixed(a, b),
Expand Down
Loading