diff options
Diffstat (limited to 'core/File.zig')
| -rw-r--r-- | core/File.zig | 140 |
1 files changed, 111 insertions, 29 deletions
diff --git a/core/File.zig b/core/File.zig index 450e4ec9..c4fc8a38 100644 --- a/core/File.zig +++ b/core/File.zig @@ -19,6 +19,7 @@ const locations = @import("locations.zig"); const Colors = @import("colors.zig"); const Tables = @import("Tables.zig"); const Stream = @import("Stream.zig"); +const jj = @import("jj.zig"); const Pane = panes.Pane; const Output = panes.Output; const Mini = panes.mini; @@ -372,7 +373,13 @@ pub fn gutterWidth(pane: *const Pane) u16 { var lines = if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content); var digits: u16 = 1; while (lines >= 10) : (lines /= 10) digits += 1; - return @max(config.PREFIX_W, digits + 1); + return @max(config.PREFIX_W, digits + 1) + laneCells(pane) + pane.layers.heat; +} + +/// The cells the changes' lanes take after the line number in this view +/// (File.Layers), none while it pins no changes. +pub fn laneCells(pane: *const Pane) u16 { + return pane.layers.lanes; } /// Convert between rendered cells and UTF-8 byte columns. Tag rows always @@ -393,7 +400,7 @@ pub fn renderedLineDisplayCol(tab: u16, pane: *const Pane, row: i32, line_text: return prefix +| rawDisplayCol(tab, line_text[prefix..], byte_col - prefix); } -fn fitEnd(tab: u16, text: []const u8, start: usize, width: usize) usize { +pub fn fitEnd(tab: u16, text: []const u8, start: usize, width: usize) usize { var end = start; var used: usize = 0; while (used < width and end < text.len) { @@ -750,6 +757,8 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who }; }; reportEdit(p, f, off, len, bytes); + // A pinned change's per-line values ride with the lines (jj.zig). + if (p.changes.answers.items.len | p.changes.heats.items.len > 0) jj.edited(p, f, off, len, bytes); if (f.mini) |*mini| mini.deinit(p.gpa); f.mini = null; if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes); @@ -1226,6 +1235,7 @@ fn pageEnd(tab: u16, pane: *Pane) usize { if (pane.wrap_n == 0) return f.scroll + pane.rows; const last = (if (pane.body_visible_rows > 0) @min(pane.wrap_n, pane.body_visible_rows) else pane.wrap_n) - 1; const line: usize = @intCast(pane.wrap_line[last]); + if (pane.wrap_col[last] < 0) return @max(line, f.scroll + 1); if (!pane.body_wrapped or line >= nlines(pane.gpa, f)) return line + 1; const text = sourceLine(pane, @intCast(line)); const width = wrapWidth(pane, pane.view.wrap) -| pane.wrap_pad[last]; @@ -1650,7 +1660,7 @@ fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void { /// The path a text's language is known by: a file at a revision's /// (jj.zig) without its `@rev`. pub fn langPath(f: *const State) []const u8 { - return if (@import("jj.zig").of(f)) |r| r.path else f.path; + return if (jj.of(f)) |r| r.path else f.path; } pub fn supportsContext(pane: *const Pane) bool { @@ -1677,6 +1687,12 @@ fn prepareContextRows(pane: *Pane, file: *State) void { pub fn wrapWidth(pane: *const Pane, wrap: bool) usize { if (!wrap or pane.rows > pane.wrap_line.len) return 0; + return bodyWidth(pane); +} + +/// The cells a body row has for text after the gutter (and a cell for a +/// wrapped line's marker). +pub fn bodyWidth(pane: *const Pane) usize { return @max(1, @as(usize, pane.cols -| gutterWidth(pane)) -| 1); } @@ -1697,7 +1713,8 @@ pub fn visualRow(tab: u16, line: []const u8, col: usize, width: usize) VisualRow pub fn bodyText(tab: u16, arena: std.mem.Allocator, pane: *Pane, f: *State) ![]const u8 { geometry(tab, pane); const out = try arena.alloc(u8, pane.geometry_len); - const filled = fillBody(tab, out, pane, f, wrapWidth(pane, pane.view.wrap), false); + const width = wrapWidth(pane, pane.view.wrap); + const filled = if (hasGhosts(pane)) fillBody(true, tab, out, pane, f, width, false) else fillBody(false, tab, out, pane, f, width, false); std.debug.assert(filled == out.len); return out; } @@ -1741,7 +1758,13 @@ fn mapRows(tab: u16, pane: *Pane, f: *State) usize { pane.body_wrapped = width > 0; f.tables.ensure(tab, pane.gpa, f); prepareContextRows(pane, f); - return fillBody(tab, null, pane, f, width, true); + return if (hasGhosts(pane)) fillBody(true, tab, null, pane, f, width, true) else fillBody(false, tab, null, pane, f, width, true); +} + +/// Whether this view of the pane shows ghost rows (jj.zig): its pinned +/// changes shown, and some. +fn hasGhosts(pane: *const Pane) bool { + return pane.layers.lanes > 0 and pane.ghosts.len > 0; } /// Everything the geometry is made of, hashed: the text (its revision and @@ -1756,7 +1779,7 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context.revision orelse 0, f.context.declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len, pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h, - pane.view.rect_h, + pane.view.rect_h, laneCells(pane) + @as(u16, pane.layers.heat), }; // One hash of them laid out as words: every reader of the map asks for // this key, a hundred times a frame. @@ -1768,9 +1791,14 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 { /// Run the file-body row walk. With no destination it is the exact sizing /// pass; with one it fills that allocation and records the wrapping map. -fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { +/// `ghosted`: the view shows ghost rows (`Pane.ghosts`), each a row of its +/// own before the line it stands at, no number, its text cut to the row; a +/// body with none walks without them. +fn fillBody(comptime ghosted: bool, tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { if (record_wrap) pane.wrap_n = 0; const prefix_width = gutterWidth(pane); + // Where a number ends: a blank, then the heat lane and the lanes after it. + const number_end = prefix_width -| laneCells(pane) -| pane.layers.heat -| 1; const total = nlines(pane.gpa, f); // The slot after the text's last newline, found once: every numbered // row below asks whether it is that one. @@ -1778,12 +1806,42 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w var body_line: usize = f.scroll; var body_at: usize = 0; var written: usize = 0; + // The first ghost row at or below the top line. + var ghost: usize = 0; + if (ghosted) while (ghost < pane.ghosts.len and pane.ghosts[ghost].at < f.scroll) { + ghost += 1; + }; for (0..if (pane.body_rows > 0) pane.body_rows else pane.rows) |i| { if (i > 0) { if (dst) |out| out[written] = '\n'; written += 1; } const context = i < pane.context_rows; + if (ghosted and !context and body_at == 0 and ghost < pane.ghosts.len and pane.ghosts[ghost].at == body_line) { + const g = pane.ghosts[ghost]; + const said = pane.ghost_text[g.text..][0..g.len]; + var fit: [jj.row_max]u8 = undefined; + // A description is one compact row whatever the scroll. + const shown = if (g.kind == .description) + jj.fitted(tab, said, bodyWidth(pane), &fit) + else if (width > 0) + said[0..fitEnd(tab, said, 0, width)] + else + said[modal.graphemeStart(said, @min(@as(usize, @intCast(@max(0, pane.hscroll))), said.len))..]; + if (record_wrap) { + pane.wrap_line[i] = @intCast(body_line); + pane.wrap_col[i] = -1 - @as(i32, @intCast(ghost)); + pane.wrap_pad[i] = 0; + pane.wrap_n = @intCast(i + 1); + } + if (dst) |out| { + @memset(out[written..][0..prefix_width], ' '); + @memcpy(out[written + prefix_width ..][0..shown.len], shown); + } + written += prefix_width + shown.len; + ghost += 1; + continue; + } const row = if (context) @as(usize, @intCast(pane.wrap_line[i])) else body_line; const at = if (context) 0 else body_at; const text = if (row < total) sourceLine(pane, @intCast(row)) else ""; @@ -1801,30 +1859,32 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w modal.graphemeStart(text, @min(@as(usize, @intCast(pane.hscroll)), text.len)) else at; - if ((width > 0 or pane.context_rows > 0) and record_wrap) { + if ((width > 0 or pane.context_rows > 0 or ghosted) and record_wrap) { pane.wrap_line[i] = @intCast(row); pane.wrap_col[i] = @intCast(cut); pane.wrap_pad[i] = @intCast(pad); pane.wrap_n = @intCast(i + 1); } if (row < total) { - var lbuf: [@max(config.PREFIX_W, 32)]u8 = undefined; - const prefix = lbuf[0..prefix_width]; - @memset(prefix, ' '); - if (at == 0 and prefix.len > 0) { - var lineno = row + 1; - var digit: usize = prefix.len - 1; - if (row == end_slot) { - prefix[digit - 1] = '~'; - } else while (true) { - digit -= 1; - prefix[digit] = '0' + @as(u8, @intCast(lineno % 10)); - lineno /= 10; - if (lineno == 0) break; + // The number only where the text is wanted: a geometry pass (no + // `dst`, a hundred a frame) counts its cells and nothing more. + if (dst) |out| { + const prefix = out[written..][0..prefix_width]; + @memset(prefix, ' '); + if (at == 0 and prefix.len > 0) { + var lineno = row + 1; + var digit: usize = number_end; + if (row == end_slot) { + prefix[digit - 1] = '~'; + } else while (true) { + digit -= 1; + prefix[digit] = '0' + @as(u8, @intCast(lineno % 10)); + lineno /= 10; + if (lineno == 0) break; + } } } - if (dst) |out| @memcpy(out[written..][0..prefix.len], prefix); - written += prefix.len; + written += prefix_width; if (dst) |out| @memcpy(out[written..][0..guide.len], guide); written += guide.len; // A table row's pads are blanks before their bytes: the row @@ -1868,13 +1928,34 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w /// made without the language's ink: highlightDiffPlain). /// /// Both are painted in recolorSyntax's one walk over the cells, each where -/// it says (one walk per painter cost a frame a sixth more). Ruled, not -/// built (PARDES.md "Changes on the source view"): 3. changes: a lane after -/// the line number, a line added whole green across it, a changed line's -/// words tinted, ghost rows for what the shown revision has not; 4. churn: -/// a heat lane after the number and a strip by the scroll thumb. +/// it says (one walk per painter cost a frame a sixth more). +/// +/// 3. changes (`Changes [revset]`, jj.zig): while `lanes` is set, a +/// lane each of the pane's pinned changes after the line number (the +/// gutter `lanes` cells wider), a line one added whole green across +/// it (jj.paint). Ruled, not built (PARDES.md "Changes on the source +/// view"): a changed line's words tinted, ghost rows for what the +/// shown revision has not. +/// 4. churn, ruled, not built: a heat lane after the number and a strip +/// by the scroll thumb. +/// +/// Two bytes, hashed in one update: a digest takes every view's of every +/// pane at every step. pub const Layers = struct { ink: bool = true, + /// Whether this view shows the pane's pinned changes (`Pardes.changes`: + /// the revset and jj's answers are the pane's), as the cells their + /// lanes take (laneCells), 0 while it does not: set from the view's + /// frontend, in a window two, which its strips of pixels fill one and + /// a half of (Surface.Lane), in a terminal three, a glyph each. + lanes: u8 = 0, + /// Whether this view shows the pane's churn (`Churn`, jj.Heat), as the + /// cells its lane takes: one after the number, before the lanes. + heat: u8 = 0, + + pub fn digest(l: *const Layers, h: *pardes.walk.Hasher) void { + h.update(&.{ @intFromBool(l.ink), l.lanes, l.heat }); + } }; pub const SynStyle = struct { fg: [3]u8, bold: bool }; @@ -1927,6 +2008,7 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. limit = line.len; } else { if (vr >= pane.wrap_n) break; + if (pane.wrap_col[vr] < 0) continue; const lrow: usize = @intCast(@max(0, pane.wrap_line[vr])); if (lrow >= total) break; base = lineStart(p.gpa, f, lrow); @@ -2074,7 +2156,7 @@ pub fn paintWordSelection( var vr: i32 = 0; while (vr + @as(i32, pane.tag_rows) < @as(i32, r.h)) : (vr += 1) { const here = pane.wrapAt(p.settings.tab, vr); - if (here.line != row) continue; + if (here.line != row or here.ghost) continue; var hi = word_hi; const next = pane.wrapAt(p.settings.tab, vr + 1); if (next.line == row) hi = @min(hi, next.at); |
