Colorize errors more
* lib/irb/color.rb (IRB::Color.colorize_code): colorize `compile_error` part as same as `on_parse_error`.
This commit is contained in:
parent
12644e8b02
commit
5b64d7ac6e
@ -108,25 +108,34 @@ module IRB # :nodoc:
|
|||||||
symbol_state = SymbolState.new
|
symbol_state = SymbolState.new
|
||||||
colored = +''
|
colored = +''
|
||||||
length = 0
|
length = 0
|
||||||
|
pos = [1, 0]
|
||||||
|
|
||||||
scan(code).each do |elem|
|
scan(code).each do |elem|
|
||||||
token = elem.event
|
token = elem.event
|
||||||
str = elem.tok
|
str = elem.tok
|
||||||
expr = elem.state
|
expr = elem.state
|
||||||
in_symbol = symbol_state.scan_token(token)
|
in_symbol = symbol_state.scan_token(token)
|
||||||
if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
|
next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
|
||||||
Reline::Unicode.escape_for_print(str).each_line do |line|
|
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 << seq.map { |s| "\e[#{s}m" }.join('')
|
||||||
colored << line.sub(/\Z/, clear)
|
colored << line.sub(/\Z/, clear)
|
||||||
end
|
|
||||||
else
|
else
|
||||||
colored << Reline::Unicode.escape_for_print(str)
|
colored << line
|
||||||
end
|
end
|
||||||
length += str.length
|
end
|
||||||
|
length += str.bytesize
|
||||||
end
|
end
|
||||||
|
|
||||||
# give up colorizing incomplete Ripper tokens
|
# give up colorizing incomplete Ripper tokens
|
||||||
return code if length != code.length
|
return code if length != code.bytesize
|
||||||
|
|
||||||
colored
|
colored
|
||||||
end
|
end
|
||||||
|
@ -66,6 +66,7 @@ module TestIRB
|
|||||||
"\t" => "\t", # not ^I
|
"\t" => "\t", # not ^I
|
||||||
"foo(*%W(bar))" => "foo(*#{RED}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED})#{CLEAR})",
|
"foo(*%W(bar))" => "foo(*#{RED}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED})#{CLEAR})",
|
||||||
"$stdout" => "#{GREEN}#{BOLD}$stdout#{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|
|
}.each do |code, result|
|
||||||
actual = with_term { IRB::Color.colorize_code(code) }
|
actual = with_term { IRB::Color.colorize_code(code) }
|
||||||
assert_equal(result, actual, "Case: colorize_code(#{code.dump})\nResult: #{humanized_literal(actual)}")
|
assert_equal(result, actual, "Case: colorize_code(#{code.dump})\nResult: #{humanized_literal(actual)}")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user