diff options
Diffstat (limited to 'test/session.zig')
| -rw-r--r-- | test/session.zig | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/session.zig b/test/session.zig index 8e03b424..b35d08ad 100644 --- a/test/session.zig +++ b/test/session.zig @@ -41,6 +41,10 @@ pub const Session = struct { /// meanwhile (a reload). hold_jobs: bool = false, held: std.ArrayList(*pardes.host_io.KindJob) = .empty, + /// The files the core asked to watch, watched as a host watches them + /// (file_watch.zig, inotify): `filesChanged` wakes them. + watch_fd: c_int = -1, + watches: pardes.file_watch.Table = @splat(null), /// A session showing one file pane holding `text`, keeping what outlives /// it in a database in memory (src/Db.zig). @@ -95,6 +99,10 @@ pub const Session = struct { pub fn deinit(s: *Session) void { s.held.deinit(pardesOf(s).gpa); + if (s.watch_fd >= 0) { + pardes.file_watch.unwatchAll(s.watch_fd, &s.watches); + _ = std.c.close(s.watch_fd); + } pardesOf(s).deinit(); s.arena.deinit(); } @@ -241,10 +249,27 @@ pub const Session = struct { // held while a test holds them (`hold_jobs`). .job => |j| if (!s.hold_jobs) pardesOf(s).perform(e) else if (pardes.host_io.KindJob.take(pardesOf(s), j.pane, j.serial, j.token)) |taken| s.held.append(pardesOf(s).gpa, taken) catch taken.drop(), .lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }), + .watch => |w| { + if (s.watch_fd < 0) s.watch_fd = pardes.file_watch.init(true); + if (s.watch_fd >= 0) _ = pardes.file_watch.applyEffect(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), s.watch_fd, &s.watches, w.pane, w.on, w.mode); + }, else => {}, }; } + /// What a host does when its watch wakes: every watched file that + /// changed on disk read again into its pane. Waits for the wake (up to + /// a second); false when none came. + pub fn filesChanged(s: *Session) !bool { + if (s.watch_fd < 0) return false; + var pfd = [_]std.c.pollfd{.{ .fd = s.watch_fd, .events = std.c.POLL.IN, .revents = 0 }}; + if (std.c.poll(&pfd, 1, 1000) <= 0) return false; + _ = pardes.file_watch.drain(s.watch_fd); + _ = pardes.file_watch.reloadChanged(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), pardesOf(s).gpa, &s.watches); + s.drain(); + return true; + } + /// The jobs held, run now and answered as the host's workers would. pub fn releaseJobs(s: *Session) void { s.hold_jobs = false; |
