pry 0.7.7.2-java → 0.8.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ class Pry
6
6
 
7
7
  # Implements tab completion for Readline in Pry
8
8
  module InputCompleter
9
-
9
+
10
10
  if Readline.respond_to?("basic_word_break_characters=")
11
11
  Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
12
12
  end
@@ -0,0 +1,6 @@
1
+ class Pry
2
+
3
+ # This proc will be instance_eval's against the active Pry instance
4
+ DEFAULT_CUSTOM_COMPLETIONS = proc { commands.commands.keys }
5
+ FILE_COMPLETIONS = proc { commands.commands.keys + Dir.entries('.') }
6
+ end
@@ -2,20 +2,16 @@ class Pry
2
2
 
3
3
  # The default hooks - display messages when beginning and ending Pry sessions.
4
4
  DEFAULT_HOOKS = {
5
-
6
- :before_session => proc do |out, target|
7
- out.puts "Beginning Pry session for #{Pry.view_clip(target.eval('self'))}"
8
5
 
6
+ :before_session => proc do |out, target|
9
7
  # ensure we're actually in a method
10
8
  meth_name = target.eval('__method__')
11
9
  file = target.eval('__FILE__')
12
10
 
13
11
  # /unknown/ for rbx
14
- if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != ""
15
- Pry.run_command "whereami", :output => out, :show_output => true, :context => target, :commands => Pry::Commands
12
+ if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e"
13
+ Pry.run_command "whereami 5", :output => out, :show_output => true, :context => target, :commands => Pry::Commands
16
14
  end
17
15
  end,
18
-
19
- :after_session => proc { |out, target| out.puts "Ending Pry session for #{Pry.view_clip(target.eval('self'))}" }
20
16
  }
21
17
  end
@@ -1,19 +1,16 @@
1
1
  class Pry
2
-
3
- # The default print object - only show first line of backtrace and
4
- # prepend output with `=>`
5
2
  DEFAULT_PRINT = proc do |output, value|
6
- case value
7
- when Exception
8
- output.puts "#{value.class}: #{value.message}"
9
- output.puts "from #{value.backtrace.first}"
3
+ if Pry.color
4
+ output.puts "=> #{CodeRay.scan(Pry.view(value), :ruby).term}"
10
5
  else
11
- if Pry.color
12
- output.puts "=> #{CodeRay.scan(Pry.view(value), :ruby).term}"
13
- else
14
- output.puts "=> #{Pry.view(value)}"
15
- end
6
+ output.puts "=> #{Pry.view(value)}"
16
7
  end
17
8
  end
9
+
10
+ # Will only show the first line of the backtrace
11
+ DEFAULT_EXCEPTION_HANDLER = proc do |output, exception|
12
+ output.puts "#{exception.class}: #{exception.message}"
13
+ output.puts "from #{exception.backtrace.first}"
14
+ end
18
15
  end
19
-
16
+
@@ -1,6 +1,6 @@
1
1
  class Pry
2
2
 
3
-
3
+
4
4
  # The default prompt; includes the target and nesting level
