summaryrefslogtreecommitdiff
path: root/dev/tui
diff options
context:
space:
mode:
Diffstat (limited to 'dev/tui')
-rw-r--r--dev/tui/tui.zig163
1 files changed, 22 insertions, 141 deletions
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index c54447c7..543be3aa 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -120,14 +120,11 @@ const Msg = union(enum) {
paste: []const u8,
paste_start,
paste_end,
- lsp_done: struct { id: u32, rows: ?[]u8 },
- lsp_status: []u8,
pipe_done: selection_pipe.Response,
/// A streaming search's rows (pardes.Stream).
search_rows: pardes.Stream.Batch,
/// A kind's job done on a worker (host_io.KindJob).
kind_job: *host_io.KindJob,
- files_changed,
fs_ready,
/// A replica's connection, handed over by the daemon (Peers).
replica: daemon.Joining,
@@ -768,6 +765,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
var no_inbox: host_io.Inbox = .{ .io = io, .gpa = gpa, .lsp_allocator = start.allocs.lsp };
defer no_inbox.deinit();
var no_ptys: host_io.Ptys = .{ .queue = &no_inbox, .prompt_rcs = undefined };
+ var no_watches: file_watch.Watches = .{ .io = io, .gpa = gpa, .queue = &no_inbox };
var sh: Shell = .{
.io = io,
.gpa = gpa,
@@ -777,7 +775,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
.loop = &loop,
.frame = &frame_arena,
.paste_buf = &paste_buf,
- .inotify_fd = -1,
+ .watches = &no_watches,
.own = start.joined.view,
.screen = &screen,
.client = &c,
@@ -954,7 +952,7 @@ const Own = struct {
.loop = o.loop,
.frame = &o.frame_arena,
.paste_buf = o.paste_buf,
- .inotify_fd = file_watch.init(false),
+ .watches = &l.watches,
.fs = l.fs,
.test_clock = host_io.testClock(),
.peers = &l.peers,
@@ -966,7 +964,6 @@ const Own = struct {
l.inbox.wake_ctx = sh;
l.peers.wake_ctx = o.loop;
l.peers.wake = wakeForReplica;
- pardes.lsp.setStatusSink(sh, lspStatusSink);
host_io.setExitWake(o.loop, wakeForExit);
const host = sh.host();
l.core.host = host;
@@ -988,28 +985,12 @@ const Own = struct {
stopInput(io, &o.screen.thread);
pardes.Kind.drawer = null;
host_io.setExitWake(null, null);
- pardes.lsp.setStatusSink(null, null);
- if (sh.lsp_task) |*t| {
- t.future.cancel(io);
- sh.lsp_task = null;
- }
sh.pipe_tasks.cancelAll(io);
sh.job_tasks.cancelAll(io);
// Its worker may be waiting on the loop's queue for room.
sh.core.stream.cancelTask();
- file_watch.stop(sh.inotify_fd);
- if (sh.watch_task) |*t| {
- t.cancel(io);
- sh.watch_task = null;
- }
- if (sh.inotify_fd >= 0) {
- _ = libc.close(sh.inotify_fd);
- sh.inotify_fd = -1;
- }
while (loop.tryEvent() catch null) |ev| switch (ev) {
.paste => |b| gpa.free(@constCast(b)),
- .lsp_done => |d| if (d.rows) |rows| sh.lsp_gpa.free(rows),
- .lsp_status => |text| sh.lsp_gpa.free(text),
.pipe_done => |response_value| {
var response = response_value;
response.deinit(gpa);
@@ -1038,7 +1019,6 @@ const Own = struct {
if (!screen.on) l.watch();
sh.threads_ok = true;
- if (sh.inotify_fd >= 0) sh.watch_task = io.concurrent(watchFiles, .{ io, sh.inotify_fd, loop }) catch null;
}
/// Detached, the unit is its clients' frame: its output is paced to it
@@ -1057,17 +1037,9 @@ const Own = struct {
deliverNote(o.loop, n);
}
- pub fn watched(o: *Own) session_loop.Watched {
- return .{ .fd = o.sh.inotify_fd, .table = &o.sh.watches };
- }
-
pub fn restoring(o: *Own, nc: *pardes.Pardes) void {
const sh = &o.sh;
const io = sh.io;
- if (sh.lsp_task) |*task| {
- task.future.cancel(io);
- sh.lsp_task = null;
- }
sh.pipe_tasks.cancelAll(io);
// One view, the terminal's (host_io.restoreCore).
const live = o.screen;
@@ -1139,7 +1111,9 @@ const Shell = struct {
own: u8 = 0,
frame: *std.heap.ArenaAllocator,
paste_buf: *std.Io.Writer.Allocating,
- inotify_fd: c_int,
+ /// The session's file watches (runtime/session.zig's); a client's,
+ /// none.
+ watches: *file_watch.Watches,
fs: ?*ninep_io.Listener = null,
/// The session's ptys (runtime/session.zig's); a client's, an empty
/// table over an empty queue.
@@ -1150,9 +1124,6 @@ const Shell = struct {
batch: host_io.Inbox.Batch = .{},
batch_at: usize = 0,
queue_woken: std.atomic.Value(bool) = .init(false),
- watches: file_watch.Table = @splat(null),
- watch_task: ?std.Io.Future(void) = null,
- lsp_task: ?host_io.Lsp.Task = null,
pipe_tasks: PipeTasks = .{},
job_tasks: PipeTasks = .{},
threads_ok: bool = false,
@@ -1248,10 +1219,10 @@ const Shell = struct {
.tty_taken = PtyCalls.ttyTaken,
.fg_name = PtyCalls.fgName,
.kill_job = PtyCalls.killJob,
- .write_file = writeFile,
+ .write_file = WatchCalls.writeFile,
.write_dump = writeDump,
- .watch_file = watchFile,
- .watch_theme = watchTheme,
+ .watch_file = WatchCalls.watchFile,
+ .watch_theme = WatchCalls.watchTheme,
.dump_themes = dumpThemes,
.set_clipboard = setClipboard,
.read_clipboard = readClipboard,
@@ -1317,6 +1288,7 @@ const Shell = struct {
}
const PtyCalls = host_io.Ptys.Calls(Shell);
+ const WatchCalls = file_watch.Watches.Calls(Shell);
fn upgradeAsked(ctx: ?*anyopaque, id: pardes.Pane.Id) void {
const pane = id.index();
@@ -1675,21 +1647,7 @@ const Shell = struct {
if (pasted.len > 0) s.feed(.{ .paste = pasted });
s.paste_buf.clearRetainingCapacity();
},
- .files_changed => s.check_files = true,
.fs_ready => {},
- .lsp_done => |d| {
- core.update(.{ .lsp_resp = .{ .id = d.id, .rows = d.rows } });
- if (d.rows) |rows| s.lsp_gpa.free(rows);
- if (s.lsp_task) |*t| if (t.id == d.id) {
- t.future.await(s.io);
- s.lsp_task = null;
- };
- },
- .lsp_status => |text| {
- var mbuf: [256]u8 = undefined;
- core.setStatus(.at(core.view().active), message.stamp(&mbuf, "lsp", text));
- s.lsp_gpa.free(text);
- },
.pipe_done => |response_value| {
var response = response_value;
core.update(.{ .pipe_resp = .{
@@ -1738,8 +1696,7 @@ const Shell = struct {
fn reloadWatched(s: *Shell) void {
if (!s.check_files) return;
s.check_files = false;
- if (file_watch.reloadChanged(s.core, s.io, s.gpa, &s.watches))
- s.loop.postEvent(.files_changed) catch {};
+ s.watches.reload(s.core);
}
/// The tty's own motions (its glide, a jump's trail) are under way.
@@ -1947,6 +1904,8 @@ const Shell = struct {
return;
},
.eof => |e| s.ptys.eof(s.core, e),
+ .files_changed => s.check_files = true,
+ .lsp, .lsp_status => s.ptys.queue.landLsp(s.core, m),
else => m.deinit(s.gpa, s.lsp_gpa),
}
}
@@ -1960,29 +1919,10 @@ const Shell = struct {
}
}
- fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void {
- const pane = id.index();
- const s = of(ctx);
- host_io.writeFile(s.core, &s.watches, pane, path, bytes);
- }
-
fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void {
host_io.writeDump(of(ctx).core, bytes);
}
- fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void {
- const pane = id.index();
- const s = of(ctx);
- if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, &s.watches, pane, on, mode))
- s.loop.postEvent(.files_changed) catch {};
- }
-
- fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void {
- const s = of(ctx);
- if (file_watch.applyThemeEffect(s.core, s.gpa, s.inotify_fd, &s.watches, generation, on))
- s.loop.postEvent(.files_changed) catch {};
- }
-
fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void {
const pane = id.index();
const s = of(ctx);
@@ -2010,16 +1950,9 @@ const Shell = struct {
fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void {
const s = of(ctx);
- if (!s.threads_ok) {
- s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
- return s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
- }
- host_io.Lsp.start(s.io, s.lsp_gpa, s.core, &s.lsp_task, req, s, deliverLspRows);
- }
-
- fn deliverLspRows(ctx: ?*anyopaque, id: u32, rows: ?[]u8) void {
- const s = of(ctx);
- s.loop.postEvent(.{ .lsp_done = .{ .id = id, .rows = rows } }) catch if (rows) |owned| s.lsp_gpa.free(owned);
+ if (s.threads_ok) return s.ptys.queue.startLsp(s.core, req);
+ s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } });
+ s.core.reportError(req.pane, "lsp", error.WorkersUnavailable);
}
fn pipe(ctx: ?*anyopaque, id: u32) void {
@@ -2075,29 +2008,6 @@ const Shell = struct {
}
};
-fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void {
- const s: *Shell = @ptrCast(@alignCast(ctx orelse return));
- const copy = s.lsp_gpa.dupe(u8, text) catch return;
- const posted = s.loop.tryPostEvent(.{ .lsp_status = copy }) catch false;
- if (!posted) s.lsp_gpa.free(copy);
-}
-
-fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) void {
- if (comptime builtin.os.tag != .linux and builtin.os.tag != .freebsd) {
- while (file_watch.wait(fd)) loop.postEvent(.files_changed) catch break;
- return;
- }
- const file: std.Io.File = .{ .handle = fd, .flags = .{ .nonblocking = false } };
- // One buffer, read into directly: a Reader around it only added a second
- // (and in a safe build, filled both as `undefined` on every start).
- var buf: [4096]u8 = undefined;
- while (true) {
- const n = file.readStreaming(io, &.{&buf}) catch break;
- if (n == 0) break;
- loop.postEvent(.files_changed) catch break;
- }
-}
-
fn keepSignal(_: posix.SIG) callconv(.c) void {}
/// A replica client's (`runClient`): a hangup is its end, whoever sent it.
@@ -2775,38 +2685,6 @@ test "on a terminal with no kitty graphics a program's 64x64 image over 8x4 cell
try std.testing.expectEqual(@as(usize, 32), painted);
}
-test "TTY queued results reject an old LSP request ID" {
- const gpa = std.testing.allocator;
- const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 });
- defer core.deinit();
- var no_peers: replica.Peers = .{};
- var shell: Shell = .{
- .io = std.testing.io,
- .gpa = gpa,
- .lsp_gpa = gpa,
- .core = core,
- .frames = core.frames,
- .loop = undefined,
- .frame = undefined,
- .paste_buf = undefined,
- .inotify_fd = -1,
- .peers = &no_peers,
- .ptys = undefined,
- };
- core.lspRequest(0, .status, "");
- const old_id = core.lsp_ask.wait.?.id;
- core.lspRequest(0, .status, "");
- const current_id = core.lsp_ask.wait.?.id;
- shell.lsp_task = .{ .id = current_id, .future = .{ .any_future = null, .result = {} } };
- _ = shell.apply(.{ .lsp_done = .{ .id = old_id, .rows = try gpa.dupe(u8, "stale result\n") } });
- try std.testing.expect(shell.lsp_task != null);
- try std.testing.expectEqual(current_id, shell.lsp_task.?.id);
- try std.testing.expectEqual(current_id, core.lsp_ask.wait.?.id);
- _ = shell.apply(.{ .lsp_done = .{ .id = current_id, .rows = try gpa.dupe(u8, "") } });
- try std.testing.expect(shell.lsp_task == null);
- try std.testing.expect(core.lsp_ask.wait == null);
-}
-
test "TTY worker setup failures finish matching LSP and pipe requests" {
const gpa = std.testing.allocator;
var failing_vtable = std.testing.io.vtable.*;
@@ -2820,6 +2698,9 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
.fail_index = if (failure == 0) 0 else std.math.maxInt(usize),
});
var no_peers: replica.Peers = .{};
+ var queue: host_io.Inbox = .{ .io = failing_io, .gpa = gpa, .lsp_allocator = failing.allocator() };
+ defer queue.deinit();
+ var ptys: host_io.Ptys = .{ .queue = &queue, .prompt_rcs = undefined };
var shell: Shell = .{
.io = failing_io,
.gpa = failing.allocator(),
@@ -2829,10 +2710,10 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
.loop = undefined,
.frame = undefined,
.paste_buf = undefined,
- .inotify_fd = -1,
+ .watches = undefined,
.threads_ok = true,
.peers = &no_peers,
- .ptys = undefined,
+ .ptys = &ptys,
};
core.lspRequest(core.view().active, .status, "");
const req: host_io.Lsp.Request = .{
@@ -2855,7 +2736,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" {
Shell.pipe(&shell, pipe_id);
try std.testing.expect(core.pipe.wait == null);
try std.testing.expectEqualStrings("abc", pane.file.?.content);
- try std.testing.expect(shell.lsp_task == null);
+ try std.testing.expect(queue.lsp_task == null);
try std.testing.expectEqual(@as(usize, 0), shell.pipe_tasks.len);
}
}