From 8ec8924cd577f71953f8e420af1cd60da92f3300 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 15 Sep 2026 13:13:36 -0300 Subject: Add optional tree-sitter declaration context --- src/panes.zig | 148 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 103 insertions(+), 45 deletions(-) (limited to 'src/panes.zig') diff --git a/src/panes.zig b/src/panes.zig index b2f05cdf..32fe8c28 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -166,6 +166,9 @@ pub const Pane = struct { wrap_line: [limits.wrap_rows]i32 = undefined, wrap_col: [limits.wrap_rows]i32 = undefined, wrap_n: u16 = 0, + /// A row map also exists for sticky headers when soft wrapping is off. + body_wrapped: bool = false, + context_rows: u16 = 0, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, @@ -352,30 +355,41 @@ pub const Pane = struct { } pub fn ensureCursorVisible(pane: *Pane) void { - // scrolloff margin, shrunk on short panes so the band stays non-empty - var margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, pane.rows) - 1, 2)); const off = pane.scroll(); + var in_context = false; + // Maps survive until the next render. A jump may have changed scroll + // already, so only treat an ancestor as pinned in the matching view. + if (pane.context_rows < pane.wrap_n and pane.wrap_line[pane.context_rows] == off) { + for (pane.wrap_line[0..pane.context_rows]) |row| { + if (row == pane.cur_row) { + in_context = true; + break; + } + } + } + // Scrolloff is measured in the ordinary body, excluding sticky rows. + var margin: i32 = @min(config.scroll_off, @divTrunc(@as(i32, pane.rows) - 1, 2)); var last = off + @as(i32, pane.rows) - 1; - if (pane.wrap_n > 0) { - const lines_shown = pane.wrap_line[pane.wrap_n - 1] - pane.wrap_line[0]; + if (pane.context_rows < pane.wrap_n) { + const lines_shown = pane.wrap_line[pane.wrap_n - 1] - pane.wrap_line[pane.context_rows]; last = off + lines_shown; margin = @min(margin, @divTrunc(@max(0, lines_shown), 2)); } - if (pane.cur_row < off + margin) { - pane.scrollBy(pane.cur_row - margin - off); // scrollBy clamps at line 0 - } else if (pane.cur_row > last - margin) { - // don't scroll a file past EOF-at-bottom-row (vim's bottom clamp); - // terminals overshoot harmlessly — the vt clamps at the live bottom - var to = pane.cur_row + margin; - if (pane.file) |*f| to = @min(to, @as(i32, @intCast(File.nlines(pane.gpa, f) -| 1))); - if (comptime Pdf.enabled) { - if (pane.pdf) |pv| - to = @min(to, @as(i32, @intCast(modal.lineCount(pv.text) -| 1))); + if (!in_context) { + if (pane.cur_row < off + margin) { + pane.scrollBy(pane.cur_row - margin - off); + } else if (pane.cur_row > last - margin) { + var to = pane.cur_row + margin; + if (pane.file) |*f| to = @min(to, @as(i32, @intCast(File.nlines(pane.gpa, f) -| 1))); + if (comptime Pdf.enabled) { + if (pane.pdf) |pv| + to = @min(to, @as(i32, @intCast(modal.lineCount(pv.text) -| 1))); + } + pane.scrollBy(@max(0, to - last)); } - pane.scrollBy(@max(0, to - last)); } if (pane.file) |f| { - if (pane.wrap_n != 0) return; + if (pane.body_wrapped) return; const w: i32 = @max(1, @as(i32, pane.cols) - @as(i32, File.gutterWidth(pane))); const hmargin: i32 = @min(config.scroll_off, @divTrunc(w - 1, 2)); const line = modal.lineSlice(f.content, @intCast(@max(0, pane.cur_row))); @@ -999,6 +1013,9 @@ pub const File = struct { highlights: []u8 = &.{}, highlight_start: usize = 0, syntax_dirty: bool = true, + tree_context: bool = false, + context_declarations: []syntax.ContextDeclaration = &.{}, + context_revision: ?u32 = null, history: *History, }; @@ -1022,6 +1039,7 @@ pub const File = struct { .vweight = pane.vweight, .collapsed = pane.collapsed, .file = .{ + .tree_context = file.tree_context, .path = file.path, .content = file.content, .content_b64 = try dump.encodeBytes(arena, file.content), @@ -1502,6 +1520,7 @@ pub const File = struct { .revision = @intFromBool(saved.dirty), .history = history, }; + pane.file.?.tree_context = saved.tree_context and supportsContext(pane); pane.cur_pinned = true; pane.cur_row = @intCast(src.scroll); pane.cols = @max(1, src.cols); @@ -1519,6 +1538,7 @@ pub const File = struct { if (file.mini) |*mini| mini.deinit(p.gpa); if (file.line_starts.len > 0) p.gpa.free(file.line_starts); if (file.highlights.len > 0) p.tree_sitter_gpa.free(file.highlights); + if (file.context_declarations.len > 0) p.tree_sitter_gpa.free(file.context_declarations); for (file.history.undo[0..file.history.undo_len]) |snap| p.gpa.free(snap.content); for (file.history.redo[0..file.history.redo_len]) |snap| p.gpa.free(snap.content); p.gpa.destroy(file.history); @@ -1656,6 +1676,12 @@ pub const File = struct { const pane = slot orelse continue; if (pane.file == null) continue; const f = &pane.file.?; + if (f.tree_context and supportsContext(pane) and f.context_revision != f.revision) { + const declarations = syntax.contextDeclarations(p.tree_sitter_gpa, f.path, f.content) catch continue; + if (f.context_declarations.len > 0) p.tree_sitter_gpa.free(f.context_declarations); + f.context_declarations = declarations; + f.context_revision = f.revision; + } if (f.mini != null) { f.syntax_dirty = false; continue; @@ -1670,14 +1696,15 @@ pub const File = struct { } 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)); - if (f.highlights.len > 0 and need_start >= f.highlight_start and + 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) { f.syntax_dirty = false; continue; } const slack: usize = if (f.highlights.len == 0) 0 else pane.rows; - const whole = pane.colorAlgo() == .locations; + const whole = pane.colorAlgo() == .locations or f.tree_context; const start = if (whole) 0 else lineStart(p.gpa, f, f.scroll -| slack); const end = if (whole) f.content.len @@ -1698,6 +1725,28 @@ pub const File = struct { } } + 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); + } + + fn prepareContextRows(pane: *Pane, file: *State) void { + pane.context_rows = 0; + if (!file.tree_context or !supportsContext(pane) or pane.rows > pane.wrap_line.len) return; + const capacity = pane.rows -| 1; // retain at least one ordinary body row + for (file.context_declarations) |declaration| { + if (declaration.start_line >= file.scroll) break; + if (declaration.end_line < file.scroll) continue; + var row = declaration.start_line; + while (row <= declaration.header_end_line and row < file.scroll) : (row += 1) { + if (pane.context_rows >= capacity) return; + if (pane.context_rows > 0 and row <= pane.wrap_line[pane.context_rows - 1]) continue; + pane.wrap_line[pane.context_rows] = @intCast(row); + pane.context_rows += 1; + } + } + } + pub fn wrapWidth(pane: *const Pane, wrap: bool) usize { if (!wrap or pane.rows > pane.wrap_line.len) return 0; return @max(1, @as(usize, pane.cols -| gutterWidth(pane)) -| 1); @@ -1717,6 +1766,8 @@ pub const File = struct { pub fn bodyText(arena: std.mem.Allocator, pane: *Pane, f: *State, wrap: bool) ![]const u8 { const width = wrapWidth(pane, wrap); + pane.body_wrapped = width > 0; + prepareContextRows(pane, f); pane.wrap_n = 0; const len = fillBody(null, pane, f, width, false); @@ -1731,32 +1782,37 @@ pub const File = struct { fn fillBody(dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize { if (record_wrap) pane.wrap_n = 0; const prefix_width = gutterWidth(pane); - // start ON the first visible line instead of walking the file to it: this - // walk was O(f.scroll) and recolorSyntax below ran the identical one again - var flines = std.mem.splitScalar(u8, f.content[lineStart(pane.gpa, f, f.scroll)..], '\n'); - if (f.scroll >= nlines(pane.gpa, f)) _ = flines.next(); - // the line the NEXT row comes from and the byte column of it that row - // starts at — the two the map records, walked forward by the loop - var abs: i32 = @intCast(f.scroll); - var at: usize = 0; - var cur = flines.next(); + const total = nlines(pane.gpa, f); + var body_line: usize = f.scroll; + var body_at: usize = 0; var written: usize = 0; for (0..pane.rows) |i| { if (i > 0) { if (dst) |out| out[written] = '\n'; written += 1; } - if (width > 0 and record_wrap) { - pane.wrap_line[i] = abs; - pane.wrap_col[i] = @intCast(at); + const context = i < pane.context_rows; + 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 ""; + // Headers use one screen row per source row, preserving signatures + // without allowing a long ancestor to consume the whole viewport. + const end = if (width == 0 or context) text.len else fitEnd(text, at, width); + const cut = if (pane.hscroll > 0 and width == 0) + modal.graphemeStart(text, @min(@as(usize, @intCast(pane.hscroll)), text.len)) + else + at; + if ((width > 0 or pane.context_rows > 0) and record_wrap) { + pane.wrap_line[i] = @intCast(row); + pane.wrap_col[i] = @intCast(cut); pane.wrap_n = @intCast(i + 1); } - if (cur) |text| { + if (row < total) { var lbuf: [@max(config.PREFIX_W, 32)]u8 = undefined; const prefix = lbuf[0..prefix_width]; @memset(prefix, ' '); if (at == 0) { - var lineno: usize = @intCast(abs + 1); + var lineno = row + 1; var digit: usize = prefix.len - 1; while (true) { digit -= 1; @@ -1767,24 +1823,18 @@ pub const File = struct { } if (dst) |out| @memcpy(out[written..][0..prefix.len], prefix); written += prefix.len; - - const end = if (width == 0) text.len else fitEnd(text, at, width); - const take = end - at; - const cut = if (pane.hscroll > 0 and width == 0) - modal.graphemeStart(text[at..end], @min(@as(usize, @intCast(pane.hscroll)), take)) - else - 0; - const shown = text[at + cut .. end]; + const shown = text[cut..end]; if (dst) |out| @memcpy(out[written..][0..shown.len], shown); written += shown.len; + } + if (!context) { if (width > 0 and end < text.len) { - at = end; + body_at = end; } else { - abs += 1; - at = 0; - cur = flines.next(); + body_line += 1; + body_at = 0; } - } else abs += 1; + } } return written; } @@ -1798,6 +1848,14 @@ pub const File = struct { const cur_line: i32 = if (active and !pane.tag_edit) gcrow else std.math.minInt(i32); // the body's first row, the way renderPane derives it (Tagbottom) const body_y = if (p.settings.tag_bottom) r.y else r.y + pardes.BOX_H; + const context_bg = p.theme().tag_bg; + for (0..@min(pane.context_rows, body_h)) |context_row| { + for (0..tw) |col| { + const cell = s.at(tx + @as(u16, @intCast(col)), body_y + @as(u16, @intCast(context_row))); + cell.default = false; + cell.style.bg = .{ .rgb = context_bg }; + } + } var vr: u16 = 0; while (vr < body_h) : (vr += 1) { const row_line: i32 = if (pane.wrap_n == 0) -- cgit v1.3