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.

tabular_data_preview: Sort filter options natually (#64764)

Oleksandr Kholiavko8eb2fd211fcb
diff --git a/crates/tabular_data_preview/Cargo.toml b/crates/tabular_data_preview/Cargo.toml
index d61635bbfc4..092353cd350 100644
--- a/crates/tabular_data_preview/Cargo.toml
+++ b/crates/tabular_data_preview/Cargo.toml
@@ -22,12 +22,12 @@ serde.workspace = true
serde_json.workspace = true
schemars.workspace = true
text.workspace = true
+util.workspace = true
[dev-dependencies]
gpui = { workspace = true, features = ["test-support"] }
project = { workspace = true, features = ["test-support"] }
proptest.workspace = true
-util.workspace = true
workspace = { workspace = true, features = ["test-support"] }
[features]
diff --git a/crates/tabular_data_preview/src/renderer/table_header.rs b/crates/tabular_data_preview/src/renderer/table_header.rs
index 1636f584433..cb58bedc1ae 100644
--- a/crates/tabular_data_preview/src/renderer/table_header.rs
+++ b/crates/tabular_data_preview/src/renderer/table_header.rs
@@ -1,5 +1,6 @@
use std::{
borrow::Cow,
+ cmp,
collections::HashMap,
sync::{
Arc,
@@ -28,6 +29,25 @@ use crate::{
types::AnyColumn,
};
+#[derive(PartialEq, Eq)]
+struct NaturallyOrdered<'a>(&'a str);
+
+impl Ord for NaturallyOrdered<'_> {
+ fn cmp(&self, other: &Self) -> cmp::Ordering {
+ util::paths::natural_sort(self.0, other.0)
+ }
+}
+
+impl PartialOrd for NaturallyOrdered<'_> {
+ fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
+ Some(self.cmp(other))
+ }
+}
+
+fn natural_content(entry: &FilterEntry) -> Option<NaturallyOrdered<'_>> {
+ entry.content.as_deref().map(NaturallyOrdered)
+}
+
struct ColumnFilterRow {
entry: FilterEntry,
is_applied: bool,
@@ -90,14 +110,14 @@ impl ColumnFilterDelegate {
FilterSortOrder::AlphaThenCount => available.sort_by(|(a, a_app), (b, b_app)| {
b_app
.cmp(a_app)
- .then_with(|| a.content.cmp(&b.content))
+ .then_with(|| natural_content(a).cmp(&natural_content(b)))
.then_with(|| b.occurred_times().cmp(&a.occurred_times()))
}),
FilterSortOrder::CountThenAlpha => available.sort_by(|(a, a_app), (b, b_app)| {
b_app
.cmp(a_app)
.then_with(|| b.occurred_times().cmp(&a.occurred_times()))
- .then_with(|| a.content.cmp(&b.content))
+ .then_with(|| natural_content(a).cmp(&natural_content(b)))
}),
}