[ruby/irb] show_doc command should take non-string argument too

(https://github.com/ruby/irb/pull/478)

Given that `show_doc` already supports syntax like `String#gsub`, it
should be able to take it in non-string form too, like `edit` and
`show_source` do. This ensures users can have a consistent syntax on
argument between different commands.
This commit is contained in:
Stan Lo 2022-12-12 17:35:43 +00:00 committed by git
parent ece6246057
commit 223d4448c8
5 changed files with 20 additions and 14 deletions

View File

@ -18,13 +18,6 @@ module IRB
args.strip.dump
end
end
private
def string_literal?(args)
sexp = Ripper.sexp(args)
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
end
end
def execute(*args)

View File

@ -16,6 +16,17 @@ module IRB
module ExtendCommand
class Help < Nop
class << self
def transform_args(args)
# Return a string literal as is for backward compatibility
if args.empty? || string_literal?(args)
args
else # Otherwise, consider the input as a String for convenience
args.strip.dump
end
end
end
category "Context"
description "Enter the mode to look up RI documents."

View File

@ -26,6 +26,13 @@ module IRB
@description = description if description
@description
end
private
def string_literal?(args)
sexp = Ripper.sexp(args)
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
end
end
if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0"

View File

@ -65,11 +65,6 @@ module IRB
end
first_line
end
def string_literal?(args)
sexp = Ripper.sexp(args)
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
end
end
def execute(str = nil)

View File

@ -379,13 +379,13 @@ module TestIRB
def test_help_and_show_doc
["help", "show_doc"].each do |cmd|
out, _ = execute_lines(
"#{cmd} 'String#gsub'\n",
"#{cmd} String#gsub\n",
"\n",
)
# the former is what we'd get without document content installed, like on CI
# the latter is what we may get locally
possible_rdoc_output = [/Nothing known about String#gsub/, /Returns a copy of self with all occurrences of the given pattern/]
possible_rdoc_output = [/Nothing known about String#gsub/, /str.gsub\(pattern\)/]
assert(possible_rdoc_output.any? { |output| output.match?(out) }, "Expect the `#{cmd}` command to match one of the possible outputs")
end
ensure