diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2024-05-01 15:25:02 -0400 |
|---|---|---|
| committer | Kevin Newton <kddnewton@gmail.com> | 2024-05-01 16:57:29 -0400 |
| commit | 5cd0abdfb58e3ff35d18ce87f1201582e0142d15 (patch) | |
| tree | eeafdb3dd8bd050b9941560a9bbc755b758115ab | |
| parent | fc8fb581cf8e86ee98a35d3e9af82eb1fda9c50f (diff) | |
[PRISM] Simplify prism error highlighting
| -rw-r--r-- | prism/prism.c | 29 | ||||
| -rw-r--r-- | test/ruby/test_parse.rb | 2 |
2 files changed, 17 insertions, 14 deletions
diff --git a/prism/prism.c b/prism/prism.c index 56413361af..ce84e64f8f 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -21549,25 +21549,28 @@ pm_parser_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_append_string(buffer, error_format.blank_prefix, error_format.blank_prefix_length); size_t column = 0; - while (column < error->column_end) { - if (column < error->column_start) { - pm_buffer_append_byte(buffer, ' '); - } else { - const uint8_t caret = column == error->column_start ? '^' : '~'; + while (column < error->column_start) { + pm_buffer_append_byte(buffer, ' '); - if (colorize) { - pm_buffer_append_string(buffer, PM_COLOR_RED, 7); - pm_buffer_append_byte(buffer, caret); - pm_buffer_append_string(buffer, PM_COLOR_RESET, 3); - } else { - pm_buffer_append_byte(buffer, caret); - } - } + size_t char_width = encoding->char_width(start + column, parser->end - (start + column)); + column += (char_width == 0 ? 1 : char_width); + } + + if (colorize) pm_buffer_append_string(buffer, PM_COLOR_RED, 7); + pm_buffer_append_byte(buffer, '^'); + + size_t char_width = encoding->char_width(start + column, parser->end - (start + column)); + column += (char_width == 0 ? 1 : char_width); + + while (column < error->column_end) { + pm_buffer_append_byte(buffer, '~'); size_t char_width = encoding->char_width(start + column, parser->end - (start + column)); column += (char_width == 0 ? 1 : char_width); } + if (colorize) pm_buffer_append_string(buffer, PM_COLOR_RESET, 3); + if (inline_messages) { pm_buffer_append_byte(buffer, ' '); assert(error->error != NULL); diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index fe649cddb9..b8de2ba952 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -18,7 +18,7 @@ class TestParse < Test::Unit::TestCase end def test_else_without_rescue - assert_syntax_error(<<-END, %r":#{__LINE__+2}: else without rescue"o, [__FILE__, __LINE__+1]) + assert_syntax_error(<<-END, %r"(:#{__LINE__+2}:|#{__LINE__+2} \|.+?\n.+?\^~.+?;) else without rescue"o, [__FILE__, __LINE__+1]) begin else 42 |
