Skip to content

Commit ad17f58

Browse files
committed
Return 1-indexed line numbers
1 parent ecf584c commit ad17f58

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

ext/prism/extension.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -987,12 +987,12 @@ inspect_node(VALUE self, VALUE source) {
987987

988988
/**
989989
* call-seq:
990-
* Debug::format_errors(source) -> String
990+
* Debug::format_errors(source, colorize) -> String
991991
*
992992
* Format the errors that are found when parsing the given source string.
993993
*/
994994
static VALUE
995-
format_errors(VALUE self, VALUE source) {
995+
format_errors(VALUE self, VALUE source, VALUE colorize) {
996996
pm_string_t input;
997997
input_load_string(&input, source);
998998

@@ -1002,7 +1002,7 @@ format_errors(VALUE self, VALUE source) {
10021002
pm_node_t *node = pm_parse(&parser);
10031003
pm_buffer_t buffer = { 0 };
10041004

1005-
pm_parser_errors_format(&parser, &buffer, true);
1005+
pm_parser_errors_format(&parser, &buffer, RTEST(colorize));
10061006

10071007
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
10081008
VALUE result = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), encoding);
@@ -1093,7 +1093,7 @@ Init_prism(void) {
10931093
rb_define_singleton_method(rb_cPrismDebug, "memsize", memsize, 1);
10941094
rb_define_singleton_method(rb_cPrismDebug, "profile_file", profile_file, 1);
10951095
rb_define_singleton_method(rb_cPrismDebug, "inspect_node", inspect_node, 1);
1096-
rb_define_singleton_method(rb_cPrismDebug, "format_errors", format_errors, 1);
1096+
rb_define_singleton_method(rb_cPrismDebug, "format_errors", format_errors, 2);
10971097

10981098
// Next, initialize the other APIs.
10991099
Init_prism_api_node();

src/prism.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17873,7 +17873,7 @@ pm_parser_errors_format_sort(const pm_list_t *error_list, const pm_newline_list_
1787317873
if (start.line == end.line) {
1787417874
column_end = (uint32_t) end.column;
1787517875
} else {
17876-
column_end = (uint32_t) (newline_list->offsets[start.line + 1] - newline_list->offsets[start.line] - 1);
17876+
column_end = (uint32_t) (newline_list->offsets[start.line] - newline_list->offsets[start.line - 1] - 1);
1787717877
}
1787817878

1787917879
// Ensure we have at least one column of error.
@@ -17892,16 +17892,16 @@ pm_parser_errors_format_sort(const pm_list_t *error_list, const pm_newline_list_
1789217892

1789317893
static inline void
1789417894
pm_parser_errors_format_line(const pm_parser_t *parser, const pm_newline_list_t *newline_list, const char *number_prefix, size_t line, pm_buffer_t *buffer) {
17895-
const uint8_t *start = &parser->start[newline_list->offsets[line]];
17895+
const uint8_t *start = &parser->start[newline_list->offsets[line - 1]];
1789617896
const uint8_t *end;
1789717897

17898-
if (line + 1 >= newline_list->size) {
17898+
if (line >= newline_list->size) {
1789917899
end = parser->end;
1790017900
} else {
17901-
end = &parser->start[newline_list->offsets[line + 1]];
17901+
end = &parser->start[newline_list->offsets[line]];
1790217902
}
1790317903

17904-
pm_buffer_append_format(buffer, number_prefix, (uint32_t) (line + 1));
17904+
pm_buffer_append_format(buffer, number_prefix, (uint32_t) line);
1790517905
pm_buffer_append_string(buffer, (const char *) start, (size_t) (end - start));
1790617906

1790717907
if (end == parser->end && end[-1] != '\n') {
@@ -17926,7 +17926,7 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
1792617926
// blank lines based on the maximum number of digits in the line numbers
1792717927
// that are going to be displayed.
1792817928
pm_error_format_t error_format;
17929-
size_t max_line_number = errors[error_list->size - 1].line + 1;
17929+
size_t max_line_number = errors[error_list->size - 1].line;
1793017930

1793117931
if (max_line_number < 10) {
1793217932
if (colorize) {
@@ -18008,7 +18008,7 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
1800818008
// the source before the error to give some context. We'll be careful not to
1800918009
// display the same line twice in case the errors are close enough in the
1801018010
// source.
18011-
uint32_t last_line = (uint32_t) -1;
18011+
uint32_t last_line = 0;
1801218012
const pm_encoding_t *encoding = parser->encoding;
1801318013

1801418014
for (size_t index = 0; index < error_list->size; index++) {
@@ -18054,7 +18054,7 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
1805418054
pm_buffer_append_string(buffer, error_format.blank_prefix, error_format.blank_prefix_length);
1805518055

1805618056
size_t column = 0;
18057-
const uint8_t *start = &parser->start[newline_list->offsets[error->line]];
18057+
const uint8_t *start = &parser->start[newline_list->offsets[error->line - 1]];
1805818058

1805918059
while (column < error->column_end) {
1806018060
if (column < error->column_start) {
@@ -18078,7 +18078,7 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
1807818078
// Here we determine how many lines of padding to display after the
1807918079
// error, depending on where the next error is in source.
1808018080
last_line = error->line;
18081-
size_t next_line = (index == error_list->size - 1) ? newline_list->size - 1 : errors[index + 1].line;
18081+
size_t next_line = (index == error_list->size - 1) ? newline_list->size : errors[index + 1].line;
1808218082

1808318083
if (next_line - last_line > 1) {
1808418084
pm_buffer_append_string(buffer, " ", 2);

src/util/pm_newline_list.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor
6262
size_t mid = left + (right - left) / 2;
6363

6464
if (list->offsets[mid] == offset) {
65-
return ((pm_line_column_t) { mid, 0 });
65+
return ((pm_line_column_t) { mid + 1, 0 });
6666
}
6767

6868
if (list->offsets[mid] < offset) {
@@ -72,7 +72,7 @@ pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor
7272
}
7373
}
7474

75-
return ((pm_line_column_t) { left - 1, offset - list->offsets[left - 1] });
75+
return ((pm_line_column_t) { left, offset - list->offsets[left - 1] });
7676
}
7777

7878
/**

templates/src/prettyprint.c.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static inline void
4040
prettyprint_location(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm_location_t *location) {
4141
pm_line_column_t start = pm_newline_list_line_column(&parser->newline_list, location->start);
4242
pm_line_column_t end = pm_newline_list_line_column(&parser->newline_list, location->end);
43-
pm_buffer_append_format(output_buffer, "(%lu,%lu)-(%lu,%lu)", (unsigned long) (start.line + 1), (unsigned long) start.column, (unsigned long) (end.line + 1), (unsigned long) end.column);
43+
pm_buffer_append_format(output_buffer, "(%lu,%lu)-(%lu,%lu)", (unsigned long) start.line, (unsigned long) start.column, (unsigned long) end.line, (unsigned long) end.column);
4444
}
4545

4646
static inline void

test/prism/format_errors_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
return if Prism::BACKEND == :FFI
4+
5+
require_relative "test_helper"
6+
7+
module Prism
8+
class FormatErrorsTest < TestCase
9+
def test_format_errors
10+
assert_equal <<~ERROR, Debug.format_errors("<>", false)
11+
> 1 | <>
12+
| ^ cannot parse the expression
13+
| ^ cannot parse the expression
14+
ERROR
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)