summaryrefslogtreecommitdiff
path: root/src/pardes.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/pardes.zig')
-rw-r--r--src/pardes.zig244
1 files changed, 73 insertions, 171 deletions
diff --git a/src/pardes.zig b/src/pardes.zig
index c2a3ba21..80ff3495 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -188,40 +188,20 @@ pub const Turn = struct {
t.release();
}
- /// The editor performed the core's effects.
- pub fn settle(t: *Turn) void {
+ /// One of `epoch`, `restores` or `lsp_answers` moved on: the editor
+ /// performed the core's effects, a shell attempted a Restore (at the
+ /// top of each loop step, when the previous step's attempt is over),
+ /// or a language server's answer was taken in.
+ pub fn bump(t: *Turn, counter: *u64) void {
const io = t.io orelse return;
- t.epoch +%= 1;
+ counter.* +%= 1;
t.settled.broadcast(io);
}
- /// With the turn: waits, turn given up meanwhile, until the editor has
- /// performed past `epoch`; stopping ends the wait early.
- pub fn awaitSettled(t: *Turn, epoch: u64) void {
- while (t.epoch == epoch) t.settled.wait(t.io.?, &t.mutex) catch return;
- }
-
- /// The shell attempted the Restore the core asked for, whichever way it
- /// went; called at the top of each loop step, when the previous step's
- /// attempt is over.
- pub fn restoreSettled(t: *Turn) void {
- const io = t.io orelse return;
- t.restores +%= 1;
- t.settled.broadcast(io);
- }
-
- pub fn lspAnswered(t: *Turn) void {
- const io = t.io orelse return;
- t.lsp_answers +%= 1;
- t.settled.broadcast(io);
- }
-
- pub fn awaitLsp(t: *Turn, answers: u64) void {
- while (t.lsp_answers == answers) t.settled.wait(t.io.?, &t.mutex) catch return;
- }
-
- pub fn awaitRestored(t: *Turn, restores: u64) void {
- while (t.restores == restores) t.settled.wait(t.io.?, &t.mutex) catch return;
+ /// With the turn: waits, turn given up meanwhile, until `counter` has
+ /// moved past `seen`; stopping ends the wait early.
+ pub fn awaitPast(t: *Turn, counter: *const u64, seen: u64) void {
+ while (counter.* == seen) t.settled.wait(t.io.?, &t.mutex) catch return;
}
fn release(t: *Turn) void {
@@ -520,18 +500,6 @@ pub const test_api = if (@import("builtin").is_test) struct {
exec.runBuiltin(p, command, id, txt, arg);
return true;
}
-
- pub fn sync(p: *Pardes) void {
- p.sync();
- }
-
- pub fn searchStep(p: *Pardes, id: usize, delta: i32) bool {
- return look.searchStep(p, id, delta);
- }
-
- pub fn heldSelection(p: *Pardes, first: usize) ?[]const u8 {
- return exec.heldSelection(p, first);
- }
} else struct {};
fn drainForSavePath(p: *Pardes, buf: []u8) ?[]const u8 {
@@ -2982,89 +2950,36 @@ fn chordName(comptime c: config.Chord) []const u8 {
}
}
-fn shortcutCount(comptime b: Builtin) comptime_int {
- var count = 0;
- if (b == .Window) count += 1;
- for (config.jump_keys) |jk| if (jk.cmd == b) {
- count += 1;
- };
- if (config.look_cmd == b) count += config.look_key.len + 1;
- if (config.exec_cmd == b) count += config.exec_key.len + 1;
- var it = std.mem.tokenizeScalar(u8, config.topbar_str, ' ');
- while (it.next()) |w| if (std.mem.eql(u8, w, @tagName(b))) {
- count += 1;
- };
- return count;
-}
-
fn shortcuts(comptime b: Builtin) []const u8 {
comptime {
@setEvalBranchQuota(20000);
- var parts: [shortcutCount(b)][]const u8 = undefined;
- var part: usize = 0;
- if (b == .Window) {
- parts[part] = chordName(config.window_prefix[0]);
- part += 1;
- }
- for (config.jump_keys) |jk| {
- if (jk.cmd == b) {
- parts[part] = chordName(jk.chord);
- part += 1;
- }
- }
+ // each part after ", "; the first one's is cut off at the end
+ var s: []const u8 = "";
+ if (b == .Window) s = s ++ ", " ++ chordName(config.window_prefix[0]);
+ for (config.jump_keys) |jk| if (jk.cmd == b) {
+ s = s ++ ", " ++ chordName(jk.chord);
+ };
if (config.look_cmd == b) {
- for (config.look_key) |k| {
- parts[part] = chordName(k);
- part += 1;
- }
- parts[part] = @tagName(config.look_button) ++ "-click";
- part += 1;
+ for (config.look_key) |k| s = s ++ ", " ++ chordName(k);
+ s = s ++ ", " ++ @tagName(config.look_button) ++ "-click";
}
if (config.exec_cmd == b) {
- for (config.exec_key) |k| {
- parts[part] = chordName(k);
- part += 1;
- }
- parts[part] = @tagName(config.exec_button) ++ "-click";
- part += 1;
+ for (config.exec_key) |k| s = s ++ ", " ++ chordName(k);
+ s = s ++ ", " ++ @tagName(config.exec_button) ++ "-click";
}
var it = std.mem.tokenizeScalar(u8, config.topbar_str, ' ');
- while (it.next()) |w| {
- if (std.mem.eql(u8, w, @tagName(b))) {
- parts[part] = "topbar";
- part += 1;
- }
- }
- var len: usize = if (parts.len == 0) 0 else 2 * (parts.len - 1);
- for (parts) |text| len += text.len;
- var out: [len]u8 = undefined;
- var at: usize = 0;
- for (parts, 0..) |text, i| {
- if (i > 0) {
- @memcpy(out[at..][0..2], ", ");
- at += 2;
- }
- @memcpy(out[at..][0..text.len], text);
- at += text.len;
- }
- return &out;
+ while (it.next()) |w| if (std.mem.eql(u8, w, @tagName(b))) {
+ s = s ++ ", topbar";
+ };
+ return if (s.len == 0) s else s[2..];
}
}
fn leaderKeys(comptime path: []const u8) []const u8 {
comptime {
- var out: [4 + 2 * path.len - 1]u8 = undefined;
- @memcpy(out[0..4], "SPC ");
- var at: usize = 4;
- for (path, 0..) |c, i| {
- if (i > 0) {
- out[at] = ' ';
- at += 1;
- }
- out[at] = c;
- at += 1;
- }
- return &out;
+ var s: []const u8 = "SPC";
+ for (path) |c| s = s ++ " " ++ [_]u8{c};
+ return s;
}
}
@@ -5658,6 +5573,21 @@ pub const Pardes = struct {
if (!p.fallback.writeFile(path, bytes)) p.saveFailed(pane, path, error.OutOfMemory);
}
+ /// Writes `bytes` to `path` for pane `id`, its directories made first:
+ /// the pane after it, if it is still the one `serial` names and the
+ /// write did not fail. A failed write changes nothing: its file goes
+ /// back to the saved revision `was`, dirty as it was.
+ fn writeChecked(p: *Pardes, id: u8, serial: u32, path: []const u8, bytes: []const u8, was: u32) ?*Pane {
+ const failures = p.fs.failures;
+ makeParents(path);
+ p.hostWriteFile(id, path, bytes);
+ const pane = p.panes[id] orelse return null;
+ if (pane.serial != serial) return null;
+ if (p.fs.failures == failures) return pane;
+ if (pane.file) |*f| f.saved_revision = was;
+ return null;
+ }
+
/// A write of `path` the host could not do: the pane stays dirty, the
/// message row says `Save <path>: <why>`, and the 9P write that asked
/// for it, waiting on the save, fails with that (`fs.late_failure`).
@@ -5740,6 +5670,18 @@ pub const Pardes = struct {
filesystem.makeDirs(std.fs.path.dirname(local) orelse "/");
}
+ /// The directory a write of `path` that failed with `err` went into:
+ /// what is there (a directory when it is no local path), whether it is
+ /// missing, and whether that is because one above refuses (EACCES).
+ fn parentTrouble(path: []const u8, err: anyerror) struct { dir: []const u8, kind: ?std.Io.File.Kind, missing: bool, denied: bool } {
+ const dir = std.fs.path.dirname(path) orelse "/";
+ const local = if (comptime hosted) filesystem.localPath(dir) else null;
+ const kind: ?std.Io.File.Kind = if (comptime hosted) (if (local) |l| exec.kindOf(l) else .directory) else .directory;
+ const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory;
+ const denied = missing and (if (comptime hosted) (if (local) |l| filesystem.deniedAbove(l) else false) else false);
+ return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied };
+ }
+
pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void {
if (p.panes[id]) |pane| if (pane.file) |*f| {
// The `-%` spelling fs.zig already uses for "make this dirty".
@@ -5747,15 +5689,11 @@ pub const Pardes = struct {
};
var what: [limits.host_path_cap + 32]u8 = undefined;
// A write into a directory that is not there: said so, in the words
- // a mount reads as ENOENT.
- const parent = std.fs.path.dirname(path) orelse "/";
- const no_dir = err == error.FileNotFound or err == error.NotDir or
- (if (comptime hosted) (if (filesystem.localPath(parent)) |local| !exec.isDirectory(local) else false) else false);
- // Not there because one above refuses: that is the reason, EACCES.
- const denied = no_dir and (if (comptime hosted) (if (filesystem.localPath(parent)) |local| filesystem.deniedAbove(local) else false) else false);
- if (denied)
+ // a mount reads as ENOENT; EACCES when one above refuses.
+ const parent = parentTrouble(path, err);
+ if (parent.denied)
p.reportFailure(id, std.fmt.bufPrint(&what, "Save {s}: permission denied", .{path}) catch "Save: permission denied")
- else if (no_dir)
+ else if (parent.missing)
p.reportFailure(id, std.fmt.bufPrint(&what, "Save {s}: no such directory", .{path}) catch "Save: no such directory")
else
p.reportError(id, std.fmt.bufPrint(&what, "Save {s}", .{path}) catch "Save", err);
@@ -5770,18 +5708,14 @@ pub const Pardes = struct {
const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path}) catch "Dump";
// Into a DumpDir that is not there: said so, ENOENT, as a Save is;
// one that is there but no directory (`/dev/null`) says that.
- const parent = std.fs.path.dirname(path) orelse "/";
- const kind: ?std.Io.File.Kind = if (comptime hosted) (if (filesystem.localPath(parent)) |local| exec.kindOf(local) else .directory) else .directory;
- const no_dir = err == error.FileNotFound or err == error.NotDir or kind != .directory;
- // Not there because one above refuses: that is why, EACCES.
- const denied = no_dir and kind == null and (if (comptime hosted) (if (filesystem.localPath(parent)) |local| filesystem.deniedAbove(local) else false) else false);
- if (denied) {
+ const parent = parentTrouble(path, err);
+ if (parent.denied and parent.kind == null) {
var said: [limits.host_path_cap + 48]u8 = undefined;
p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: permission denied", .{operation}) catch "Dump: permission denied");
- } else if (kind != null and kind != .directory) {
+ } else if (parent.kind != null and parent.kind != .directory) {
var said: [limits.host_path_cap + 48]u8 = undefined;
- p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: {s} is not a directory", .{ operation, parent }) catch "Dump: not a directory");
- } else if (no_dir) {
+ p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: {s} is not a directory", .{ operation, parent.dir }) catch "Dump: not a directory");
+ } else if (parent.missing) {
var said: [limits.host_path_cap + 48]u8 = undefined;
p.reportFailure(p.active, std.fmt.bufPrint(&said, "{s}: no such directory", .{operation}) catch "Dump: no such directory");
} else p.reportError(p.active, operation, err);
@@ -5836,22 +5770,13 @@ pub const Pardes = struct {
.save_file => |sf| {
const pane = p.panes[sf.pane] orelse return;
const f = if (pane.file) |*file| file else return;
- const serial = pane.serial;
const was = f.saved_revision;
- const failures = p.fs.failures;
f.saved_revision = f.revision;
- makeParents(f.path);
- p.hostWriteFile(sf.pane, f.path, f.content);
- const saved_pane = p.panes[sf.pane] orelse return;
- if (saved_pane.serial != serial) return;
- const saved = if (saved_pane.file) |*file| file else return;
// A save the host could not do (saveFailed) is its err
// record alone, not a `save`, and changes nothing: a clean
// file stays clean.
- if (p.fs.failures != failures) {
- saved.saved_revision = was;
- return;
- }
+ const saved_pane = p.writeChecked(sf.pane, pane.serial, f.path, f.content, was) orelse return;
+ const saved = if (saved_pane.file) |*file| file else return;
// Edited while it was written: what is on disk is not this.
if (saved.saved_revision != saved.revision) return;
saved.disk_gone = false;
@@ -5867,28 +5792,16 @@ pub const Pardes = struct {
const pane = p.panes[st.pane] orelse return;
if (pane.serial != st.serial) return; // a recycled slot: not ours
if (pane.file) |*f| {
- const was = f.saved_revision;
- const failures = p.fs.failures;
- makeParents(st.path.slice());
- p.hostWriteFile(st.pane, st.path.slice(), f.content);
- const after = p.panes[st.pane] orelse return;
- if (after.serial != st.serial) return;
- const g = if (after.file) |*file| file else return;
// Failed, it changes nothing: not the name, not dirty.
- if (p.fs.failures != failures) {
- g.saved_revision = was;
- return;
- }
+ const after = p.writeChecked(st.pane, st.serial, st.path.slice(), f.content, f.saved_revision) orelse return;
+ if (after.file == null) return;
if (st.promote) p.promoteSaved(st.pane, st.path.slice()) else ctlfs.events.noteLogAs(p, .save, after, st.path.slice());
return;
}
if (!pane.isTerminal()) return;
const text = panes.terminal.screenTextAlloc(pane, p.gpa) catch return;
defer p.gpa.free(text);
- makeParents(st.path.slice());
- const failures = p.fs.failures;
- p.hostWriteFile(st.pane, st.path.slice(), text);
- if (p.fs.failures == failures) if (p.panes[st.pane]) |still| if (still.serial == st.serial)
+ if (p.writeChecked(st.pane, st.serial, st.path.slice(), text, 0)) |still|
ctlfs.events.noteLogAs(p, .save, still, st.path.slice());
},
.write_dump => {
@@ -5929,17 +5842,9 @@ pub const Pardes = struct {
}
for (writes.items) |w| {
const was = if (p.panes[w.pane]) |pane| (if (pane.file) |f| f.saved_revision else 0) else 0;
- const failures = p.fs.failures;
- makeParents(w.path);
- p.hostWriteFile(w.pane, w.path, w.bytes);
- const pane = p.panes[w.pane] orelse continue;
- if (pane.serial != w.serial) continue;
- const f = if (pane.file) |*file| file else continue;
// Failed, it changes nothing, as save_text's does not.
- if (p.fs.failures != failures) {
- f.saved_revision = was;
- continue;
- }
+ const pane = p.writeChecked(w.pane, w.serial, w.path, w.bytes, was) orelse continue;
+ const f = if (pane.file) |*file| file else continue;
if (w.promote) {
p.promoteSaved(w.pane, w.path);
} else if (w.clean != null and w.clean.? == f.revision) {
@@ -5994,7 +5899,7 @@ pub const Pardes = struct {
ctlfs.runClosedLines(p);
while (p.nextQueued()) |ev| p.update(ev);
while (p.nextEffect()) |e| p.perform(e);
- turn.settle();
+ turn.bump(&turn.epoch);
// A quitting frame has already freed what it would draw.
if (p.quit) return;
if (v.poll_frame) |f| f(h.ctx);
@@ -6234,10 +6139,7 @@ pub const Pardes = struct {
// ---- tag + selection text (chord sources) ----
pub fn paneIdOf(p: *const Pardes, pane: *const Pane) ?usize {
- for (p.panes, 0..) |slot, id| if (slot) |sp| {
- if (sp == pane) return id;
- };
- return null;
+ return std.mem.findScalar(?*const Pane, &p.panes, pane);
}
/// Cycle only the body modes supported by this pane.
@@ -7036,7 +6938,7 @@ pub const Pardes = struct {
}
pub fn lspResponse(p: *Pardes, id: u32, response: ?[]const u8) void {
- defer turn.lspAnswered();
+ defer turn.bump(&turn.lsp_answers);
const w = p.lsp_wait orelse return;
if (w.id != id) return; // superseded by a newer press, or the pane died
p.lsp_wait = null;