summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2024-03-27 12:04:15 -0400
committerKevin Newton <kddnewton@gmail.com>2024-03-27 13:03:11 -0400
commit9b816e674a4ecadde047212827e3bbe87cd61345 (patch)
treeba17148ac905e9a066596165c7bf623b17dbd66b
parentc50b6425b4b5b42f1177b2c4eb6fd61d9d95b4d4 (diff)
[ruby/prism] Add option for inlining messages for error formatting
https://github.com/ruby/prism/commit/af0204a8ab
-rw-r--r--prism/extension.c2
-rw-r--r--prism/prism.c11
-rw-r--r--prism/prism.h4
3 files changed, 10 insertions, 7 deletions
diff --git a/prism/extension.c b/prism/extension.c
index 5b5c0593c9..921e197783 100644
--- a/prism/extension.c
+++ b/prism/extension.c
@@ -1184,7 +1184,7 @@ format_errors(VALUE self, VALUE source, VALUE colorize) {
pm_node_t *node = pm_parse(&parser);
pm_buffer_t buffer = { 0 };
- pm_parser_errors_format(&parser, &buffer, RTEST(colorize));
+ pm_parser_errors_format(&parser, &parser.error_list, &buffer, RTEST(colorize), true);
rb_encoding *encoding = rb_enc_find(parser.encoding->name);
VALUE result = rb_enc_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer), encoding);
diff --git a/prism/prism.c b/prism/prism.c
index c6353f451b..b8787bb9a9 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -19671,8 +19671,7 @@ pm_parser_errors_format_line(const pm_parser_t *parser, const pm_newline_list_t
* Format the errors on the parser into the given buffer.
*/
PRISM_EXPORTED_FUNCTION void
-pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool colorize) {
- const pm_list_t *error_list = &parser->error_list;
+pm_parser_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_t *buffer, bool colorize, bool inline_messages) {
assert(error_list->size != 0);
// First, we're going to sort all of the errors by line number using an
@@ -19831,10 +19830,12 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
column += (char_width == 0 ? 1 : char_width);
}
- pm_buffer_append_byte(buffer, ' ');
+ if (inline_messages) {
+ pm_buffer_append_byte(buffer, ' ');
+ const char *message = error->error->message;
+ pm_buffer_append_string(buffer, message, strlen(message));
+ }
- const char *message = error->error->message;
- pm_buffer_append_string(buffer, message, strlen(message));
pm_buffer_append_byte(buffer, '\n');
// Here we determine how many lines of padding to display after the
diff --git a/prism/prism.h b/prism/prism.h
index 34540b9441..70e1e9df49 100644
--- a/prism/prism.h
+++ b/prism/prism.h
@@ -222,10 +222,12 @@ const char * pm_token_type_human(pm_token_type_t token_type);
* Format the errors on the parser into the given buffer.
*
* @param parser The parser to format the errors for.
+ * @param error_list The list of errors to format.
* @param buffer The buffer to format the errors into.
* @param colorize Whether or not to colorize the errors with ANSI escape sequences.
+ * @param inline_messages Whether or not to inline the messages with the source.
*/
-PRISM_EXPORTED_FUNCTION void pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool colorize);
+PRISM_EXPORTED_FUNCTION void pm_parser_errors_format(const pm_parser_t *parser, const pm_list_t *error_list, pm_buffer_t *buffer, bool colorize, bool inline_messages);
// We optionally support dumping to JSON. For systems that don't want or need
// this functionality, it can be turned off with the PRISM_EXCLUDE_JSON define.