Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ members = [".", "plugins/nightcrow-recovery", "plugins/nightcrow-memory"]
# was never built at — a class of drift the release check could only report
# after the fact.
[workspace.package]
version = "0.1.28"
version = "0.1.29"

[package]
name = "nightcrow"
Expand Down Expand Up @@ -44,7 +44,7 @@ exclude = [
]

[dependencies]
ratatui = { version = "0.30", features = ["crossterm"] }
ratatui = { version = "0.30", features = ["crossterm", "unstable-rendered-line-info"] }
crossterm = "0.29"
git2 = { version = "0.21", features = ["vendored-openssl", "vendored-libgit2"] }
syntect = "5.3"
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/git-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Paths used only as commit objects or Git pathspecs go through `validate_commit_p

`DiffLine` preserves the old/new line numbers supplied by libgit2. Unified mode uses two gutter columns, split mode one old and one new column, and file view displays file line numbers. The gutter and body render as separate `Paragraph`s so horizontal scrolling affects only the body; gutter width is computed from the maximum number in the whole hunk and a minimum width.

Wrap mode cannot be combined with horizontal scrolling and resets `scroll_x` to zero when enabled. In wrap mode the gutter is included in the body; split mode ignores wrap to preserve old/new row alignment. Vertical scroll and search indexes use logical lines. `DiffPaneView` cycles through `Diff → Split → File`, skipping File when the selected file cannot be opened.
Wrap mode cannot be combined with horizontal scrolling and resets `scroll_x` to zero when enabled. Wrapped TUI lines reserve the gutter across all continuation rows and display line numbers and diff markers on the first row. Split mode keeps old/new rows aligned by clipping to one screen row per side. The browser uses horizontal scrolling for long diff and file lines and its virtualized previews use fixed-height rows. Vertical scroll and search indexes use logical lines. `DiffPaneView` cycles through `Diff → Split → File`, skipping File when the selected file cannot be opened.

Syntax highlighting runs synchronously on the UI thread, so both the diff pane and the file overlay highlight only the leading `HIGHLIGHT_LINE_CAP` body lines of the current generation. A hunk past the cap has no cached entry and a hunk that straddles it keeps only its in-budget prefix; the renderers emit plain text for the rest while keeping the add/remove background derived from the line kind. Each generation reuses the unchanged leading hunks, or the leading lines of a grown hunk, from the previous cache and resumes syntect from saved parser/highlighter state, so an appended diff or file overlay highlights only its new tail. A changed line, hunk shape, or syntax stops reuse at that point and the remainder is recomputed exactly as a full pass; a hunk header change alone does not invalidate the cache because headers do not feed the segments. The file overlay applies the same rule to line-boundary appends and falls back when its last cached line was unterminated. Search and the split layout still cover every line because they read the full hunk and search indexes, not the highlight cache, and the file overlay keeps per-line byte offsets so its unhighlighted tail can be rendered without rescanning the content. The lowercased search cache is also built on demand by `ensure_lower_cache`: a load without an active query skips the lowercase pass, and a search pays it once per generation.

Expand Down
54 changes: 32 additions & 22 deletions src/ui/diff_viewer/gutter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ fn lineno_text(no: Option<u32>) -> String {
/// separate `Paragraph`s because `Paragraph::scroll` shifts the whole line, so
/// a gutter span in the body's paragraph would slide off the left edge;
/// vertical scroll is instead expressed by *which* lines the caller collected.
/// With `wrap` set that split is abandoned: a wrapped body line occupies
/// several screen rows while its gutter line still occupies one, so the number
/// is folded into the body line instead, where wrapping carries it along.
pub(crate) fn render_gutter_and_body(
frame: &mut Frame,
inner: Rect,
Expand All @@ -73,12 +70,7 @@ pub(crate) fn render_gutter_and_body(
wrap: bool,
) {
if wrap {
frame.render_widget(
// `trim: false` keeps a continuation row's leading whitespace, which
// in source code is the indentation.
Paragraph::new(merge_gutter_into_body(gutter, body)).wrap(Wrap { trim: false }),
inner,
);
render_wrapped_rows(frame, inner, gutter_width, gutter, body);
return;
}
let cols = Layout::default()
Expand All @@ -89,18 +81,36 @@ pub(crate) fn render_gutter_and_body(
frame.render_widget(Paragraph::new(body).scroll((0, scroll_x)), cols[1]);
}

/// Prepend each gutter line's spans to the body line it belongs to; the two
/// vectors are built in lockstep by the callers, so index `i` pairs row `i`.
fn merge_gutter_into_body<'a>(gutter: Vec<Line<'a>>, body: Vec<Line<'a>>) -> Vec<Line<'a>> {
fn render_wrapped_rows(
frame: &mut Frame,
inner: Rect,
gutter_width: u16,
gutter: Vec<Line<'_>>,
body: Vec<Line<'_>>,
) {
let mut gutter = gutter.into_iter();
body.into_iter()
.map(|line| match gutter.next() {
Some(g) => {
let mut spans = g.spans;
spans.extend(line.spans);
Line::from(spans)
}
None => line,
})
.collect()
let mut y = inner.top();
for line in body {
let remaining_height = inner.bottom().saturating_sub(y);
if remaining_height == 0 {
break;
}

// Headers and placeholders have no gutter contents and use the full width.
let number = gutter.next().filter(|line| line.width() > 0);
let reserved = if let Some(number) = number {
let width = gutter_width.min(inner.width);
frame.render_widget(number, Rect::new(inner.x, y, width, 1));
width
} else {
0
};
let width = inner.width - reserved;
let paragraph = Paragraph::new(line).wrap(Wrap { trim: false });
let height = paragraph
.line_count(width)
.clamp(1, usize::from(remaining_height)) as u16;
frame.render_widget(paragraph, Rect::new(inner.x + reserved, y, width, height));
y += height;
}
}
30 changes: 16 additions & 14 deletions src/ui/diff_viewer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ pub fn render(
// loaded the pane holds only a placeholder message, which has no line to
// number.
let digits = digits_for(app.diff_pane().max_line_number() as usize);
let wrap = app.diff_pane().wrap;
let gutter_width = if total_lines == 0 {
0
} else {
unified_gutter_width(digits)
unified_gutter_width(digits) + u16::from(wrap)
};

let mut lines: Vec<Line> = Vec::with_capacity(visible_height);
Expand All @@ -121,8 +122,6 @@ pub fn render(
hunk.header.as_str(),
Style::default().fg(Color::Cyan),
)));
// Blank, but full width: a header row with no gutter cell would
// start its `@@` one column left of the body's left edge.
gutter_lines.push(Line::from(""));
}

Expand Down Expand Up @@ -158,10 +157,19 @@ pub fn render(
LineKind::Context => " ",
};

let mut spans = vec![Span::styled(
prefix,
Style::default().fg(Color::DarkGray).bg(bg),
)];
let gutter_style = Style::default().fg(Color::DarkGray).bg(bg);
let number = Span::styled(
unified_gutter_text(diff_line.old_lineno, diff_line.new_lineno, digits),
gutter_style,
);
let marker = Span::styled(prefix, gutter_style);
let mut spans = Vec::new();
if wrap {
gutter_lines.push(Line::from(vec![number, marker]));
} else {
gutter_lines.push(Line::from(number));
spans.push(marker);
}

// Read from the prebuilt highlight cache; the shape is guaranteed
// to match `hunks` after `ensure_highlight_cache`, so a mismatch
Expand All @@ -186,12 +194,6 @@ pub fn render(
}

lines.push(Line::from(spans));
// Same background as the row so the number reads as part of it
// rather than as a column floating beside the highlight.
gutter_lines.push(Line::from(Span::styled(
unified_gutter_text(diff_line.old_lineno, diff_line.new_lineno, digits),
Style::default().fg(Color::DarkGray).bg(bg),
)));
}
}

Expand Down Expand Up @@ -239,7 +241,7 @@ pub fn render(
gutter_lines,
lines,
app.diff_pane().scroll_x.min(u16::MAX as usize) as u16,
app.diff_pane().wrap,
wrap,
);

if let Some(sa) = search_area {
Expand Down
21 changes: 21 additions & 0 deletions src/ui/diff_viewer/tests/file_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ fn file_view_line_numbers_stay_put_when_the_body_scrolls_sideways() {
);
}

#[test]
fn file_view_wraps_wide_text_under_the_body_column() {
let mut app = app_with_files(vec!["src/lib.rs"]);
app.git.view.mode = ViewMode::Status;
app.git.view.diff.view = DiffPaneView::File;
app.git.view.diff.wrap = true;
app.git.view.diff.file_view.key =
Some(crate::app::FileViewKey::Status("src/lib.rs".to_string()));
app.git
.view
.diff
.file_view
.set_content("start 가나다라마바\nnext row\n".to_string());

let screen = drawn_file_view(&mut app, 20, 8, 0);
let first = screen.iter().find(|row| row.contains("start")).unwrap();
let continuation = screen.iter().find(|row| row.contains('가')).unwrap();
assert_eq!(col_of(first, "start"), col_of(continuation, "가"));
assert!(screen.iter().any(|row| row.contains("2 next row")));
}

/// The file view reads its own horizontal offset, not `diff.scroll_x`.
fn drawn_file_view(app: &mut App, width: u16, height: u16, scroll_x: usize) -> Vec<String> {
app.git.view.diff.file_view.scroll_x = scroll_x;
Expand Down
26 changes: 18 additions & 8 deletions src/ui/diff_viewer/tests/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ fn wrapping_folds_a_long_line_onto_several_rows() {
);
}

/// With wrapping on the gutter is folded into the body line, because a separate
/// gutter paragraph would desynchronise the moment a line spans two rows.
#[test]
fn wrapping_keeps_the_line_number_on_the_row_the_line_starts_on() {
let mut app = app_showing(long_line_hunk(), DiffPaneView::Diff);
fn wrapping_aligns_continuations_and_keeps_numbers_with_their_lines() {
let mut hunk = long_line_hunk();
hunk.lines[0].kind = LineKind::Added;
hunk.lines[0].old_lineno = None;
hunk.lines.push(DiffLine {
kind: LineKind::Added,
content: "next row".to_string(),
old_lineno: None,
new_lineno: Some(8),
});
let mut app = app_showing(hunk, DiffPaneView::Diff);
app.git.view.diff.wrap = true;

let screen = drawn(&mut app, 40, 10, 0);
Expand All @@ -64,17 +71,20 @@ fn wrapping_keeps_the_line_number_on_the_row_the_line_starts_on() {
.expect("the row the line starts on");

assert!(
first.contains('7'),
first.contains("7 +alpha"),
"the number travels with its own line: {first:?}"
);
let continuation = screen
.iter()
.find(|l| l.contains("juliett") && !l.contains("alpha"))
.expect("a continuation row");
assert!(
!continuation.contains('7'),
"a continuation row must not be numbered again: {continuation:?}"
let body_column = col_of(first, "alpha");
assert_eq!(
left_columns(continuation, body_column),
format!("│{}", " ".repeat(body_column - 1)),
"the continuation must leave the gutter and marker columns blank"
);
assert!(screen.iter().any(|row| row.contains("8 +next row")));
}

#[test]
Expand Down

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion viewer-ui/dist/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions viewer-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion viewer-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nightcrow-viewer-ui",
"private": true,
"version": "0.1.28",
"version": "0.1.29",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
Loading