From 97c7c6e5686cfb777126810a1da1baaeb83bd2ea Mon Sep 17 00:00:00 2001 From: tompng Date: Wed, 18 Sep 2024 00:29:54 +0900 Subject: [PATCH 1/2] Fix debug command in nomultiline mode --- lib/irb/context.rb | 15 +++++++++++++++ lib/irb/debug.rb | 6 ++---- lib/irb/input-method.rb | 21 +++------------------ test/irb/yamatanooroti/test_rendering.rb | 22 ++++++++++++++++++++++ 4 files changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/irb/context.rb b/lib/irb/context.rb index fafe99d57..886c34f7e 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -649,6 +649,21 @@ def parse_command(code) end end + def colorize_code(input, complete:) + if IRB.conf[:USE_COLORIZE] && IRB::Color.colorable? + lvars = local_variables || [] + if parse_command(input) + name, sep, arg = input.split(/(\s+)/, 2) + arg = IRB::Color.colorize_code(arg, complete: complete, local_variables: lvars) + "#{IRB::Color.colorize(name, [:BOLD])}\e[m#{sep}#{arg}" + else + IRB::Color.colorize_code(input, complete: complete, local_variables: lvars) + end + else + Reline::Unicode.escape_for_print(input) + end + end + def inspect_last_value # :nodoc: @inspect_method.inspect_value(@last_value) end diff --git a/lib/irb/debug.rb b/lib/irb/debug.rb index 1ec2335a8..28aa7510f 100644 --- a/lib/irb/debug.rb +++ b/lib/irb/debug.rb @@ -57,9 +57,7 @@ def DEBUGGER__.capture_frames(*args) DEBUGGER__::ThreadClient.prepend(SkipPathHelperForIRB) end - if !@output_modifier_defined && !DEBUGGER__::CONFIG[:no_hint] - irb_output_modifier_proc = Reline.output_modifier_proc - + if !@output_modifier_defined && !DEBUGGER__::CONFIG[:no_hint] && irb.context.io.is_a?(RelineInputMethod) Reline.output_modifier_proc = proc do |output, complete:| unless output.strip.empty? cmd = output.split(/\s/, 2).first @@ -69,7 +67,7 @@ def DEBUGGER__.capture_frames(*args) end end - irb_output_modifier_proc.call(output, complete: complete) + IRB.CurrentContext.colorize_code(output, complete: complete) end @output_modifier_defined = true diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index 82c1e7329..ece0ab2cb 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -265,24 +265,9 @@ def initialize(completor) @completion_params = [preposing, target, postposing, bind] @completor.completion_candidates(preposing, target, postposing, bind: bind) } - Reline.output_modifier_proc = - if IRB.conf[:USE_COLORIZE] - proc do |output, complete: | - next unless IRB::Color.colorable? - lvars = IRB.CurrentContext&.local_variables || [] - if IRB.CurrentContext&.parse_command(output) - name, sep, arg = output.split(/(\s+)/, 2) - arg = IRB::Color.colorize_code(arg, complete: complete, local_variables: lvars) - "#{IRB::Color.colorize(name, [:BOLD])}\e[m#{sep}#{arg}" - else - IRB::Color.colorize_code(output, complete: complete, local_variables: lvars) - end - end - else - proc do |output| - Reline::Unicode.escape_for_print(output) - end - end + Reline.output_modifier_proc = proc do |output, complete:| + IRB.CurrentContext.colorize_code(output, complete: complete) + end Reline.dig_perfect_match_proc = ->(matched) { display_document(matched) } Reline.autocompletion = IRB.conf[:USE_AUTOCOMPLETE] diff --git a/test/irb/yamatanooroti/test_rendering.rb b/test/irb/yamatanooroti/test_rendering.rb index 44e07a3a1..834c501d5 100644 --- a/test/irb/yamatanooroti/test_rendering.rb +++ b/test/irb/yamatanooroti/test_rendering.rb @@ -507,6 +507,28 @@ def test_debug_integration_doesnt_hint_non_debugger_commands File.unlink(script) if script end + def test_debug_integration_doesnt_hint_debugger_commands_in_nomultiline_mode + write_irbrc <<~'LINES' + IRB.conf[:USE_SINGLELINE] = true + LINES + script = Tempfile.create(["debug", ".rb"]) + script.write <<~RUBY + puts 'start IRB' + binding.irb + RUBY + script.close + start_terminal(40, 80, %W{ruby -I#{@pwd}/lib #{script.to_path}}, startup_message: 'start IRB') + write("debug\n") + write("pp 1") + close + + screen = result.join("\n").sub(/\n*\z/, "\n") + # submitted input shouldn't contain hint + assert_include(screen, "irb:rdbg(main):002> pp 1\n") + ensure + File.unlink(script) if script + end + private def write_irbrc(content) From ee267e79c7fe06f18383736725d4d0ccffc0833f Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 19 Sep 2024 02:28:35 +0900 Subject: [PATCH 2/2] context.colorize_code -> context.colorize_input --- lib/irb/context.rb | 2 +- lib/irb/debug.rb | 14 ++++++-------- lib/irb/input-method.rb | 4 ++-- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 886c34f7e..668a823f5 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -649,7 +649,7 @@ def parse_command(code) end end - def colorize_code(input, complete:) + def colorize_input(input, complete:) if IRB.conf[:USE_COLORIZE] && IRB::Color.colorable? lvars = local_variables || [] if parse_command(input) diff --git a/lib/irb/debug.rb b/lib/irb/debug.rb index 28aa7510f..cd64b77ad 100644 --- a/lib/irb/debug.rb +++ b/lib/irb/debug.rb @@ -57,20 +57,18 @@ def DEBUGGER__.capture_frames(*args) DEBUGGER__::ThreadClient.prepend(SkipPathHelperForIRB) end - if !@output_modifier_defined && !DEBUGGER__::CONFIG[:no_hint] && irb.context.io.is_a?(RelineInputMethod) - Reline.output_modifier_proc = proc do |output, complete:| - unless output.strip.empty? - cmd = output.split(/\s/, 2).first + if !DEBUGGER__::CONFIG[:no_hint] && irb.context.io.is_a?(RelineInputMethod) + Reline.output_modifier_proc = proc do |input, complete:| + unless input.strip.empty? + cmd = input.split(/\s/, 2).first if !complete && DEBUGGER__.commands.key?(cmd) - output = output.sub(/\n$/, " # debug command\n") + input = input.sub(/\n$/, " # debug command\n") end end - IRB.CurrentContext.colorize_code(output, complete: complete) + irb.context.colorize_input(input, complete: complete) end - - @output_modifier_defined = true end true diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index ece0ab2cb..210d3da78 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -265,8 +265,8 @@ def initialize(completor) @completion_params = [preposing, target, postposing, bind] @completor.completion_candidates(preposing, target, postposing, bind: bind) } - Reline.output_modifier_proc = proc do |output, complete:| - IRB.CurrentContext.colorize_code(output, complete: complete) + Reline.output_modifier_proc = proc do |input, complete:| + IRB.CurrentContext.colorize_input(input, complete: complete) end Reline.dig_perfect_match_proc = ->(matched) { display_document(matched) } Reline.autocompletion = IRB.conf[:USE_AUTOCOMPLETE]