pry 0.9.9.6pre2-java → 0.9.10-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/CHANGELOG +41 -0
  2. data/CONTRIBUTORS +27 -26
  3. data/README.markdown +4 -4
  4. data/Rakefile +2 -2
  5. data/lib/pry.rb +25 -19
  6. data/lib/pry/cli.rb +31 -10
  7. data/lib/pry/code.rb +41 -83
  8. data/lib/pry/command.rb +87 -76
  9. data/lib/pry/command_set.rb +13 -20
  10. data/lib/pry/completion.rb +139 -121
  11. data/lib/pry/config.rb +4 -0
  12. data/lib/pry/core_extensions.rb +88 -31
  13. data/lib/pry/default_commands/cd.rb +31 -8
  14. data/lib/pry/default_commands/context.rb +4 -58
  15. data/lib/pry/default_commands/easter_eggs.rb +1 -1
  16. data/lib/pry/default_commands/editing.rb +21 -14
  17. data/lib/pry/default_commands/find_method.rb +5 -7
  18. data/lib/pry/default_commands/gist.rb +187 -0
  19. data/lib/pry/default_commands/hist.rb +6 -6
  20. data/lib/pry/default_commands/input_and_output.rb +73 -129
  21. data/lib/pry/default_commands/introspection.rb +107 -52
  22. data/lib/pry/default_commands/ls.rb +1 -1
  23. data/lib/pry/default_commands/misc.rb +0 -5
  24. data/lib/pry/default_commands/whereami.rb +92 -0
  25. data/lib/pry/helpers/base_helpers.rb +6 -1
  26. data/lib/pry/helpers/command_helpers.rb +30 -9
  27. data/lib/pry/helpers/documentation_helpers.rb +7 -7
  28. data/lib/pry/helpers/options_helpers.rb +1 -1
  29. data/lib/pry/helpers/text.rb +7 -9
  30. data/lib/pry/history.rb +15 -2
  31. data/lib/pry/hooks.rb +1 -1
  32. data/lib/pry/indent.rb +17 -10
  33. data/lib/pry/method.rb +35 -19
  34. data/lib/pry/module_candidate.rb +130 -0
  35. data/lib/pry/pry_class.rb +54 -22
  36. data/lib/pry/pry_instance.rb +71 -14
  37. data/lib/pry/repl_file_loader.rb +80 -0
  38. data/lib/pry/version.rb +1 -1
  39. data/lib/pry/wrapped_module.rb +121 -142
  40. data/pry.gemspec +13 -13
  41. data/test/candidate_helper1.rb +11 -0
  42. data/test/candidate_helper2.rb +8 -0
  43. data/test/helper.rb +16 -0
  44. data/test/test_code.rb +1 -1
  45. data/test/test_command.rb +364 -270
  46. data/test/test_command_integration.rb +235 -267
  47. data/test/test_completion.rb +36 -0
  48. data/test/test_control_d_handler.rb +45 -0
  49. data/test/test_default_commands/example.erb +5 -0
  50. data/test/test_default_commands/test_cd.rb +316 -11
  51. data/test/test_default_commands/test_context.rb +143 -192
  52. data/test/test_default_commands/test_documentation.rb +81 -14
  53. data/test/test_default_commands/test_find_method.rb +10 -2
  54. data/test/test_default_commands/test_input.rb +102 -111
  55. data/test/test_default_commands/test_introspection.rb +17 -12
  56. data/test/test_default_commands/test_ls.rb +8 -6
  57. data/test/test_default_commands/test_shell.rb +18 -15
  58. data/test/test_default_commands/test_show_source.rb +170 -44
  59. data/test/test_exception_whitelist.rb +6 -2
  60. data/test/test_hooks.rb +32 -0
  61. data/test/test_input_stack.rb +19 -16
  62. data/test/test_method.rb +0 -4
  63. data/test/test_prompt.rb +60 -0
  64. data/test/test_pry.rb +23 -31
  65. data/test/test_pry_defaults.rb +75 -57
  66. data/test/test_syntax_checking.rb +12 -11
  67. data/test/test_wrapped_module.rb +103 -0
  68. metadata +72 -26
