diff options
Diffstat (limited to 'core/Messages.zig')
| -rw-r--r-- | core/Messages.zig | 200 |
1 files changed, 170 insertions, 30 deletions
diff --git a/core/Messages.zig b/core/Messages.zig index b4950b4f..282482ea 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -262,30 +262,49 @@ pub const Message = struct { /// and is not logged; a message is an event. pub const setStatus = pardes.tape.taped(setStatusInner); fn setStatusInner(p: *Pardes, id: Pane.Id, text: []const u8) void { - say(p, .progress, id.index(), text); + // No text: the pane's progress is over, and goes. + if (text.len == 0) { + const serial: u32 = if (id.index() < MAX_PANES) if (p.panes[id.index()]) |pane| pane.serial else 0 else 0; + const m = &p.messages; + for (m.live[0..m.live_len], 0..) |*s, i| if (s.kind == .progress and s.pane == serial) { + m.remove(i); + p.needs_frame = true; + break; + }; + return; + } + _ = say(p, .progress, id.index(), text); } pub const setMessage = pardes.tape.taped(setMessageInner); fn setMessageInner(p: *Pardes, id: Pane.Id, text: []const u8) void { - say(p, .notice, id.index(), text); + _ = say(p, .notice, id.index(), text); } /// Say `text`, a message of `kind` about pane `id` (MAX_PANES and up: /// none in particular), its cause first: logged (progress is not), and -/// live (`keep`). -pub fn say(p: *Pardes, kind: Kind, id: usize, text: []const u8) void { +/// live (`keep`); the message, for the places it names. +pub fn say(p: *Pardes, kind: Kind, id: usize, text: []const u8) *Said { if (kind != .progress) logMessage(p, id, kind, text); const pane: ?*Pane = if (id < MAX_PANES and !p.fs.session_write) p.panes[id] else null; var src: Buf(96) = .{}; - if (pane) |cause| { - if (cause.file) |f| src.set(f.path) else if (if (cause.addon) |k| k.path.len > 0 else false) src.set(cause.addon.?.path) else if (cause.command) |line| { - const first = line[0 .. std.mem.indexOfAny(u8, line, "\r\n") orelse line.len]; - var cut = @min(first.len, 64); - while (cut < first.len and cut > 0 and first[cut] & 0xC0 == 0x80) cut -= 1; - _ = src.print(config.pane_addr ++ "{d} {s}", .{ cause.serial, first[0..cut] }); - } else _ = src.print(config.pane_addr ++ "{d}", .{cause.serial}); - } - _ = keep(p, kind, if (pane) |cause| cause.serial else 0, src.slice(), text); + if (pane) |cause| causeWord(cause, &src); + return keep(p, kind, if (pane) |cause| cause.serial else 0, src.slice(), text); +} + +/// A pane as a message names its cause: a file's path (a kind's too), else +/// `@pN` and the command it runs. +fn causeWord(pane: *const Pane, out: *Buf(96)) void { + if (pane.file) |f| return out.set(f.path); + if (pane.addon) |k| if (k.path.len > 0) return out.set(k.path); + const line = pane.command orelse { + _ = out.print(config.pane_addr ++ "{d}", .{pane.serial}); + return; + }; + const first = line[0 .. std.mem.indexOfAny(u8, line, "\r\n") orelse line.len]; + var cut = @min(first.len, 64); + while (cut < first.len and cut > 0 and first[cut] & 0xC0 == 0x80) cut -= 1; + _ = out.print(config.pane_addr ++ "{d} {s}", .{ pane.serial, first[0..cut] }); } /// Make a message live, and return it (for its places and its detail): @@ -349,7 +368,7 @@ pub fn keep(p: *Pardes, kind: Kind, serial: u32, src: []const u8, text: []const return s; } -fn remove(m: *Messages, i: usize) void { +pub fn remove(m: *Messages, i: usize) void { std.mem.copyForwards(Said, m.live[i .. m.live_len - 1], m.live[i + 1 .. m.live_len]); m.live_len -= 1; } @@ -379,6 +398,80 @@ pub fn headline(m: *const Messages) ?*const Said { return best; } +/// The kind a pane's mark shows, one cell after its box (SPEC ยง10.2): the +/// most important live error, question, warning or progress about it (an +/// echo or a notice marks nothing); null for none. +pub fn markOf(m: *const Messages, pane: *const Pane) ?Kind { + var best: ?Kind = null; + for (m.live[0..m.live_len]) |*s| { + if (s.kind == .echo or s.kind == .notice or !s.about(pane)) continue; + if (best == null or s.kind.rank() > best.?.rank()) best = s.kind; + } + return best; +} + +/// A kind's ink, for its glyph, its mark and its words at a line. +pub fn ink(chrome: *const pardes.colors.ChromeTheme, kind: Kind) [3]u8 { + return switch (kind) { + .@"error" => chrome.diagnostic_error, + .warn => chrome.diagnostic_warning, + .question => chrome.diagnostic_info, + else => chrome.diagnostic_hint, + }; +} + +/// The places a compiler's report names put on `s`: each +/// `path:line:col: error|warning|note: words` row (a relative path is +/// `dir`'s), its words there, and the rows after the first error, up to +/// the next place, its detail. How many it found; none without an error. +pub fn readPlaces(s: *Said, dir: []const u8, rows: []const []const u8) usize { + var errors: usize = 0; + var detailing = false; + for (rows) |raw| { + const row = std.mem.trimEnd(u8, raw, " \t\r\n"); + const found = placeOf(row) orelse { + if (detailing and row.len > 0 and s.detail_len < Said.max_detail) { + s.detail[s.detail_len] = .{}; + s.detail[s.detail_len].set(row); + s.detail_len += 1; + } + continue; + }; + detailing = false; + const is_error = std.mem.startsWith(u8, found.text, "error"); + if (is_error) { + detailing = errors == 0; + errors += 1; + } + if (s.places_len == Said.max_places) continue; + const place = &s.places[s.places_len]; + place.* = .{ .line = found.line, .col = found.col }; + if (std.Io.Dir.path.isAbsolute(found.path)) place.path.set(found.path) else _ = place.path.print("{s}/{s}", .{ std.mem.trimEnd(u8, dir, "/"), found.path }); + place.text.set(if (std.mem.startsWith(u8, found.text, "error: ")) found.text["error: ".len..] else found.text); + s.places_len += 1; + } + if (errors == 0) s.places_len = 0; + return s.places_len; +} + +/// A compiler's place row: `path:line:col: error|warning|note...`. +fn placeOf(row: []const u8) ?struct { path: []const u8, line: u32, col: u32, text: []const u8 } { + const a = std.mem.indexOfScalar(u8, row, ':') orelse return null; + const path = row[0..a]; + if (path.len == 0 or std.mem.indexOfAny(u8, path, " \t") != null) return null; + var rest = row[a + 1 ..]; + const b = std.mem.indexOfScalar(u8, rest, ':') orelse return null; + const line = std.fmt.parseInt(u32, rest[0..b], 10) catch return null; + rest = rest[b + 1 ..]; + const c = std.mem.indexOfScalar(u8, rest, ':') orelse return null; + const col = std.fmt.parseInt(u32, rest[0..c], 10) catch return null; + const text = std.mem.trim(u8, rest[c + 1 ..], " "); + for ([_][]const u8{ "error", "warning", "note" }) |word| { + if (std.mem.startsWith(u8, text, word)) return .{ .path = path, .line = line, .col = col, .text = text }; + } + return null; +} + /// The words the newest live message said of `pane` has, "" for none: /// what its band shows, and what a write that failed on it answers. pub fn saidBy(m: *const Messages, pane: *const Pane) []const u8 { @@ -641,9 +734,10 @@ pub fn reportFailure(p: *Pardes, id: usize, text: []const u8) void { if (failing_write) { p.fs.unlogged = true; defer p.fs.unlogged = false; - return say(p, .@"error", id, text); + _ = say(p, .@"error", id, text); + return; } - say(p, .@"error", id, text); + _ = say(p, .@"error", id, text); } /// The one place that decides which transient lines a pane is showing and @@ -799,26 +893,26 @@ test "an echo or a notice retires those before it, an error the error before it, const id = p.view().active; const m = &p.messages; m.live_len = 0; - say(p, .notice, id, "first"); - say(p, .echo, id, "second"); + _ = say(p, .notice, id, "first"); + _ = say(p, .echo, id, "second"); try std.testing.expectEqual(@as(u8, 1), m.live_len); try std.testing.expectEqualStrings("second", m.headline().?.text.slice()); - say(p, .@"error", id, "broke"); - say(p, .warn, id, "careful"); + _ = say(p, .@"error", id, "broke"); + _ = say(p, .warn, id, "careful"); // The echo went with the error; the warning stands beside it. try std.testing.expectEqual(@as(u8, 2), m.live_len); try std.testing.expectEqual(Kind.@"error", m.headline().?.kind); - say(p, .@"error", id, "broke again"); + _ = say(p, .@"error", id, "broke again"); try std.testing.expectEqual(@as(u8, 2), m.live_len); try std.testing.expectEqualStrings("broke again", m.headline().?.text.slice()); // The same again right after itself: one message, counted, its newest // wording (and clock) kept. - for (0..9) |_| say(p, .@"error", id, "broke again"); + for (0..9) |_| _ = say(p, .@"error", id, "broke again"); try std.testing.expectEqual(@as(u8, 2), m.live_len); try std.testing.expectEqual(@as(u16, 10), m.headline().?.n); // Progress updates its line in place and retires nothing. - say(p, .progress, id, "Changes: jj 1/4"); - say(p, .progress, id, "Changes: jj 3/4"); + _ = say(p, .progress, id, "Changes: jj 1/4"); + _ = say(p, .progress, id, "Changes: jj 3/4"); try std.testing.expectEqual(@as(u8, 3), m.live_len); try std.testing.expectEqualStrings("Changes: jj 3/4", m.saidBy(p.panes[id].?)); try std.testing.expectEqual(Kind.@"error", m.headline().?.kind); @@ -831,7 +925,7 @@ test "an echo or a notice retires those before it, an error the error before it, m.live_len = 0; for (0..max_live + 3) |i| { var buf: [16]u8 = undefined; - say(p, .warn, id, std.fmt.bufPrint(&buf, "w{d}", .{i}) catch unreachable); + _ = say(p, .warn, id, std.fmt.bufPrint(&buf, "w{d}", .{i}) catch unreachable); } try std.testing.expectEqual(@as(u8, max_live), m.live_len); try std.testing.expectEqualStrings("w3", m.live[0].text.slice()); @@ -899,14 +993,14 @@ test "the notice row leads with the most important live message, its count and p try std.testing.expectEqual(@as(usize, 60), panes.File.displayWidth(p.settings.tab, row.text)); // A notice, then an error and a warning: the error leads, newest first // within a rank; the others are counted. - say(p, .notice, id, "indexed"); - say(p, .warn, id, "careful"); - say(p, .@"error", id, "broke"); + _ = say(p, .notice, id, "indexed"); + _ = say(p, .warn, id, "careful"); + _ = say(p, .@"error", id, "broke"); row = noticeRow(p, 60, &buf); try std.testing.expectEqualStrings("\u{2717} /test.txt broke", row.text[0..row.left]); try std.testing.expect(std.mem.endsWith(u8, row.text, "+1 \u{b7} 3 messages \u{b7} SPC h m")); // A burst of the same: one message, counted; the row keeps its width. - for (0..9) |_| say(p, .@"error", id, "broke"); + for (0..9) |_| _ = say(p, .@"error", id, "broke"); row = noticeRow(p, 60, &buf); try std.testing.expectEqualStrings("\u{2717} /test.txt broke \u{d7}10", row.text[0..row.left]); try std.testing.expectEqual(@as(usize, 60), panes.File.displayWidth(p.settings.tab, row.text)); @@ -950,6 +1044,52 @@ test "an idle core wants no wake, and a message wakes it once, when it dims" { try std.testing.expectEqual(@as(?u64, null), p.nextWake()); // A warning is bright twice as long; with no linger nothing wakes. try std.testing.expect(p.executeBuiltinLine(p.view().active, "MessageLinger 0")); - say(p, .warn, p.view().active, "careful"); + _ = say(p, .warn, p.view().active, "careful"); try std.testing.expectEqual(@as(?u64, null), p.nextWake()); } + +test "a compiler's report names its places: each place's words, the first error's rows after it its detail, a relative path the run's directory's" { + var s: Said = .{}; + const rows = [_][]const u8{ + "install", + "+- compile exe pardes Debug native 1 errors", + "core/pick.zig:25:12: error: expected 2 argument(s), found 3", + " const best = rank(q, name, 3) orelse continue;", + " ^~~~", + "core/look.zig:14:5: note: function declared here", + "pub fn rank(q: []const u8, name: []const u8) ?u32 {", + "error: the following command failed with 1 compilation errors:", + }; + try std.testing.expectEqual(@as(usize, 2), readPlaces(&s, "/src/pardes/", &rows)); + try std.testing.expectEqualStrings("/src/pardes/core/pick.zig", s.places[0].path.slice()); + try std.testing.expectEqual(@as(u32, 25), s.places[0].line); + try std.testing.expectEqual(@as(u32, 12), s.places[0].col); + try std.testing.expectEqualStrings("expected 2 argument(s), found 3", s.places[0].text.slice()); + try std.testing.expectEqualStrings("note: function declared here", s.places[1].text.slice()); + try std.testing.expectEqual(@as(u8, 2), s.detail_len); + try std.testing.expectEqualStrings(" const best = rank(q, name, 3) orelse continue;", s.detail[0].slice()); + try std.testing.expectEqualStrings(" ^~~~", s.detail[1].slice()); + // No error among them: no places. + var none: Said = .{}; + try std.testing.expectEqual(@as(usize, 0), readPlaces(&none, "/", &.{ "x.zig:1:1: note: only a note", "grep: nothing" })); +} + +test "a burst of the same message is one message, counted, and moves nothing on the screen" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20 }); + defer p.deinit(); + const pane = try p.setTestFile("text\n"); + const id = p.view().active; + _ = say(p, .@"error", id, "workspace/didChangeWatchedFiles: 3 files"); + p.sync(); + const rects = p.view().rects; + const tag_rows = pane.tag_rows; + for (0..9) |_| _ = say(p, .@"error", id, "workspace/didChangeWatchedFiles: 3 files"); + p.sync(); + try std.testing.expectEqual(@as(u8, 1), p.messages.live_len); + try std.testing.expectEqual(@as(u16, 10), p.messages.headline().?.n); + try std.testing.expectEqual(rects, p.view().rects); + try std.testing.expectEqual(tag_rows, pane.tag_rows); + var buf: [notice_row_cap]u8 = undefined; + const row = noticeRow(p, 78, &buf); + try std.testing.expect(std.mem.endsWith(u8, row.text[0..row.left], "3 files \u{d7}10")); +} |
