Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix debug command in nomultiline mode #1006

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,21 @@ def parse_command(code)
end
end

def colorize_code(input, complete:)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel colorize_input express the intent better and is less likely to be confused with Color.colorize_code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks better, but I have one concern. colorize_input colorizes output. I think it is an input, but Reline's api calls it output.
Do you think the code below is acceptable? IMO... maybe yes?

Reline.output_modifier_proc = proc do |output, complete:|
  IRB.CurrentContext.colorize_input(output, complete: complete)
end

Copy link
Member

@st0012 st0012 Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In IRB's context, it is input indeed. And in Reline's context, I think calling it both input or output are technically correct? Though since there's no input_modifier_proc, I feel it'd be a better name for the library user's perspective.

In this case, I feel a comment to clarify it would be enough, and maybe rename the output variable to input?

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
Expand Down
6 changes: 2 additions & 4 deletions lib/irb/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
tompng marked this conversation as resolved.
Show resolved Hide resolved
end

@output_modifier_defined = true
Expand Down
21 changes: 3 additions & 18 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
22 changes: 22 additions & 0 deletions test/irb/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading