summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ninep/pane.zig69
1 files changed, 32 insertions, 37 deletions
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig
index 90934791..3895414b 100644
--- a/src/ninep/pane.zig
+++ b/src/ninep/pane.zig
@@ -549,10 +549,16 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply
};
}
-/// A write joins the step being made only when it is the same open's (or
-/// the step is anyone's, `joined_by` 0): another open's starts its own.
-fn joinOnly(pane: *Pane, handle: u32) void {
+/// spliceBody for a write of the open `handle`, in the undo step its open
+/// is making: it joins the step being made only when it is the same open's
+/// (or the step is anyone's, `joined_by` 0), another open's starting its
+/// own.
+fn spliceJoined(p: *Pardes, pane: *Pane, q0: usize, q1: usize, bytes: []const u8, handle: u32) ?usize {
if (pane.fs.joined_by != 0 and pane.fs.joined_by != handle) pane.fs.joined = null;
+ const take = spliceBody(p, pane, q0, q1, bytes) orelse return null;
+ pane.fs.joined = fileOf(pane).?.revision;
+ pane.fs.joined_by = handle;
+ return take;
}
/// How long writes in a row may pause before the editor's own step puts
@@ -577,10 +583,7 @@ pub fn flushBatch(p: *Pardes) void {
const pane = p.panes[id].?;
if (fileOf(pane) == null) return;
const before = dotOf(pane);
- joinOnly(pane, b.handle);
- const take = spliceBody(p, pane, b.at, b.at, b.bytes.items) orelse return;
- pane.fs.joined = fileOf(pane).?.revision;
- pane.fs.joined_by = b.handle;
+ const take = spliceJoined(p, pane, b.at, b.at, b.bytes.items, b.handle) orelse return;
setDot(pane, .{
.q0 = shiftOne(before.q0, clip(b.at), 0, clip(take)),
.q1 = shiftOne(before.q1, clip(b.at), 0, clip(take)),
@@ -588,20 +591,28 @@ pub fn flushBatch(p: *Pardes) void {
if (!pane.fs.noscroll) showOffset(pane, b.at + take);
}
-/// The held bytes a write at `at` on this open can go on from: its own
-/// batch, when it ends right at `at`; any other is put in first.
-fn batchFor(p: *Pardes, req: Req, pane: *Pane, at: usize) ?*@TypeOf(p.fs.batch) {
+/// How many bytes this open's write holds back (`fs.batch`) for its pane.
+fn heldBytes(p: *Pardes, req: Req, pane: *Pane) usize {
const b = &p.fs.batch;
- const mine = b.serial == pane.serial and b.node == req.node and b.handle == req.handle;
- if (b.bytes.items.len > 0 and !(mine and b.at + b.bytes.items.len == at)) flushBatch(p);
- // Only an open's writes are held: one with no record (a test's, handle
- // 0) is put in at once. A hold past 64 MiB is put in first.
+ return if (b.serial == pane.serial and b.node == req.node and b.handle == req.handle) b.bytes.items.len else 0;
+}
+
+/// Holds a write at `at` on this open with the bytes it goes on from: its
+/// own batch, when it ends right at `at`; any other is put in first. The
+/// bytes taken, or null when the write is not held (one with no record, a
+/// test's handle 0, is put in at once; a hold past 64 MiB is put in first).
+fn holdWrite(p: *Pardes, req: Req, pane: *Pane, at: usize) error{OutOfMemory}!?usize {
+ const b = &p.fs.batch;
+ if (b.bytes.items.len > 0 and !(heldBytes(p, req, pane) > 0 and b.at + b.bytes.items.len == at)) flushBatch(p);
if (req.handle == 0 or b.bytes.items.len > 64 << 20) {
flushBatch(p);
return null;
}
if (b.bytes.items.len == 0) b.* = .{ .serial = pane.serial, .node = req.node, .handle = req.handle, .at = at, .bytes = b.bytes };
- return b;
+ const take = wholeUtf8(req.data);
+ try b.bytes.appendSlice(p.gpa, req.data[0..take]);
+ b.last_ns = p.now_ns;
+ return take;
}
fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
@@ -613,19 +624,10 @@ fn writeBody(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
p.emitWrite(id, req.data[0..take]);
return .{ .tag = req.tag, .written = @intCast(take) };
}
- const held = if (p.fs.batch.serial == pane.serial and p.fs.batch.node == req.node and p.fs.batch.handle == req.handle) p.fs.batch.bytes.items.len else 0;
- const at = bodyOf(pane).len + held;
- if (batchFor(p, req, pane, at)) |b| {
- const take = wholeUtf8(req.data);
- b.bytes.appendSlice(p.gpa, req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
- b.last_ns = p.now_ns;
- return .{ .tag = req.tag, .written = @intCast(take) };
- }
- joinOnly(pane, req.handle);
- const take = spliceBody(p, pane, at, at, req.data) orelse
+ const at = bodyOf(pane).len + heldBytes(p, req, pane);
+ if (holdWrite(p, req, pane, at) catch return Reply.fail(req.tag, E.NOMEM)) |take| return .{ .tag = req.tag, .written = @intCast(take) };
+ const take = spliceJoined(p, pane, at, at, req.data, req.handle) orelse
return Reply.fail(req.tag, E.NOMEM);
- pane.fs.joined = fileOf(pane).?.revision;
- pane.fs.joined_by = req.handle;
if (!pane.fs.noscroll) showOffset(pane, at + take);
return .{ .tag = req.tag, .written = @intCast(take) };
}
@@ -714,25 +716,18 @@ fn writeData(p: *Pardes, req: Req, pane: *Pane) Reply {
if (fileOf(pane) == null) return tree.failText(req.tag, E.INVAL, e_no_text);
const pf = &pane.fs;
if (pf.addr_failed) return tree.failText(req.tag, E.INVAL, e_addr_failed);
- const held = if (p.fs.batch.serial == pane.serial and p.fs.batch.node == req.node and p.fs.batch.handle == req.handle) p.fs.batch.bytes.items.len else 0;
- clampAddr(pf, bodyOf(pane).len + held);
+ clampAddr(pf, bodyOf(pane).len + heldBytes(p, req, pane));
const q0: usize = pf.addr.q0;
const q1: usize = @max(q0, @as(usize, pf.addr.q1));
// An insert (an empty address) goes on from the open's held bytes.
- if (q1 == q0) if (batchFor(p, req, pane, q0)) |b| {
- const take = wholeUtf8(req.data);
- b.bytes.appendSlice(p.gpa, req.data[0..take]) catch return Reply.fail(req.tag, E.NOMEM);
- b.last_ns = p.now_ns;
+ if (q1 == q0) if (holdWrite(p, req, pane, q0) catch return Reply.fail(req.tag, E.NOMEM)) |take| {
pf.addr = .{ .q0 = clip(q0 + take), .q1 = clip(q0 + take) };
return .{ .tag = req.tag, .written = @intCast(take) };
};
flushBatch(p);
const before = dotOf(pane);
- joinOnly(pane, req.handle);
- const take = spliceBody(p, pane, q0, q1, req.data) orelse
+ const take = spliceJoined(p, pane, q0, q1, req.data, req.handle) orelse
return Reply.fail(req.tag, E.NOMEM);
- pf.joined = fileOf(pane).?.revision;
- pf.joined_by = req.handle;
setDot(pane, .{
.q0 = shiftOne(before.q0, clip(q0), clip(q1 - q0), clip(take)),
.q1 = shiftOne(before.q1, clip(q0), clip(q1 - q0), clip(take)),