Skip to content

Commit

Permalink
feat: Syntax highlight of buzz code in error reports
Browse files Browse the repository at this point in the history
closes #57
  • Loading branch information
giann committed Oct 20, 2023
1 parent c6cd264 commit 4c160b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4627,8 +4627,17 @@ pub const CallNode = struct {
if (missing_arguments.count() > 0) {
var missing = std.ArrayList(u8).init(codegen.gc.allocator);
const missing_writer = missing.writer();
for (missing_arguments.keys()) |key| {
try missing_writer.print("{s}, ", .{key.string});
for (missing_arguments.keys(), 0..) |key, i| {
try missing_writer.print(
"{s}{s}",
.{
key.string,
if (i < missing_arguments.keys().len - 1)
", "
else
"",
},
);
}
defer missing.deinit();
codegen.reporter.reportErrorFmt(
Expand Down
23 changes: 21 additions & 2 deletions src/reporter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Token = @import("token.zig").Token;
const o = @import("obj.zig");
const ObjTypeDef = o.ObjTypeDef;
const PlaceholderDef = o.PlaceholderDef;
const Scanner = @import("scanner.zig").Scanner;

const Self = @This();

Expand Down Expand Up @@ -178,6 +179,12 @@ pub const Report = struct {
pub fn report(self: *Report, reporter: *Self, out: anytype) !void {
assert(self.items.len > 0);

const colorterm = std.os.getenv("COLORTERM");
const true_color = if (colorterm) |ct|
std.mem.eql(u8, ct, "24bit") or std.mem.eql(u8, ct, "truecolor")
else
false;

// Print main error message
const main_item = self.items[0];

Expand Down Expand Up @@ -305,7 +312,7 @@ pub const Report = struct {
}

try out.print(
" {: >5} {s} {s}\n\x1b[0m",
" {: >5} {s} ",
.{
l + 1,
if (line_index == 0 and (reported_files.count() == 1 or index > 0))
Expand All @@ -314,10 +321,22 @@ pub const Report = struct {
"╰─"
else
"│ ",
src_line,
},
);

if (l == line) {
var scanner = Scanner.init(
reporter.allocator,
"reporter",
src_line,
);
scanner.highlight(out, true_color);
} else {
try out.writeAll(src_line);
}

try out.writeAll("\n\x1b[0m");

if (l == line) {
// Print error cursors
try out.print(" \x1b[2m┆ \x1b[0m ", .{});
Expand Down

0 comments on commit 4c160b5

Please sign in to comment.