summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig51
1 files changed, 50 insertions, 1 deletions
diff --git a/src/File.zig b/src/File.zig
index d795eaa1..dd6b2d34 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -1208,7 +1208,12 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
// the caller did not say, is what the line index wants anyway.
var twins: [pardes.MAX_PANES]usize = undefined;
const ntwins = twinsOf(p, f, &twins);
- const span: ?Span = span_in orelse if (ntwins > 0) spanOf(f.content, new) else null;
+ // The views not live keep their place in it as twins do (views.zig).
+ const viewed = pardes.views.several(p);
+ const span: ?Span = span_in orelse if (ntwins > 0 or viewed) spanOf(f.content, new) else null;
+ var parked: ParkedPlaces = .{};
+ if (viewed) parked.before(p, f, span.?, new.len);
+ defer if (viewed) parked.after(p);
var moved: [pardes.MAX_PANES]struct { cursor: usize, scroll: usize } = undefined;
for (twins[0..ntwins], 0..) |id, i| {
const t = p.panes[id].?;
@@ -1301,6 +1306,50 @@ pub fn setContentSpan(p: *Pardes, f: *State, new: []u8, span_in: ?Span) void {
}
}
+/// Each other view's cursor and scroll on a text being changed, as offsets
+/// moved by the splice (`spliced`), put back as places in the new text once
+/// it is in: a view not live keeps its place, as a twin does. Its
+/// selections go, as a twin's do.
+const ParkedPlaces = struct {
+ const max = pardes.MAX_PANES * pardes.views.max;
+ at: [max]struct { pane: *Pane, view: u8, cursor: usize, scroll: usize } = undefined,
+ n: usize = 0,
+
+ fn before(pp: *ParkedPlaces, p: *Pardes, f: *State, span: Span, new_len: usize) void {
+ for (p.panes) |slot| {
+ const pane = slot orelse continue;
+ const tf = if (pane.file) |*file| file else continue;
+ if (tf != f and (f.twin == 0 or tf.twin != f.twin)) continue;
+ for (0..pardes.views.max) |k| {
+ if (k == p.view or !pardes.views.isOpen(p, @intCast(k)) or pp.n == max) continue;
+ const v = &pane.pv[k];
+ const at = lineStart(p.gpa, tf, @intCast(@max(0, v.body.cur_row))) + @as(usize, @intCast(@max(0, v.body.cur_col)));
+ pp.at[pp.n] = .{
+ .pane = pane,
+ .view = @intCast(k),
+ .cursor = spliced(@min(at, f.content.len), f.content.len, new_len, span),
+ .scroll = spliced(@min(lineStart(p.gpa, tf, v.scroll), f.content.len), f.content.len, new_len, span),
+ };
+ pp.n += 1;
+ }
+ }
+ }
+
+ fn after(pp: *ParkedPlaces, p: *Pardes) void {
+ for (pp.at[0..pp.n]) |a| {
+ const tf = &(a.pane.file orelse continue);
+ const v = &a.pane.pv[a.view];
+ const cursor = positionIn(p.gpa, tf, @min(a.cursor, tf.content.len));
+ v.body.cur_row = @intCast(cursor.row);
+ v.body.cur_col = @intCast(cursor.col);
+ v.body.vsel.active = false;
+ v.body.msel.active = false;
+ v.body.nsel = 0;
+ v.scroll = positionIn(p.gpa, tf, @min(a.scroll, tf.content.len)).row;
+ }
+ }
+};
+
/// Where an offset into the old text is in the new, after the splice that
/// kept `span` at either end: past it, moved by what it added; inside it,
/// at its start, as acme moves a clone's q0 (textinsert, textdelete).