Skip to content

Commit 98ef298

Browse files
committed
WIP
1 parent 212e8b7 commit 98ef298

File tree

7 files changed

+68
-15
lines changed

7 files changed

+68
-15
lines changed

lib/irb/cmd/chws.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class ChangeWorkspace < Nop
2626
description "Change the current workspace to an object."
2727

2828
def execute(*obj)
29-
irb_context.change_workspace(*obj)
29+
objs = obj.map { |o| irb_context.workspace.binding.eval(o)}
30+
irb_context.change_workspace(*objs)
3031
irb_context.main
3132
end
3233
end

lib/irb/cmd/ls.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ def self.transform_args(args)
2121
end
2222
end
2323

24+
def execute_with_raw_args(raw_args)
25+
raw_args = raw_args.strip
26+
27+
if raw_args.empty?
28+
execute
29+
else
30+
if match = raw_args.match(/\A(?<args>.+\s|)(-g|-G)\s+(?<grep>[^\s]+)\s*\z/)
31+
args = match[:args]
32+
33+
if !args.empty?
34+
execute(evaluate(args), grep: /#{match[:grep]}/)
35+
else
36+
execute(grep: /#{match[:grep]}/)
37+
end
38+
else
39+
execute(evaluate(raw_args))
40+
end
41+
end
42+
end
43+
2444
def execute(*arg, grep: nil)
2545
o = Output.new(grep: grep)
2646

lib/irb/cmd/measure.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def initialize(*args)
1313
end
1414

1515
def execute(type = nil, arg = nil, &block)
16+
if type
17+
type = type.sub(/\A:/, '').to_sym
18+
end
1619
# Please check IRB.init_config in lib/irb/init.rb that sets
1720
# IRB.conf[:MEASURE_PROC] to register default "measure" methods,
1821
# "measure :time" (abbreviated as "measure") and "measure :stackprof".

lib/irb/cmd/nop.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ def initialize(irb_context)
4646
def execute(*opts)
4747
#nop
4848
end
49+
50+
def transform_args(raw_args)
51+
if string_literal?(raw_args)
52+
evaluate(raw_args)
53+
else
54+
raw_args
55+
end
56+
end
57+
58+
def execute_with_raw_args(raw_args)
59+
if raw_args.nil? || raw_args.empty?
60+
execute
61+
else
62+
raw_args = raw_args.strip
63+
64+
args =
65+
if respond_to?(:transform_args)
66+
transform_args(raw_args)
67+
else
68+
evaluate(raw_args)
69+
end
70+
execute(args)
71+
end
72+
end
73+
74+
private
75+
76+
def string_literal?(args)
77+
sexp = Ripper.sexp(args)
78+
sexp && sexp.size == 2 && sexp.last&.first&.first == :string_literal
79+
end
80+
81+
def evaluate(str)
82+
eval(str, @irb_context.workspace.binding)
83+
end
4984
end
5085
end
5186

lib/irb/cmd/pushws.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class PushWorkspace < Workspaces
2525
description "Push an object to the workspace stack."
2626

2727
def execute(*obj)
28-
irb_context.push_workspace(*obj)
28+
objs = obj.map { |o| irb_context.workspace.binding.eval(o)}
29+
irb_context.push_workspace(*objs)
2930
super
3031
end
3132
end

lib/irb/cmd/show_source.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ class ShowSource < Nop
1313
description "Show the source code of a given method or constant."
1414

1515
class << self
16-
def transform_args(args)
17-
# Return a string literal as is for backward compatibility
18-
if args.empty? || string_literal?(args)
19-
args
20-
else # Otherwise, consider the input as a String for convenience
21-
args.strip.dump
22-
end
23-
end
24-
2516
def find_source(str, irb_context)
2617
case str
2718
when /\A[A-Z]\w*(::[A-Z]\w*)*\z/ # Const::Name

lib/irb/context.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,13 @@ def evaluate(line, line_no, exception: nil) # :nodoc:
490490

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

497-
set_last_value(@workspace.evaluate(line, irb_path, line_no))
494+
if command_class
495+
command_class.new(self).execute_with_raw_args(args)
496+
set_last_value(nil)
497+
else
498+
set_last_value(@workspace.evaluate(line, irb_path, line_no))
499+
end
498500
end
499501

500502
def inspect_last_value # :nodoc:

0 commit comments

Comments
 (0)