[ruby/error_highlight] Experimentally support a custom formatter

https://github.com/ruby/error_highlight/commit/f40a1de20e
This commit is contained in:
Yusuke Endoh 2021-06-30 12:28:22 +09:00 committed by git
parent db7e9b1aac
commit f428ced69c
4 changed files with 74 additions and 28 deletions

View File

@ -15,7 +15,7 @@ module ErrorHighlight
# first_column: Integer, # first_column: Integer,
# last_lineno: Integer, # last_lineno: Integer,
# last_column: Integer, # last_column: Integer,
# line: String, # snippet: String,
# } | nil # } | nil
def self.spot(...) def self.spot(...)
Spotter.new(...).spot Spotter.new(...).spot
@ -103,13 +103,13 @@ module ErrorHighlight
spot_op_cdecl spot_op_cdecl
end end
if @line && @beg_column && @end_column && @beg_column < @end_column if @snippet && @beg_column && @end_column && @beg_column < @end_column
return { return {
first_lineno: @beg_lineno, first_lineno: @beg_lineno,
first_column: @beg_column, first_column: @beg_column,
last_lineno: @end_lineno, last_lineno: @end_lineno,
last_column: @end_column, last_column: @end_column,
line: @line, snippet: @snippet,
} }
else else
return nil return nil
@ -135,10 +135,10 @@ module ErrorHighlight
lines = @fetch[lineno, @node.last_lineno] lines = @fetch[lineno, @node.last_lineno]
if mid == :[] && lines.match(/\G\s*(\[(?:\s*\])?)/, nd_recv.last_column) if mid == :[] && lines.match(/\G\s*(\[(?:\s*\])?)/, nd_recv.last_column)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@line = lines[/.*\n/] @snippet = lines[/.*\n/]
@beg_lineno = @end_lineno = lineno @beg_lineno = @end_lineno = lineno
if nd_args if nd_args
if nd_recv.last_lineno == nd_args.last_lineno && @line.match(/\s*\]/, nd_args.last_column) if nd_recv.last_lineno == nd_args.last_lineno && @snippet.match(/\s*\]/, nd_args.last_column)
@end_column = $~.end(0) @end_column = $~.end(0)
end end
else else
@ -152,15 +152,15 @@ module ErrorHighlight
@end_column = $~.end(3) @end_column = $~.end(3)
if i = lines[..@beg_column].rindex("\n") if i = lines[..@beg_column].rindex("\n")
@beg_lineno = @end_lineno = lineno + lines[..@beg_column].count("\n") @beg_lineno = @end_lineno = lineno + lines[..@beg_column].count("\n")
@line = lines[i + 1..] @snippet = lines[i + 1..]
@beg_column -= i + 1 @beg_column -= i + 1
@end_column -= i + 1 @end_column -= i + 1
else else
@line = lines @snippet = lines
@beg_lineno = @end_lineno = lineno @beg_lineno = @end_lineno = lineno
end end
elsif mid.to_s =~ /\A\W+\z/ && lines.match(/\G\s*(#{ Regexp.quote(mid) })=.*\n/, nd_recv.last_column) elsif mid.to_s =~ /\A\W+\z/ && lines.match(/\G\s*(#{ Regexp.quote(mid) })=.*\n/, nd_recv.last_column)
@line = $` + $& @snippet = $` + $&
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@end_column = $~.end(1) @end_column = $~.end(1)
end end
@ -192,16 +192,16 @@ module ErrorHighlight
nd_recv, mid, nd_args = @node.children nd_recv, mid, nd_args = @node.children
*nd_args, _nd_last_arg, _nil = nd_args.children *nd_args, _nd_last_arg, _nil = nd_args.children
fetch_line(nd_recv.last_lineno) fetch_line(nd_recv.last_lineno)
if mid == :[]= && @line.match(/\G\s*(\[)/, nd_recv.last_column) if mid == :[]= && @snippet.match(/\G\s*(\[)/, nd_recv.last_column)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
args_last_column = $~.end(0) args_last_column = $~.end(0)
if nd_args.last && nd_recv.last_lineno == nd_args.last.last_lineno if nd_args.last && nd_recv.last_lineno == nd_args.last.last_lineno
args_last_column = nd_args.last.last_column args_last_column = nd_args.last.last_column
end end
if @line.match(/\s*\]\s*=/, args_last_column) if @snippet.match(/\s*\]\s*=/, args_last_column)
@end_column = $~.end(0) @end_column = $~.end(0)
end end
elsif @line.match(/\G\s*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column) elsif @snippet.match(/\G\s*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@end_column = $~.end(1) @end_column = $~.end(1)
end end
@ -217,7 +217,7 @@ module ErrorHighlight
def spot_attrasgn_for_args def spot_attrasgn_for_args
nd_recv, mid, nd_args = @node.children nd_recv, mid, nd_args = @node.children
fetch_line(nd_recv.last_lineno) fetch_line(nd_recv.last_lineno)
if mid == :[]= && @line.match(/\G\s*\[/, nd_recv.last_column) if mid == :[]= && @snippet.match(/\G\s*\[/, nd_recv.last_column)
@beg_column = $~.end(0) @beg_column = $~.end(0)
if nd_recv.last_lineno == nd_args.last_lineno if nd_recv.last_lineno == nd_args.last_lineno
@end_column = nd_args.last_column @end_column = nd_args.last_column
@ -239,13 +239,13 @@ module ErrorHighlight
fetch_line(nd_recv.last_lineno) fetch_line(nd_recv.last_lineno)
if nd_arg if nd_arg
# binary operator # binary operator
if @line.match(/\G\s*(#{ Regexp.quote(op) })/, nd_recv.last_column) if @snippet.match(/\G\s*(#{ Regexp.quote(op) })/, nd_recv.last_column)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@end_column = $~.end(1) @end_column = $~.end(1)
end end
else else
# unary operator # unary operator
if @line[...nd_recv.first_column].match(/(#{ Regexp.quote(op.to_s.sub(/@\z/, "")) })\s*\(?\s*\z/) if @snippet[...nd_recv.first_column].match(/(#{ Regexp.quote(op.to_s.sub(/@\z/, "")) })\s*\(?\s*\z/)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@end_column = $~.end(1) @end_column = $~.end(1)
end end
@ -273,7 +273,7 @@ module ErrorHighlight
def spot_fcall_for_name def spot_fcall_for_name
mid, _nd_args = @node.children mid, _nd_args = @node.children
fetch_line(@node.first_lineno) fetch_line(@node.first_lineno)
if @line.match(/(#{ Regexp.quote(mid) })/, @node.first_column) if @snippet.match(/(#{ Regexp.quote(mid) })/, @node.first_column)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@end_column = $~.end(1) @end_column = $~.end(1)
end end
@ -315,13 +315,13 @@ module ErrorHighlight
def spot_op_asgn1_for_name def spot_op_asgn1_for_name
nd_recv, op, nd_args, _nd_rhs = @node.children nd_recv, op, nd_args, _nd_rhs = @node.children
fetch_line(nd_recv.last_lineno) fetch_line(nd_recv.last_lineno)
if @line.match(/\G\s*(\[)/, nd_recv.last_column) if @snippet.match(/\G\s*(\[)/, nd_recv.last_column)
bracket_beg_column = $~.begin(1) bracket_beg_column = $~.begin(1)
args_last_column = $~.end(0) args_last_column = $~.end(0)
if nd_args && nd_recv.last_lineno == nd_args.last_lineno if nd_args && nd_recv.last_lineno == nd_args.last_lineno
args_last_column = nd_args.last_column args_last_column = nd_args.last_column
end end
if @line.match(/\s*\](\s*)(#{ Regexp.quote(op) })=()/, args_last_column) if @snippet.match(/\s*\](\s*)(#{ Regexp.quote(op) })=()/, args_last_column)
case @name case @name
when :[], :[]= when :[], :[]=
@beg_column = bracket_beg_column @beg_column = bracket_beg_column
@ -340,7 +340,7 @@ module ErrorHighlight
def spot_op_asgn1_for_args def spot_op_asgn1_for_args
nd_recv, mid, nd_args, nd_rhs = @node.children nd_recv, mid, nd_args, nd_rhs = @node.children
fetch_line(nd_recv.last_lineno) fetch_line(nd_recv.last_lineno)
if mid == :[]= && @line.match(/\G\s*\[/, nd_recv.last_column) if mid == :[]= && @snippet.match(/\G\s*\[/, nd_recv.last_column)
@beg_column = $~.end(0) @beg_column = $~.end(0)
if nd_recv.last_lineno == nd_rhs.last_lineno if nd_recv.last_lineno == nd_rhs.last_lineno
@end_column = nd_rhs.last_column @end_column = nd_rhs.last_column
@ -362,7 +362,7 @@ module ErrorHighlight
def spot_op_asgn2_for_name def spot_op_asgn2_for_name
nd_recv, _qcall, attr, op, _nd_rhs = @node.children nd_recv, _qcall, attr, op, _nd_rhs = @node.children
fetch_line(nd_recv.last_lineno) fetch_line(nd_recv.last_lineno)
if @line.match(/\G\s*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column) if @snippet.match(/\G\s*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column)
case @name case @name
when attr when attr
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@ -399,8 +399,8 @@ module ErrorHighlight
@beg_column = nd_parent.last_column @beg_column = nd_parent.last_column
@end_column = @node.last_column @end_column = @node.last_column
else else
@line = @fetch[@node.last_lineno] @snippet = @fetch[@node.last_lineno]
if @line[...@node.last_column].match(/#{ Regexp.quote(const) }\z/) if @snippet[...@node.last_column].match(/#{ Regexp.quote(const) }\z/)
@beg_column = $~.begin(0) @beg_column = $~.begin(0)
@end_column = $~.end(0) @end_column = $~.end(0)
end end
@ -414,8 +414,8 @@ module ErrorHighlight
nd_lhs, op, _nd_rhs = @node.children nd_lhs, op, _nd_rhs = @node.children
*nd_parent_lhs, _const = nd_lhs.children *nd_parent_lhs, _const = nd_lhs.children
if @name == op if @name == op
@line = @fetch[nd_lhs.last_lineno] @snippet = @fetch[nd_lhs.last_lineno]
if @line.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column) if @snippet.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column)
@beg_column = $~.begin(1) @beg_column = $~.begin(1)
@end_column = $~.end(1) @end_column = $~.end(1)
end end
@ -424,12 +424,12 @@ module ErrorHighlight
@end_column = nd_lhs.last_column @end_column = nd_lhs.last_column
if nd_parent_lhs.empty? # example: ::C += 1 if nd_parent_lhs.empty? # example: ::C += 1
if nd_lhs.first_lineno == nd_lhs.last_lineno if nd_lhs.first_lineno == nd_lhs.last_lineno
@line = @fetch[nd_lhs.last_lineno] @snippet = @fetch[nd_lhs.last_lineno]
@beg_column = nd_lhs.first_column @beg_column = nd_lhs.first_column
end end
else # example: Foo::Bar::C += 1 else # example: Foo::Bar::C += 1
if nd_parent_lhs.last.last_lineno == nd_lhs.last_lineno if nd_parent_lhs.last.last_lineno == nd_lhs.last_lineno
@line = @fetch[nd_lhs.last_lineno] @snippet = @fetch[nd_lhs.last_lineno]
@beg_column = nd_parent_lhs.last.last_column @beg_column = nd_parent_lhs.last.last_column
end end
end end
@ -438,7 +438,7 @@ module ErrorHighlight
def fetch_line(lineno) def fetch_line(lineno)
@beg_lineno = @end_lineno = lineno @beg_lineno = @end_lineno = lineno
@line = @fetch[lineno] @snippet = @fetch[lineno]
end end
end end

View File

@ -1,3 +1,5 @@
require_relative "formatter"
module ErrorHighlight module ErrorHighlight
module CoreExt module CoreExt
# This is a marker to let `DidYouMean::Correctable#original_message` skip # This is a marker to let `DidYouMean::Correctable#original_message` skip
@ -34,8 +36,7 @@ module ErrorHighlight
end end
if spot if spot
marker = " " * spot[:first_column] + "^" * (spot[:last_column] - spot[:first_column]) points = ErrorHighlight.formatter.message_for(spot)
points = "\n\n#{ spot[:line] }#{ marker }"
msg << points if !msg.include?(points) msg << points if !msg.include?(points)
end end

View File

@ -0,0 +1,24 @@
module ErrorHighlight
class DefaultFormatter
def message_for(spot)
# currently only a one-line code snippet is supported
if spot[:first_lineno] == spot[:last_lineno]
marker = " " * spot[:first_column] + "^" * (spot[:last_column] - spot[:first_column])
"\n\n#{ spot[:snippet] }#{ marker }"
else
""
end
end
end
def self.formatter
@@formatter
end
def self.formatter=(formatter)
@@formatter = formatter
end
self.formatter = DefaultFormatter.new
end

View File

@ -981,4 +981,25 @@ nil can't be coerced into Integer
end end
end end
def test_custom_formatter
custom_formatter = Object.new
def custom_formatter.message_for(spot)
"\n\n" + spot.inspect
end
original_formatter, ErrorHighlight.formatter = ErrorHighlight.formatter, custom_formatter
assert_error_message(NoMethodError, <<~END) do
undefined method `time' for 1:Integer
{:first_lineno=>#{ __LINE__ + 3 }, :first_column=>7, :last_lineno=>#{ __LINE__ + 3 }, :last_column=>12, :snippet=>" 1.time {}\\n"}
END
1.time {}
end
ensure
ErrorHighlight.formatter = original_formatter
end
end end