Skip to content

Commit

Permalink
Revert "Colorize error part more"
Browse files Browse the repository at this point in the history
This reverts commit c7f3c22.
  • Loading branch information
ko1 committed May 29, 2019
1 parent c7f3c22 commit 8187ffa
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions lib/irb/color.rb
Expand Up @@ -56,7 +56,7 @@ module Color
on_tstring_content: [[RED], ALL],
on_tstring_end: [[RED], ALL],
on_words_beg: [[RED], ALL],
ERROR: [[RED, REVERSE], ALL],
on_parse_error: [[RED, REVERSE], ALL],
}
rescue NameError
# Give up highlighting Ripper-incompatible older Ruby
Expand All @@ -66,16 +66,9 @@ module Color

class Lexer < Ripper::Lexer
if method_defined?(:token)
def self.lex(code)
new(code).parse.sort_by {|elem| elem.pos.dup << elem.event}
def on_parse_error(mesg)
@buf.push Elem.new([lineno(), column()], __callee__, token(), state())
end

def on_error(mesg)
# :ERROR comes before other :on_ symbols
@buf.push Elem.new([lineno(), column()], :ERROR, token(), state())
end
alias compile_error on_error
alias on_parse_error on_error
end
end

Expand Down Expand Up @@ -117,43 +110,33 @@ def colorize_code(code)
symbol_state = SymbolState.new
colored = +''
length = 0
pos = [1, 0]

Lexer.lex(code).each do |elem|
Lexer.new(code).parse.sort_by(&:pos).each do |elem|
token = elem.event
str = elem.tok
expr = elem.state
in_symbol = symbol_state.scan_token(token)
next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
Reline::Unicode.escape_for_print(str).each_line do |line|
if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
Reline::Unicode.escape_for_print(str).each_line do |line|
colored << seq.map { |s| "\e[#{s}m" }.join('')
colored << line.sub(/\Z/, clear)
else
colored << line
end
if line.end_with?("\n")
pos[0] += 1
pos[1] = 0
else
pos[1] += line.bytesize
end
else
colored << Reline::Unicode.escape_for_print(str)
end
length += str.bytesize
length += str.length
end

# give up colorizing incomplete Ripper tokens
return code if length != code.bytesize
return code if length != code.length

colored
end

private

def dispatch_seq(token, expr, str, in_symbol:)
if token == :ERROR
TOKEN_SEQ_EXPRS[token][0]
elsif in_symbol
if in_symbol
[YELLOW]
elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)
[CYAN, BOLD]
Expand Down

0 comments on commit 8187ffa

Please sign in to comment.