[前][次][番号順一覧][スレッド一覧]

ruby-changes:66641

From: Yusuke <ko1@a...>
Date: Wed, 30 Jun 2021 12:49:33 +0900 (JST)
Subject: [ruby-changes:66641] f428ced69c (master): [ruby/error_highlight] Experimentally support a custom formatter

https://git.ruby-lang.org/ruby.git/commit/?id=f428ced69c

From f428ced69c70473b8405aae9c98828aa6f69b254 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Wed, 30 Jun 2021 12:28:22 +0900
Subject: [ruby/error_highlight] Experimentally support a custom formatter

https://github.com/ruby/error_highlight/commit/f40a1de20e
---
 lib/error_highlight/base.rb                  | 52 ++++++++++++++--------------
 lib/error_highlight/core_ext.rb              |  5 +--
 lib/error_highlight/formatter.rb             | 24 +++++++++++++
 test/error_highlight/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 https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L15
   #    first_column: Integer,
   #    last_lineno: Integer,
   #    last_column: Integer,
-  #    line: String,
+  #    snippet: String,
   #  } | nil
   def self.spot(...)
     Spotter.new(...).spot
@@ -103,13 +103,13 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L103
         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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L135
       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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L152
         @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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L192
       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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L217
     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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L239
       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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L273
     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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L315
     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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L340
     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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L362
     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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L399
         @beg_column = nd_parent.last_column
         @end_column = @node.last_column
       else
-        @line = @fetch[@node.last_lineno]
-        if @line[...@n..._column].match(/#{ Regexp.quote(const) }\z/)
+        @snippet = @fetch[@node.last_lineno]
+        if @snippet[...@n..._column].match(/#{ Regexp.quote(const) }\z/)
           @beg_column = $~.begin(0)
           @end_column = $~.end(0)
         end
@@ -414,8 +414,8 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L414
       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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L424
         @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 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L438
 
     def fetch_line(lineno)
       @beg_lineno = @end_lineno = lineno
-      @line = @fetch[lineno]
+      @snippet = @fetch[lineno]
     end
   end
 
diff --git a/lib/error_highlight/c (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]