diff: Add support for tabs
596a46478013ed659bea4a90d103e7cdb20e933c
1 parent
565c2b81
crates/diff/src/viewer.rs
+41 -4
| 63 | 63 | const STICKY_DIVIDER: char = '╴'; |
|
| 64 | 64 | /// Rename arrow. |
|
| 65 | 65 | const RENAME_ARROW: &str = "→"; |
|
| 66 | 66 | /// Missing trailing newline marker. |
|
| 67 | 67 | const NO_NEWLINE_MARKER: &str = "\\ No newline at end of file"; |
|
| 68 | + | /// Rendered source text replacement for a single source tab. |
|
| 69 | + | const SOURCE_TAB_MARKER: &str = "· "; |
|
| 68 | 70 | /// Marker cell shown between hunks in the diff marker column. |
|
| 69 | 71 | const HUNK_GAP_MARKER_CELL: &str = " ⋮ "; |
|
| 70 | 72 | /// Default syntax theme selection stored in highlight keys. |
|
| 71 | 73 | const DEFAULT_THEME: &str = "auto"; |
|
| 72 | 74 | /// Number of rows in a sticky file header. |
| 1632 | 1634 | /// Cached entries already include the word-diff overlay (applied once in |
|
| 1633 | 1635 | /// [`Self::apply_highlights`]). On a cache miss -- the window before the |
|
| 1634 | 1636 | /// highlight worker responds -- a plain Line is returned; the word-diff |
|
| 1635 | 1637 | /// background lands once the cached overlaid line arrives. |
|
| 1636 | 1638 | fn rendered_source(&self, meta: &DiffLineMeta, cols: usize) -> Line { |
|
| 1637 | - | self.highlights |
|
| 1638 | - | .get(&self.key_for(meta)) |
|
| 1639 | - | .cloned() |
|
| 1640 | - | .unwrap_or_else(|| Line::new(Text::truncate(meta.text.as_str(), cols, "…"))) |
|
| 1639 | + | let has_tabs = meta.text.contains('\t'); |
|
| 1640 | + | if let Some(line) = self.highlights.get(&self.key_for(meta)) { |
|
| 1641 | + | let line = line.clone(); |
|
| 1642 | + | return if has_tabs { |
|
| 1643 | + | Self::expand_source_tabs(line).truncated(cols, "…") |
|
| 1644 | + | } else { |
|
| 1645 | + | line.truncated(cols, "…") |
|
| 1646 | + | }; |
|
| 1647 | + | } |
|
| 1648 | + | if has_tabs { |
|
| 1649 | + | Self::expand_source_tabs(Line::new(meta.text.as_str())).truncated(cols, "…") |
|
| 1650 | + | } else { |
|
| 1651 | + | Line::new(Text::truncate(meta.text.as_str(), cols, "…")) |
|
| 1652 | + | } |
|
| 1653 | + | } |
|
| 1654 | + | ||
| 1655 | + | /// Returns a source line with tabs rendered as fixed-width visible cells. |
|
| 1656 | + | fn expand_source_tabs(line: Line) -> Line { |
|
| 1657 | + | let mut expanded = Line::blank(); |
|
| 1658 | + | for label in line { |
|
| 1659 | + | if !label.content().contains('\t') { |
|
| 1660 | + | expanded.push(label); |
|
| 1661 | + | continue; |
|
| 1662 | + | } |
|
| 1663 | + | let style = label.paint.style(); |
|
| 1664 | + | let tab_style = style.fg(colors::FAINT); |
|
| 1665 | + | let mut start = 0; |
|
| 1666 | + | for (index, _) in label.content().match_indices('\t') { |
|
| 1667 | + | if start < index { |
|
| 1668 | + | expanded.push(Label::new(&label.content()[start..index]).style(style)); |
|
| 1669 | + | } |
|
| 1670 | + | expanded.push(Label::new(SOURCE_TAB_MARKER).style(tab_style)); |
|
| 1671 | + | start = index + 1; |
|
| 1672 | + | } |
|
| 1673 | + | if start < label.content().len() { |
|
| 1674 | + | expanded.push(Label::new(&label.content()[start..]).style(style)); |
|
| 1675 | + | } |
|
| 1676 | + | } |
|
| 1677 | + | expanded |
|
| 1641 | 1678 | } |
|
| 1642 | 1679 | ||
| 1643 | 1680 | /// Returns a line-number cell. |
|
| 1644 | 1681 | fn number_cell(value: u32, foreground: Color, background: Color) -> Line { |
|
| 1645 | 1682 | Line::new(Label::new(&value.to_string()).fg(foreground).bg(background)) |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
assets/css/style.css
+0 -0
assets/css/style.css
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden.rs
+8 -2
| 1 | 1 | #![allow(clippy::unwrap_used)] |
|
| 2 | 2 | ||
| 3 | 3 | use std::collections::BTreeSet; |
|
| 4 | 4 | ||
| 5 | 5 | use rustik_diff::{ |
|
| 6 | - | Decode, DiffAction, DiffDocument, DiffOptions, DiffViewer, FileId, FileTreeMode, |
|
| 6 | + | Decode, DiffAction, DiffDocument, DiffOptions, DiffSide, DiffViewer, FileId, FileTreeMode, |
|
| 7 | 7 | HighlightResult, |
|
| 8 | 8 | }; |
|
| 9 | - | use rustik_tui::{Buffer, Color, Label, Line, Rect, Scroll}; |
|
| 9 | + | use rustik_tui::{Buffer, Color, Label, Line, Rect, Scroll, colors}; |
|
| 10 | 10 | ||
| 11 | 11 | #[test] |
|
| 12 | 12 | fn expanded_file_card_matches_golden_frame() { |
|
| 13 | 13 | assert_golden( |
|
| 14 | 14 | "expanded_file_card_64x9.txt", |
| 228 | 228 | 64, |
|
| 229 | 229 | 5, |
|
| 230 | 230 | ); |
|
| 231 | 231 | } |
|
| 232 | 232 | ||
| 233 | + | #[test] |
|
| 234 | + | fn source_tabs_render_as_four_cells_with_diff_background() { |
|
| 235 | + | let palette = DiffOptions::default().diff_palette; |
|
| 236 | + | let mut viewer = viewer_from_str( |
|
| 237 | + | "\ |
|
| 238 | + |
a.rs
+0 -0