Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Mar 19, 2023
1 parent 92086b3 commit d479524
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 25 deletions.
11 changes: 0 additions & 11 deletions lib/irb/cmd/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ 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."

Expand Down
4 changes: 3 additions & 1 deletion lib/irb/cmd/irb_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IrbInfo < Nop
description "Show information about IRB."

def execute
Class.new {
c = Class.new {
def inspect
str = "Ruby version: #{RUBY_VERSION}\n"
str += "IRB version: #{IRB.version}\n"
Expand All @@ -29,6 +29,8 @@ def inspect
end
alias_method :to_s, :inspect
}.new
puts c
c
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/irb/cmd/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ def self.transform_args(args)
end
end

def execute_with_raw_args(raw_args)
raw_args = raw_args.strip

if raw_args.empty?
execute
else
if match = raw_args.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\z/)
args = match[:args]

if !args.empty?
execute(evaluate(args), grep: /#{match[:grep]}/)
else
execute(grep: /#{match[:grep]}/)
end
else
execute(evaluate(raw_args))
end
end
end

def execute(*arg, grep: nil)
o = Output.new(grep: grep)

Expand Down
35 changes: 35 additions & 0 deletions lib/irb/cmd/nop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@ def irb
def execute(*opts)
#nop
end

def transform_args(raw_args)
if string_literal?(raw_args)
evaluate(raw_args)
else
raw_args
end
end

def execute_with_raw_args(raw_args)
if raw_args.nil? || raw_args.empty?
execute
else
raw_args = raw_args.strip

args =
if respond_to?(:transform_args)
transform_args(raw_args)
else
evaluate(raw_args)
end
execute(args)
end
end

private

def string_literal?(args)
sexp = Ripper.sexp(args)
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
end

def evaluate(str)
eval(str, @irb_context.workspace.binding)
end
end
end

Expand Down
9 changes: 0 additions & 9 deletions lib/irb/cmd/show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ class ShowSource < Nop
description "Show the source code of a given method or constant."

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

def find_source(str, irb_context)
case str
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name
Expand Down
9 changes: 5 additions & 4 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,12 @@ def evaluate(line, line_no, exception: nil) # :nodoc:

# Hook command-specific transformation
command_class = ExtendCommandBundle.load_command(command)
if command_class&.respond_to?(:transform_args)
line = "#{command} #{command_class.transform_args(args)}"
end

set_last_value(@workspace.evaluate(line, irb_path, line_no))
if command_class
command_class.new(self).execute_with_raw_args(args)
else
set_last_value(@workspace.evaluate(line, irb_path, line_no))
end
end

def inspect_last_value # :nodoc:
Expand Down

0 comments on commit d479524

Please sign in to comment.