aboutsummaryrefslogtreecommitdiffstats
path: root/lib/irb
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
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')
-rw-r--r--lib/irb/color.rb10
-rw-r--r--lib/irb/input-method.rb4
2 files changed, 13 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
diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb
index 412edcce24..bc144dca9c 100644
--- a/lib/irb/input-method.rb
+++ b/lib/irb/input-method.rb
@@ -222,6 +222,10 @@ module IRB
end
Reline.completion_append_character = nil
Reline.completion_proc = IRB::InputCompletor::CompletionProc
+ Reline.output_modifier_proc = proc do |output|
+ next unless IRB::Color.colorable?
+ IRB::Color.colorize_code(output)
+ end
Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
end