From 657c51636080e9b3c9c2215d05b08f3cccf68e70 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 12 Jun 2024 20:58:28 +0200 Subject: Pad ‘^~~~^’-style diagnostic ranges properly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/error.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/error.cpp') diff --git a/src/error.cpp b/src/error.cpp index 03d96219b..5a4cb6a0c 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -323,8 +323,13 @@ gb_internal isize show_error_on_line(TokenPos const &pos, TokenPos end) { } error_out("\n\t"); - for (i32 i = 0; i < offset; i++) { - error_out(" "); + for (i32 rune_width, off = 0; off < offset; off += rune_width) { + i32 rune; + rune_width = cast(i32)utf8proc_iterate((u8 const *)line_text + off, line_len - off, &rune); + int w = utf8proc_charwidth(rune); + if (w > 0) { + error_out("%.*s", w, " "); + } } terminal_set_colours(TerminalStyle_Bold, TerminalColour_Green); -- cgit v1.2.3 From 9f190f3937628595ecd8f6c784b85d7be6e0254a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 13 Jun 2024 16:42:04 +0200 Subject: Generate ranges of the correct length --- src/error.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/error.cpp') diff --git a/src/error.cpp b/src/error.cpp index 5a4cb6a0c..cdf3b806a 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -336,15 +336,27 @@ gb_internal isize show_error_on_line(TokenPos const &pos, TokenPos end) { error_out("^"); if (end.file_id == pos.file_id) { + i32 rune; + if (end.line > pos.line) { - for (i32 i = offset; i < line_len; i++) { - error_out("~"); + for (i32 rune, rune_width, off = offset; off < line_len; off += rune_width) { + rune_width = cast(i32)utf8proc_iterate((u8 const *)line_text + off, line_len - off, &rune); + int w = utf8proc_charwidth(rune); + if (w > 0) { + error_out("%.*s", w, "~~~~"); + } } } else if (end.line == pos.line && end.column > pos.column) { - for (i32 i = 1; i < error_length-1+squiggle_extra; i++) { + i32 columns = squiggle_extra; + for (i32 rune, rune_width, off = offset; off < offset + error_length - 1; off += rune_width) { + rune_width = cast(i32)utf8proc_iterate((u8 const *)line_text + off, line_len - off, &rune); + columns += utf8proc_charwidth(rune); + } + + for (i32 i = 0; i < columns - 1; i++) { error_out("~"); } - if (error_length > 1 && squiggle_extra == 0) { + if (columns > 0 && squiggle_extra == 0) { error_out("^"); } } -- cgit v1.2.3 From ca9d1f940d4118f0652d11cbcd50dc4209d54720 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 13 Jun 2024 17:23:30 +0200 Subject: Just change squiggle_extra type to i32. --- src/error.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/error.cpp') diff --git a/src/error.cpp b/src/error.cpp index cdf3b806a..a63dec43a 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -296,7 +296,7 @@ gb_internal isize show_error_on_line(TokenPos const &pos, TokenPos end) { terminal_set_colours(TerminalStyle_Bold, TerminalColour_White); - isize squiggle_extra = 0; + i32 squiggle_extra = 0; if (line_len > MAX_LINE_LENGTH_PADDED) { i32 left = MAX_TAB_WIDTH; -- cgit v1.2.3 From 9f7ac1469fc364330ada811032d295ba36f6e078 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 13 Jun 2024 16:42:04 +0200 Subject: Generate ranges of the correct length --- src/error.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/error.cpp') diff --git a/src/error.cpp b/src/error.cpp index 5a4cb6a0c..1e24325c5 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -336,15 +336,27 @@ gb_internal isize show_error_on_line(TokenPos const &pos, TokenPos end) { error_out("^"); if (end.file_id == pos.file_id) { + i32 rune; + if (end.line > pos.line) { - for (i32 i = offset; i < line_len; i++) { - error_out("~"); + for (i32 rune, rune_width, off = offset; off < line_len; off += rune_width) { + rune_width = cast(i32)utf8proc_iterate((u8 const *)line_text + off, line_len - off, &rune); + int w = utf8proc_charwidth(rune); + if (w > 0) { + error_out("%.*s", w, "~~~~"); + } } } else if (end.line == pos.line && end.column > pos.column) { - for (i32 i = 1; i < error_length-1+squiggle_extra; i++) { + isize columns = squiggle_extra; + for (i32 rune, rune_width, off = offset; off < offset + error_length - 1; off += rune_width) { + rune_width = cast(i32)utf8proc_iterate((u8 const *)line_text + off, line_len - off, &rune); + columns += utf8proc_charwidth(rune); + } + + for (isize i = 1; i < columns; i++) { error_out("~"); } - if (error_length > 1 && squiggle_extra == 0) { + if (columns > 0 && squiggle_extra == 0) { error_out("^"); } } -- cgit v1.2.3