ruby-changes:55926
From: Takashi <ko1@a...>
Date: Fri, 31 May 2019 06:23:30 +0900 (JST)
Subject: [ruby-changes:55926] Takashi Kokubun: 6e052817f9 (trunk): Abstract away Ripper::Lexer#scan in IRB::Color#scan
https://git.ruby-lang.org/ruby.git/commit/?id=6e052817f9 From 6e052817f95095217b67256aff48cedbd57717cf Mon Sep 17 00:00:00 2001 From: Takashi Kokubun <takashikkbn@g...> Date: Fri, 31 May 2019 06:17:56 +0900 Subject: 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. diff --git a/lib/irb/color.rb b/lib/irb/color.rb index 0dbb16b..41b559b 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -98,31 +98,16 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L98 "#{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: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L127 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] -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/