@@ -145,6 +145,10 @@ class Pry
145
145
  # @return [Boolean]
146
146
  attr_accessor :should_load_rc
147
147
 
148
+ # Determines whether the local rc file (./.pryrc) should be loaded.
149
+ # @return [Boolean]
150
+ attr_accessor :should_load_local_rc
151
+
148
152
  # Determines whether plugins should be loaded.
149
153
  # @return [Boolean]
150
154
  attr_accessor :should_load_plugins
@@ -1,53 +1,110 @@
1
1
  class Object
2
- # Start a Pry REPL. This method only differs from Pry.start in that it
3
- # assumes that the target is `self`. It also accepts and passses the same
4
- # exact options hash that Pry.start accepts. POSSIBLE DEPRICATION WARNING:
5
- # In the future the backwards compatibility with pry(binding) could be
6
- # removed so please properly use Object.pry or if you use pry(binding)
7
- # switch Pry.start(binding).
8
-
9
- # @param [Binding] the binding or options hash if no binding needed.
10
- # @param [Hash] the options hash.
11
- # @example First
12
- # "dummy".pry
13
- # @example Second
2
+ # Start a Pry REPL on self.
3
+ #
4
+ # If `self` is a Binding then that will be used to evaluate expressions;
5
+ # otherwise a new binding will be created.
6
+ #
7
+ # @param [Object] object the object or binding to pry
8
+ # (__deprecated__, use `object.pry`)
9
+ # @param [Hash] hash the options hash
10
+ # @example With a binding
14
11
  # binding.pry
15
- # @example An example with options
12
+ # @example On any object
13
+ # "dummy".pry
14
+ # @example With options
16
15
  # def my_method
17
16
  # binding.pry :quiet => true
18
17
  # end
19
18
  # my_method()
20
-
21
- def pry(*args)
22
- if args.first.is_a?(Hash) || args.length == 0
23
- args.unshift(self)
19
+ # @see Pry.start
20
+ def pry(object=nil, hash={})
21
+ if object.nil? || Hash === object
22
+ Pry.start(self, object || {})
23
+ else
24
+ Pry.start(object, hash)
24
25
  end
25
-
26
- Pry.start(*args)
27
26
  end
28
27
 
29
28
  # Return a binding object for the receiver.
29
+ #
30
+ # The `self` of the binding is set to the current object, and it contains no
31
+ # local variables.
32
+ #
33
+ # The default definee (http://yugui.jp/articles/846) is set such that:
34
+ #
35
+ # * If `self` is a class or module, then new methods created in the binding
36
+ # will be defined in that class or module (as in `class Foo; end`).
37
+ # * If `self` is a normal object, then new methods created in the binding will
38
+ # be defined on its singleton class (as in `class << self; end`).
39
+ # * If `self` doesn't have a real singleton class (i.e. it is a Fixnum, Float,
40
+ # Symbol, nil, true, or false), then new methods will be created on the
41
+ # object's class (as in `self.class.class_eval{ }`)
42
+ #
43
+ # Newly created constants, including classes and modules, will also be added
44
+ # to the default definee.
45
+ #
46
+ # @return [Binding]
30
47
  def __binding__
48
+ # When you're cd'd into a class, methods you define should be added to it.
31
49
  if is_a?(Module)
50
+ # class_eval sets both self and the default definee to this class.
32
51
  return class_eval "binding"
33
52
  end
34
53
 
