pry 0.9.8pre3-i386-mswin32 → 0.9.8pre5-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/pry/cli.rb CHANGED
@@ -14,7 +14,7 @@ class Pry
14
14
  # @return [Array] The Procs that process the parsed options.
15
15
  attr_accessor :option_processors
16
16
 
17
- # Add another set of CLI options
17
+ # Add another set of CLI options (a Slop block)
18
18
  def add_options(&block)
19
19
  if options
20
20
  old_options = options
data/lib/pry/command.rb CHANGED
@@ -14,12 +14,34 @@ class Pry
14
14
  # Properties of the command itself (as passed as arguments to
15
15
  # {CommandSet#command} or {CommandSet#command_class}).
16
16
  class << self
17
+ attr_accessor :block
17
18
  attr_accessor :name
18
19
  attr_accessor :description
19
- attr_accessor :options
20
- attr_accessor :block
20
+ attr_accessor :command_options
21
+
22
+ # Define or get the command's description
23
+ def description(arg=nil)
24
+ @description = arg if arg
25
+ @description
26
+ end
27
+
28
+ # Define or get the command's options
29
+ def command_options(arg=nil)
30
+ @command_options = arg if arg
31
+ @command_options
32
+ end
33
+ # backward compatibility
34
+ alias_method :options, :command_options
35
+ alias_method :options=, :command_options=
36
+
37
+ # Define or get the command's banner
38
+ def banner(arg=nil)
39
+ @banner = arg if arg
40
+ @banner || description
41
+ end
21
42
  end
22
43
 
44
+
23
45
  # Make those properties accessible to instances
24
46
  def name; self.class.name; end
25
47
  def description; self.class.description; end
@@ -47,7 +69,7 @@ class Pry
47
69
  klass.send(:include, helpers)
48
70
  klass.name = name
49
71
  klass.description = description
50
- klass.options = options
72
+ klass.command_options = options
51
73
  klass.block = block
52
74
  klass
53
75
  end
@@ -276,10 +298,19 @@ class Pry
276
298
  # backwards compatibility
277
299
  alias_method :opts, :context
278
300
 
301
+ # Call the block that was registered with this command.
302
+ #
303
+ # @param *String the arguments passed
304
+ # @return Object the return value of the block
279
305
  def call(*args)
280
306
  instance_exec(*correct_arg_arity(block.arity, args), &block)
281
307
  end
282
308
 
309
+ # Fix the number of arguments we pass to a block to avoid arity warnings.
310
+ #
311
+ # @param Number the arity of the block
312
+ # @param Array the arguments to pass
313
+ # @return Array a (possibly shorter) array of the arguments to pass
283
314
  def correct_arg_arity(arity, args)
284
315
  case
285
316
  when arity < 0
@@ -304,12 +335,6 @@ class Pry
304
335
  # necessary, you can also override {setup} which will be called before {options}, for example to
305
336
  # require any gems your command needs to run, or to set up state.
306
337
  class ClassCommand < Command
307
- class << self
308
- def banner(arg=nil)
309
- @banner = arg if arg
310
- @banner || description
311
- end
312
- end
313
338
 
314
339
  attr_accessor :opts
315
340
  attr_accessor :args
@@ -81,11 +81,13 @@ class Pry
81
81
  # # hello john, nice number: 10
82
82
  # # pry(main)> help number
83
83
  # # number-N regex command
84
- def command(name, description="No description.", options={}, &block)
84
+ def block_command(name, description="No description.", options={}, &block)
85
+ description, options = ["No description.", description] if description.is_a?(Hash)
85
86
  options = default_options(name).merge!(options)
86
87
 
87
88
  commands[name] = Pry::BlockCommand.subclass(name, description, options, helper_module, &block)
88
89
  end
90
+ alias_method :command, :block_command
89
91
 
90
92
  # Defines a new Pry command class.
91
93
  #
@@ -96,7 +98,7 @@ class Pry
96
98
  # @param &Block The class body's definition.
97
99
  #
98
100
  # @example
99
- # Pry::Commands.command_class "echo", "echo's the input", :shellwords => false do
101
+ # Pry::Commands.create_command "echo", "echo's the input", :shellwords => false do
100
102
  # def options(opt)
