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

ruby-changes:55910

From: Nobuyoshi <ko1@a...>
Date: Wed, 29 May 2019 22:13:22 +0900 (JST)
Subject: [ruby-changes:55910] Nobuyoshi Nakada: 5b64d7ac6e (trunk): Colorize errors more

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

From 5b64d7ac6e7cbf759b859428f125539e58bac0bd Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 29 May 2019 22:03:47 +0900
Subject: Colorize errors more

* lib/irb/color.rb (IRB::Color.colorize_code): colorize
  `compile_error` part as same as `on_parse_error`.

diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index caa7f50..0dbb16b 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -108,25 +108,34 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L108
         symbol_state = SymbolState.new
         colored = +''
         length = 0
+        pos = [1, 0]
 
         scan(code).each do |elem|
           token = elem.event
           str = elem.tok
           expr = elem.state
           in_symbol = symbol_state.scan_token(token)
-          if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
-            Reline::Unicode.escape_for_print(str).each_line do |line|
+          next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
+          str.each_line do |line|
+            if line.end_with?("\n")
+              pos[0] += 1
+              pos[1] = 0
+            else
+              pos[1] += line.bytesize
+            end
+            line = Reline::Unicode.escape_for_print(line)
+            if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
               colored << seq.map { |s| "\e[#{s}m" }.join('')
               colored << line.sub(/\Z/, clear)
+            else
+              colored << line
             end
-          else
-            colored << Reline::Unicode.escape_for_print(str)
           end
-          length += str.length
+          length += str.bytesize
         end
 
         # give up colorizing incomplete Ripper tokens
-        return code if length != code.length
+        return code if length != code.bytesize
 
         colored
       end
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index edca2d7..778874a 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -66,6 +66,7 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L66
         "\t" => "\t", # not ^I
         "foo(*%W(bar))" => "foo(*#{RED}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED})#{CLEAR})",
         "$stdout" => "#{GREEN}#{BOLD}$stdout#{CLEAR}",
+        "'foo' + 'bar" => "#{RED}'#{CLEAR}#{RED}foo#{CLEAR}#{RED}'#{CLEAR} + #{RED}'#{CLEAR}#{RED}#{REVERSE}bar#{CLEAR}",
       }.each do |code, result|
         actual = with_term { IRB::Color.colorize_code(code) }
         assert_equal(result, actual, "Case: colorize_code(#{code.dump})\nResult: #{humanized_literal(actual)}")
-- 
cgit v0.10.2


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

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