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.

csv_preview: Make filter unavailability order-independent (#61796)

Oleksandr Kholiavko17d71d2b6d42
diff --git a/Cargo.lock b/Cargo.lock
index a8d41d773ff..06575e38333 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4584,6 +4584,7 @@ dependencies = [
"gpui",
"log",
"picker",
+ "proptest",
"text",
"ui",
"workspace",
diff --git a/crates/csv_preview/Cargo.toml b/crates/csv_preview/Cargo.toml
index 1dbec1cc58d..bada16f1f50 100644
--- a/crates/csv_preview/Cargo.toml
+++ b/crates/csv_preview/Cargo.toml
@@ -19,6 +19,9 @@ workspace.workspace = true
log.workspace = true
text.workspace = true
+[dev-dependencies]
+proptest.workspace = true
+
[features]
dev-tools = []
diff --git a/crates/csv_preview/src/renderer/table_header.rs b/crates/csv_preview/src/renderer/table_header.rs
index bc96f239026..44f93bae75c 100644
--- a/crates/csv_preview/src/renderer/table_header.rs
+++ b/crates/csv_preview/src/renderer/table_header.rs
@@ -71,15 +71,16 @@ impl ColumnFilterDelegate {
cx: &mut Context<Picker<Self>>,
) -> Self {
let mut available: Vec<(FilterEntry, bool)> = Vec::new();
- let mut hidden: Vec<(FilterEntry, AnyColumn)> = Vec::new();
+ let mut hidden: Vec<(FilterEntry, AnyColumn, bool)> = Vec::new();
for (entry, state) in column_filters.iter() {
match state {
FilterEntryState::Available { is_applied } => {
available.push((entry.clone(), *is_applied))
}
- FilterEntryState::Unavailable { blocked_by } => {
- hidden.push((entry.clone(), *blocked_by))
- }
+ FilterEntryState::Unavailable {
+ blocked_by,
+ is_applied,
+ } => hidden.push((entry.clone(), *blocked_by, *is_applied)),
}
}
@@ -110,9 +111,9 @@ impl ColumnFilterDelegate {
.chain(
hidden
.into_iter()
- .map(|(entry, blocked_by)| ColumnFilterRow {
+ .map(|(entry, blocked_by, is_applied)| ColumnFilterRow {
entry,
- is_applied: false,
+ is_applied,
hidden_by: Some(blocked_by),
}),
)
@@ -247,8 +248,11 @@ impl ColumnFilterDelegate {
row.is_applied = is_applied;
row.hidden_by = None;
}
- FilterEntryState::Unavailable { blocked_by } => {
- row.is_applied = false;
+ FilterEntryState::Unavailable {
+ blocked_by,
+ is_applied,
+ } => {
+ row.is_applied = is_applied;
row.hidden_by = Some(blocked_by);
}
}
@@ -286,10 +290,15 @@ impl PickerDelegate for ColumnFilterDelegate {
cx.notify();
}
+ /// A hidden (blocked-by-another-filter) row can still be selected if it's currently
+ /// applied, so the user can uncheck it from the popover; a hidden-and-unapplied row
+ /// can't be newly selected, since doing so would only add a filter guaranteed to
+ /// leave zero rows.
fn can_select(&self, ix: usize, _window: &mut Window, _cx: &mut Context<Picker<Self>>) -> bool {
match self.filtered.get(ix) {
Some(ColumnFilterListEntry::Row { row_index, .. }) => {
- self.rows[*row_index].hidden_by.is_none()
+ let row = &self.rows[*row_index];
+ row.hidden_by.is_none() || row.is_applied
}
_ => false,
}
@@ -360,7 +369,7 @@ impl PickerDelegate for ColumnFilterDelegate {
return;
};
let row_index = *row_index;
- if self.rows[row_index].hidden_by.is_some() {
+ if self.rows[row_index].hidden_by.is_some() && !self.rows[row_index].is_applied {
return;
}
let col = self.col;
@@ -410,24 +419,20 @@ impl PickerDelegate for ColumnFilterDelegate {
.single_line()
.truncate();
- if row.hidden_by.is_some() {
- return Some(
- ListItem::new(("csv-filter-hidden", ix))
- .disabled(true)
- .inset(true)
- .spacing(ListItemSpacing::Sparse)
- .child(label.color(Color::Disabled))
- .end_slot(
- Label::new(count_text)
- .size(LabelSize::Small)
- .color(Color::Disabled),
- )
- .into_any_element(),
- );
- }
+ // Hidden-and-unapplied rows are disabled (can't newly select a value that
+ // would zero out the table); hidden-and-applied rows stay interactive so the
+ // user can uncheck them, same as a normal available row, just muted to signal
+ // it's currently blocked by another filter.
+ let is_hidden = row.hidden_by.is_some();
+ let color = if is_hidden {
+ Color::Disabled
+ } else {
+ Color::Muted
+ };
Some(
ListItem::new(("csv-filter-value", ix))
+ .disabled(is_hidden && !row.is_applied)
.inset(true)
.spacing(ListItemSpacing::Sparse)
.toggle_state(selected)
@@ -435,18 +440,16 @@ impl PickerDelegate for ColumnFilterDelegate {
h_flex()
.flex_none()
.when(!row.is_applied, |el| el.invisible())
- .child(
- Icon::new(IconName::Check)
- .size(IconSize::Small)
- .color(Color::Accent),
- ),
- )
- .child(label)
- .end_slot(
- Label::new(count_text)
- .size(LabelSize::Small)
- .color(Color::Muted),
+ .child(Icon::new(IconName::Check).size(IconSize::Small).color(
+ if is_hidden {
+ Color::Disabled
+ } else {
+ Color::Accent
+ },
+ )),
)
+ .child(label.color(color))
+ .end_slot(Label::new(count_text).size(LabelSize::Small).color(color))
.tooltip(Tooltip::text(value_text))
.into_any_element(),
)
@@ -459,6 +462,9 @@ impl PickerDelegate for ColumnFilterDelegate {
_window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Option<AnyElement> {
+ if !self.view.read(cx).engine.has_active_filters(self.col) {
+ return None;
+ }
let selected_rows: usize = self
.rows
.iter()
@@ -471,9 +477,6 @@ impl PickerDelegate for ColumnFilterDelegate {
.filter(|row| row.hidden_by.is_none())
.map(|row| row.entry.occurred_times())
.sum();
- if selected_rows == 0 {
- return None;
- }
let col = self.col;
Some(
diff --git a/crates/csv_preview/src/table_data_engine.rs b/crates/csv_preview/src/table_data_engine.rs
index f2613215fa5..98b4ee45d0c 100644
--- a/crates/csv_preview/src/table_data_engine.rs
+++ b/crates/csv_preview/src/table_data_engine.rs
@@ -103,7 +103,7 @@ impl DisplayToDataMapping {
}
fn apply_filtering(&mut self, filter_stack: &FilterStack, rows: &[TableRow<TableCell>]) {
- self.retained_rows = retain_rows(rows, filter_stack);
+ self.retained_rows = retain_rows(rows, filter_stack, None);
}
/// Merges pre-computed sorting and filtering into the final display mapping
diff --git a/crates/csv_preview/src/table_data_engine/filtering_by_column.rs b/crates/csv_preview/src/table_data_engine/filtering_by_column.rs
index b2b8a78c05a..6c339d901ef 100644
--- a/crates/csv_preview/src/table_data_engine/filtering_by_column.rs
+++ b/crates/csv_preview/src/table_data_engine/filtering_by_column.rs
@@ -12,8 +12,17 @@ use crate::{
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum FilterEntryState {
- Available { is_applied: bool },
- Unavailable { blocked_by: AnyColumn },
+ Available {
+ is_applied: bool,
+ },
+ /// Selecting this value would leave zero rows given every other active column's
+ /// filter. Still carries `is_applied`: a value can be both currently checked
+ /// *and* blocked (another column's filter was applied afterward), and the UI
+ /// needs to keep showing it checked so the user can uncheck it.
+ Unavailable {
+ blocked_by: AnyColumn,
+ is_applied: bool,
+ },
}
#[derive(Debug, Clone)]
@@ -32,8 +41,6 @@ impl FilterEntry {
#[derive(Debug, Default, Clone)]
pub(crate) struct FilterStack {
- /// Columns in the order their first filter was applied, used to compute cascade availability
- activation_order: Vec<AnyColumn>,
/// Which cell values are currently allowed for each filtered column
retention_config: HashMap<AnyColumn, HashSet<Option<SharedString>>>,
}
@@ -43,12 +50,12 @@ impl TableDataEngine {
self.filter_stack.retention_config.contains_key(&col)
}
- /// Get available filters for a specific column with cascade behavior.
- ///
- /// A filter entry is "unavailable" when all of its rows are hidden by a
- /// filter on an earlier-activated column, meaning selecting it would show
- /// zero rows. The cascade walk stops at `column` so that the column's own
- /// current filter does not affect its own entry availability.
+ /// Marks an entry unavailable if choosing it would leave zero rows, so users
+ /// aren't offered filter values that lead to an empty table. Every other
+ /// active column's filter counts toward this, regardless of activation
+ /// order: `column`'s own filter is excluded (so its own current selection
+ /// doesn't block its other values), but a column filtered after `column`
+ /// blocks exactly as much as one filtered before it.
pub(crate) fn get_filters_for_column(
&self,
column: AnyColumn,
@@ -58,47 +65,6 @@ impl TableDataEngine {
.get(&column)
.ok_or_else(|| anyhow::anyhow!("Expected {column:?} to have filter entries"))?;
- let mut unavailable_entries: HashMap<Option<SharedString>, AnyColumn> = HashMap::new();
-
- for &column_applied_previously in &self.filter_stack.activation_order {
- if column_applied_previously == column {
- break;
- }
-
- let retained_values = self
- .filter_stack
- .retention_config
- .get(&column_applied_previously)
- .ok_or_else(|| {
- anyhow::anyhow!(
- "Expected {column_applied_previously:?} to have retained entries \
- as it is present in the filter stack"
- )
- })?;
-
- // Rows that survive the filter on `column_applied_previously`
- let retained_rows: HashSet<DataRow> = self
- .contents
- .rows
- .iter()
- .enumerate()
- .filter(|(_, row)| {
- let cell_value = row
- .get(column_applied_previously)
- .and_then(|cell| cell.display_value().cloned());
- retained_values.contains(&cell_value)
- })
- .map(|(index, _)| DataRow(index))
- .collect();
-
- // An entry is unavailable when none of its rows survive the parent filter
- for entry in all_column_entries {
- if !entry.rows.iter().any(|row| retained_rows.contains(row)) {
- unavailable_entries.insert(entry.content.clone(), column_applied_previously);
- }
- }
- }
-
let empty = HashSet::new();
let active_column_filters = self
.filter_stack
@@ -106,27 +72,61 @@ impl TableDataEngine {
.get(&column)
.unwrap_or(&empty);
- Ok(Arc::new(
- all_column_entries
- .iter()
- .map(|entry| {
- let state = if let Some(&blocked_by) = unavailable_entries.get(&entry.content) {
- FilterEntryState::Unavailable { blocked_by }
- } else {
- FilterEntryState::Available {
- is_applied: active_column_filters.contains(&entry.content),
- }
- };
- (entry.clone(), state)
- })
- .collect(),
- ))
+ // Rows that survive every *other* active column's filter. `column`'s own
+ // filter is excluded so its entries reflect what selecting them would
+ // add rather than being constrained by the current selection.
+ let rows_passing_other_filters =
+ retain_rows(&self.contents.rows, &self.filter_stack, Some(column));
+
+ // Only used to populate `Unavailable::blocked_by`, which the UI doesn't
+ // display by name; any other currently active column is a valid choice.
+ let blocking_column = self
+ .filter_stack
+ .retention_config
+ .keys()
+ .find(|&&col| col != column)
+ .copied();
+
+ all_column_entries
+ .iter()
+ .map(|entry| {
+ let adjusted_rows: Vec<DataRow> = entry
+ .rows
+ .iter()
+ .filter(|row| rows_passing_other_filters.contains(row))
+ .copied()
+ .collect();
+
+ let is_applied = active_column_filters.contains(&entry.content);
+ let state = if adjusted_rows.is_empty() {
+ let blocked_by = blocking_column.ok_or_else(|| {
+ anyhow::anyhow!(
+ "Expected an active column other than {column:?} to block \
+ {:?} when it has no rows passing other filters",
+ entry.content
+ )
+ })?;
+ FilterEntryState::Unavailable {
+ blocked_by,
+ is_applied,
+ }
+ } else {
+ FilterEntryState::Available { is_applied }
+ };
+
+ Ok((
+ FilterEntry {
+ content: entry.content.clone(),
+ rows: adjusted_rows,
+ },
+ state,
+ ))
+ })
+ .collect::<anyhow::Result<Vec<_>>>()
+ .map(Arc::new)
}
pub(crate) fn clear_filters_for_col(&mut self, col: AnyColumn) {
- self.filter_stack
- .activation_order
- .retain(|&entry| entry != col);
self.filter_stack.retention_config.remove(&col);
}
@@ -172,9 +172,6 @@ impl TableDataEngine {
if entries.len() == 1 {
self.filter_stack.retention_config.remove(&column);
- self.filter_stack
- .activation_order
- .retain(|&entry| entry != column);
} else {
entries.remove(&value);
}
@@ -182,10 +179,6 @@ impl TableDataEngine {
}
fn apply_filter(&mut self, column: AnyColumn, value: Option<SharedString>) {
- // Track the column only on its first activation to preserve cascade order
- if !self.filter_stack.activation_order.contains(&column) {
- self.filter_stack.activation_order.push(column);
- }
self.filter_stack
.retention_config
.entry(column)
@@ -226,10 +219,12 @@ pub fn calculate_available_filters(
available_filters
}
-/// Returns the set of data rows that survive all active filters in the stack.
+/// Returns the set of data rows that survive all active filters in the stack,
+/// optionally ignoring the filter on `exclude` without cloning the stack.
pub fn retain_rows(
content_rows: &[TableRow<TableCell>],
filter_stack: &FilterStack,
+ exclude: Option<AnyColumn>,
) -> HashSet<DataRow> {
let config = &filter_stack.retention_config;
if config.is_empty() {
@@ -241,6 +236,9 @@ pub fn retain_rows(
.enumerate()
.filter(|(_, row)| {
config.iter().all(|(col, allowed_values)| {
+ if Some(*col) == exclude {
+ return true;
+ }
let cell_value = row.get(*col).and_then(|cell| cell.display_value().cloned());
allowed_values.contains(&cell_value)
})
@@ -248,3 +246,127 @@ pub fn retain_rows(
.map(|(index, _)| DataRow(index))
.collect()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::types::TableLikeContent;
+ use proptest::prelude::*;
+
+ fn small_value() -> impl Strategy<Value = &'static str> {
+ prop::sample::select(&["a", "b", "c"])
+ }
+
+ /// Generates a small rectangular CSV table (header + data rows) built
+ /// from a tiny value alphabet, so duplicate values across rows/columns
+ /// are frequent enough to exercise cross-column blocking. Returns the
+ /// column count alongside the CSV text since callers need it to bound
+ /// generated column indices.
+ fn csv_table() -> impl Strategy<Value = (usize, String)> {
+ (2usize..=6, 2usize..=3).prop_flat_map(|(rows, cols)| {
+ prop::collection::vec(prop::collection::vec(small_value(), cols), rows).prop_map(
+ move |grid| {
+ let header = (0..cols)
+ .map(|c| format!("c{c}"))
+ .collect::<Vec<_>>()
+ .join(",");
+ let mut lines = vec![header];
+ lines.extend(grid.into_iter().map(|row| row.join(",")));
+ (cols, lines.join("\n"))
+ },
+ )
+ })
+ }
+
+ fn build_engine(csv_text: &str) -> TableDataEngine {
+ let mut engine = TableDataEngine::default();
+ engine.contents = Arc::new(TableLikeContent::from_str(csv_text.to_string()));
+ engine.calculate_available_filters();
+ engine
+ }
+
+ /// Resolves raw `(column_index, value_index)` pairs (generated modulo
+ /// nothing in particular, so out of range in general) into concrete,
+ /// deduplicated `(AnyColumn, value)` toggles by reading each column's
+ /// entries from an untouched engine and wrapping indices to valid ranges.
+ fn resolve_toggles(
+ engine: &TableDataEngine,
+ cols: usize,
+ raw: &[(usize, usize)],
+ ) -> Vec<(AnyColumn, Option<SharedString>)> {
+ let mut toggles = Vec::new();
+ for &(col_idx, value_idx) in raw {
+ let column = AnyColumn::new(col_idx % cols);
+ let entries = engine.get_filters_for_column(column).unwrap();
+ if entries.is_empty() {
+ continue;
+ }
+ let value = entries[value_idx % entries.len()].0.content.clone();
+ let pair = (column, value);
+ if !toggles.contains(&pair) {
+ toggles.push(pair);
+ }
+ }
+ toggles
+ }
+
+ /// Per-column `(value, is_available, is_applied)` triples, flattening
+ /// `FilterEntryState` for comparison while dropping `blocked_by`: any
+ /// other currently active column is an equally valid blocker, so its
+ /// *identity* legitimately differs across activation orders, but
+ /// availability and applied-ness must not.
+ type StateSnapshot = Vec<(AnyColumn, Vec<(Option<SharedString>, bool, bool)>)>;
+
+ fn apply_and_snapshot(
+ engine: &mut TableDataEngine,
+ cols: usize,
+ toggles: &[(AnyColumn, Option<SharedString>)],
+ ) -> StateSnapshot {
+ for (column, value) in toggles {
+ engine.toggle_filter(*column, value.clone()).unwrap();
+ }
+
+ (0..cols)
+ .map(|col_idx| {
+ let column = AnyColumn::new(col_idx);
+ let entries = engine.get_filters_for_column(column).unwrap();
+ let mut column_snapshot: Vec<(Option<SharedString>, bool, bool)> = entries
+ .iter()
+ .map(|(entry, state)| match *state {
+ FilterEntryState::Available { is_applied } => {
+ (entry.content.clone(), true, is_applied)
+ }
+ FilterEntryState::Unavailable { is_applied, .. } => {
+ (entry.content.clone(), false, is_applied)
+ }
+ })
+ .collect();
+ // `all_column_entries` comes from a `HashMap`, so its iteration
+ // order isn't stable across engine instances built from the
+ // same data; sort so that's not mistaken for a real divergence.
+ column_snapshot.sort();
+ (column, column_snapshot)
+ })
+ .collect()
+ }
+
+ proptest! {
+ #[test]
+ fn filter_states_are_order_independent(
+ (cols, csv_text) in csv_table(),
+ raw_toggles in prop::collection::vec((0usize..3, 0usize..3), 0..8),
+ ) {
+ let toggles = resolve_toggles(&build_engine(&csv_text), cols, &raw_toggles);
+
+ let mut forward_engine = build_engine(&csv_text);
+ let forward = apply_and_snapshot(&mut forward_engine, cols, &toggles);
+
+ let mut reversed = toggles.clone();
+ reversed.reverse();
+ let mut backward_engine = build_engine(&csv_text);
+ let backward = apply_and_snapshot(&mut backward_engine, cols, &reversed);
+
+ prop_assert_eq!(forward, backward);
+ }
+ }
+}