From f40a1de20e2a9dc110ae6e41d76ee72c65539e28 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Wed, 30 Jun 2021 12:28:22 +0900 Subject: [PATCH] Experimentally support a custom formatter --- lib/error_highlight/base.rb | 52 ++++++++++++++++---------------- lib/error_highlight/core_ext.rb | 5 +-- lib/error_highlight/formatter.rb | 24 +++++++++++++++ test/test_error_highlight.rb | 21 +++++++++++++ 4 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 lib/error_highlight/formatter.rb diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb index 49c7725..5f3a86b 100644 --- a/lib/error_highlight/base.rb +++ b/lib/error_highlight/base.rb @@ -15,7 +15,7 @@ module ErrorHighlight # first_column: Integer, # last_lineno: Integer, # last_column: Integer, - # line: String, + # snippet: String, # } | nil def self.spot(...) Spotter.new(...).spot @@ -103,13 +103,13 @@ def spot spot_op_cdecl end - if @line && @beg_column && @end_column && @beg_column < @end_column + if @snippet && @beg_column && @end_column && @beg_column < @end_column return { first_lineno: @beg_lineno, first_column: @beg_column, last_lineno: @end_lineno, last_column: @end_column, - line: @line, + snippet: @snippet, } else return nil @@ -135,10 +135,10 @@ def spot_call_for_name lines = @fetch[lineno, @node.last_lineno] if mid == :[] && lines.match(/\G\s*(\[(?:\s*\])?)/, nd_recv.last_column) @beg_column = $~.begin(1) - @line = lines[/.*\n/] + @snippet = lines[/.*\n/] @beg_lineno = @end_lineno = lineno 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 else @@ -152,15 +152,15 @@ def spot_call_for_name @end_column = $~.end(3) if i = lines[..@beg_column].rindex("\n") @beg_lineno = @end_lineno = lineno + lines[..@beg_column].count("\n") - @line = lines[i + 1..] + @snippet = lines[i + 1..] @beg_column -= i + 1 @end_column -= i + 1 else - @line = lines + @snippet = lines @beg_lineno = @end_lineno = lineno end 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) @end_column = $~.end(1) end @@ -192,16 +192,16 @@ def spot_attrasgn_for_name nd_recv, mid, nd_args = @node.children *nd_args, _nd_last_arg, _nil = nd_args.children 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) args_last_column = $~.end(0) if nd_args.last && nd_recv.last_lineno == nd_args.last.last_lineno args_last_column = nd_args.last.last_column end - if @line.match(/\s*\]\s*=/, args_last_column) + if @snippet.match(/\s*\]\s*=/, args_last_column) @end_column = $~.end(0) 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) @end_column = $~.end(1) end @@ -217,7 +217,7 @@ def spot_attrasgn_for_name def spot_attrasgn_for_args nd_recv, mid, nd_args = @node.children 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) if nd_recv.last_lineno == nd_args.last_lineno @end_column = nd_args.last_column @@ -239,13 +239,13 @@ def spot_opcall_for_name fetch_line(nd_recv.last_lineno) if nd_arg # 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) @end_column = $~.end(1) end else # 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) @end_column = $~.end(1) end @@ -273,7 +273,7 @@ def spot_opcall_for_args def spot_fcall_for_name mid, _nd_args = @node.children 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) @end_column = $~.end(1) end @@ -315,13 +315,13 @@ def spot_vcall def spot_op_asgn1_for_name nd_recv, op, nd_args, _nd_rhs = @node.children 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) args_last_column = $~.end(0) if nd_args && nd_recv.last_lineno == nd_args.last_lineno args_last_column = nd_args.last_column 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 when :[], :[]= @beg_column = bracket_beg_column @@ -340,7 +340,7 @@ def spot_op_asgn1_for_name def spot_op_asgn1_for_args nd_recv, mid, nd_args, nd_rhs = @node.children 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) if nd_recv.last_lineno == nd_rhs.last_lineno @end_column = nd_rhs.last_column @@ -362,7 +362,7 @@ def spot_op_asgn1_for_args def spot_op_asgn2_for_name nd_recv, _qcall, attr, op, _nd_rhs = @node.children 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 when attr @beg_column = $~.begin(1) @@ -399,8 +399,8 @@ def spot_colon2 @beg_column = nd_parent.last_column @end_column = @node.last_column else - @line = @fetch[@node.last_lineno] - if @line[...@node.last_column].match(/#{ Regexp.quote(const) }\z/) + @snippet = @fetch[@node.last_lineno] + if @snippet[...@node.last_column].match(/#{ Regexp.quote(const) }\z/) @beg_column = $~.begin(0) @end_column = $~.end(0) end @@ -414,8 +414,8 @@ def spot_op_cdecl nd_lhs, op, _nd_rhs = @node.children *nd_parent_lhs, _const = nd_lhs.children if @name == op - @line = @fetch[nd_lhs.last_lineno] - if @line.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column) + @snippet = @fetch[nd_lhs.last_lineno] + if @snippet.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column) @beg_column = $~.begin(1) @end_column = $~.end(1) end @@ -424,12 +424,12 @@ def spot_op_cdecl @end_column = nd_lhs.last_column if nd_parent_lhs.empty? # example: ::C += 1 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 end else # example: Foo::Bar::C += 1 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 end end @@ -438,7 +438,7 @@ def spot_op_cdecl def fetch_line(lineno) @beg_lineno = @end_lineno = lineno - @line = @fetch[lineno] + @snippet = @fetch[lineno] end end diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb index 0ca2d13..3cfc729 100644 --- a/lib/error_highlight/core_ext.rb +++ b/lib/error_highlight/core_ext.rb @@ -1,3 +1,5 @@ +require_relative "formatter" + module ErrorHighlight module CoreExt # This is a marker to let `DidYouMean::Correctable#original_message` skip @@ -34,8 +36,7 @@ def to_s end if spot - marker = " " * spot[:first_column] + "^" * (spot[:last_column] - spot[:first_column]) - points = "\n\n#{ spot[:line] }#{ marker }" + points = ErrorHighlight.formatter.message_for(spot) msg << points if !msg.include?(points) end diff --git a/lib/error_highlight/formatter.rb b/lib/error_highlight/formatter.rb new file mode 100644 index 0000000..a3d6510 --- /dev/null +++ b/lib/error_highlight/formatter.rb @@ -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 diff --git a/test/test_error_highlight.rb b/test/test_error_highlight.rb index f2a27aa..a74c445 100644 --- a/test/test_error_highlight.rb +++ b/test/test_error_highlight.rb @@ -981,4 +981,25 @@ def v.foo; 1; 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