mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-24 01:20:29 +01:00
d528c665e1
## Objective Extend the existing tabular preview to support `.jsonl` and `.ndjson` files, making it easier to compare fields across records. Discussed in https://github.com/zed-industries/zed/discussions/62917. ## Solution Parse each nonblank line as a JSON object and reuse the existing table preview. Top-level keys become columns in first-seen order, with new keys from later records appended to the right. - Preserve JSON values, including large numbers, without numeric conversion. - Display nested objects and arrays as compact JSON. - Keep missing fields, `null`, `"null"`, and empty strings distinct. - Show invalid-record errors with source line numbers and recover after edits. - Preserve sorting and filters unless column names or order change. - Add JSONL/NDJSON to the existing preview shortcuts and document the behavior. Also fix a crash found during CSV regression testing: deleting rows could leave the renderer using an old row mapping against shorter data. Clear the mapping and virtual list together before recomputing them. The preview remains read-only, accepts object records only, and uses the existing text-based sorting and full-file parsing behavior. ## Testing - All 20 tests in `tabular_data_preview` passed. - The multiline CSV deletion regression passed across 20 scheduler seeds. - `cargo fmt -p tabular_data_preview -- --check` passed. - `./script/clippy -p tabular_data_preview` passed. - `cargo build -p zed --bin zed` passed. - `git diff --check` passed. - Manual QA on macOS, including retesting the reported CSV crash. Windows and Linux were not tested locally. Large-file performance has not been benchmarked. To try it, open a `.jsonl` or `.ndjson` file and run `tabular data: open preview` or `tabular data: open preview to the side`. ## Self-Review Checklist - [x] I've reviewed my own diff for quality, security, and reliability - [x] Unsafe blocks (if any) have justifying comments - [x] The content adheres to Zed's UI standards - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable ## Showcase <img width="2358" height="1576" alt="image" src="https://github.com/user-attachments/assets/8f7e7725-5a5c-465d-aa0a-d61cbb22d04f" /> The example shows source-order columns, fields introduced by later records, nested values, and the distinction between missing values, nulls, and strings. Release Notes: - Added JSONL and NDJSON support to the tabular data preview. --------- Co-authored-by: Anthony Eid <anthony@zed.dev>
183 lines
7.2 KiB
Rust
183 lines
7.2 KiB
Rust
use crate::types::TableCell;
|
|
use gpui::{AnyElement, Entity};
|
|
use std::ops::Range;
|
|
use ui::{ColumnWidthConfig, ResizableColumnsState, Table, UncheckedTableRow, div, prelude::*};
|
|
|
|
use crate::{
|
|
TabularDataPreviewPane,
|
|
settings::RowRenderMechanism,
|
|
types::{AnyColumn, DisplayCellId, DisplayRow},
|
|
};
|
|
|
|
impl TabularDataPreviewPane {
|
|
/// Creates a new table.
|
|
/// Column number is derived from the `ResizableColumnsState` entity.
|
|
pub(crate) fn create_table(
|
|
&self,
|
|
current_widths: &Entity<ResizableColumnsState>,
|
|
cx: &mut Context<Self>,
|
|
) -> AnyElement {
|
|
self.create_table_inner(
|
|
self.engine.d2d_mapping().visible_row_count(),
|
|
current_widths,
|
|
cx,
|
|
)
|
|
}
|
|
|
|
fn create_table_inner(
|
|
&self,
|
|
row_count: usize,
|
|
current_widths: &Entity<ResizableColumnsState>,
|
|
cx: &mut Context<Self>,
|
|
) -> AnyElement {
|
|
let cols = current_widths.read(cx).cols();
|
|
// Create headers array with interactive elements
|
|
let mut headers = Vec::with_capacity(cols);
|
|
|
|
headers.push(self.create_row_identifier_header(cx));
|
|
|
|
// Add the actual data headers with sort buttons
|
|
for i in 0..(cols - 1) {
|
|
let header_text = self
|
|
.engine
|
|
.contents
|
|
.headers
|
|
.get(AnyColumn(i))
|
|
.and_then(|h| h.display_value().cloned())
|
|
.unwrap_or_else(|| format!("Col {}", i + 1).into());
|
|
|
|
headers.push(self.create_header_element_with_sort_button(
|
|
header_text,
|
|
cx,
|
|
AnyColumn::from(i),
|
|
));
|
|
}
|
|
|
|
Table::new(cols)
|
|
.interactable(&self.table_interaction_state)
|
|
.width_config(ColumnWidthConfig::Resizable(current_widths.clone()))
|
|
.header(headers)
|
|
.disable_base_style()
|
|
.pin_cols(1)
|
|
.map(|table| {
|
|
let row_identifier_text_color = cx.theme().colors().editor_line_number;
|
|
match self.settings.rendering_with {
|
|
RowRenderMechanism::VariableList => {
|
|
table.variable_row_height_list(row_count, self.list_state.clone(), {
|
|
cx.processor(move |this, display_row: usize, _window, cx| {
|
|
this.performance_metrics.rendered_indices.push(display_row);
|
|
|
|
let display_row = DisplayRow(display_row);
|
|
Self::render_single_table_row(
|
|
this,
|
|
cols,
|
|
display_row,
|
|
row_identifier_text_color,
|
|
this.row_height,
|
|
cx,
|
|
)
|
|
.unwrap_or_else(|| panic!("Expected to render a table row"))
|
|
})
|
|
})
|
|
}
|
|
RowRenderMechanism::UniformList => {
|
|
table.uniform_list("tabular-data-table", row_count, {
|
|
cx.processor(move |this, range: Range<usize>, _window, cx| {
|
|
// Record all display indices in the range for performance metrics
|
|
this.performance_metrics
|
|
.rendered_indices
|
|
.extend(range.clone());
|
|
|
|
let row_height = this.row_height;
|
|
range
|
|
.filter_map(|display_index| {
|
|
Self::render_single_table_row(
|
|
this,
|
|
cols,
|
|
DisplayRow(display_index),
|
|
row_identifier_text_color,
|
|
row_height,
|
|
cx,
|
|
)
|
|
})
|
|
.collect()
|
|
})
|
|
})
|
|
}
|
|
}
|
|
})
|
|
.into_any_element()
|
|
}
|
|
|
|
/// Render a single table row
|
|
///
|
|
/// Used both by UniformList and VariableRowHeightList
|
|
fn render_single_table_row(
|
|
this: &TabularDataPreviewPane,
|
|
cols: usize,
|
|
display_row: DisplayRow,
|
|
row_identifier_text_color: gpui::Hsla,
|
|
row_height: Pixels,
|
|
cx: &Context<TabularDataPreviewPane>,
|
|
) -> Option<UncheckedTableRow<AnyElement>> {
|
|
// Get the actual row index from our sorted indices
|
|
let data_row = this.engine.d2d_mapping().get_data_row(display_row)?;
|
|
let row = this.engine.contents.get_row(data_row)?;
|
|
|
|
let mut elements = Vec::with_capacity(cols);
|
|
elements.push(this.create_row_identifier_cell(display_row, data_row, cx)?);
|
|
|
|
// Remaining columns: actual table data
|
|
for col in (0..this.engine.contents.number_of_cols).map(AnyColumn) {
|
|
let table_cell = row.expect_get(col);
|
|
|
|
// TODO: Introduce `<null>` cell type
|
|
let cell_content = table_cell.display_value().cloned().unwrap_or_default();
|
|
|
|
let display_cell_id = DisplayCellId::new(display_row, col);
|
|
|
|
let cell = div()
|
|
.size_full()
|
|
.when(
|
|
!this.settings.multiline_cells_effectively_enabled(),
|
|
|div| {
|
|
div.whitespace_nowrap()
|
|
.text_ellipsis()
|
|
.h(row_height)
|
|
.overflow_hidden()
|
|
},
|
|
)
|
|
.child(TabularDataPreviewPane::create_selectable_cell(
|
|
display_cell_id,
|
|
cell_content,
|
|
this.settings.vertical_alignment,
|
|
cx,
|
|
));
|
|
|
|
elements.push(
|
|
div()
|
|
.size_full()
|
|
.when(this.settings.show_debug_info, |parent| {
|
|
parent.child(div().text_color(row_identifier_text_color).child(
|
|
match table_cell {
|
|
TableCell::Real { position: pos, .. } => {
|
|
let slv = pos.start.timestamp().value;
|
|
let so = pos.start.offset;
|
|
let elv = pos.end.timestamp().value;
|
|
let eo = pos.end.offset;
|
|
format!("Pos {so}(L{slv})-{eo}(L{elv})")
|
|
}
|
|
TableCell::Virtual => "Virtual cell".into(),
|
|
TableCell::Generated(_) => "Generated cell".into(),
|
|
},
|
|
))
|
|
})
|
|
.child(cell)
|
|
.into_any_element(),
|
|
);
|
|
}
|
|
|
|
Some(elements)
|
|
}
|
|
}
|