aboutsummaryrefslogtreecommitdiffstats
path: root/lib/irb/color.rb
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2019-05-24 21:21:22 -0700
committerTakashi Kokubun <takashikkbn@gmail.com>2019-05-24 23:54:52 -0700
commitb83119be9e9a8611063142541993e4823a025622 (patch)
tree8bb0e47325d65d75876c1ee7aad35666dc9d3f4e /lib/irb/color.rb
parent3c6e1a8cf911d312edeb9dfcc9153be68867ca4f (diff)
downloadruby-b83119be9e9a8611063142541993e4823a025622.tar.gz
Incremental syntax highlight for IRB source lines
Closes: https://github.com/ruby/ruby/pull/2202
Diffstat (limited to 'lib/irb/color.rb')
-rw-r--r--lib/irb/color.rb10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index eb95da8229..30a8fb5973 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -77,13 +77,21 @@ module IRB # :nodoc:
return code unless colorable?
colored = +''
+ length = 0
Ripper.lex(code).each do |(_line, _col), token, str, expr|
if seq = dispatch_seq(token, expr, str)
- colored << "#{seq.map { |s| "\e[#{s}m" }.join('')}#{str}#{clear}"
+ str.each_line do |line|
+ colored << "#{seq.map { |s| "\e[#{s}m" }.join('')}#{line}#{clear}"
+ end
else
colored << str
end
+ length += str.length
end
+
+ # give up colorizing incomplete Ripper tokens
+ return code if length != code.length
+
colored
end