Automatic delimiter detection for CSV/TSV files with manual override capability. The system analyzes file content to detect the most likely delimiter (comma, tab, semicolon, or pipe) and allows users to manually override the detection via the status bar.
src/markdown/csv_viewer.rs- Delimiter detection algorithm and CsvViewerStatesrc/app.rs- Status bar delimiter picker UIsrc/config/session.rs- Per-file delimiter persistence
The detect_delimiter() function analyzes the first 10 lines of the file and scores each candidate delimiter:
pub const DELIMITERS: &[u8] = &[b',', b'\t', b';', b'|'];
pub fn detect_delimiter(content: &str) -> DelimiterInfo {
// Sample first 10 lines
// Score each delimiter by:
// 1. Column consistency across lines (major factor)
// 2. Successful CSV parse without errors
// 3. Reasonable column count (2-50 preferred)
// 4. Non-empty cells bonus
}Scoring criteria:
- Consistency score: Lines with matching column counts score higher
- Column count: Single-column results are penalized (likely wrong delimiter)
- Parse success: CSV parse errors reduce the score
- Non-empty cells: Files with more data in columns score higher
pub struct CsvViewerState {
delimiter_override: Option<u8>, // Manual override
detected_delimiter: Option<u8>, // Cached auto-detection
// ... other fields
}Methods:
effective_delimiter()- Returns override or detected delimiterset_delimiter(u8)- Set manual overrideclear_delimiter_override()- Return to auto-detect
When a CSV/TSV file is open in Rendered mode:
- Shows
Delim: ,(or appropriate symbol) - Shows
✓when manually overridden - Click opens dropdown with:
- "Auto-detect" option
- Manual delimiter choices (Comma, Tab, Semicolon, Pipe)
The SessionTabState includes:
pub csv_delimiter: Option<u8>, // Manual delimiter overrideDelimiter preferences are:
- Saved when the session is saved
- Restored when the file is reopened
- Matched by file path during restoration
CSV/TSV files support split view mode:
- Left pane: Raw text editor
- Right pane: Table viewer with delimiter detection
csvcrate - CSV parsing and delimiter handling
- Open a CSV/TSV file
- Delimiter is auto-detected and applied
- To override: click delimiter indicator in status bar
- Select desired delimiter from dropdown
- Table re-parses with new delimiter
- Preference is saved per-file in session
test_md/test_data.csv- Comma-separatedtest_md/test_data.tsv- Tab-separatedtest_md/test_data_semicolon.csv- Semicolon-separated