101
103
  # opt.banner "Usage: echo [-u | -d] <string to echo>"
102
104
  # opt.on :u, :upcase, "ensure the output is all upper-case"
@@ -112,13 +114,15 @@ class Pry
112
114
  # end
113
115
  # end
114
116
  #
115
- def command_class(name, description="No description.", options={}, &block)
117
+ def create_command(name, description="No description.", options={}, &block)
118
+ description, options = ["No description.", description] if description.is_a?(Hash)
116
119
  options = default_options(name).merge!(options)
117
120
 
118
121
  commands[name] = Pry::ClassCommand.subclass(name, description, options, helper_module, &block)
119
122
  commands[name].class_eval(&block)
120
123
  commands[name]
121
124
  end
125
+ alias_method :command_class, :create_command
122
126
 
123
127
  # Execute a block of code before a command is invoked. The block also
124
128
  # gets access to parameters that will be passed to the command and
@@ -334,7 +338,7 @@ class Pry
334
338
 
335
339
  help_text << commands.map do |key, command|
336
340
  if command.description && !command.description.empty?
337
- "#{command.options[:listing]}".ljust(18) + command.description
341
+ "#{command.options[:listing].to_s.ljust(18)} #{command.description}"
338
342
  end
339
343
  end.compact.sort.join("\n")
340
344
 
@@ -24,10 +24,7 @@ class Pry
24
24
  when "."
25
25
  next
26
26
  when ".."
27
- if stack.one?
28
- _pry_.binding_stack.clear
29
- throw(:breakout)
30
- else
27
+ unless stack.size == 1
31
28
  stack.pop
32
29
  end
33
30
  else
@@ -97,18 +97,24 @@ USAGE
97
97
  opt.on :l, :lines, "Only gist a subset of lines (only works with -m and -f)", :optional => true, :as => Range, :default => 1..-1
98
98
  opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional => true,
99
99
  :as => Range, :default => -5..-1 do |range|
100
+ self.input_ranges ||= []
100
101
  input_ranges << absolute_index_range(range, _pry_.input_array.length)
101
102
  end
102
103
  end
103
104
 
104
105
  def process
106
+ self.content = ""
107
+
105
108
  if opts.present?(:in)
106
109
  in_option
107
- elsif opts.present?(:file)
110
+ end
111
+ if opts.present?(:file)
108
112
  file_option
109
- elsif opts.present?(:doc)
113
+ end
114
+ if opts.present?(:doc)
110
115
  doc_option
111
- elsif opts.present?(:method)
116
+ end
117
+ if opts.present?(:method)
112
118
  method_option
113
119
  end
114
120
 
@@ -117,7 +123,6 @@ USAGE
117
123
 
118
124
  def in_option
119
125
  self.code_type = :ruby
120
- self.content = ""
121
126
 
122
127
  input_ranges.each do |range|
123
128
  input_expressions = _pry_.input_array[range] || []
@@ -136,19 +141,19 @@ USAGE
136
141
  def file_option
137
142
  whole_file = File.read(File.expand_path(opts[:f]))
138
143
  if opts.present?(:lines)
139
- self.content = restrict_to_lines(whole_file, opts[:l])
144
+ self.content << restrict_to_lines(whole_file, opts[:l])
140
145
  else
141
- self.content = whole_file
146
+ self.content << whole_file
142
147
  end
143
148
  end
144
149
 
145
150
  def doc_option
146
151
  meth = get_method_or_raise(opts[:d], target, {})
147
- self.content = meth.doc
152
+ self.content << meth.doc
148
153
  self.code_type = meth.source_type
149
154
 
150
155
  text.no_color do
151
- self.content = process_comment_markup(self.content, self.code_type)
156
+ self.content << process_comment_markup(self.content, self.code_type)
152
157
  end
153
158
  self.code_type = :plain
154
159
  end
@@ -157,9 +162,9 @@ USAGE
157
162
  meth = get_method_or_raise(opts[:m], target, {})
158
163
  method_source = meth.source
159
164
  if opts.present?(:lines)
160
- self.content = restrict_to_lines(method_source, opts[:l])
165
+ self.content << restrict_to_lines(method_source, opts[:l])
161
166
  else
