diff options
Diffstat (limited to 'core/Messages.zig')
| -rw-r--r-- | core/Messages.zig | 82 |
1 files changed, 79 insertions, 3 deletions
diff --git a/core/Messages.zig b/core/Messages.zig index 5a36e166..e05961b5 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -97,6 +97,10 @@ pub const Place = struct { line: u32 = 1, col: u32 = 1, text: Buf(128) = .{}, + /// Its file's text changed since it was named (`edited`): its line may + /// be another's now, so its words are not drawn at it (a mark on the + /// wrong line is worse than none) until a run names it again. + stale: bool = false, }; /// The questions the session asks in the background, and their answers. @@ -489,6 +493,16 @@ fn placeOf(row: []const u8) ?struct { path: []const u8, line: u32, col: u32, tex return null; } +/// An edit of the text of the file at `path` (File.edit): the places that +/// name it no longer know their lines (`Place.stale`); the box's mark and +/// the notice row keep them until the next run. Bounded by the live +/// messages' places. +pub fn edited(m: *Messages, path: []const u8) void { + for (m.live[0..m.live_len]) |*s| for (s.places[0..s.places_len]) |*place| { + if (std.mem.eql(u8, place.path.slice(), path)) place.stale = true; + }; +} + /// The question waiting on `pane`, an index into `live`; null for none. pub fn waiting(m: *const Messages, pane: *const Pane) ?usize { for (m.live[0..m.live_len], 0..) |*s, i| if (s.kind == .question and s.pane == pane.serial) return i; @@ -1099,11 +1113,12 @@ pub fn detailUnder(p: *const Pardes, pane: *Pane, active: bool, body_h: u16) ?st return null; } -/// The first live message naming line `line` (1-based) of `path`: the one -/// whose words are drawn after that line (body_layer messageGhosts). +/// The first live message naming line `line` (1-based) of `path`, its +/// place not stale: the one whose words are drawn after that line +/// (body_layer messageGhosts). fn firstAt(m: *const Messages, path: []const u8, line: u32) ?usize { for (m.live[0..m.live_len], 0..) |*s, i| for (s.places[0..s.places_len]) |*place| { - if (line > 0 and place.line == line and std.mem.eql(u8, place.path.slice(), path)) return i; + if (line > 0 and place.line == line and !place.stale and std.mem.eql(u8, place.path.slice(), path)) return i; }; return null; } @@ -1910,3 +1925,64 @@ test "SPC e lands on its place's column, where jumps walk and where they open a try std.testing.expectEqual(@as(i32, 8), landed.body.cur_col); } } + +test "a file changed since the run that named its places draws none of their words at its lines (UX DESIGN H): a line put in above draws them at neither line, the box's mark and the notice row kept; the next run's error draws at its new line, and an edit on that line quiets it at once" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile("const std = @import(\"std\");\n\npub fn print() void {\n var s = 1;\n _ = s;\n}\n"); + const id = p.view().active; + p.messages.live_len = 0; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + var buf: [512]u8 = undefined; + // The row its words are drawn on, null for none. + const Shown = struct { + fn row(pp: *Pardes, arena: *std.heap.ArenaAllocator, out: *[512]u8) !?[]const u8 { + _ = arena.reset(.retain_capacity); + pp.sync(); + const s = try pp.render(arena.allocator()); + for (0..s.rows) |y| { + var n: usize = 0; + for (0..s.cols) |x| { + const g = s.cells[y * s.cols + x].grapheme(); + if (n + g.len > out.len) break; + @memcpy(out[n..][0..g.len], g); + n += g.len; + } + if (std.mem.indexOf(u8, out[0..n], "\u{2717} local variable is never mutated") != null) return out[0..n]; + } + return null; + } + }; + for (0..2) |run| { + // A run names line 4 (the first) or 5 (the next, after the line + // put in above): its pane's next run takes the last one's error + // away (exec.zig) before it says its own. + _ = p.messages.retire(&.{.@"error"}); + const err = keep(p, .@"error", 0, "@p2 zig build", "exit 2"); + err.places[0] = .{ .line = @intCast(4 + run), .col = 9 }; + err.places[0].path.set("/test.txt"); + err.places[0].text.set("local variable is never mutated"); + err.places_len = 1; + const drawn = try Shown.row(p, &frame, &buf) orelse return error.WordsNotDrawn; + try std.testing.expect(std.mem.indexOf(u8, drawn, "var s = 1; \u{2717}") != null); + if (run == 1) break; + // A language server's edit puts a line in above it (File.load, a + // code action's way): the words are at neither its line nor the + // line it went to. + const f = &pane.file.?; + panes.File.load(p, f, try std.mem.concat(p.gpa, u8, &.{ "const std = @import(\"std\");\nconst Counts = u32;\n", f.content["const std = @import(\"std\");\n".len..] })); + try std.testing.expect(try Shown.row(p, &frame, &buf) == null); + // Its box's mark and the notice row's place stay until the run. + const s = try p.render(frame.allocator()); + const r = p.view().rects[id]; + try std.testing.expectEqualStrings("\u{2717}", s.at(r.x + config.GUTTER - 1, p.tagTop(pane, r)).grapheme()); + var row_buf: [notice_row_cap]u8 = undefined; + try std.testing.expect(std.mem.indexOf(u8, noticeRow(p, 97, &row_buf).text, "/test.txt:4:9") != null); + } + // An edit on the line, the fix: quiet at once. + const f = &pane.file.?; + const var_at = std.mem.indexOf(u8, f.content, "var s") orelse return error.NoLine; + try std.testing.expect(panes.File.replace(p, f, var_at, "var".len, "const")); + try std.testing.expect(try Shown.row(p, &frame, &buf) == null); +} |
