summaryrefslogtreecommitdiff
path: root/src/Stream.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Stream.zig')
-rw-r--r--src/Stream.zig49
1 files changed, 34 insertions, 15 deletions
diff --git a/src/Stream.zig b/src/Stream.zig
index ace73ca4..e42d4a16 100644
--- a/src/Stream.zig
+++ b/src/Stream.zig
@@ -140,6 +140,7 @@ const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null,
/// a parse's (those count from 0 in `gen`, and it would take two billion
/// searches to reach it).
const paint_bit: u32 = 1 << 31;
+const max_paint_bytes = 512 << 10;
/// Where a worker hands a batch: false when it was turned away (the inbox
/// closed, the wait for room canceled), and the worker stops.
@@ -204,6 +205,10 @@ parse_ns: u64 = 0,
/// The paint running (one at a time), its generation's count, and the
/// painted stretch it gave, kept until the view gets there (tree_sitter_gpa).
paint: ?PaintTask = null,
+/// One told to stop and not waited for: joined when its answer comes (a
+/// paint of a long line runs on in its query; waiting for it here held the
+/// loop for half a second). No new paint starts while it is out.
+paint_retired: ?PaintTask = null,
paint_count: u32 = 0,
ahead: []u8 = &.{},
ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{},
@@ -237,18 +242,20 @@ pub fn cancelParse(s: *Stream) void {
t.job.deinit();
}
-/// The paint's worker told to stop and joined.
+/// The paints' workers told to stop and joined (the session's end).
pub fn cancelPaint(s: *Stream) void {
- const t = s.paint orelse return;
- s.paint = null;
- t.job.stop.store(true, .release);
- if (t.future) |f| {
- pardes.turn.yield();
- var future = f;
- future.cancel(t.io.?);
- pardes.turn.back();
+ for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| {
+ const t = slot.* orelse continue;
+ slot.* = null;
+ t.job.stop.store(true, .release);
+ if (t.future) |f| {
+ pardes.turn.yield();
+ var future = f;
+ future.cancel(t.io.?);
+ pardes.turn.back();
+ }
+ t.job.deinit();
}
- t.job.deinit();
}
/// The painted stretch kept for pane `serial`'s text at `revision`, if it
@@ -271,12 +278,19 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont
if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return;
const a = s.ahead_for;
if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return;
+ // A stretch bigger than this (a few lines of a minified file) is not
+ // painted ahead: the loop paints what it shows of it.
+ if (to - from > max_paint_bytes) return;
if (s.paint) |t| {
// One under way for the same text that reaches into the stretch
// asked for is let finish: held keys move the stretch every frame,
// and a paint restarted every frame would never land.
if (t.job.serial == serial and t.job.revision == revision and t.job.start < to and t.job.start + t.job.source.len > from) return;
- s.cancelPaint();
+ // Else it is told to stop and left to end on its own, once.
+ if (s.paint_retired != null) return;
+ t.job.stop.store(true, .release);
+ s.paint_retired = t;
+ s.paint = null;
}
if (!syntax.readyApart(path)) return;
const job = p.gpa.create(PaintJob) catch return;
@@ -297,8 +311,10 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont
}
fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void {
- const styles = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch return;
- if (!sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done })) return;
+ // Stopped or failed, it still answers (with nothing), so the loop
+ // knows to join it.
+ const styles: []u8 = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch @constCast(&[_]u8{});
+ _ = sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done });
}
/// A paint's answer: its task joined, its stretch kept for when the view
@@ -306,8 +322,9 @@ fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void {
fn receivePaint(p: *Pardes, b: *const Batch) void {
const s = &p.stream;
const painted = b.painted.?;
- if (s.paint) |t| if (t.job.gen == b.gen) {
- s.paint = null;
+ // Its task joined: it has sent its last, so the join is a return away.
+ for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| if (slot.*) |t| if (t.job.gen == b.gen) {
+ slot.* = null;
if (t.future) |f| {
pardes.turn.yield();
var future = f;
@@ -315,6 +332,8 @@ fn receivePaint(p: *Pardes, b: *const Batch) void {
pardes.turn.back();
}
t.job.deinit();
+ // A retired one's answer is for a stretch nobody wants now.
+ if (slot == &s.paint_retired) return;
};
const id = p.paneBySerial(painted.serial) orelse return;
const f = p.panes[id].?.file orelse return;