authorgravatar for me@paperdave.netdave caruso <me@paperdave.net> 2024-07-23 14:42:29-07:00
committergravatar for me@paperdave.netdave caruso <me@paperdave.net> 2024-07-23 14:42:29-07:00
logd28747e59ec8a41c28a8dde92b0fdf533df836cb
tree83f38b2fa1fbe21746dc19683e023c38222d4b93
parentd75fca14692f14f7bdc5720aa5d86d682ac2f12d
signature Commit is signed but in an unrecognized format.

report formatting


2 files changed, 36 insertions(+), 10 deletions(-)

CHANGELOG.md+2
......@@ -2,6 +2,8 @@
22
33- feat: clarify "defined runtime representation" and "can be represented at runtime"
44- fix(sema): fix passing structs as function params
5- fix: diagnostic box drawing characters line up after line 9
6- fix: diagnostics on last line of file print correctly
57
68## 0.1.4
79
src/core/Report.zig+34-10
......@@ -610,8 +610,8 @@ const Prefix = enum {
610610
611611 fn symbol(prefix: Prefix, ascii: bool) []const u8 {
612612 return switch (prefix) {
613 .@"error" => if (ascii) " X " else " ✘ ",
614 .warn => if (ascii) " ! " else " ▲ ",
613 .@"error" => if (ascii) "X" else "✘",
614 .warn => if (ascii) "!" else "▲",
615615 .note => "",
616616 };
617617 }
......@@ -676,6 +676,17 @@ pub fn formatItem(
676676
677677 assert(ctx.components.items.len > 0);
678678
679 const width = brk: {
680 var max: usize = 0;
681 for (ctx.components.items) |component| {
682 if (component.location) |loc| {
683 max = @max(loc.loc.line.oneBased(), max);
684 }
685 }
686
687 break :brk if (max > 0) std.fmt.count("{d}", .{max}) else 0;
688 };
689
679690 for (ctx.components.items, 1..) |component, i| {
680691 const prefix: Prefix = if (i == 1)
681692 switch (item_type) {
......@@ -686,15 +697,18 @@ pub fn formatItem(
686697
687698 const is_last = i == ctx.components.items.len;
688699
700 try writer.writeByteNTimes(' ', width);
701
689702 if (i != 1) {
690703 try tty.setColor(writer, .dim);
691 try writer.writeAll(" │ ");
704 try writer.writeAll("│");
692705 try tty.setColor(writer, .reset);
693706 }
694707
695708 try tty.setColor(writer, prefix.color());
696709 try tty.setColor(writer, .bold);
697710 try writer.writeAll(prefix.symbol(false));
711 try writer.writeAll(" ");
698712 try writer.writeAll(@tagName(prefix));
699713 try tty.setColor(writer, .reset);
700714 try tty.setColor(writer, .dim);
......@@ -712,7 +726,8 @@ pub fn formatItem(
712726 const at = component.location orelse {
713727 if (is_last) {
714728 try tty.setColor(writer, .dim);
715 try writer.print(" {s}", .{"┴"});
729 try writer.writeByteNTimes(' ', width);
730 try writer.print("{s}", .{"┴"});
716731 try writer.writeAll("\n");
717732 try tty.setColor(writer, .reset);
718733 }
......@@ -721,8 +736,13 @@ pub fn formatItem(
721736
722737 const line_before = utf8_source[at.loc.line_start..at.byte];
723738 const trimmed_before = std.mem.trimLeft(u8, line_before, " \t\r");
724 const line_after = utf8_source[at.byte + at.slice.len .. at.loc.line_end + 1];
725 const width = std.fmt.count("{d}", .{at.loc.line.oneBased()});
739 // The text after the highlighted component can be out of bounds if the
740 // highlighted token is EOF, which is hardcoded to " " to highlight
741 // empty space after the line.
742 const line_after = if (at.byte + at.slice.len < at.loc.line_end)
743 utf8_source[at.byte + at.slice.len .. at.loc.line_end]
744 else
745 "";
726746
727747 try tty.setColor(writer, .dim);
728748 try writer.writeByteNTimes(' ', width);
......@@ -741,12 +761,13 @@ pub fn formatItem(
741761 try writer.writeAll(at.slice);
742762 try tty.setColor(writer, .reset);
743763 try writer.writeAll(line_after);
764 try writer.writeAll("\n");
744765 try tty.setColor(writer, .dim);
745 try writer.writeAll(if (is_last) " ╰" else " ├");
766 try writer.writeByteNTimes(' ', width);
767 try writer.writeAll(if (is_last) "╰" else "├");
746768 try writer.writeBytesNTimes(
747769 "─",
748 width +
749 (at.byte - at.loc.line_start) -
770 1 + (at.byte - at.loc.line_start) -
750771 (line_before.len - trimmed_before.len),
751772 );
752773 try tty.setColor(writer, .reset);
......@@ -912,7 +933,10 @@ pub const Highlight = union(enum) {
912933
913934 break :loc .{
914935 .loc = location,
915 .slice = if (token.tag != .eof) token.slice(ast.utf8_source) else "\n",
936 .slice = if (token.tag != .eof)
937 token.slice(ast.utf8_source)
938 else
939 " ",
916940 .byte = token.offset,
917941 };
918942 },