5
5
  DEFAULT_PROMPT = [
6
6
  proc do |target_self, nest_level|
@@ -11,7 +11,7 @@ class Pry
11
11
  "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}> "
12
12
  end
13
13
  end,
14
-
14
+
15
15
  proc do |target_self, nest_level|
16
16
  if nest_level == 0
17
17
  "pry(#{Pry.view_clip(target_self)})* "
@@ -23,4 +23,9 @@ class Pry
23
23
 
24
24
  # A simple prompt - doesn't display target or nesting level
25
25
  SIMPLE_PROMPT = [proc { ">> " }, proc { ">* " }]
26
+
27
+ SHELL_PROMPT = [
28
+ proc { |target_self, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " },
29
+ proc { |target_self, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} * " }
30
+ ]
26
31
  end
@@ -1,9 +1,9 @@
1
1
  # @author John Mair (banisterfiend)
2
2
  class Pry
3
3
 
4
- # The RC Files to load.
4
+ # The RC Files to load.
5
5
  RC_FILES = ["~/.pryrc"]
6
-
6
+
7
7
  # class accessors
8
8
  class << self
9
9
 
@@ -49,6 +49,10 @@ class Pry
49
49
  # Pry instances.
50
50
  attr_accessor :print
51
51
 
52
+ # @return [Proc] The Proc to use for printing exceptions by default by all
53
+ # Pry instances.
54
+ attr_accessor :exception_handler
55
+
52
56
  # Get/Set the Hash that defines Pry hooks used by default by all Pry
53
57
  # instances.
54
58
  # @return [Hash] The hooks used by default by all Pry instances.
@@ -57,6 +61,14 @@ class Pry
57
61
  # :after_session => proc { puts "goodbye" }
58
62
  attr_accessor :hooks
59
63
 
64
+
65
+ # Get/Set the Proc that defines extra Readline completions (on top
66
+ # of the ones defined for IRB).
67
+ # @return [Proc] The Proc that defines extra Readline completions (on top
68
+ # @example Add file names to completion list
69
+ # Pry.custom_completions = proc { Dir.entries('.') }
70
+ attr_accessor :custom_completions
71
+
60
72
  # Get the array of Procs to be used for the prompts by default by
61
73
  # all Pry instances.
62
74
  # @return [Array<Proc>] The array of Procs to be used for the
@@ -68,11 +80,15 @@ class Pry
68
80
  attr_accessor :cmd_ret_value
69
81
 
70
82
  # Determines whether colored output is enabled.
71
- # @return [Boolean]
83
+ # @return [Boolean]
72
84
  attr_accessor :color
73
85
 
86
+ # Determines whether paging (of long blocks of text) is enabled.
87
+ # @return [Boolean]
88
+ attr_accessor :pager
89
+
74
90
  # Determines whether the rc file (~/.pryrc) should be loaded.
75
- # @return [Boolean]
91
+ # @return [Boolean]
76
92
  attr_accessor :should_load_rc
77
93
 
78
94
  # Set to true if Pry is invoked from command line using `pry` executable
@@ -93,7 +109,7 @@ class Pry
93
109
  load(file_name) if File.exists?(file_name)
94
110
  end
95
111
  end
96
-
112
+
97
113
  # Start a Pry REPL.
98
114
  # This method also loads the files specified in `Pry::RC_FILES` the
99
115
  # first time it is invoked.
@@ -107,7 +123,7 @@ class Pry
107
123
  load_rc
108
124
  @rc_loaded = true
109
125
  end
110
-
126
+
111
127
  new(options).repl(target)
112
128
  end
113
129
 
@@ -117,11 +133,14 @@ class Pry
117
133
  # @return [String] The string representation of `obj`.
118
134
  def self.view(obj)
119
135
  case obj
120
- when String, Hash, Array, Symbol, nil
136
+ when String, Hash, Array, Symbol, Exception, nil
121
137
  obj.inspect
122
138
  else
123
139
  obj.to_s
124
140
  end
141
+
142
+ rescue NoMethodError
143
+ "unknown"
125
144
  end
126
145
 
127
146
  # A version of `Pry.view` that clips the output to `max_size` chars.
@@ -158,17 +177,18 @@ class Pry
158
177
  def self.run_command(arg_string, options={})
159
178
  name, arg_string = arg_string.split(/\s+/, 2)
160
179
  arg_string = "" if !arg_string
161
-
180
+
162
181
  options = {
163
182
  :context => TOPLEVEL_BINDING,
164
183
  :show_output => false,
165
184
  :output => Pry.output,
166
185
  :commands => Pry.commands
167
186
  }.merge!(options)
168
-
187
+
169
188
  null_output = Object.new.tap { |v| v.instance_eval { def puts(*) end } }
170
-
171
- commands = options[:commands].clone
189
+
190
+ commands = options[:commands]
191
+
172
192
  commands.output = options[:show_output] ? options[:output] : null_output
173
193
  commands.target = Pry.binding_for(options[:context])
174
194
 
@@ -188,8 +208,11 @@ class Pry
188
208
  @commands = Pry::Commands
189
209
  @prompt = DEFAULT_PROMPT
190
210
  @print = DEFAULT_PRINT
211
+ @exception_handler = DEFAULT_EXCEPTION_HANDLER
191
212
  @hooks = DEFAULT_HOOKS
213
+ @custom_completions = DEFAULT_CUSTOM_COMPLETIONS
192
214
  @color = true
215
+ @pager = true
193
216
  @should_load_rc = true
194
217
  @rc_loaded = false
195
218
  @cli = false
@@ -1,8 +1,13 @@
1
+ direc = File.dirname(__FILE__)
2
+
3
+ require "#{direc}/command_processor.rb"
4
+
1
5
  class Pry
2
6
 
3
7
  # The list of configuration options.
4
8
  CONFIG_OPTIONS = [:input, :output, :commands, :print,
5
- :prompt, :hooks]
9
+ :exception_handler, :prompt, :hooks,
10
+ :custom_completions]
6
11
 
7
12
  attr_accessor *CONFIG_OPTIONS
8
13
 
@@ -13,8 +18,8 @@ class Pry
13
18
 
14
19
  # Create a new `Pry` object.
15
20
  # @param [Hash] options The optional configuration parameters.
16
- # @option options [#readline] :input The object to use for input.
17
- # @option options [#puts] :output The object to use for output.
21
+ # @option options [#readline] :input The object to use for input.
22
+ # @option options [#puts] :output The object to use for output.
18
23
  # @option options [Pry::CommandBase] :commands The object to use for commands. (see commands.rb)
19
24
  # @option options [Hash] :hooks The defined hook Procs (see hooks.rb)
20
25
  # @option options [Array<Proc>] :default_prompt The array of Procs to use for the prompts. (see prompts.rb)
@@ -29,6 +34,8 @@ class Pry
29
34
  CONFIG_OPTIONS.each do |key|
30
35
  instance_variable_set("@#{key}", default_options[key])
31
36
  end
37
+
38
+ @command_processor = CommandProcessor.new(self)
32
39
  end
33
40
 
34
41
  # Get nesting data.
@@ -71,8 +78,8 @@ class Pry
71
78
  Pry.active_instance = self
72
79
 
73
80
  # Make sure special locals exist
74
- target.eval("_pry_ = Pry.active_instance")
75
- target.eval("_ = Pry.last_result")
81
+ target.eval("_pry_ = ::Pry.active_instance")
82
+ target.eval("_ = ::Pry.last_result")
76
83
  self.session_target = target
77
84
  end
78
85
 
@@ -85,7 +92,7 @@ class Pry
85
92
 
86
93
  # If break_data is an array, then the last element is the return value
87
94
  break_level, return_value = Array(break_data)
88
-
95
+
89
96
  # keep throwing until we reach the desired nesting level
90
97
  if nesting_level != break_level
91
98
  throw :breakout, break_data
@@ -93,7 +100,7 @@ class Pry
93
100
 
94
101
  return_value
95
102
  end
96
-
103
+
97
104
  # Start a read-eval-print-loop.
98
105
  # If no parameter is given, default to top-level (main).
99
106
  # @param [Object, Binding] target The receiver of the Pry session
@@ -107,7 +114,7 @@ class Pry
107
114
  target_self = target.eval('self')
108
115
 
109
116
  repl_prologue(target)
110
-
117
+
111
118
  # cannot rely on nesting.level as
112
119
  # nesting.level changes with new sessions
113
120
  nesting_level = nesting.size
@@ -123,7 +130,7 @@ class Pry
123
130
 
124
131
  # if one was provided, return the return value
125
132
  return return_value if return_value
126
-
133
+
127
134
  # otherwise return the target_self
128
135
  target_self
129
136
  end
@@ -135,13 +142,17 @@ class Pry
135
142
  # Pry.new.rep(Object.new)
136
143
  def rep(target=TOPLEVEL_BINDING)
137
144
  target = Pry.binding_for(target)
138
- print.call output, re(target)
145
+ result = re(target)
146
+
147
+ show_result(result) if should_print?
139
148
  end
140
149
 
141
150
  # Perform a read-eval
142
151
  # If no parameter is given, default to top-level (main).
143
152
  # @param [Object, Binding] target The receiver of the read-eval-print
144
- # @return [Object] The result of the eval or an `Exception` object in case of error.
153
+ # @return [Object] The result of the eval or an `Exception` object in case of
154
+ # error. In the latter case, you can check whether the exception was raised
155
+ # or is just the result of the expression using #last_result_is_exception?
145
156
  # @example
146
157
  # Pry.new.re(Object.new)
147
158
  def re(target=TOPLEVEL_BINDING)
@@ -149,13 +160,14 @@ class Pry
149
160
 
150
161
  if input == Readline
151
162
  # Readline tab completion
152
- Readline.completion_proc = Pry::InputCompleter.build_completion_proc(target, commands.commands.keys)
163
+ Readline.completion_proc = Pry::InputCompleter.build_completion_proc target, instance_eval(&custom_completions)
153
164
  end
154
165
 
155
-
156
166
  # save the pry instance to active_instance
157
167
  Pry.active_instance = self
158
- target.eval("_pry_ = Pry.active_instance")
168
+ target.eval("_pry_ = ::Pry.active_instance")
169
+
170
+ @last_result_is_exception = false
159
171
 
160
172
  # eval the expression and save to last_result
161
173
  # Do not want __FILE__, __LINE__ here because we need to distinguish
@@ -165,6 +177,7 @@ class Pry
165
177
  rescue SystemExit => e
166
178
  exit
167
179
  rescue Exception => e
180
+ @last_result_is_exception = true
168
181
  set_last_exception(e, target)
169
182
  end
170
183
 
@@ -179,15 +192,38 @@ class Pry
179
192
  # Pry.new.r(Object.new)
180
193
  def r(target=TOPLEVEL_BINDING)
181
194
  target = Pry.binding_for(target)
195
+ @suppress_output = false
182
196
  eval_string = ""
183
197
 
198
+ val = ""
184
199
  loop do
185
200
  val = retrieve_line(eval_string, target)
186
201
  process_line(val, eval_string, target)
187
- break eval_string if valid_expression?(eval_string)
202
+ break if valid_expression?(eval_string)
203
+ end
204
+
205
+ @suppress_output = true if eval_string =~ /;\Z/ || null_input?(val)
206
+
207
+ eval_string
208
+ end
209
+
210
+ # FIXME should delete this method? it's exposing an implementation detail!
211
+ def show_result(result)
212
+ if last_result_is_exception?
213
+ exception_handler.call output, result
214
+ else
215
+ print.call output, result
188
216
  end
189
217
  end
190
218
 
219
+ # Returns true if input is "" and a command is not returning a
220
+ # value.
221
+ # @param [String] val The input string.
222
+ # @return [Boolean] Whether the input is null.
223
+ def null_input?(val)
224
+ val.empty? && !Pry.cmd_ret_value
225
+ end
226
+
191
227
  # Read a line of input and check for ^d, also determine prompt to use.
192
228
  # This method should not need to be invoked directly.
193
229
  # @param [String] eval_string The cumulative lines of input.
@@ -200,9 +236,9 @@ class Pry
200
236
  # exit session if we receive EOF character
201
237
  if !val
202
238
  output.puts
203
- throw(:breakout, nesting.level)
239
+ throw(:breakout, nesting.level)
204
240
  end
205
-
241
+
206
242
  val
207
243
  end
208
244
 
@@ -212,13 +248,13 @@ class Pry
212
248
  # @param [String] eval_string The cumulative lines of input.
213
249
  # @target [Binding] target The target of the Pry session.
214
250
  def process_line(val, eval_string, target)
215
- val.chomp!
216
- Pry.cmd_ret_value = process_commands(val, eval_string, target)
217
-
251
+ val.rstrip!
252
+ Pry.cmd_ret_value = @command_processor.process_commands(val, eval_string, target)
253
+
218
254
  if Pry.cmd_ret_value
219
255
  eval_string << "Pry.cmd_ret_value\n"
220
256
  else
221
- eval_string << "#{val}\n"
257
+ eval_string << "#{val}\n" if !val.empty?
222
258
  end
223
259
  end
224
260
 
@@ -228,7 +264,7 @@ class Pry
228
264
  # @param [Binding] target The binding to set `_` on.
229
265
  def set_last_result(result, target)
230
266
  Pry.last_result = result
231
- target.eval("_ = Pry.last_result")
267
+ target.eval("_ = ::Pry.last_result")
232
268
  end
233
269
 
234
270
  # Set the last exception for a session.
@@ -237,87 +273,14 @@ class Pry
237
273
  # @param [Binding] target The binding to set `_ex_` on.
238
274
  def set_last_exception(ex, target)
239
275
  Pry.last_exception = ex
240
- target.eval("_ex_ = Pry.last_exception")
241
- end
242
-
243
- # Determine whether a Pry command was matched and return command data
244
- # and argument string.
245
- # This method should not need to be invoked directly.
246
- # @param [String] val The line of input.
247
- # @return [Array] The command data and arg string pair
248
- def command_matched(val)
249
- _, cmd_data = commands.commands.find do |name, cmd_data|
250
- /^#{name}(?!\S)(?:\s+(.+))?/ =~ val
251
- end
252
-
253
- [cmd_data, $1]
254
- end
255
-
256
- # Process Pry commands. Pry commands are not Ruby methods and are evaluated
257
- # prior to Ruby expressions.
258
- # Commands can be modified/configured by the user: see `Pry::Commands`
259
- # This method should not need to be invoked directly - it is called
260
- # by `Pry#r`.
261
- # @param [String] val The current line of input.
262
- # @param [String] eval_string The cumulative lines of input for
263
- # multi-line input.
264
- # @param [Binding] target The receiver of the commands.
265
- def process_commands(val, eval_string, target)
266
- def val.clear() replace("") end
267
- def eval_string.clear() replace("") end
268
-
269
- cmd_data, args_string = command_matched(val)
270
-
271
- # no command was matched, so return to caller
272
- return if !cmd_data
273
-
274
- args = args_string ? Shellwords.shellwords(args_string) : []
275
- action = cmd_data[:action]
276
- keep_retval = cmd_data[:keep_retval]
277
-
278
- options = {
279
- :val => val,
280
- :eval_string => eval_string,
281
- :nesting => nesting,
282
- :commands => commands.commands
283
- }
284
-
285
- ret_value = execute_command(target, action, options, *args)
286
-
287
- # return value of block only if :keep_retval is true
288
- ret_value if keep_retval
276
+ target.eval("_ex_ = ::Pry.last_exception")
289
277
  end
290
278
 
291
- # Execute a Pry command.
292
- # This method should not need to be invoked directly.
293
- # @param [Binding] target The target of the Pry session.
294
- # @param [Proc] action The proc that implements the command.
295
- # @param [Hash] options The options to set on the Commands object.
296
- # @param [Array] args The command arguments.
297
- def execute_command(target, action, options, *args)
298
-
299
- # set some useful methods to be used by the action blocks
300
- commands.opts = options
301
- commands.target = target
302
- commands.output = output
303
-
304
- case action.arity <=> 0
305
- when -1
306
-
307
- # Use instance_exec() to make the `opts` method, etc available
308
- ret_val = commands.instance_exec(*args, &action)
309
- when 1, 0
310
-
311
- # ensure that we get the right number of parameters
312
- # since 1.8.7 complains about incorrect arity (1.9.2
313
- # doesn't care)
314
- args_with_corrected_arity = args.values_at *0..(action.arity - 1)
315
- ret_val = commands.instance_exec(*args_with_corrected_arity, &action)
316
- end
317
-
318
- options[:val].clear
319
-
320
- ret_val
279
+ # @return [Boolean] True if the last result is an exception that was raised,
280
+ # as opposed to simply an instance of Exception (like the result of
281
+ # Exception.new)
282
+ def last_result_is_exception?
283
+ @last_result_is_exception
321
284
  end
322
285
 
323
286
  # Returns the next line of input to be used by the pry instance.
@@ -332,14 +295,28 @@ class Pry
332
295
  # as it has a second parameter.
333
296
  input.readline(current_prompt, true)
334
297
  else
335
- if input.method(:readline).arity == 1
336
- input.readline(current_prompt)
337
- else
338
- input.readline
298
+ begin
299
+ if input.method(:readline).arity == 1
300
+ input.readline(current_prompt)
301
+ else
302
+ input.readline
303
+ end
304
+
305
+ rescue EOFError
306
+ self.input = Readline
307
+ ""
339
308
  end
340
309
  end
341
310
  end
342
311
 
312
+ # Whether the print proc should be invoked.
313
+ # Currently only invoked if the output is not suppressed OR the last result
314
+ # is an exception regardless of suppression.
315
+ # @return [Boolean] Whether the print proc should be invoked.
316
+ def should_print?
317
+ !@suppress_output || last_result_is_exception?
318
+ end
319
+
343
320
  # Returns the appropriate prompt to use.
344
321
  # This method should not need to be invoked directly.
345
322
  # @param [Boolean] first_line Whether this is the first line of input
@@ -355,7 +332,7 @@ class Pry
355
332
  end
356
333
  end
357
334
 
358
- if RUBY_VERSION =~ /1.9/
335
+ if RUBY_VERSION =~ /1.9/ && RUBY_ENGINE == "ruby"
359
336
  require 'ripper'
360
337
 
361
338
  # Determine if a string of code is a valid Ruby expression.