Skip to content

Commit

Permalink
Rewrite on_scan proc to be more readable.
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Sep 19, 2022
1 parent 52446eb commit da54e7f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/irb/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,18 @@ def scan(code, allow_last_error:)
end

on_scan = proc do |elem|
str = elem.tok
start_pos = line_positions[elem.pos[0] - 1] + elem.pos[1]
end_pos = start_pos + str.bytesize
next if start_pos < byte_pos

yield(nil, inner_code.byteslice(byte_pos...start_pos), nil) if byte_pos < start_pos
yield(elem.event, str, elem.state)
byte_pos = end_pos
# yield uncolorable code
if byte_pos < start_pos
yield(nil, inner_code.byteslice(byte_pos...start_pos), nil)
end

if byte_pos <= start_pos
str = elem.tok
yield(elem.event, str, elem.state)
byte_pos = start_pos + str.bytesize
end
end

if lexer.respond_to?(:scan) # Ruby 2.7+
Expand Down

0 comments on commit da54e7f

Please sign in to comment.