162
- self.content = method_source
167
+ self.content << method_source
163
168
  end
164
169
 
165
170
  self.code_type = meth.source_type
@@ -9,7 +9,7 @@ class Pry
9
9
  end
10
10
 
11
11
  command "show-input", "Show the contents of the input buffer for the current multi-line expression." do
12
- render_output(false, 1, Pry.color ? CodeRay.scan(eval_string, :ruby).term : eval_string)
12
+ render_output(false, 1, colorize_code(eval_string))
13
13
  end
14
14
 
15
15
  command(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/, "Amend a line of input in multi-line mode. Type `amend-line --help` for more information. Aliases %",
@@ -19,45 +19,102 @@ class Pry
19
19
 
20
20
  end
21
21
 
22
- command "reload-command", "Reload a command. reload-command CMD_NAME CMD_SET" do |command_name, set_name|
23
- if command_name.nil?
24
- raise CommandError, "Must provide command name"
25
- end
22
+ command_class "reload-command", "Reload a Pry command." do
23
+ banner <<-BANNER
24
+ Usage: reload-command command
25
+ Reload a Pry command.
26
+ BANNER
26
27
 
27
- if set_name.nil?
28
- raise CommandError, "Must provide command set name"
29
- end
28
+ def process
29
+ command = _pry_.commands.find_command(args.first)
30
+
31
+ if command.nil?
32
+ raise Pry::CommandError, 'No command found.'
33
+ end
30
34
 
31
- cmd = Pry.config.commands.commands[command_name]
32
- file_name = cmd.block.source_location.first
35
+ source_code = command.block.source
36
+ file, lineno = command.block.source_location
33
37
 
34
- silence_warnings do
35
- load file_name
38
+ set = Pry::CommandSet.new do
39
+ eval(source_code, binding, file, lineno)
40
+ end
41
+
42
+ _pry_.commands.delete(command.name)
43
+ _pry_.commands.import(set)
36
44
  end
37
- Pry.config.commands.import target.eval(set_name)
38
- _pry_.commands.import target.eval(set_name)
39
- set_file_and_dir_locals(file_name)
40
45
  end
41
46
 
42
- command "edit-command", "Edit a command. edit-command CMD_NAME CMD_SET" do |command_name, set_name|
43
- if command_name.nil?
44
- raise CommandError, "Must provide command name"
47
+ command_class "edit-command", "Edit a Pry command." do
48
+ banner <<-BANNER
49
+ Usage: edit-command [options] command
50
+ Edit a Pry command.
51
+ BANNER
52
+
53
+ def initialize env
54
+ @pry = env[:pry_instance]
55
+ @command = nil
56
+ super(env)
45
57
  end
46
58
 
47
- if set_name.nil?
48
- raise CommandError, "Must provide command set name"
59
+ def options(opt)
60
+ opt.on :p, :patch, 'Perform a in-memory edit of a command'
61
+ end
62
+
63
+ def process
64
+ @command = @pry.commands.find_command(args.first)
65
+
66
+ if @command.nil?
67
+ raise Pry::CommandError, 'Command not found.'
68
+ end
69
+
70
+ case
71
+ when opts.present?(:patch)
72
+ edit_temporarily
73
+ else
74
+ edit_permanently
75
+ end
49
76
  end
50
77
 
51
- cmd = Pry.config.commands.commands[command_name]
52
- file_name = cmd.block.source_location.first
78
+ def edit_permanently
79
+ file, lineno = @command.block.source_location
80
+ invoke_editor(file, lineno)
81
+
82
+ command_set = silence_warnings do
83
+ eval File.read(file), TOPLEVEL_BINDING, file, 1
84
+ end
85
+
86
+ unless command_set.is_a?(Pry::CommandSet)
87
+ raise Pry::CommandError,
88
+ "Expected file '#{file}' to return a CommandSet"
89
+ end
90
+
91
+ @pry.commands.delete(@command.name)
92
+ @pry.commands.import(command_set)
93
+ set_file_and_dir_locals(file)
94
+ end
95
+
96
+ def edit_temporarily
97
+ source_code = Pry::Method(@command.block).source
98
+ modified_code = nil
99
+
100
+ temp_file do |f|
101
+ f.write(source_code)
102
+ f.flush
103
+
104
+ invoke_editor(f.path, 1)
105
+ modified_code = File.read(f.path)
106
+ end
107
+
108
+ command_set = CommandSet.new do
109
+ silence_warnings do
110
+ pry = Pry.new :input => StringIO.new(modified_code)
111
+ pry.rep(binding)
112
+ end
113
+ end
53
114
 
54
- invoke_editor(*cmd.block.source_location)
55
- silence_warnings do
56
- load file_name
115
+ @pry.commands.delete(@command.name)
116
+ @pry.commands.import(command_set)
57
117
  end
58
- Pry.config.commands.import target.eval(set_name)
59
- _pry_.commands.import target.eval(set_name)
60
- set_file_and_dir_locals(file_name)
61
118
  end
62
119
 
63
120
  end
data/lib/pry/hooks.rb CHANGED
@@ -5,6 +5,58 @@ class Pry
5
5
  @hooks = {}
6
6
  end
7
7
 
8
+ # Ensure that duplicates have their @hooks object
9
+ def initialize_copy(orig)
10
+ hooks_dup = @hooks.dup
11
+ @hooks.each do |k, v|
12
+ hooks_dup[k] = v.dup
13
+ end
14
+
15
+ @hooks = hooks_dup
16
+ end
17
+
18
+ def hooks
19
+ @hooks
20
+ end
21
+ protected :hooks
22
+
23
+ # Destructively merge the contents of two `Pry:Hooks` instances.
24
+ # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
25
+
26
+
27
+
28
+ # TODO: implement by iterating over parameter and only overwriting
29
+ # elements in receiver if they exist in parameter, and adding
30
+ # other paramater elements to the end of the original's array
31
+ def merge!(other)
32
+ @hooks.merge!(other.dup.hooks) do |key, v1, v2|
33
+ merge_arrays(v1, v2)
34
+ end
35
+
36
+ self
37
+ end
38
+
39
+ def merge_arrays(array1, array2)
40
+ uniq_keeping_last(array1 + array2, &:first)
41
+ end
42
+ private :merge_arrays
43
+
44
+ def uniq_keeping_last(input, &block)
45
+ hash, output = {}, []
46
+ input.reverse.each{ |i| hash[block[i]] ||= (output.unshift i) }
47
+ output
48
+ end
49
+ private :uniq_keeping_last
50
+
51
+ # Return a new `Pry::Hooks` instance containing a merge of the contents of two `Pry:Hooks` instances,
52
+ # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
53
+ # @return [Pry::Hooks] The new hash.
54
+ def merge(other)
55
+ self.dup.tap do |v|
56
+ v.merge!(other)
57
+ end
58
+ end
59
+
8
60
  # Add a new hook to be executed for the `name` even.
9
61
  # @param [Symbol] event_name The name of the event.
10
62
  # @param [Symbol] hook_name The name of the hook.
@@ -91,10 +143,12 @@ class Pry
91
143
 
92
144
  # Clear all hooks functions for a given event.
93
145
  # @param [String] event_name The name of the event.
94
- def clear(event_name)
146
+ def delete_hooks(event_name)
95
147
  @hooks[event_name] = []
96
148
  end
97
149
 
150
+ alias_method :clear, :delete_hooks
151
+
98
152
  # @param [Symbol] event_name Name of the event.
99
153
  # @param [Symbol] hook_name Name of the hook.
100
154
  # @return [Boolean] Whether the hook by the name `hook_name`
data/lib/pry/method.rb CHANGED
@@ -1,5 +1,17 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  class Pry
3
+ class << self
4
+ # If the given object is a `Pry::Method`, return it unaltered. If it's
5
+ # anything else, return it wrapped in a `Pry::Method` instance.
6
+ def Method(obj)
7
+ if obj.is_a? Pry::Method
8
+ obj
9
+ else
10
+ Pry::Method.new(obj)
11
+ end
12
+ end
13
+ end
14
+
3
15
  class Method
4
16
  include RbxMethod if Helpers::BaseHelpers.rbx?
5
17