diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2019-05-31 06:17:56 +0900 |
---|---|---|
committer | Takashi Kokubun <takashikkbn@gmail.com> | 2019-05-31 06:21:17 +0900 |
commit | 6e052817f95095217b67256aff48cedbd57717cf (patch) | |
tree | 0489ac42b140213ae47ad749cda6d67b3bbf0bcd /lib/irb | |
parent | 8f83fe3b02f6689cd1ea85ec59c6fad12066f301 (diff) | |
download | ruby-6e052817f95095217b67256aff48cedbd57717cf.tar.gz |
Abstract away Ripper::Lexer#scan in IRB::Color#scan
because 5b64d7ac6e7cbf759b859428f125539e58bac0bd made it hard to
understand #colorize_code for me and this change is needed for my next
commit.
Diffstat (limited to 'lib/irb')
-rw-r--r-- | lib/irb/color.rb | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb index 0dbb16b60b..41b559bc6b 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -98,31 +98,16 @@ module IRB # :nodoc: "#{seq}#{text}#{clear}" end - def scan(code) - Ripper::Lexer.new(code).scan - end - def colorize_code(code) return code unless colorable? symbol_state = SymbolState.new colored = +'' length = 0 - pos = [1, 0] - scan(code).each do |elem| - token = elem.event - str = elem.tok - expr = elem.state + scan(code) do |token, str, expr| in_symbol = symbol_state.scan_token(token) - 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('') @@ -142,6 +127,26 @@ module IRB # :nodoc: private + def scan(code) + pos = [1, 0] + + Ripper::Lexer.new(code).scan.each do |elem| + str = elem.tok + 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 + end + + yield(elem.event, str, elem.state) + end + end + def dispatch_seq(token, expr, str, in_symbol:) if token == :on_parse_error or token == :compile_error TOKEN_SEQ_EXPRS[token][0] |