35
- unless respond_to? :__binding_impl__
54
+ unless respond_to?(:__pry__)
55
+ binding_impl_method = [<<-METHOD, __FILE__, __LINE__ + 1]
56
+ # Get a binding with 'self' set to self, and no locals.
57
+ #
58
+ # The default definee is determined by the context in which the
59
+ # definition is eval'd.
60
+ #
61
+ # Please don't call this method directly, see {__binding__}.
62
+ #
63
+ # @return [Binding]
64
+ def __pry__
65
+ binding
66
+ end
67
+ METHOD
68
+
69
+ # The easiest way to check whether an object has a working singleton class
70
+ # is to try and define a method on it. (just checking for the presence of
71
+ # the singleton class gives false positives for `true` and `false`).
72
+ # __pry__ is just the closest method we have to hand, and using
73
+ # it has the nice property that we can memoize this check.
36
74
  begin
37
- instance_eval %{
38
- def __binding_impl__
39
- binding
40
- end
41
- }
75
+ # instance_eval sets the default definee to the object's singleton class
76
+ instance_eval(*binding_impl_method)
77
+
78
+ # If we can't define methods on the Object's singleton_class. Then we fall
79
+ # back to setting the default definee to be the Object's class. That seems
80
+ # nicer than having a REPL in which you can't define methods.
42
81
  rescue TypeError
43
- self.class.class_eval %{
44
- def __binding_impl__
45
- binding
46
- end
47
- }
82
+ # class_eval sets the default definee to self.class
83
+ self.class.class_eval(*binding_impl_method)
48
84
  end
49
85
  end
50
86
 
51
- __binding_impl__
87
+ __pry__
88
+ end
89
+ end
90
+
91
+ # There's a splat bug on jruby in 1.9 emulation mode, which breaks the
92
+ # pp library.
93
+ #
94
+ # * http://jira.codehaus.org/browse/JRUBY-6687
95
+ # * https://github.com/pry/pry/issues/568
96
+ #
97
+ # Until that gets fixed upstream, let's monkey-patch here:
98
+ if [[1, 2]].pretty_inspect == "[1]\n"
99
+ class Array
100
+ def pretty_print(q)
101
+ q.group(1, '[', ']') {
102
+ i = 0
103
+ q.seplist(self) { |*|
104
+ q.pp self[i]
105
+ i += 1
106
+ }
107
+ }
108
+ end
52
109
  end
53
110
  end
@@ -9,41 +9,63 @@ class Pry
9
9
  Usage: cd [OPTIONS] [--help]
10
10
 
11
11
  Move into new context (object or scope). As in unix shells use
12
- `cd ..` to go back and `cd /` to return to Pry top-level).
13
- Complex syntax (e.g cd ../@x/y) also supported.
12
+ `cd ..` to go back, `cd /` to return to Pry top-level and `cd -`
13
+ to toggle between last two scopes).
14
+ Complex syntax (e.g `cd ../@x/y`) also supported.
14
15
 
15
16
  e.g: `cd @x`
16
- e.g: `cd ..
17
+ e.g: `cd ..`
17
18
  e.g: `cd /`
19
+ e.g: `cd -`
18
20
 
19
21
  https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
20
22
  BANNER
21
23
 
22
24
  def process
23
- path = arg_string.split(/\//)
24
- stack = _pry_.binding_stack.dup
25
+ # Extract command arguments. Delete blank arguments like " ", but
26
+ # don't delete empty strings like "".
27
+ path = arg_string.split(/\//).delete_if { |a| a =~ /\A\s+\z/ }
28
+ stack = _pry_.binding_stack.dup
29
+ old_stack = state.old_stack || []
25
30
 
26
- # special case when we only get a single "/", return to root
27
- stack = [stack.first] if path.empty?
31
+ # Special case when we only get a single "/", return to root.
32
+ if path.empty?
33
+ state.old_stack = stack.dup unless old_stack.empty?
34
+ stack = [stack.first]
35
+ end
28
36
 
29
- path.each do |context|
37
+ path.each_with_index do |context, i|
30
38
  begin
31
39
  case context.chomp
32
40
  when ""
41
+ state.old_stack = stack.dup
33
42
  stack = [stack.first]
34
43
  when "::"
44
+ state.old_stack = stack.dup
35
45
  stack.push(TOPLEVEL_BINDING)
36
46
  when "."
37
47
  next
38
48
  when ".."
39
49
  unless stack.size == 1
50
+ # Don't rewrite old_stack if we're in complex expression
51
+ # (e.g.: `cd 1/2/3/../4).
52
+ state.old_stack = stack.dup if path.first == ".."
40
53
  stack.pop
