diff options
Diffstat (limited to 'src/File.zig')
| -rw-r--r-- | src/File.zig | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/src/File.zig b/src/File.zig index 1e553bc9..e387abab 100644 --- a/src/File.zig +++ b/src/File.zig @@ -200,6 +200,9 @@ pub const State = struct { /// and (while it is the one index) `line_starts`, each with its own /// scroll, cursor and syntax; 0, none. See `zerox`. twin: u32 = 0, + /// In its twin group's Follow chain (builtins.Follow), whose panes + /// show the text as consecutive pages (followChain). + follow: bool = false, }; pub const NodeStep = struct { ranges: []modal.Selection, primary: usize }; @@ -1020,7 +1023,10 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who const cursor = positionIn(p.gpa, tf, moved[i].cursor); t.body.cur_row = @intCast(cursor.row); t.body.cur_col = @intCast(cursor.col); + const was = tf.scroll; tf.scroll = positionIn(p.gpa, tf, moved[i].scroll).row; + // Moved by the edit, not scrolled: a Follow chain reflows from its head. + t.follow_scroll +%= tf.scroll -% was; } return true; } @@ -1094,7 +1100,9 @@ const ParkedPlaces = struct { v.body.cur_col = @intCast(cursor.col); v.body.vsel.active = false; v.body.nsel = 0; + const was = v.scroll; v.scroll = positionIn(p.gpa, tf, @min(a.scroll, tf.content.len)).row; + v.follow_scroll +%= v.scroll -% was; } } }; @@ -1232,6 +1240,7 @@ pub fn leaveTwins(p: *Pardes, f: *State) void { f.history = History.create(p.gpa) catch f.history; // The one left is no twin now (syncTwins drops the group). if (n == 1) p.panes[twins[0]].?.file.?.twin = 0; + if (n == 1) unchain(p.panes[twins[0]].?); } pub const TwinGroup = struct { @@ -1265,6 +1274,7 @@ pub fn syncTwins(p: *Pardes) void { } if (n < 2) { if (n == 1) members[0].twin = 0; + if (n == 1) unchain(member_panes[0]); p.gpa.free(group.path); _ = p.twin_groups.swapRemove(g); continue; @@ -1305,10 +1315,142 @@ pub fn syncTwins(p: *Pardes) void { std.debug.assert(other.content.ptr == members[0].content.ptr and other.content.len == members[0].content.len); std.debug.assert(other.revision == members[0].revision); }; + // Only a group with a chain pays for one. + for (members[0..n]) |f| if (f.follow) { + followChain(p, group.id); + break; + }; g += 1; } } +/// Follow's chain in twin group `twin`, as the live view lays it out: its +/// panes in column order show the text as consecutive pages, each starting +/// on the line after the one before ends. A pane scrolled (by anything: +/// the wheel, a page key, a jump, the cursor past a chain's end) moves the +/// others with it; the cursor going past a page's edge takes the keyboard +/// to the page that shows its line (ensureCursorVisible leaves the +/// keyboard's page's inner edges alone, `follow_edges`). A chain of one is +/// no chain. +fn followChain(p: *Pardes, twin: u32) void { + const v = p.view(); + var chain: [pardes.MAX_PANES]*Pane = undefined; + var ids: [pardes.MAX_PANES]usize = undefined; + var n: usize = 0; + for (v.cols[0..v.ncol]) |col| for (col.panes[0..col.n]) |id| { + const pane = p.panes[id] orelse continue; + const f = if (pane.file) |*file| file else continue; + if (f.twin != twin or !f.follow) continue; + chain[n] = pane; + ids[n] = id; + n += 1; + }; + if (n < 2) { + for (chain[0..n]) |pane| unchain(pane); + return; + } + // The head is the text's first page, never a follower of another. + dropUnfollow(chain[0]); + // The cursor moved past its page's edge: the keyboard goes to the page + // showing its line, else to the chain's end, which scrolls to it. + const active = for (ids[0..n], 0..) |id, k| { + if (id == v.active) break k; + } else null; + if (active) |k| if (v.header == null and chain[k].focus == .body) hop: { + const m = chain[k]; + const row: usize = @intCast(@max(0, m.body.cur_row)); + if (m.follow_crossed and (row < m.file.?.scroll or row >= pageEnd(m))) { + const j = for (chain[0..n], 0..) |o, i| { + if (row >= o.file.?.scroll and row < pageEnd(o)) break i; + } else if (row < m.file.?.scroll) 0 else n - 1; + if (j == k) break :hop; + const to = chain[j]; + to.body.cur_row = m.body.cur_row; + to.body.cur_col = m.body.cur_col; + to.body.cur_pinned = true; + to.body.vsel.active = false; + to.body.nsel = 0; + to.body.mode = m.body.mode; + m.body.mode = .normal; + v.active = ids[j]; + to.follow_edges = @as(u2, @intFromBool(j > 0)) | @as(u2, @intFromBool(j + 1 < n)) << 1; + to.ensureCursorVisible(); + } + }; + // The page that moved (the keyboard's first), else the head, sets the + // others: back to the head, then on from it. + var anchor: usize = 0; + for (chain[0..n], 0..) |o, k| if (o.file.?.scroll != o.follow_scroll) { + anchor = k; + if (ids[k] == v.active) break; + }; + var k = anchor; + while (k > 0) : (k -= 1) { + const prev = chain[k - 1]; + const want = chain[k].file.?.scroll; + // The furthest scroll whose page ends at `want` or before. + var s = want -| prev.rows; + setPageScroll(prev, s); + while (s < want) : (s += 1) { + setPageScroll(prev, s + 1); + if (pageEnd(prev) <= want) continue; + setPageScroll(prev, s); + break; + } + } + for (chain[1..n], 0..) |o, i| setPageScroll(o, pageEnd(chain[i])); + // Only the keyboard's page crosses its edges: a dot written to another + // scrolls it there, and the chain with it. + for (chain[0..n], ids[0..n], 0..) |o, id, i| { + o.follow_scroll = o.file.?.scroll; + o.follow_crossed = false; + o.follow_edges = if (id != v.active) 0 else @as(u2, @intFromBool(i > 0)) | @as(u2, @intFromBool(i + 1 < n)) << 1; + } +} + +/// The line a page after `pane`'s starts on: the one after its last row's, +/// or that line again when the pane wraps it on past its bottom. +fn pageEnd(pane: *Pane) usize { + const f = &pane.file.?; + if (pane.collapsed) return f.scroll; + geometry(pane); + 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.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]; + return if (fitEnd(text, @intCast(pane.wrap_col[last]), width) < text.len) @max(line, f.scroll + 1) else line + 1; +} + +fn setPageScroll(pane: *Pane, line: usize) void { + const f = &pane.file.?; + const to = @min(line, nlines(pane.gpa, f) -| 1); + if (to == f.scroll) return; + f.scroll = to; + f.syntax_dirty = true; +} + +/// `pane` out of its Follow chain (Unfollow, or a chain down to it): a +/// plain twin with the scroll it has. +pub fn unchain(pane: *Pane) void { + pane.file.?.follow = false; + pane.follow_edges = 0; + pane.follow_crossed = false; + dropUnfollow(pane); +} + +/// The Unfollow word Follow gave a pane's tag (builtins.Follow), gone. +fn dropUnfollow(pane: *Pane) void { + const own = pane.tag.own orelse return; + const at = std.mem.indexOf(u8, own, " Unfollow") orelse return; + const end = at + " Unfollow".len; + if (end < own.len and std.mem.indexOfScalar(u8, " \t\n", own[end]) == null) return; + const cut = std.mem.concat(pane.gpa, u8, &.{ own[0..at], own[end..] }) catch return; + pane.gpa.free(own); + pane.tag.own = cut; +} + /// Whether `head`, a text's first bytes, holds a diff, as a command pane's /// rows are told one (diff.looksLikeDiff, its first 200 lines). pub fn looksLikeDiff(head: []const u8) bool { |
