Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
csv_preview: Add row filtering feature (#60339)
Oleksandr Kholiavkod9e35975c238
diff --git a/crates/csv_preview/src/csv_preview.rs b/crates/csv_preview/src/csv_preview.rsindex 322777da042..8f8ba768754 100644--- a/crates/csv_preview/src/csv_preview.rs+++ b/crates/csv_preview/src/csv_preview.rs@@ -8,7 +8,7 @@ use std::{time::{Duration, Instant},};-use crate::table_data_engine::TableDataEngine;+use crate::table_data_engine::{DisplayToDataMapping, TableDataEngine};use ui::{AbsoluteLength, ResizableColumnsState, SharedString, TableInteractionState,TableResizeBehavior, prelude::*,@@ -41,6 +41,10 @@ pub struct CsvPreviewView {pub(crate) table_interaction_state: Entity<TableInteractionState>,pub(crate) column_widths: ColumnWidths,pub(crate) parsing_task: Option<Task<anyhow::Result<()>>>,+ pub(crate) is_parsing: bool,+ /// Background task computing the display-to-data mapping after a filter/sort change.+ /// Stored here so that a new change cancels the previous in-flight computation.+ pub(crate) filter_sort_task: Option<Task<()>>,pub(crate) settings: CsvPreviewSettings,/// Performance metrics for debugging and monitoring CSV operations.pub(crate) performance_metrics: PerformanceMetrics,@@ -178,9 +182,11 @@ impl CsvPreviewView {table_interaction_state,column_widths: ColumnWidths::new(cx, 1),parsing_task: None,+ is_parsing: false,+ filter_sort_task: None,performance_metrics: PerformanceMetrics::default(),list_state: gpui::ListState::new(contents.rows.len(), ListAlignment::Top, px(1.))- .measure_all(),+ .with_uniform_item_height(px(24.)),settings: CsvPreviewSettings::default(),last_parse_end_time: None,engine: TableDataEngine::default(),@@ -194,22 +200,54 @@ impl CsvPreviewView {pub(crate) fn editor_state(&self) -> &EditorState {&self.active_editor_state}- pub(crate) fn apply_sort(&mut self) {- self.performance_metrics.record("Sort", || {- self.engine.apply_sort();- });+ pub(crate) fn apply_sort(&mut self, cx: &mut Context<Self>) {+ self.apply_filter_sort(cx);}- /// Update ordered indices when ordering or content changes- pub(crate) fn apply_filter_sort(&mut self) {- self.performance_metrics.record("Filter&sort", || {- self.engine.calculate_d2d_mapping();- });+ pub fn clear_filters(&mut self, col: types::AnyColumn, cx: &mut Context<Self>) {+ self.engine.clear_filters_for_col(col);+ self.apply_filter_sort(cx);+ }- // Update list state with filtered row count- let visible_rows = self.engine.d2d_mapping().visible_row_count();- self.list_state =- gpui::ListState::new(visible_rows, ListAlignment::Top, px(100.)).measure_all();+ pub fn toggle_filter(+ &mut self,+ col: types::AnyColumn,+ value: Option<SharedString>,+ cx: &mut Context<Self>,+ ) {+ if let Err(err) = self.engine.toggle_filter(col, value) {+ log::error!("Failed to toggle filter: {err}");+ return;+ }+ self.apply_filter_sort(cx);+ }++ /// Spawns a background task to recompute the display-to-data mapping after a filter or sort+ /// change. Storing the task cancels any previous in-flight computation automatically.+ pub(crate) fn apply_filter_sort(&mut self, cx: &mut Context<Self>) {+ let contents = self.engine.contents.clone();+ let filter_stack = self.engine.filter_stack.clone();+ let sorting = self.engine.applied_sorting;++ self.filter_sort_task = Some(cx.spawn(async move |this, cx| {+ let mapping = cx+ .background_spawn(async move {+ DisplayToDataMapping::compute(&contents, &filter_stack, sorting)+ })+ .await;++ this.update(cx, |view, cx| {+ view.engine.set_d2d_mapping(mapping);+ let visible_rows = view.engine.d2d_mapping().visible_row_count();+ // Approximation of single csv table row height. Will be re-measured on scrolling.+ // This cheap solution allow to render scrollbar with fraction of a cost compared to `.measure_all()` call+ let approximate_height = px(24.);+ view.list_state+ .reset_with_uniform_height(visible_rows, approximate_height);+ cx.notify();+ })+ .ok();+ }));}pub fn resolve_active_item_as_csv_editor(@@ -301,7 +339,7 @@ impl PerformanceMetrics {.map(|(name, (duration, time))| {let took = duration.as_secs_f32() * 1000.;let ago = time.elapsed().as_secs();- format!("{name}: {took:.2}ms {ago}s ago")+ format!("{name}: {took:.3}ms {ago}s ago")}).collect::<Vec<_>>().join("\n")diff --git a/crates/csv_preview/src/parser.rs b/crates/csv_preview/src/parser.rsindex efa3573d7aa..116c8912a38 100644--- a/crates/csv_preview/src/parser.rs+++ b/crates/csv_preview/src/parser.rs@@ -1,3 +1,5 @@+use std::sync::Arc;+use crate::{CsvPreviewView,types::TableLikeContent,@@ -23,6 +25,7 @@ impl CsvPreviewView {cx: &mut Context<Self>,) {let editor = self.active_editor_state.editor.clone();+ self.is_parsing = true;self.parsing_task = Some(self.parse_csv_in_background(wait_for_debounce, editor, cx));}@@ -80,11 +83,13 @@ impl CsvPreviewView {.insert("Parsing", (parse_duration, Instant::now()));log::debug!("Parsed {} rows", parsed_csv.rows.len());- view.engine.contents = parsed_csv;+ view.engine.contents = Arc::new(parsed_csv);+ view.engine.calculate_available_filters();view.sync_column_widths(cx);view.last_parse_end_time = Some(parse_end_time);- view.apply_filter_sort();+ view.is_parsing = false;+ view.apply_filter_sort(cx);cx.notify();})})diff --git a/crates/csv_preview/src/renderer/performance_metrics_overlay.rs b/crates/csv_preview/src/renderer/performance_metrics_overlay.rsindex 3d0cf50cf1d..d9e7ce6ad9b 100644--- a/crates/csv_preview/src/renderer/performance_metrics_overlay.rs+++ b/crates/csv_preview/src/renderer/performance_metrics_overlay.rs@@ -20,7 +20,7 @@ impl CsvPreviewView {let children = div().absolute()- .top_24()+ .bottom_8().right_4().px_3().py_2()diff --git a/crates/csv_preview/src/renderer/preview_view.rs b/crates/csv_preview/src/renderer/preview_view.rsindex 90500d53d06..335633656af 100644--- a/crates/csv_preview/src/renderer/preview_view.rs+++ b/crates/csv_preview/src/renderer/preview_view.rs@@ -1,6 +1,6 @@use std::time::Instant;-use ui::{div, prelude::*};+use ui::{SpinnerLabel, div, prelude::*};use crate::CsvPreviewView;@@ -11,12 +11,12 @@ impl Render for CsvPreviewView {let render_prep_start = Instant::now();let table_with_settings = v_flex().size_full()- .p_4().bg(theme.colors().editor_background).track_focus(&self.focus_handle).child(self.render_settings_panel(window, cx)).child({- if self.engine.contents.number_of_cols == 0 {+ let is_parsing = self.is_parsing;+ if is_parsing || self.engine.contents.number_of_cols == 0 {div().flex().items_center()@@ -25,7 +25,15 @@ impl Render for CsvPreviewView {.text_ui(cx).font_buffer(cx).text_color(cx.theme().colors().text_muted)- .child("No CSV content to display")+ .when(is_parsing, |div| {+ div.child(+ h_flex()+ .gap_2()+ .child(SpinnerLabel::new())+ .child("Loading…"),+ )+ })+ .when(!is_parsing, |div| div.child("No CSV content to display")).into_any_element()} else {self.create_table(&self.column_widths.widths, cx)diff --git a/crates/csv_preview/src/renderer/row_identifiers.rs b/crates/csv_preview/src/renderer/row_identifiers.rsindex 06a26e4696e..e24441d3885 100644--- a/crates/csv_preview/src/renderer/row_identifiers.rs+++ b/crates/csv_preview/src/renderer/row_identifiers.rs@@ -171,6 +171,7 @@ impl CsvPreviewView {.px_1().border_b_1().border_color(cx.theme().colors().border_variant)+ .bg(cx.theme().colors().panel_background).h_full().text_ui(cx)// Row identifiers are always centereddiff --git a/crates/csv_preview/src/renderer/settings.rs b/crates/csv_preview/src/renderer/settings.rsindex cafa2a4c1bd..18c185bb0f3 100644--- a/crates/csv_preview/src/renderer/settings.rs+++ b/crates/csv_preview/src/renderer/settings.rs@@ -3,7 +3,10 @@ use ui::{IntoElement as _, ParentElement as _, Styled as _, Tooltip, Window, div, h_flex,};-use crate::{CsvPreviewView, settings::VerticalAlignment};+use crate::{+ CsvPreviewView,+ settings::{FilterSortOrder, VerticalAlignment},+};///// Settings related /////impl CsvPreviewView {@@ -18,6 +21,11 @@ impl CsvPreviewView {VerticalAlignment::Center => "Center",};+ let current_filter_sort_text = match self.settings.filter_sort_order {+ FilterSortOrder::AlphaThenCount => "A-Z, then Count",+ FilterSortOrder::CountThenAlpha => "Count, then A-Z",+ };+let view = cx.entity();let alignment_dropdown_menu = ContextMenu::build(window, cx, |menu, _window, _cx| {menu.entry("Top", None, {@@ -40,6 +48,27 @@ impl CsvPreviewView {})});+ let filter_sort_dropdown_menu = ContextMenu::build(window, cx, |menu, _window, _cx| {+ menu.entry("A-Z, then Count", None, {+ let view = view.clone();+ move |_window, cx| {+ view.update(cx, |this, cx| {+ this.settings.filter_sort_order = FilterSortOrder::AlphaThenCount;+ cx.notify();+ });+ }+ })+ .entry("Count, then A-Z", None, {+ let view = view.clone();+ move |_window, cx| {+ view.update(cx, |this, cx| {+ this.settings.filter_sort_order = FilterSortOrder::CountThenAlpha;+ cx.notify();+ });+ }+ })+ });+let panel = h_flex().gap_4().p_2()@@ -68,6 +97,28 @@ impl CsvPreviewView {"Choose vertical text alignment within cells",)),),+ )+ .child(+ h_flex()+ .gap_2()+ .items_center()+ .child(+ div()+ .text_sm()+ .text_color(cx.theme().colors().text_muted)+ .child("Filter Sort:"),+ )+ .child(+ DropdownMenu::new(+ ElementId::Name("filter-sort-order-dropdown".into()),+ current_filter_sort_text,+ filter_sort_dropdown_menu,+ )+ .trigger_size(ButtonSize::Compact)+ .trigger_tooltip(Tooltip::text(+ "Choose how filter values are sorted in the filter menu",+ )),+ ),);#[cfg(feature = "dev-tools")]diff --git a/crates/csv_preview/src/renderer/table_header.rs b/crates/csv_preview/src/renderer/table_header.rsindex 05652b49a48..1ce8d34d22b 100644--- a/crates/csv_preview/src/renderer/table_header.rs+++ b/crates/csv_preview/src/renderer/table_header.rs@@ -1,9 +1,13 @@use gpui::ElementId;-use ui::{Tooltip, prelude::*};+use ui::{ContextMenu, PopoverMenu, Tooltip, prelude::*};use crate::{CsvPreviewView,- table_data_engine::sorting_by_column::{AppliedSorting, SortDirection},+ settings::FilterSortOrder,+ table_data_engine::{+ filtering_by_column::{FilterEntry, FilterEntryState},+ sorting_by_column::{AppliedSorting, SortDirection},+ },types::AnyColumn,};@@ -22,7 +26,12 @@ impl CsvPreviewView {.w_full().font_buffer(cx).child(div().child(header_text))- .child(h_flex().gap_1().child(self.create_sort_button(cx, col_idx)))+ .child(+ h_flex()+ .gap_1()+ .child(self.create_filter_button(cx, col_idx))+ .child(self.create_sort_button(cx, col_idx)),+ ).into_any_element()}@@ -82,9 +91,191 @@ impl CsvPreviewView {};this.engine.applied_sorting = new_sorting;- this.apply_sort();+ this.apply_sort(cx);cx.notify();}));sort_btn}++ fn create_filter_button(+ &self,+ cx: &mut Context<'_, CsvPreviewView>,+ col: AnyColumn,+ ) -> PopoverMenu<ContextMenu> {+ let has_active_filters = self.engine.has_active_filters(col);++ PopoverMenu::new(ElementId::NamedInteger(+ "filter-menu".into(),+ col.get() as u64,+ ))+ .trigger_with_tooltip(+ Button::new(+ ElementId::NamedInteger("filter-button".into(), col.get() as u64),+ if has_active_filters { "⛊" } else { "⛉" },+ )+ .size(ButtonSize::Compact)+ .style(if has_active_filters {+ ButtonStyle::Filled+ } else {+ ButtonStyle::Subtle+ }),+ Tooltip::text(if has_active_filters {+ "Column has active filters. Click to manage"+ } else {+ "No filters applied. Click to add filters"+ }),+ )+ .menu({+ let view_entity = cx.entity();+ move |window, cx| {+ let view = view_entity.read(cx);+ let column_filters = match view.engine.get_filters_for_column(col) {+ Ok(filters) => filters,+ Err(err) => {+ log::error!("Failed to get filters for column: {err}");+ return None;+ }+ };+ let filter_sort_order = view.settings.filter_sort_order;+ let filter_menu = Self::create_filter_menu(+ window,+ cx,+ view_entity.clone(),+ col,+ &column_filters,+ has_active_filters,+ filter_sort_order,+ );+ Some(filter_menu)+ }+ })+ }++ fn create_filter_menu(+ window: &mut ui::Window,+ cx: &mut ui::App,+ view_entity: gpui::Entity<CsvPreviewView>,+ col: AnyColumn,+ column_filters: &[(FilterEntry, FilterEntryState)],+ has_active_filters: bool,+ sort_order: FilterSortOrder,+ ) -> gpui::Entity<ContextMenu> {+ let mut available: Vec<&FilterEntry> = column_filters+ .iter()+ .filter_map(|(entry, state)| {+ matches!(state, FilterEntryState::Available { .. }).then_some(entry)+ })+ .collect();++ match sort_order {+ FilterSortOrder::AlphaThenCount => available.sort_by(|a, b| {+ a.content+ .cmp(&b.content)+ .then_with(|| b.occurred_times().cmp(&a.occurred_times()))+ }),+ FilterSortOrder::CountThenAlpha => available.sort_by(|a, b| {+ b.occurred_times()+ .cmp(&a.occurred_times())+ .then_with(|| a.content.cmp(&b.content))+ }),+ }++ let unavailable: Vec<(&FilterEntry, AnyColumn)> = column_filters+ .iter()+ .filter_map(|(entry, state)| {+ if let FilterEntryState::Unavailable { blocked_by } = state {+ Some((entry, *blocked_by))+ } else {+ None+ }+ })+ .collect();++ // Pre-build applied-state lookup before moving into the closure+ let applied_states: Vec<(FilterEntry, bool)> = column_filters+ .iter()+ .filter_map(|(entry, state)| {+ if let FilterEntryState::Available { is_applied } = state {+ Some((entry.clone(), *is_applied))+ } else {+ None+ }+ })+ .collect();++ let available_cloned: Vec<FilterEntry> = available.iter().map(|e| (*e).clone()).collect();+ let unavailable_cloned: Vec<(FilterEntry, AnyColumn)> = unavailable+ .into_iter()+ .map(|(e, col)| (e.clone(), col))+ .collect();++ ContextMenu::build(window, cx, move |menu, _, _| {+ let mut menu = menu;++ if has_active_filters {+ menu = menu+ .toggleable_entry("Clear all", false, ui::IconPosition::Start, None, {+ let view_entity = view_entity.clone();+ move |_window, cx| {+ view_entity.update(cx, |view, cx| {+ view.clear_filters(col, cx);+ cx.notify();+ });+ }+ })+ .separator();+ }++ for entry in &available_cloned {+ let is_applied = applied_states+ .iter()+ .find(|(e, _)| e.content == entry.content)+ .map_or(false, |(_, applied)| *applied);++ let label: SharedString =+ format_filter_label(entry.content.as_ref(), entry.occurred_times()).into();+ let entry_value = entry.content.clone();++ menu = menu.toggleable_entry(&label, is_applied, ui::IconPosition::Start, None, {+ let view_entity = view_entity.clone();+ move |_window, cx| {+ view_entity.update(cx, |view, cx| {+ view.toggle_filter(col, entry_value.clone(), cx);+ cx.notify();+ });+ }+ });+ }++ if !unavailable_cloned.is_empty() {+ menu = menu.separator().header("Hidden by other filters");+ for (entry, _blocked_by) in &unavailable_cloned {+ let label: SharedString =+ format_filter_label(entry.content.as_ref(), entry.occurred_times()).into();+ menu = menu.custom_entry(+ {+ let label = label.clone();+ move |_window, cx| {+ div()+ .px_2()+ .text_color(cx.theme().colors().text_muted)+ .child(label.clone())+ .into_any_element()+ }+ },+ |_, _| {},+ );+ }+ }++ menu+ })+ }+}++fn format_filter_label(content: Option<&SharedString>, count: usize) -> String {+ match content {+ Some(s) => format!("{s} ({count})"),+ None => format!("<null> ({count})"),+ }}diff --git a/crates/csv_preview/src/settings.rs b/crates/csv_preview/src/settings.rsindex 215d681c28f..2d5cc78c5f5 100644--- a/crates/csv_preview/src/settings.rs+++ b/crates/csv_preview/src/settings.rs@@ -1,10 +1,10 @@#[derive(Default, Clone, Copy, PartialEq)]pub enum RowRenderMechanism {/// More correct for multiline content, but slower.- #[allow(dead_code)] // Will be used when settings ui is added+ #[default]VariableList,/// Default behaviour for now while resizable columns are being stabilized.- #[default]+ #[allow(dead_code)] // Will be used when settings ui is addedUniformList,}@@ -26,11 +26,21 @@ pub enum RowIdentifiers {RowNum,}+#[derive(Default, Clone, Copy, PartialEq)]+pub enum FilterSortOrder {+ /// Sort alphabetically (A→Z), then by number of occurrences descending within ties+ #[default]+ AlphaThenCount,+ /// Sort by number of occurrences descending, then alphabetically within ties+ CountThenAlpha,+}+#[derive(Clone, Default)]pub(crate) struct CsvPreviewSettings {pub(crate) rendering_with: RowRenderMechanism,pub(crate) vertical_alignment: VerticalAlignment,pub(crate) numbering_type: RowIdentifiers,+ pub(crate) filter_sort_order: FilterSortOrder,pub(crate) show_debug_info: bool,#[cfg(feature = "dev-tools")]pub(crate) show_perf_metrics_overlay: bool,diff --git a/crates/csv_preview/src/table_data_engine.rs b/crates/csv_preview/src/table_data_engine.rsindex 382b41a2850..f2613215fa5 100644--- a/crates/csv_preview/src/table_data_engine.rs+++ b/crates/csv_preview/src/table_data_engine.rs@@ -5,22 +5,32 @@//!//! It's designed to contain core logic of operations without relying on `CsvPreviewView`, context or window handles.-use std::{collections::HashMap, sync::Arc};+use std::{+ collections::{HashMap, HashSet},+ sync::Arc,+};use ui::table_row::TableRow;use crate::{- table_data_engine::sorting_by_column::{AppliedSorting, sort_data_rows},- types::{DataRow, DisplayRow, TableCell, TableLikeContent},+ table_data_engine::{+ filtering_by_column::{FilterEntry, FilterStack, calculate_available_filters, retain_rows},+ sorting_by_column::{AppliedSorting, sort_data_rows},+ },+ types::{AnyColumn, DataRow, DisplayRow, TableCell, TableLikeContent},};+pub mod filtering_by_column;pub mod sorting_by_column;#[derive(Default)]pub(crate) struct TableDataEngine {+ pub filter_stack: FilterStack,+ /// Pre-computed unique values per column, used to populate filter menus+ all_filters: HashMap<AnyColumn, Vec<FilterEntry>>,pub applied_sorting: Option<AppliedSorting>,d2d_mapping: DisplayToDataMapping,- pub contents: TableLikeContent,+ pub contents: Arc<TableLikeContent>,}impl TableDataEngine {@@ -28,32 +38,47 @@ impl TableDataEngine {&self.d2d_mapping}- pub(crate) fn apply_sort(&mut self) {- self.d2d_mapping- .apply_sorting(self.applied_sorting, &self.contents.rows);- self.d2d_mapping.merge_mappings();+ pub(crate) fn set_d2d_mapping(&mut self, mapping: DisplayToDataMapping) {+ self.d2d_mapping = mapping;}- /// Applies sorting and filtering to the data and produces display to data mapping- pub(crate) fn calculate_d2d_mapping(&mut self) {- self.d2d_mapping- .apply_sorting(self.applied_sorting, &self.contents.rows);- self.d2d_mapping.merge_mappings();+ /// Recomputes the unique filter entries for every column from the current table data.+ /// Must be called after content changes (e.g. after parsing).+ pub fn calculate_available_filters(&mut self) {+ self.all_filters =+ calculate_available_filters(&self.contents.rows, self.contents.number_of_cols);}}/// Relation of Display (rendered) rows to Data (src) rows with applied transformations/// Transformations applied:/// - sorting by column+/// - filtering by column values#[derive(Debug, Default)]pub struct DisplayToDataMapping {- /// All rows sorted, regardless of applied filtering. Applied every time sorting changes+ /// All rows sorted, regardless of applied filtering. Recomputed every time sorting changespub sorted_rows: Vec<DataRow>,- /// Filtered and sorted rows. Computed cheaply from `sorted_mapping` and `filtered_out_rows`+ /// Rows that survive the active filters. Recomputed every time filters change+ pub retained_rows: HashSet<DataRow>,+ /// Merged result: sorted rows intersected with retained rowspub mapping: Arc<HashMap<DisplayRow, DataRow>>,}impl DisplayToDataMapping {+ /// Computes the full display-to-data mapping from owned inputs.+ /// Intended to be called from a background thread.+ pub(crate) fn compute(+ contents: &Arc<TableLikeContent>,+ filter_stack: &FilterStack,+ sorting: Option<AppliedSorting>,+ ) -> Self {+ let mut mapping = Self::default();+ mapping.apply_sorting(sorting, &contents.rows);+ mapping.apply_filtering(filter_stack, &contents.rows);+ mapping.merge_mappings();+ mapping+ }+/// Get the data row for a given display rowpub fn get_data_row(&self, display_row: DisplayRow) -> Option<DataRow> {self.mapping.get(&display_row).copied()@@ -77,11 +102,16 @@ impl DisplayToDataMapping {self.sorted_rows = sorted_rows;}- /// Take pre-computed sorting and filtering results, and apply them to the mapping+ fn apply_filtering(&mut self, filter_stack: &FilterStack, rows: &[TableRow<TableCell>]) {+ self.retained_rows = retain_rows(rows, filter_stack);+ }++ /// Merges pre-computed sorting and filtering into the final display mappingfn merge_mappings(&mut self) {self.mapping = Arc::new(self.sorted_rows.iter()+ .filter(|data_row| self.retained_rows.contains(data_row)).enumerate().map(|(display, data)| (DisplayRow(display), *data)).collect(),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.rsnew file mode 100644index 00000000000..b2b8a78c05a--- /dev/null+++ b/crates/csv_preview/src/table_data_engine/filtering_by_column.rs@@ -0,0 +1,250 @@+use std::{+ collections::{HashMap, HashSet},+ sync::Arc,+};++use ui::{SharedString, table_row::TableRow};++use crate::{+ table_data_engine::TableDataEngine,+ types::{AnyColumn, DataRow, TableCell},+};++#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]+pub enum FilterEntryState {+ Available { is_applied: bool },+ Unavailable { blocked_by: AnyColumn },+}++#[derive(Debug, Clone)]+pub struct FilterEntry {+ /// Content to display. None if cell is virtual+ pub content: Option<SharedString>,+ /// List of rows in which this value occurs+ pub rows: Vec<DataRow>,+}++impl FilterEntry {+ pub(crate) fn occurred_times(&self) -> usize {+ self.rows.len()+ }+}++#[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>>>,+}++impl TableDataEngine {+ pub(crate) fn has_active_filters(&self, col: AnyColumn) -> bool {+ 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.+ pub(crate) fn get_filters_for_column(+ &self,+ column: AnyColumn,+ ) -> anyhow::Result<Arc<Vec<(FilterEntry, FilterEntryState)>>> {+ let all_column_entries = self+ .all_filters+ .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+ .retention_config+ .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(),+ ))+ }++ 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);+ }++ /// Toggle a filter value for a column. Returns `true` if the filter was+ /// added, `false` if it was removed.+ pub(crate) fn toggle_filter(+ &mut self,+ column: AnyColumn,+ value: Option<SharedString>,+ ) -> anyhow::Result<bool> {+ let is_currently_applied = self+ .filter_stack+ .retention_config+ .get(&column)+ .is_some_and(|filters| filters.contains(&value));++ if is_currently_applied {+ self.remove_filter(column, value)?;+ Ok(false)+ } else {+ self.apply_filter(column, value);+ Ok(true)+ }+ }++ fn remove_filter(+ &mut self,+ column: AnyColumn,+ value: Option<SharedString>,+ ) -> anyhow::Result<()> {+ let entries = self+ .filter_stack+ .retention_config+ .get_mut(&column)+ .ok_or_else(|| {+ anyhow::anyhow!("Expected {column:?} to be present in active filters")+ })?;++ debug_assert!(+ entries.contains(&value),+ "Expected value to be present in {column:?} active filters"+ );++ if entries.len() == 1 {+ self.filter_stack.retention_config.remove(&column);+ self.filter_stack+ .activation_order+ .retain(|&entry| entry != column);+ } else {+ entries.remove(&value);+ }+ Ok(())+ }++ 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)+ .or_default()+ .insert(value);+ }+}++/// Calculate available filter entries for each column from the table data.+pub fn calculate_available_filters(+ content_rows: &[TableRow<TableCell>],+ number_of_cols: usize,+) -> HashMap<AnyColumn, Vec<FilterEntry>> {+ let mut available_filters = HashMap::new();++ for col_idx in 0..number_of_cols {+ let column = AnyColumn::new(col_idx);+ let mut value_to_rows: HashMap<Option<SharedString>, Vec<DataRow>> = HashMap::new();++ for (row_index, row) in content_rows.iter().enumerate() {+ let cell_value = row+ .get(column)+ .and_then(|cell| cell.display_value().cloned());+ value_to_rows+ .entry(cell_value)+ .or_default()+ .push(DataRow(row_index));+ }++ let filter_entries: Vec<FilterEntry> = value_to_rows+ .into_iter()+ .map(|(content, rows)| FilterEntry { content, rows })+ .collect();++ available_filters.insert(column, filter_entries);+ }++ available_filters+}++/// Returns the set of data rows that survive all active filters in the stack.+pub fn retain_rows(+ content_rows: &[TableRow<TableCell>],+ filter_stack: &FilterStack,+) -> HashSet<DataRow> {+ let config = &filter_stack.retention_config;+ if config.is_empty() {+ return (0..content_rows.len()).map(DataRow).collect();+ }++ content_rows+ .iter()+ .enumerate()+ .filter(|(_, row)| {+ config.iter().all(|(col, allowed_values)| {+ let cell_value = row.get(*col).and_then(|cell| cell.display_value().cloned());+ allowed_values.contains(&cell_value)+ })+ })+ .map(|(index, _)| DataRow(index))+ .collect()+}diff --git a/crates/gpui/src/elements/list.rs b/crates/gpui/src/elements/list.rsindex 85b38d5234e..62b481bc530 100644--- a/crates/gpui/src/elements/list.rs+++ b/crates/gpui/src/elements/list.rs@@ -338,6 +338,17 @@ impl ListState {self}+ /// Pre-populate every unmeasured item with a uniform height hint so the scrollbar thumb+ /// is correctly sized from the first frame, without measuring all items up front.+ ///+ /// As items are actually rendered their real heights replace the hint, so the scrollbar+ /// converges to the exact size over time. This is a cheaper alternative to [`Self::measure_all`]+ /// for lists where items have roughly uniform heights (e.g. table rows).+ pub fn with_uniform_item_height(self, height: Pixels) -> Self {+ self.apply_uniform_item_height(height);+ self+ }+/// Reset this instantiation of the list state.////// Note that this will cause scroll events to be dropped until the next paint.@@ -355,6 +366,33 @@ impl ListState {self.splice(0..old_count, element_count);}+ /// Reset the list to `element_count` items, pre-populating every item with a+ /// uniform height hint so the scrollbar thumb is correctly sized from the first+ /// frame even for off-screen items.+ pub fn reset_with_uniform_height(&self, element_count: usize, height: Pixels) {+ self.reset(element_count);+ self.apply_uniform_item_height(height);+ }++ fn apply_uniform_item_height(&self, height: Pixels) {+ let size_hint = Size {+ width: px(0.),+ height,+ };+ let mut state = self.0.borrow_mut();+ let new_items = state+ .items+ .iter()+ .map(|item| ListItem::Unmeasured {+ size_hint: Some(item.size_hint().unwrap_or(size_hint)),+ focus_handle: item.focus_handle(),+ })+ .collect::<Vec<_>>();+ let mut tree = SumTree::default();+ tree.extend(new_items, ());+ state.items = tree;+ }+/// Remeasure all items while preserving proportional scroll position.////// Use this when item heights may have changed (e.g., font size changes)