summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 15:23:45 -0300
committerGabriel Schneider <[email protected]>2026-10-24 21:00:32 -0300
commita9e33e52d789c7433da1da17a8c82093c9114243 (patch)
tree0fb83bc2be369b5a36c86ce8173fb9114503528b /src/File.zig
parente28b0992e9cd8dcedea5ed435566277ee6a93d5b (diff)
downloadpardes-a9e33e52d789c7433da1da17a8c82093c9114243.tar.gz
pardes-a9e33e52d789c7433da1da17a8c82093c9114243.zip
anim 7: what the probe found: the paced wait spins (asleep, the CPU's clock fell and an input's frame took 2-5x as long), a glide measures the core's line count first and emits only the rows it shows, a far jump paints only its view, a long line 16 KB round the caret, the caret's sideways scroll measured from the view's edge, a paint never joined on the loop nor asked while the view stands still, rewritten generated panes do not glide, pictures decode off the loop
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig101
1 files changed, 88 insertions, 13 deletions
diff --git a/src/File.zig b/src/File.zig
index 798e3e84..6df49c98 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -26,6 +26,7 @@ const terminal = panes.terminal;
const Text = panes.Text;
const SYNTAX_CONTEXT_AFTER_ROWS: usize = 2;
+const long_paint: usize = 16 << 10;
/// Content and primary selection at one file edit boundary. Keeping only the
/// primary avoids putting Text.max_selections ranges in every history entry.
@@ -1724,8 +1725,23 @@ pub fn refreshHighlights(p: *Pardes) void {
f.syntax_dirty = false;
continue;
}
- const need_start = lineStart(p.gpa, f, f.scroll);
- const need_end = @max(need_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS));
+ // ponytail: a stretch over `long_paint` bytes (a minified line) is
+ // painted only around the caret, so a key typed in a 500 KB line
+ // parses 16 KB, not the line (190 ms a key in ReleaseSafe; 25 ms at
+ // 64 KB, so 16, still eight views of 80x25); its first
+ // tokens may be coloured as if the line began there. The upgrade: an
+ // incremental parse of the whole text, edited in place.
+ const caret = @min(f.content.len, lineStart(p.gpa, f, @intCast(@max(0, pane.body.cur_row))) + @as(usize, @intCast(@max(0, pane.body.cur_col))));
+ const around = struct {
+ fn clamp(a: usize, b: usize, at: usize) [2]usize {
+ if (b - a <= long_paint) return .{ a, b };
+ const lo = std.math.clamp(at -| long_paint / 2, a, b - long_paint);
+ return .{ lo, lo + long_paint };
+ }
+ };
+ const need = around.clamp(lineStart(p.gpa, f, f.scroll), @max(lineStart(p.gpa, f, f.scroll), lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS)), caret);
+ const need_start = need[0];
+ const need_end = need[1];
if ((!f.tree_context or (f.highlight_start == 0 and f.highlights.len == f.content.len)) and
f.highlights.len > 0 and need_start >= f.highlight_start and
need_end <= f.highlight_start + f.highlights.len)
@@ -1734,7 +1750,11 @@ pub fn refreshHighlights(p: *Pardes) void {
if (!f.tree_context and pane.colorAlgo() == .source) paintAhead(p, pane, f);
continue;
}
- const slack: usize = if (f.highlights.len == 0) 0 else pane.rows;
+ // A view far from what is painted (a jump: ge, a Look) paints only
+ // itself on the frame, a third of the work; the stretch around it
+ // comes painted ahead (paintAhead) where there are workers.
+ const far = f.highlights.len == 0 or need_end <= f.highlight_start or need_start >= f.highlight_start + f.highlights.len;
+ const slack: usize = if (far and p.host.vtable.search != null) 0 else if (f.highlights.len == 0) 0 else pane.rows;
const whole = pane.colorAlgo() == .locations or f.tree_context;
// Painted ahead on a worker (Stream.paintAhead): taken, no parse.
if (!whole and pane.colorAlgo() == .source) if (Stream.takeAhead(p, pane.serial, f.revision, need_start, need_end)) |got| {
@@ -1747,11 +1767,14 @@ pub fn refreshHighlights(p: *Pardes) void {
continue;
};
if (pane.colorAlgo() == .source and !whole) p.stream.painted_on_loop += 1;
- const start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack);
- const end = if (whole)
+ const wide_start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack);
+ const wide_end = if (whole)
f.content.len
else
- @max(start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack));
+ @max(wide_start, lineStart(p.gpa, f, f.scroll + pane.rows + SYNTAX_CONTEXT_AFTER_ROWS + slack));
+ const window = if (whole) [2]usize{ wide_start, wide_end } else around.clamp(wide_start, wide_end, caret);
+ const start = window[0];
+ const end = window[1];
const new_highlights = (switch (pane.colorAlgo()) {
.none => @as([]u8, &.{}),
.diff => syntax.highlightDiff(p.tree_sitter_gpa, f.content, start, end),
@@ -1772,24 +1795,76 @@ pub fn refreshHighlights(p: *Pardes) void {
}
}
-/// The stretch a scroll of `pane` is heading into, two views on the way it
-/// last went, asked of a worker (Stream.paintAhead) so it is painted by the
-/// time the view gets there.
+/// The stretch a scroll of `pane` is heading into, the view and three more
+/// the way it last went, asked of a worker (Stream.paintAhead) so it is
+/// painted by the time the view gets there. From the view itself: two
+/// views out, an upward stretch stopped short of the view's own bottom
+/// rows, and a half page up (or down) missed it by a line and painted on
+/// the frame, 11-17 ms in ReleaseSafe.
fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void {
const seen = &p.stream.ahead_seen;
- if (seen.serial == pane.serial and f.scroll != seen.scroll) seen.dir = if (f.scroll > seen.scroll) 1 else -1;
+ // A view standing still (typing in it) needs nothing ahead: each edit
+ // is a new revision, and a paint asked at every keystroke was a copy
+ // of the stretch and a worker's start on the loop per key.
+ if (seen.serial == pane.serial and f.scroll == seen.scroll) return;
+ if (seen.serial == pane.serial) seen.dir = if (f.scroll > seen.scroll) 1 else -1;
if (seen.serial != pane.serial) seen.dir = 1;
seen.serial = pane.serial;
seen.scroll = f.scroll;
const rows: usize = pane.rows;
- const view = if (seen.dir > 0) f.scroll + 2 * rows else f.scroll -| 2 * rows;
- const start = lineStart(p.gpa, f, view -| rows);
- const end = @max(start, lineStart(p.gpa, f, view + 2 * rows + SYNTAX_CONTEXT_AFTER_ROWS));
+ const start = lineStart(p.gpa, f, if (seen.dir > 0) f.scroll else f.scroll -| 3 * rows);
+ const end = @max(start, lineStart(p.gpa, f, f.scroll + (if (seen.dir > 0) 4 * rows else rows) + SYNTAX_CONTEXT_AFTER_ROWS));
// Painted already, as far as that goes.
if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return;
Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end);
}
+test "a long line is painted only around the caret" {
+ if (!syntax.enabled) return;
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ const line = try std.testing.allocator.alloc(u8, 600 << 10);
+ defer std.testing.allocator.free(line);
+ for (line, 0..) |*c, i| c.* = if (i % 2 == 0) '1' else ',';
+ const pane = try p.setTestFile(line);
+ p.gpa.free(pane.file.?.path);
+ pane.file.?.path = try p.gpa.dupe(u8, "/tmp/long.json");
+ pane.body.cur_col = 300 << 10;
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ _ = try p.render(arena.allocator());
+ const f = &pane.file.?;
+ try std.testing.expect(f.highlights.len == long_paint);
+ try std.testing.expect(f.highlight_start <= 300 << 10 and f.highlight_start + f.highlights.len >= 300 << 10);
+}
+
+test "a caret deep in a long line scrolls the view sideways to it, measured from the view's edge" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ p.settings.wrap = false;
+ const line = try std.testing.allocator.alloc(u8, 500 << 10);
+ defer std.testing.allocator.free(line);
+ @memset(line, 'a');
+ const pane = try p.setTestFile(line);
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ _ = try p.render(arena.allocator());
+ // Right, far: the caret ends a margin in from the right edge.
+ pane.body.cur_col = 400_000;
+ pane.ensureCursorVisible();
+ const w: i32 = @as(i32, pane.cols) - @as(i32, gutterWidth(pane));
+ const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2));
+ try std.testing.expectEqual(@as(i32, 400_000 - (w - 1 - hmargin)), pane.hscroll);
+ // Back left of the view: a margin in from the left edge.
+ pane.body.cur_col = 100_000;
+ pane.ensureCursorVisible();
+ try std.testing.expectEqual(@as(i32, 100_000 - hmargin), pane.hscroll);
+ // At the start: none.
+ pane.body.cur_col = 1;
+ pane.ensureCursorVisible();
+ try std.testing.expectEqual(@as(i32, 0), pane.hscroll);
+}
+
pub fn supportsContext(pane: *const Pane) bool {
const file = pane.file orelse return false;
return file.output == null and file.mini == null and syntax.supportsPath(file.path);