Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

project_panel: Add extension sort order (#65776)

Chinmay Talnikard7adf3e7e278
diff --git a/assets/settings/default.json b/assets/settings/default.json
index bbe25523d24..ed88af25e32 100644
--- a/assets/settings/default.json
+++ b/assets/settings/default.json
@@ -967,7 +967,7 @@
// "files_first"
"sort_mode": "directories_first",
// Whether to sort file and folder names case-sensitively in the project panel.
- // This setting can take four values:
+ // This setting can take five values:
//
// 1. Case-insensitive natural sort with lowercase preferred in ties (default):
// "default"
@@ -980,6 +980,10 @@
// 4. Pure Unicode codepoint comparison.
// No case folding, no natural number sorting:
// "unicode"
+ // 5. Files are grouped by extension, with extensionless files (including
+ // dotfiles such as ".gitignore") first and natural sort by name within
+ // each group:
+ // "extension"
"sort_order": "default",
// Whether to show error and warning count badges next to file names in the project panel.
"diagnostic_badges": false,
diff --git a/crates/project_panel/benches/sorting.rs b/crates/project_panel/benches/sorting.rs
index 9e11e0a1146..5fe1af87d05 100644
--- a/crates/project_panel/benches/sorting.rs
+++ b/crates/project_panel/benches/sorting.rs
@@ -55,6 +55,7 @@ fn criterion_benchmark(c: &mut Criterion) {
("Upper", ProjectPanelSortOrder::Upper),
("Lower", ProjectPanelSortOrder::Lower),
("Unicode", ProjectPanelSortOrder::Unicode),
+ ("Extension", ProjectPanelSortOrder::Extension),
];
for (mode_name, mode) in &modes {
diff --git a/crates/project_panel/src/project_panel_tests.rs b/crates/project_panel/src/project_panel_tests.rs
index 815dfd55ea1..55772893ef7 100644
--- a/crates/project_panel/src/project_panel_tests.rs
+++ b/crates/project_panel/src/project_panel_tests.rs
@@ -11619,6 +11619,57 @@ async fn test_sort_mode_mixed(cx: &mut gpui::TestAppContext) {
);
}
+#[gpui::test]
+async fn test_sort_order_extension(cx: &mut gpui::TestAppContext) {
+ init_test(cx);
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ "/root",
+ json!({
+ "zebra.rs": "",
+ "carrot": {},
+ "banana.py": "",
+ "Makefile": "",
+ "apple.RS": "",
+ "aardvark.py": "",
+ }),
+ )
+ .await;
+
+ let project = Project::test(fs.clone(), ["/root".as_ref()], cx).await;
+ let window = cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace = window
+ .read_with(cx, |mw, _| mw.workspace().clone())
+ .unwrap();
+ let cx = &mut VisualTestContext::from_window(window.into(), cx);
+
+ cx.update(|_, cx| {
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |settings| {
+ settings.project_panel.get_or_insert_default().sort_order =
+ Some(settings::ProjectPanelSortOrder::Extension);
+ });
+ });
+ });
+
+ let panel = workspace.update_in(cx, ProjectPanel::new);
+ cx.run_until_parked();
+
+ assert_eq!(
+ visible_entries_as_strings(&panel, 0..50, cx),
+ &[
+ "v root",
+ " > carrot",
+ " Makefile",
+ " aardvark.py",
+ " banana.py",
+ " apple.RS",
+ " zebra.rs",
+ ]
+ );
+}
+
#[gpui::test]
async fn test_sort_mode_files_first(cx: &mut gpui::TestAppContext) {
init_test(cx);
diff --git a/crates/settings_content/src/workspace.rs b/crates/settings_content/src/workspace.rs
index 0a6429ec2e5..15b2f84ecc7 100644
--- a/crates/settings_content/src/workspace.rs
+++ b/crates/settings_content/src/workspace.rs
@@ -1056,6 +1056,11 @@ pub enum ProjectPanelSortOrder {
/// Pure Unicode codepoint comparison. No case folding, no natural number sorting.
/// Uppercase ASCII sorts before lowercase. Accented characters sort after ASCII.
Unicode,
+ /// Files are grouped by their last extension (case-insensitive) in ascending order,
+ /// with extensionless files, including dotfiles such as `.gitignore`, first.
+ /// Dotfiles with a further suffix, such as `.config.json`, keep that extension.
+ /// Within a group, names use the default natural sort.
+ Extension,
}
impl From<ProjectPanelSortMode> for util::paths::SortMode {
@@ -1075,6 +1080,7 @@ impl From<ProjectPanelSortOrder> for util::paths::SortOrder {
ProjectPanelSortOrder::Upper => Self::Upper,
ProjectPanelSortOrder::Lower => Self::Lower,
ProjectPanelSortOrder::Unicode => Self::Unicode,
+ ProjectPanelSortOrder::Extension => Self::Extension,
}
}
}
diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs
index d34dd77a0ac..03c595ce88e 100644
--- a/crates/util/src/paths.rs
+++ b/crates/util/src/paths.rs
@@ -1078,6 +1078,11 @@ pub enum SortOrder {
/// Pure Unicode codepoint comparison. No case folding, no natural number sorting.
/// Uppercase ASCII sorts before lowercase. Accented characters sort after ASCII.
Unicode,
+ /// Files are grouped by their last extension (case-insensitive) in ascending order,
+ /// with extensionless files, including dotfiles such as `.gitignore`, first.
+ /// Dotfiles with a further suffix, such as `.config.json`, keep that extension.
+ /// Within a group, names use the same natural sort as `Default`.
+ Extension,
}
/// Controls how files and directories are ordered relative to each other.
@@ -1176,6 +1181,17 @@ pub fn compare_rel_paths_by(
let (b_stem, b_ext) = b_leaf_file
.then(|| stem_and_extension(component_b))
.unwrap_or_default();
+ if order == SortOrder::Extension {
+ // Directories have no extension and must take part in this comparison too,
+ // otherwise mixed mode would produce an inconsistent (non-transitive) ordering.
+ let extension_ordering = a_ext
+ .unwrap_or_default()
+ .to_lowercase()
+ .cmp(&b_ext.unwrap_or_default().to_lowercase());
+ if !extension_ordering.is_eq() {
+ return extension_ordering;
+ }
+ }
let a_key = if a_leaf_file {
a_stem
} else {
@@ -1974,6 +1990,86 @@ mod tests {
);
}
+ #[perf]
+ fn compare_rel_paths_extension_groups_files() {
+ let mut paths = vec![
+ (RelPath::from_unix_str("b.rs").unwrap(), true),
+ (RelPath::from_unix_str("a.py").unwrap(), true),
+ (RelPath::from_unix_str("a.RS").unwrap(), true),
+ (RelPath::from_unix_str("file10.rs").unwrap(), true),
+ (RelPath::from_unix_str("file2.rs").unwrap(), true),
+ (RelPath::from_unix_str("Makefile").unwrap(), true),
+ (RelPath::from_unix_str(".gitignore").unwrap(), true),
+ (RelPath::from_unix_str(".config.json").unwrap(), true),
+ (RelPath::from_unix_str(".env.local").unwrap(), true),
+ (RelPath::from_unix_str("a.css").unwrap(), true),
+ (RelPath::from_unix_str("archive.tar.gz").unwrap(), true),
+ (RelPath::from_unix_str("src").unwrap(), false),
+ ];
+ paths.sort_by(|&a, &b| {
+ compare_rel_paths_by(a, b, SortMode::DirectoriesFirst, SortOrder::Extension)
+ });
+ assert_eq!(
+ paths,
+ vec![
+ (RelPath::from_unix_str("src").unwrap(), false),
+ (RelPath::from_unix_str(".gitignore").unwrap(), true),
+ (RelPath::from_unix_str("Makefile").unwrap(), true),
+ (RelPath::from_unix_str("a.css").unwrap(), true),
+ (RelPath::from_unix_str("archive.tar.gz").unwrap(), true),
+ (RelPath::from_unix_str(".config.json").unwrap(), true),
+ (RelPath::from_unix_str(".env.local").unwrap(), true),
+ (RelPath::from_unix_str("a.py").unwrap(), true),
+ (RelPath::from_unix_str("a.RS").unwrap(), true),
+ (RelPath::from_unix_str("b.rs").unwrap(), true),
+ (RelPath::from_unix_str("file2.rs").unwrap(), true),
+ (RelPath::from_unix_str("file10.rs").unwrap(), true),
+ ]
+ );
+ }
+
+ #[perf]
+ fn compare_rel_paths_extension_with_files_first() {
+ let mut paths = vec![
+ (RelPath::from_unix_str("z.rs").unwrap(), true),
+ (RelPath::from_unix_str("a.txt").unwrap(), true),
+ (RelPath::from_unix_str("m").unwrap(), false),
+ (RelPath::from_unix_str("b.py").unwrap(), true),
+ ];
+ paths.sort_by(|&a, &b| {
+ compare_rel_paths_by(a, b, SortMode::FilesFirst, SortOrder::Extension)
+ });
+ assert_eq!(
+ paths,
+ vec![
+ (RelPath::from_unix_str("b.py").unwrap(), true),
+ (RelPath::from_unix_str("z.rs").unwrap(), true),
+ (RelPath::from_unix_str("a.txt").unwrap(), true),
+ (RelPath::from_unix_str("m").unwrap(), false),
+ ]
+ );
+ }
+
+ #[perf]
+ fn compare_rel_paths_extension_mixed_is_consistent() {
+ let mut paths = vec![
+ (RelPath::from_unix_str("z.rs").unwrap(), true),
+ (RelPath::from_unix_str("a.txt").unwrap(), true),
+ (RelPath::from_unix_str("m").unwrap(), false),
+ (RelPath::from_unix_str("b").unwrap(), true),
+ ];
+ paths.sort_by(|&a, &b| compare_rel_paths_by(a, b, SortMode::Mixed, SortOrder::Extension));
+ assert_eq!(
+ paths,
+ vec![
+ (RelPath::from_unix_str("b").unwrap(), true),
+ (RelPath::from_unix_str("m").unwrap(), false),
+ (RelPath::from_unix_str("z.rs").unwrap(), true),
+ (RelPath::from_unix_str("a.txt").unwrap(), true),
+ ]
+ );
+ }
+
#[perf]
fn compare_rel_paths_mixed_same_stem_different_extension() {
// Files with same stem but different extensions should sort by extension
diff --git a/docs/src/project-panel.md b/docs/src/project-panel.md
index 8619cbc3875..9fbe29ee4d5 100644
--- a/docs/src/project-panel.md
+++ b/docs/src/project-panel.md
@@ -180,6 +180,10 @@ The `project_panel.sort_order` setting controls name comparison:
- `"upper"` — uppercase names grouped first, then lowercase.
- `"lower"` — lowercase names grouped first, then uppercase.
- `"unicode"` — raw Unicode codepoint order with no case folding.
+- `"extension"` — files grouped by extension (case-insensitive, last suffix
+ only), with extensionless files, including dotfiles such as `.gitignore`,
+ first. Dotfiles with a further suffix, such as `.config.json`, keep that
+ extension. Files with the same extension are sorted by name.
## Other Actions
diff --git a/docs/src/reference/all-settings.md b/docs/src/reference/all-settings.md
index fc14b42f7e8..e206f51fc8c 100644
--- a/docs/src/reference/all-settings.md
+++ b/docs/src/reference/all-settings.md
@@ -6024,6 +6024,16 @@ Available variables:
}
```
+5. Files are grouped by their last extension (case-insensitive) in ascending order, with extensionless files, including dotfiles such as `.gitignore`, first. Dotfiles with a further suffix, such as `.config.json`, keep that extension. Files with the same extension are sorted by name using natural sort, and directories are placed according to `sort_mode`.
+
+```json [settings]
+{
+ "project_panel": {
+ "sort_order": "extension"
+ }
+}
+```
+
### Auto Open
- Description: Control whether files are opened automatically after different creation flows in the project panel.
diff --git a/docs/src/visual-customization.md b/docs/src/visual-customization.md
index 3ab4692c3fd..6253f5d5cf5 100644
--- a/docs/src/visual-customization.md
+++ b/docs/src/visual-customization.md
@@ -515,6 +515,7 @@ TBD: Centered layout related settings
// "upper": Uppercase names grouped before lowercase, natural sort within.
// "lower": Lowercase names grouped before uppercase, natural sort within.
// "unicode": Pure Unicode codepoint comparison, no case folding.
+ // "extension": Files grouped by extension, extensionless files (including dotfiles like ".gitignore") first.
"sort_order": "default",
// Whether to hide the root entry when only one folder is open in the window;
// this also affects how file paths appear in the file finder history.