41
54
  end
55
+ when "-"
56
+ unless old_stack.empty?
57
+ # Interchange current stack and old stack with each other.
58
+ stack, state.old_stack = state.old_stack, stack
59
+ end
42
60
  else
61
+ state.old_stack = stack.dup if i == 0
43
62
  stack.push(Pry.binding_for(stack.last.eval(context)))
44
63
  end
45
64
 
46
65
  rescue RescuableException => e
66
+ # Restore old stack to its initial values.
67
+ state.old_stack = old_stack
68
+
47
69
  output.puts "Bad object path: #{arg_string.chomp}. Failed trying to resolve: #{context}"
48
70
  output.puts e.inspect
49
71
  return
@@ -52,6 +74,7 @@ class Pry
52
74
 
53
75
  _pry_.binding_stack = stack
54
76
  end
77
+
55
78
  end
56
79
  end
57
80
  end
@@ -1,6 +1,7 @@
1
1
  require "pry/default_commands/ls"
2
2
  require "pry/default_commands/cd"
3
3
  require "pry/default_commands/find_method"
4
+ require "pry/default_commands/whereami"
4
5
 
5
6
  class Pry
6
7
  module DefaultCommands
@@ -9,62 +10,7 @@ class Pry
9
10
  import Ls
10
11
  import Cd
11
12
  import FindMethod
12
-
13
- create_command "whereami" do
14
- description "Show code surrounding the current context."
15
- banner <<-BANNER
16
- Usage: whereami [OPTIONS]
17
- BANNER
18
-
19
- def setup
20
- @method = Pry::Method.from_binding(target)
21
- end
22
-
23
- def process
24
- if show_method?
25
- file = @method.source_file
26
- start = @method.source_range.begin
27
- finish = @method.source_range.end
28
- marker = target.eval("__LINE__")
29
- else
30
- file = target.eval("__FILE__")
31
- start = target.eval("__LINE__")
32
- finish = (args.first && args.first.to_i) || 5
33
- marker = start
34
- end
35
-
36
- if invalid_file?(file)
37
- raise Pry::CommandError,
38
- "Cannot find local context. Did you use binding.pry?"
39
- end
40
-
41
- # TODO: refactor.
42
- if show_method?
43
- code = Pry::Code.from_file(file).between(start, finish)
44
- else
45
- code = Pry::Code.from_file(file).around(start, finish)
46
- end
47
-
48
- desc = (@method && @method.name_with_owner) || ""
49
-
50
- if !code.empty?
51
- output.puts "\n#{text.bold('From:')} #{file} @ line #{start} #{desc}:\n\n"
52
- output.puts code.with_line_numbers.with_marker(marker)
53
- output.puts
54
- end
55
- end
56
-
57
- private
58
-
59
- def show_method?
60
- args.empty? && @method && !@method.instance_of?(Pry::Method::Disowned) && @method.source_range.count < 20
61
- end
62
-
63
- def invalid_file?(file)
64
- file != Pry.eval_path &&
65
- (file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e")
66
- end
67
- end
13
+ import Whereami
68
14
 
69
15
  create_command "pry-backtrace", "Show the backtrace for the Pry session." do
70
16
  banner <<-BANNER
@@ -91,7 +37,7 @@ class Pry
91
37
  exec "pry"
92
38
  end
93
39
 
94
- create_command /wtf([?!]*)/, "Show the backtrace of the most recent exception" do
40
+ create_command(/wtf([?!]*)/, "Show the backtrace of the most recent exception") do
95
41
  options :listing => 'wtf?'
96
42
 
97
43
  banner <<-BANNER
@@ -127,7 +73,7 @@ class Pry
127
73
  end
128
74
 
129
75
  # N.B. using a regular expresion here so that "raise-up 'foo'" does the right thing.
130
- create_command /raise-up(!?\b.*)/, :listing => 'raise-up' do
76
+ create_command(/raise-up(!?\b.*)/, :listing => 'raise-up') do
131
77
  description "Raise an exception out of the current pry instance."
132
78
  banner <<-BANNER
133
79
  Raise up, like exit, allows you to quit pry. Instead of returning a value however, it raises an exception.
@@ -77,7 +77,7 @@ end
77
77
  \____/ \________________________|
78
78
  EOS
79
79
 
80
- if defined?(Win32::Console)
80
+ if windows_ansi?
81
81
  move_up = proc { |n| "\e[#{n}F" }
82
82
  else
83
83
  move_up = proc { |n| "\e[#{n}A\e[0G" }
@@ -1,4 +1,5 @@
1
1
  require 'tempfile'
2
+ require 'shellwords'
2
3
  require 'pry/default_commands/hist'
3
4
 
4
5
  class Pry
@@ -37,10 +38,10 @@ class Pry
37
38
  BANNER
38
39
 
39
40
  def options(opt)
40
- opt.on :e, :ex, "Open the file that raised the most recent exception (_ex_.file)", :optional => true, :as => Integer
41
- opt.on :i, :in, "Open a temporary file containing the Nth line of _in_. N may be a range.", :optional => true, :as => Range, :default => -1..-1
41
+ opt.on :e, :ex, "Open the file that raised the most recent exception (_ex_.file)", :optional_argument => true, :as => Integer
42
+ opt.on :i, :in, "Open a temporary file containing the Nth input expression. N may be a range.", :optional_argument => true, :as => Range, :default => -1..-1
42
43
  opt.on :t, :temp, "Open an empty temporary file"
43
- opt.on :l, :line, "Jump to this line in the opened file", true, :as => Integer
44
+ opt.on :l, :line, "Jump to this line in the opened file", :argument => true, :as => Integer
44
45
  opt.on :n, :"no-reload", "Don't automatically reload the edited code"
45
46
  opt.on :c, :"current", "Open the current __FILE__ and at __LINE__ (as returned by `whereami`)."
46
47
  opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
@@ -89,6 +90,7 @@ class Pry
89
90
  f.puts(content)
90
91
  f.flush
91
92
  reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
93
+ f.close(false)
92
94
  invoke_editor(f.path, line, reload)
93
95
  if reload
94
96
  silence_warnings do
@@ -140,8 +142,12 @@ class Pry
140
142
  line = opts[:l].to_i if opts.present?(:line)
141
143
 
142
144
  reload = opts.present?(:reload) || ((opts.present?(:ex) || file_name.end_with?(".rb")) && !opts.present?(:'no-reload')) && !Pry.config.disable_auto_reload
143
- invoke_editor(file_name, line, reload)
144
- set_file_and_dir_locals(file_name)
145
+
146
+ # Sanitize blanks.
147
+ sanitized_file_name = Shellwords.escape(file_name)
148
+
149
+ invoke_editor(sanitized_file_name, line, reload)
150
+ set_file_and_dir_locals(sanitized_file_name)
145
151
 
146
152
  if reload
147
153
  silence_warnings do
@@ -209,6 +215,7 @@ class Pry
209
215
  temp_file do |f|
210
216
  f.puts lines.join
211
217
  f.flush
218
+ f.close(false)
212
219
  invoke_editor(f.path, 0, true)
213
220
 
214
221
  if @method.alias?
@@ -352,26 +359,26 @@ class Pry
352
359
  end
353
360
 
354
361
  def options(opt)
355
- opt.on :m, :method, "Play a method's source.", true do |meth_name|
362
+ opt.on :m, :method, "Play a method's source.", :argument => true do |meth_name|
356
363
  meth = get_method_or_raise(meth_name, target, {})
357
364
  self.content << meth.source
358
365
  end
359
- opt.on :d, :doc, "Play a method's documentation.", true do |meth_name|
366
+ opt.on :d, :doc, "Play a method's documentation.", :argument => true do |meth_name|
360
367
  meth = get_method_or_raise(meth_name, target, {})
361
368
  text.no_color do
362
- self.content << process_comment_markup(meth.doc, :ruby)
369
+ self.content << process_comment_markup(meth.doc)
363
370
  end
364
371
  end
365
- opt.on :c, :command, "Play a command's source.", true do |command_name|
372
+ opt.on :c, :command, "Play a command's source.", :argument => true do |command_name|
366
373
  command = find_command(command_name)
367
- block = Pry::Method.new(find_command(command_name).block)
374
+ block = Pry::Method.new(command.block)
368
375
  self.content << block.source
369
376
  end
370
- opt.on :f, :file, "Play a file.", true do |file|
377
+ opt.on :f, :file, "Play a file.", :argument => true do |file|
371
378
  self.content << File.read(File.expand_path(file))
372
379
  end
373
- opt.on :l, :lines, "Only play a subset of lines.", :optional => true, :as => Range, :default => 1..-1
374
- opt.on :i, :in, "Play entries from Pry's input expression history. Takes an index or range. Note this can only replay pure Ruby code, not Pry commands.", :optional => true,
380
+ opt.on :l, :lines, "Only play a subset of lines.", :optional_argument => true, :as => Range, :default => 1..-1
381
+ opt.on :i, :in, "Play entries from Pry's input expression history. Takes an index or range. Note this can only replay pure Ruby code, not Pry commands.", :optional_argument => true,
375
382
  :as => Range, :default => -5..-1 do |range|
376
383
  input_expressions = _pry_.input_array[range] || []
377
384
  Array(input_expressions).each { |v| self.content << v }
@@ -389,7 +396,7 @@ class Pry
389
396
  begin
390
397
  self.content << target.eval(arg)
391
398
  rescue Pry::RescuableException
392
- raise CommandError, "Prblem when evaling #{arg}."
399
+ raise CommandError, "Problem when evaling #{arg}."
393
400
  end
394
401
  end
395
402
  end
@@ -18,8 +18,8 @@ class Pry
18
18
  Use the `-n` switch (the default) to search for methods whose name matches the given regex.
19
19
  Use the `-c` switch to search for methods that contain the given code.
20
20
 
21
- e.g find re Pry # find all methods whose name match /re/ inside the Pry namespace. Matches Pry#repl, etc.
22
- e.g find -c 'output.puts' Pry # find all methods that contain the code: output.puts inside the Pry namepsace.
21
+ e.g find-method re Pry # find all methods whose name match /re/ inside the Pry namespace. Matches Pry#repl, etc.
22
+ e.g find-method -c 'output.puts' Pry # find all methods that contain the code: output.puts inside the Pry namepsace.
23
23
  BANNER
24
24
 
25
25
  def setup
@@ -90,9 +90,7 @@ class Pry
90
90
  # @yieldparam klazz Each class/module in the namespace.
91
91
  #
92
92
  def recurse_namespace(klass, done={}, &block)
93
- if done[klass] || !(Module === klass)
94
- return
95
- end
93
+ return if !(Module === klass) || done[klass]
96
94
 
97
95
  done[klass] = true
98
96
 
@@ -102,7 +100,7 @@ class Pry
102
100
  next if klass.autoload?(name)
103
101
  begin
104
102
  const = klass.const_get(name)
105
- rescue RescuableException => e
103
+ rescue RescuableException
106
104
  # constant loading is an inexact science at the best of times,
107
105
  # this often happens when a constant was .autoload? but someone
108
106
  # tried to load it. It's now not .autoload? but will still raise
@@ -160,7 +158,7 @@ class Pry
160
158
  search_all_methods(namespace) do |meth|
161
159
  begin
162
160
  meth.source =~ regex
163
- rescue RescuableException => e
161
+ rescue RescuableException
164
162
  false
165
163
  end
166
164
  end