pry 0.12.1-java → 0.14.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +166 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +132 -118
  7. data/lib/pry/basic_object.rb +8 -4
  8. data/lib/pry/block_command.rb +22 -0
  9. data/lib/pry/class_command.rb +194 -0
  10. data/lib/pry/cli.rb +43 -54
  11. data/lib/pry/code.rb +40 -28
  12. data/lib/pry/code/code_file.rb +28 -24
  13. data/lib/pry/code/code_range.rb +4 -2
  14. data/lib/pry/code/loc.rb +15 -8
  15. data/lib/pry/code_object.rb +40 -38
  16. data/lib/pry/color_printer.rb +47 -46
  17. data/lib/pry/command.rb +162 -360
  18. data/lib/pry/command_set.rb +76 -73
  19. data/lib/pry/command_state.rb +31 -0
  20. data/lib/pry/commands/amend_line.rb +86 -81
  21. data/lib/pry/commands/bang.rb +18 -14
  22. data/lib/pry/commands/bang_pry.rb +15 -11
  23. data/lib/pry/commands/cat.rb +61 -54
  24. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  25. data/lib/pry/commands/cat/exception_formatter.rb +71 -60
  26. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  28. data/lib/pry/commands/cd.rb +40 -35
  29. data/lib/pry/commands/change_inspector.rb +29 -22
  30. data/lib/pry/commands/change_prompt.rb +44 -39
  31. data/lib/pry/commands/clear_screen.rb +16 -10
  32. data/lib/pry/commands/code_collector.rb +148 -133
  33. data/lib/pry/commands/disable_pry.rb +23 -19
  34. data/lib/pry/commands/easter_eggs.rb +19 -30
  35. data/lib/pry/commands/edit.rb +184 -161
  36. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  37. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  38. data/lib/pry/commands/exit.rb +39 -35
  39. data/lib/pry/commands/exit_all.rb +24 -20
  40. data/lib/pry/commands/exit_program.rb +20 -16
  41. data/lib/pry/commands/find_method.rb +168 -160
  42. data/lib/pry/commands/fix_indent.rb +16 -12
  43. data/lib/pry/commands/help.rb +140 -133
  44. data/lib/pry/commands/hist.rb +151 -150
  45. data/lib/pry/commands/import_set.rb +20 -16
  46. data/lib/pry/commands/jump_to.rb +25 -21
  47. data/lib/pry/commands/list_inspectors.rb +35 -28
  48. data/lib/pry/commands/ls.rb +124 -102
  49. data/lib/pry/commands/ls/constants.rb +59 -42
  50. data/lib/pry/commands/ls/formatter.rb +50 -46
  51. data/lib/pry/commands/ls/globals.rb +38 -34
  52. data/lib/pry/commands/ls/grep.rb +17 -13
  53. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  54. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  55. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  56. data/lib/pry/commands/ls/local_names.rb +26 -22
  57. data/lib/pry/commands/ls/local_vars.rb +38 -28
  58. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  59. data/lib/pry/commands/ls/methods.rb +44 -43
  60. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  61. data/lib/pry/commands/ls/self_methods.rb +23 -22
  62. data/lib/pry/commands/nesting.rb +21 -17
  63. data/lib/pry/commands/play.rb +93 -82
  64. data/lib/pry/commands/pry_backtrace.rb +22 -17
  65. data/lib/pry/commands/pry_version.rb +15 -11
  66. data/lib/pry/commands/raise_up.rb +27 -22
  67. data/lib/pry/commands/reload_code.rb +60 -48
  68. data/lib/pry/commands/reset.rb +16 -12
  69. data/lib/pry/commands/ri.rb +55 -45
  70. data/lib/pry/commands/save_file.rb +45 -43
  71. data/lib/pry/commands/shell_command.rb +51 -51
  72. data/lib/pry/commands/shell_mode.rb +21 -17
  73. data/lib/pry/commands/show_doc.rb +80 -68
  74. data/lib/pry/commands/show_info.rb +189 -171
  75. data/lib/pry/commands/show_input.rb +16 -11
  76. data/lib/pry/commands/show_source.rb +110 -45
  77. data/lib/pry/commands/stat.rb +35 -31
  78. data/lib/pry/commands/switch_to.rb +21 -15
  79. data/lib/pry/commands/toggle_color.rb +20 -16
  80. data/lib/pry/commands/watch_expression.rb +89 -86
  81. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  82. data/lib/pry/commands/whereami.rb +156 -148
  83. data/lib/pry/commands/wtf.rb +75 -50
  84. data/lib/pry/config.rb +307 -25
  85. data/lib/pry/config/attributable.rb +22 -0
  86. data/lib/pry/config/lazy_value.rb +29 -0
  87. data/lib/pry/config/memoized_value.rb +34 -0
  88. data/lib/pry/config/value.rb +24 -0
  89. data/lib/pry/control_d_handler.rb +28 -0
  90. data/lib/pry/core_extensions.rb +9 -7
  91. data/lib/pry/editor.rb +48 -21
  92. data/lib/pry/env.rb +18 -0
  93. data/lib/pry/exception_handler.rb +43 -0
  94. data/lib/pry/exceptions.rb +13 -16
  95. data/lib/pry/forwardable.rb +5 -1
  96. data/lib/pry/helpers.rb +2 -0
  97. data/lib/pry/helpers/base_helpers.rb +58 -59
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  100. data/lib/pry/helpers/options_helpers.rb +14 -7
  101. data/lib/pry/helpers/platform.rb +7 -5
  102. data/lib/pry/helpers/table.rb +33 -26
  103. data/lib/pry/helpers/text.rb +22 -19
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +17 -8
  106. data/lib/pry/indent.rb +54 -50
  107. data/lib/pry/input_completer.rb +248 -230
  108. data/lib/pry/input_lock.rb +8 -9
  109. data/lib/pry/inspector.rb +36 -24
  110. data/lib/pry/last_exception.rb +45 -45
  111. data/lib/pry/method.rb +141 -85
  112. data/lib/pry/method/disowned.rb +16 -4
  113. data/lib/pry/method/patcher.rb +12 -3
  114. data/lib/pry/method/weird_method_locator.rb +68 -44
  115. data/lib/pry/object_path.rb +33 -25
  116. data/lib/pry/output.rb +121 -35
  117. data/lib/pry/pager.rb +41 -42
  118. data/lib/pry/prompt.rb +108 -46
  119. data/lib/pry/pry_class.rb +61 -103
  120. data/lib/pry/pry_instance.rb +217 -185
  121. data/lib/pry/repl.rb +18 -22
  122. data/lib/pry/repl_file_loader.rb +27 -21
  123. data/lib/pry/ring.rb +11 -6
  124. data/lib/pry/slop.rb +574 -563
  125. data/lib/pry/slop/commands.rb +164 -169
  126. data/lib/pry/slop/option.rb +172 -168
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable.rb +59 -61
  130. data/lib/pry/testable/evalable.rb +21 -12
  131. data/lib/pry/testable/mockable.rb +18 -10
  132. data/lib/pry/testable/pry_tester.rb +71 -56
  133. data/lib/pry/testable/utility.rb +29 -21
  134. data/lib/pry/testable/variables.rb +49 -43
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +27 -0
  137. data/lib/pry/wrapped_module.rb +51 -42
  138. data/lib/pry/wrapped_module/candidate.rb +21 -14
  139. metadata +34 -33
  140. data/lib/pry/commands.rb +0 -6
  141. data/lib/pry/commands/disabled_commands.rb +0 -2
  142. data/lib/pry/commands/gem_cd.rb +0 -26
  143. data/lib/pry/commands/gem_install.rb +0 -32
  144. data/lib/pry/commands/gem_list.rb +0 -33
  145. data/lib/pry/commands/gem_open.rb +0 -29
  146. data/lib/pry/commands/gem_readme.rb +0 -25
  147. data/lib/pry/commands/gem_search.rb +0 -40
  148. data/lib/pry/commands/gem_stats.rb +0 -83
  149. data/lib/pry/commands/gist.rb +0 -102
  150. data/lib/pry/commands/install_command.rb +0 -54
  151. data/lib/pry/config/behavior.rb +0 -255
  152. data/lib/pry/config/convenience.rb +0 -28
  153. data/lib/pry/config/default.rb +0 -159
  154. data/lib/pry/config/memoization.rb +0 -48
  155. data/lib/pry/plugins.rb +0 -122
  156. data/lib/pry/rubygem.rb +0 -84
  157. data/lib/pry/terminal.rb +0 -91
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  # This class is responsible for taking a string (identifying a
3
5
  # command/class/method/etc) and returning the relevant type of object.
@@ -42,29 +44,30 @@ class Pry
42
44
  # @note If a module defined by C was extended with a lot of methods written
43
45
  # in Ruby, this method would fail.
44
46
  def c_module?
45
- if is_a?(WrappedModule)
47
+ return unless is_a?(WrappedModule)
46
48
 
47
- method_locations = wrapped.methods(false).map do |m|
48
- wrapped.method(m).source_location
49
- end
49
+ method_locations = wrapped.methods(false).map do |m|
50
+ wrapped.method(m).source_location
51
+ end
50
52
 
51
- method_locations.concat(wrapped.instance_methods(false).map do |m|
52
- wrapped.instance_method(m).source_location
53
- end)
53
+ method_locations.concat(
54
+ wrapped.instance_methods(false).map do |m|
55
+ wrapped.instance_method(m).source_location
56
+ end
57
+ )
54
58
 
55
- c_methods = method_locations.grep(nil).count
56
- ruby_methods = method_locations.count - c_methods
59
+ c_methods = method_locations.grep(nil).count
60
+ ruby_methods = method_locations.count - c_methods
57
61
 
58
- c_methods > ruby_methods
59
- end
62
+ c_methods > ruby_methods
60
63
  end
61
64
  end
62
65
 
63
66
  include Pry::Helpers::CommandHelpers
64
67
 
65
68
  class << self
66
- def lookup(str, _pry_, options = {})
67
- co = new(str, _pry_, options)
69
+ def lookup(str, pry_instance, options = {})
70
+ co = new(str, pry_instance, options)
68
71
 
69
72
  co.default_lookup || co.method_or_class_lookup ||
70
73
  co.command_lookup || co.empty_lookup
@@ -73,24 +76,25 @@ class Pry
73
76
 
74
77
  attr_accessor :str
75
78
  attr_accessor :target
76
- attr_accessor :_pry_
79
+ attr_accessor :pry_instance
77
80
  attr_accessor :super_level
78
81
 
79
- def initialize(str, _pry_, options = {})
82
+ def initialize(str, pry_instance, options = {})
80
83
  options = {
81
- super: 0,
84
+ super: 0
82
85
  }.merge!(options)
83
86
 
84
87
  @str = str
85
- @_pry_ = _pry_
86
- @target = _pry_.current_context
88
+ @pry_instance = pry_instance
89
+ @target = pry_instance.current_context
87
90
  @super_level = options[:super]
88
91
  end
89
92
 
93
+ # TODO: just make it so find_command_by_match_or_listing doesn't raise?
90
94
  def command_lookup
91
- # TODO: just make it so find_command_by_match_or_listing doesn't
92
- # raise?
93
- _pry_.commands.find_command_by_match_or_listing(str) rescue nil
95
+ pry_instance.commands.find_command_by_match_or_listing(str)
96
+ rescue StandardError
97
+ nil
94
98
  end
95
99
 
96
100
  # when no paramter is given (i.e CodeObject.lookup(nil)), then we
@@ -112,8 +116,8 @@ class Pry
112
116
 
113
117
  # lookup variables and constants and `self` that are not modules
114
118
  def default_lookup
115
-
116
- # we skip instance methods as we want those to fall through to method_or_class_lookup()
119
+ # we skip instance methods as we want those to fall through to
120
+ # method_or_class_lookup()
117
121
  if safe_to_evaluate?(str) && !looks_like_an_instance_method?(str)
118
122
  obj = target.eval(str)
119
123
 
@@ -123,22 +127,22 @@ class Pry
123
127
  Pry::Method(obj)
124
128
  elsif !obj.is_a?(Module)
125
129
  Pry::WrappedModule(obj.class)
126
- else
127
- nil
128
130
  end
129
131
  end
130
-
131
132
  rescue Pry::RescuableException
132
133
  nil
133
134
  end
134
135
 
135
136
  def method_or_class_lookup
136
- obj = case str
137
- when /\S+\(\)\z/
138
- Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target)
139
- else
140
- Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
141
- end
137
+ obj =
138
+ case str
139
+ when /\S+\(\)\z/
140
+ Pry::Method.from_str(str.sub(/\(\)\z/, ''), target) ||
141
+ Pry::WrappedModule.from_str(str, target)
142
+ else
143
+ Pry::WrappedModule.from_str(str, target) ||
144
+ Pry::Method.from_str(str, target)
145
+ end
142
146
 
143
147
  lookup_super(obj, super_level)
144
148
  end
@@ -182,14 +186,12 @@ class Pry
182
186
  # @param [Object] obj
183
187
  # @param [Fixnum] super_level How far up the super chain to ascend.
184
188
  def lookup_super(obj, super_level)
185
- return nil if !obj
189
+ return unless obj
186
190
 
187
191
  sup = obj.super(super_level)
188
- if !sup
189
- raise Pry::CommandError, "No superclass found for #{obj.wrapped}"
190
- else
191
- sup
192
- end
192
+ raise Pry::CommandError, "No superclass found for #{obj.wrapped}" unless sup
193
+
194
+ sup
193
195
  end
194
196
  end
195
197
  end
@@ -1,65 +1,66 @@
1
- # PP subclass for streaming inspect output in color.
1
+ # frozen_string_literal: true
2
+
3
+ require 'pp'
4
+ require 'English'
5
+
2
6
  class Pry
7
+ # PP subclass for streaming inspect output in color.
3
8
  class ColorPrinter < ::PP
4
- OBJ_COLOR = begin
5
- code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
6
- if code.start_with? "\e"
7
- code
8
- else
9
- "\e[0m\e[0;#{code}m"
9
+ Pry::SyntaxHighlighter.overwrite_coderay_comment_token!
10
+
11
+ def self.default(_output, value, pry_instance)
12
+ pry_instance.pager.open do |pager|
13
+ pager.print pry_instance.config.output_prefix
14
+ pp(value, pager, pry_instance.output.width - 1)
10
15
  end
11
16
  end
12
17
 
13
- CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
14
-
15
- def self.pp(obj, out = $>, width = 79, newline = "\n")
16
- q = ColorPrinter.new(out, width, newline)
17
- q.guard_inspect_key { q.pp obj }
18
- q.flush
19
- out << "\n"
18
+ def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
19
+ queue = ColorPrinter.new(output, max_width, "\n")
20
+ queue.guard_inspect_key { queue.pp(obj) }
21
+ queue.flush
22
+ output << "\n"
20
23
  end
21
24
 
22
- def text(str, width = str.length)
23
- # Don't recolorize output with color [Issue #751]
24
- if str.include?("\e[")
25
- super "#{str}\e[0m", width
26
- elsif str.start_with?('#<') || str == '=' || str == '>'
27
- super highlight_object_literal(str), width
28
- else
29
- super CodeRay.scan(str, :ruby).term, width
30
- end
25
+ def pp(object)
26
+ return super unless object.is_a?(String)
27
+
28
+ # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
29
+ # Strings prettier
30
+ text(object.inspect)
31
+ rescue StandardError => exception
32
+ raise if exception.is_a?(Pry::Pager::StopPaging)
33
+
34
+ text(highlight_object_literal(inspect_object(object)))
31
35
  end
32
36
 
33
- def pp(obj)
34
- if String === obj
35
- # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
36
- # Strings prettier
37
- text(obj.inspect)
37
+ def text(str, max_width = str.length)
38
+ if str.include?("\e[")
39
+ super("#{str}\e[0m", max_width)
40
+ elsif str.start_with?('#<') || %w[= >].include?(str)
41
+ super(highlight_object_literal(str), max_width)
38
42
  else
39
- super
40
- end
41
- rescue => e
42
- raise if e.is_a? Pry::Pager::StopPaging
43
-
44
- begin
45
- str = obj.inspect
46
- rescue Exception
47
- # Read the class name off of the singleton class to provide a default
48
- # inspect.
49
- singleton = class << obj; self; end
50
- ancestors = Pry::Method.safe_send(singleton, :ancestors)
51
- klass = ancestors.reject { |k| k == singleton }.first
52
- obj_id = obj.__id__.to_s(16) rescue 0
53
- str = "#<#{klass}:0x#{obj_id}>"
43
+ super(SyntaxHighlighter.highlight(str), max_width)
54
44
  end
55
-
56
- text highlight_object_literal(str)
57
45
  end
58
46
 
59
47
  private
60
48
 
61
49
  def highlight_object_literal(object_literal)
62
- "#{OBJ_COLOR}#{object_literal}\e[0m"
50
+ code = Pry::SyntaxHighlighter.keyword_token_color
51
+ obj_color = code.start_with?("\e") ? code : "\e[0m\e[0;#{code}m"
52
+ "#{obj_color}#{object_literal}\e[0m"
53
+ end
54
+
55
+ def inspect_object(object)
56
+ object.inspect
57
+ rescue StandardError
58
+ # Read the class name off of the singleton class to provide a default
59
+ # inspect.
60
+ singleton = class << object; self; end
61
+ ancestors = Pry::Method.safe_send(singleton, :ancestors)
62
+ klass = ancestors.find { |k| k != singleton }
63
+ "#<#{klass}:0x#{object.__id__.to_s(16)}>"
63
64
  end
64
65
  end
65
66
  end
data/lib/pry/command.rb CHANGED
@@ -1,19 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'delegate'
2
- require 'pry/helpers/documentation_helpers'
4
+ require 'shellwords'
3
5
 
4
6
  class Pry
5
7
  # The super-class of all commands, new commands should be created by calling
6
- # {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
7
- # which creates a ClassCommand. Please don't use this class directly.
8
+ # {Pry::CommandSet#command} which creates a BlockCommand or
9
+ # {Pry::CommandSet#create_command} which creates a ClassCommand. Please don't
10
+ # use this class directly.
8
11
  class Command
9
12
  extend Helpers::DocumentationHelpers
10
13
  extend CodeObject::Helpers
11
14
 
15
+ include Pry::Helpers::BaseHelpers
16
+ include Pry::Helpers::CommandHelpers
17
+ include Pry::Helpers::Text
18
+
12
19
  # represents a void return value for a command
13
20
  VOID_VALUE = Object.new
14
21
 
15
22
  # give it a nice inspect
16
- def VOID_VALUE.inspect() "void" end
23
+ def VOID_VALUE.inspect
24
+ "void"
25
+ end
17
26
 
18
27
  # Properties of the command itself (as passed as arguments to
19
28
  # {CommandSet#command} or {CommandSet#create_command}).
@@ -45,8 +54,8 @@ class Pry
45
54
  @command_options
46
55
  end
47
56
  # backward compatibility
48
- alias_method :options, :command_options
49
- alias_method :options=, :command_options=
57
+ alias options command_options
58
+ alias options= command_options=
50
59
 
51
60
  # Define or get the command's banner
52
61
  def banner(arg = nil)
@@ -70,43 +79,25 @@ class Pry
70
79
  def source_file
71
80
  Array(block.source_location).first
72
81
  end
73
- alias_method :file, :source_file
82
+ alias file source_file
74
83
 
75
84
  def source_line
76
85
  Array(block.source_location).last
77
86
  end
78
- alias_method :line, :source_line
87
+ alias line source_line
79
88
 
80
89
  def default_options(match)
81
90
  {
82
- requires_gem: [],
83
91
  keep_retval: false,
84
92
  argument_required: false,
85
93
  interpolate: true,
86
94
  shellwords: true,
87
- listing: (String === match ? match : match.inspect),
95
+ listing: (match.is_a?(String) ? match : match.inspect),
88
96
  use_prefix: true,
89
97
  takes_block: false
90
98
  }
91
99
  end
92
- end
93
-
94
- # Make those properties accessible to instances
95
- def name; self.class.name; end
96
-
97
- def match; self.class.match; end
98
-
99
- def description; self.class.description; end
100
-
101
- def block; self.class.block; end
102
-
103
- def command_options; self.class.options; end
104
100
 
105
- def command_name; self.class.command_name; end
106
-
107
- def source; self.class.source; end
108
-
109
- class << self
110
101
  def name
111
102
  super.to_s == "" ? "#<class(Pry::Command #{match.inspect})>" : super
112
103
  end
@@ -116,7 +107,7 @@ class Pry
116
107
  end
117
108
 
118
109
  def command_name
119
- self.options[:listing]
110
+ options[:listing]
120
111
  end
121
112
 
122
113
  # Create a new command with the given properties.
@@ -161,24 +152,21 @@ class Pry
161
152
  # @return [Fixnum]
162
153
  def match_score(val)
163
154
  if command_regex =~ val
164
- Regexp.last_match.size > 1 ? Regexp.last_match.begin(1) : Regexp.last_match.end(0)
155
+ if Regexp.last_match.size > 1
156
+ Regexp.last_match.begin(1)
157
+ else
158
+ Regexp.last_match.end(0)
159
+ end
165
160
  else
166
161
  -1
167
162
  end
168
163
  end
169
164
 
170
- # @deprecated Replaced with {Pry::Hooks#add_hook}. Left for compatibility.
171
- # Store hooks to be run before or after the command body.
172
- def hooks
173
- Pry.hooks
174
- end
175
-
176
165
  def command_regex
177
- pr = Pry.respond_to?(:config) ? Pry.config.command_prefix : ""
178
- prefix = convert_to_regex(pr)
166
+ prefix = convert_to_regex(Pry.config.command_prefix)
179
167
  prefix = "(?:#{prefix})?" unless options[:use_prefix]
180
168
 
181
- /^#{prefix}#{convert_to_regex(match)}(?!\S)/
169
+ /\A#{prefix}#{convert_to_regex(match)}(?!\S)/
182
170
  end
183
171
 
184
172
  def convert_to_regex(obj)
@@ -195,21 +183,24 @@ class Pry
195
183
  # manually overridden if necessary.
196
184
  # Group should not be changed once it is initialized.
197
185
  def group(name = nil)
198
- @group ||= if name
199
- name
200
- else
201
- case Pry::Method(block).source_file
202
- when %r{/pry/.*_commands/(.*).rb}
203
- $1.capitalize.gsub(/_/, " ")
204
- when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)}
205
- name, version = $1, $2
206
- "#{name} (v#{version})"
207
- when /pryrc/
208
- "pryrc"
209
- else
210
- "(other)"
211
- end
212
- end
186
+ @group ||= begin
187
+ name || case Pry::Method(block).source_file
188
+ when %r{/pry/.*_commands/(.*).rb}
189
+ Regexp.last_match(1).capitalize.tr('_', " ")
190
+ when /(pry-\w+)-([\d\.]+([\w\.]+)?)/
191
+ name = Regexp.last_match(1)
192
+ version = Regexp.last_match(2)
193
+ "#{name} (v#{version})"
194
+ when /pryrc/
195
+ "pryrc"
196
+ else
197
+ "(other)"
198
+ end
199
+ end
200
+ end
201
+
202
+ def state
203
+ Pry::CommandState.default.state_for(match)
213
204
  end
214
205
  end
215
206
 
@@ -223,7 +214,8 @@ class Pry
223
214
  attr_accessor :context
224
215
  attr_accessor :command_set
225
216
  attr_accessor :hooks
226
- attr_accessor :_pry_
217
+ attr_accessor :pry_instance
218
+ alias _pry_= pry_instance=
227
219
 
228
220
  # The block we pass *into* a command so long as `:takes_block` is
229
221
  # not equal to `false`
@@ -233,6 +225,47 @@ class Pry
233
225
  # end
234
226
  attr_accessor :command_block
235
227
 
228
+ # Instantiate a command, in preparation for calling it.
229
+ # @param [Hash] context The runtime context to use with this command.
230
+ def initialize(context = {})
231
+ self.context = context
232
+ self.target = context[:target]
233
+ self.output = context[:output]
234
+ self.eval_string = context[:eval_string]
235
+ self.command_set = context[:command_set]
236
+ self.hooks = context[:hooks]
237
+ self.pry_instance = context[:pry_instance]
238
+ end
239
+
240
+ # Make those properties accessible to instances
241
+ def name
242
+ self.class.name
243
+ end
244
+
245
+ def match
246
+ self.class.match
247
+ end
248
+
249
+ def description
250
+ self.class.description
251
+ end
252
+
253
+ def block
254
+ self.class.block
255
+ end
256
+
257
+ def command_options
258
+ self.class.options
259
+ end
260
+
261
+ def command_name
262
+ self.class.command_name
263
+ end
264
+
265
+ def source
266
+ self.class.source
267
+ end
268
+
236
269
  # Run a command from another command.
237
270
  # @param [String] command_string The string that invokes the command
238
271
  # @param [Array] args Further arguments to pass to the command
@@ -243,8 +276,8 @@ class Pry
243
276
  # @example
244
277
  # run "amend-line", "5", 'puts "hello world"'
245
278
  def run(command_string, *args)
246
- command_string = _pry_.config.command_prefix.to_s + command_string
247
- complete_string = "#{command_string} #{args.join(" ")}".rstrip
279
+ command_string = pry_instance.config.command_prefix.to_s + command_string
280
+ complete_string = "#{command_string} #{args.join(' ')}".rstrip
248
281
  command_set.process_line(complete_string, context)
249
282
  end
250
283
 
@@ -256,24 +289,15 @@ class Pry
256
289
  VOID_VALUE
257
290
  end
258
291
 
259
- include Pry::Helpers::BaseHelpers
260
- include Pry::Helpers::CommandHelpers
261
- include Pry::Helpers::Text
262
-
263
- # Instantiate a command, in preparation for calling it.
264
- # @param [Hash] context The runtime context to use with this command.
265
- def initialize(context = {})
266
- self.context = context
267
- self.target = context[:target]
268
- self.output = context[:output]
269
- self.eval_string = context[:eval_string]
270
- self.command_set = context[:command_set]
271
- self.hooks = context[:hooks]
272
- self._pry_ = context[:pry_instance]
292
+ def _pry_
293
+ Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
294
+ pry_instance
273
295
  end
274
296
 
275
297
  # @return [Object] The value of `self` inside the `target` binding.
276
- def target_self; target.eval('self'); end
298
+ def target_self
299
+ target.eval('self')
300
+ end
277
301
 
278
302
  # @return [Hash] Pry commands can store arbitrary state
279
303
  # here. This state persists between subsequent command invocations.
@@ -283,7 +307,7 @@ class Pry
283
307
  # state.my_state = "my state" # this will not conflict with any
284
308
  # # `state.my_state` used in another command.
285
309
  def state
286
- _pry_.command_state[match] ||= Pry::Config.from_hash({})
310
+ self.class.state
287
311
  end
288
312
 
289
313
  # Revaluate the string (str) and perform interpolation.
@@ -304,7 +328,7 @@ class Pry
304
328
  # the current scope.
305
329
  def check_for_command_collision(command_match, arg_string)
306
330
  collision_type = target.eval("defined?(#{command_match})")
307
- collision_type ||= 'local-variable' if arg_string.match(%r{\A\s*[-+*/%&|^]*=})
331
+ collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=}
308
332
 
309
333
  if collision_type
310
334
  output.puts(
@@ -312,7 +336,7 @@ class Pry
312
336
  "which conflicts with a #{collision_type}.\n\n"
313
337
  )
314
338
  end
315
- rescue Pry::RescuableException
339
+ rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions
316
340
  end
317
341
 
318
342
  # Extract necessary information from a line that Command.matches? this
@@ -330,12 +354,14 @@ class Pry
330
354
  # @param [String] val The line of input
331
355
  # @return [Array]
332
356
  def tokenize(val)
333
- val.replace(interpolate_string(val)) if command_options[:interpolate]
357
+ val = interpolate_string(val) if command_options[:interpolate]
334
358
 
335
359
  self.class.command_regex =~ val
336
360
 
337
361
  # please call Command.matches? before Command#call_safely
338
- raise CommandError, "fatal: called a command which didn't match?!" unless Regexp.last_match
362
+ unless Regexp.last_match
363
+ raise CommandError, "fatal: called a command which didn't match?!"
364
+ end
339
365
 
340
366
  captures = Regexp.last_match.captures
341
367
  pos = Regexp.last_match.end(0)
@@ -348,11 +374,16 @@ class Pry
348
374
  # process and pass a block if one is found
349
375
  pass_block(arg_string) if command_options[:takes_block]
350
376
 
351
- if arg_string
352
- args = command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
353
- else
354
- args = []
355
- end
377
+ args =
378
+ if arg_string
379
+ if command_options[:shellwords]
380
+ Shellwords.shellwords(arg_string)
381
+ else
382
+ arg_string.split(" ")
383
+ end
384
+ else
385
+ []
386
+ end
356
387
 
357
388
  [val[0..pos].rstrip, arg_string, captures, args]
358
389
  end
@@ -363,7 +394,9 @@ class Pry
363
394
  def process_line(line)
364
395
  command_match, arg_string, captures, args = tokenize(line)
365
396
 
366
- check_for_command_collision(command_match, arg_string) if Pry.config.collision_warning
397
+ if Pry.config.collision_warning
398
+ check_for_command_collision(command_match, arg_string)
399
+ end
367
400
 
368
401
  self.arg_string = arg_string
369
402
  self.captures = captures
@@ -371,38 +404,15 @@ class Pry
371
404
  call_safely(*(captures + args))
372
405
  end
373
406
 
374
- # Pass a block argument to a command.
375
- # @param [String] arg_string The arguments (as a string) passed to the command.
376
- # We inspect these for a '| do' or a '| {' and if we find it we use it
377
- # to start a block input sequence. Once we have a complete
378
- # block, we save it to an accessor that can be retrieved from the command context.
379
- # Note that if we find the '| do' or '| {' we delete this and the
380
- # elements following it from `arg_string`.
381
- def pass_block(arg_string)
382
- # Workaround for weird JRuby bug where rindex in this case can return nil
383
- # even when there's a match.
384
- arg_string.scan(/\| *(?:do|\{)/)
385
- block_index = $~ && $~.offset(0)[0]
386
-
387
- return if !block_index
388
-
389
- block_init_string = arg_string.slice!(block_index..-1)[1..-1]
390
- prime_string = "proc #{block_init_string}\n"
391
-
392
- if !Pry::Code.complete_expression?(prime_string)
393
- block_string = _pry_.r(target, prime_string)
394
- else
395
- block_string = prime_string
396
- end
397
-
398
- begin
399
- self.command_block = target.eval(block_string)
400
- rescue Pry::RescuableException
401
- raise CommandError, "Incomplete block definition."
402
- end
407
+ # Generate completions for this command
408
+ #
409
+ # @param [String] _search The line typed so far
410
+ # @return [Array<String>] Completion words
411
+ def complete(_search)
412
+ []
403
413
  end
404
414
 
405
- private :pass_block
415
+ private
406
416
 
407
417
  # Run the command with the given `args`.
408
418
  #
@@ -413,19 +423,6 @@ class Pry
413
423
  # @return [Object] The return value of the `#call` method, or
414
424
  # {Command::VOID_VALUE}.
415
425
  def call_safely(*args)
416
- unless dependencies_met?
417
- gems_needed = Array(command_options[:requires_gem])
418
- gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) }
419
- output.puts(<<WARN)
420
- The command #{Helpers::Text.bold(command_name)} is unavailable because it requires the following
421
- gems to be installed: #{(gems_not_installed.join(", "))}
422
-
423
- Type #{Helpers::Text.bold('install-command ' + command_name)} to install the required gems
424
- and activate this command.
425
- WARN
426
- return void
427
- end
428
-
429
426
  if command_options[:argument_required] && args.empty?
430
427
  raise CommandError, "The command '#{command_name}' requires an argument."
431
428
  end
@@ -444,26 +441,41 @@ WARN
444
441
  Symbol.instance_eval { define_method(:call, call_method) } if call_method
445
442
  end
446
443
 
447
- # Are all the gems required to use this command installed?
448
- #
449
- # @return Boolean
450
- def dependencies_met?
451
- @dependencies_met ||= command_dependencies_met?(command_options)
452
- end
444
+ # Pass a block argument to a command.
445
+ # @param [String] arg_string The arguments (as a string) passed to the command.
446
+ # We inspect these for a '| do' or a '| {' and if we find it we use it
447
+ # to start a block input sequence. Once we have a complete
448
+ # block, we save it to an accessor that can be retrieved from the command context.
449
+ # Note that if we find the '| do' or '| {' we delete this and the
450
+ # elements following it from `arg_string`.
451
+ def pass_block(arg_string)
452
+ # Workaround for weird JRuby bug where rindex in this case can return nil
453
+ # even when there's a match.
454
+ arg_string.scan(/\| *(?:do|\{)/)
455
+ block_index = $LAST_MATCH_INFO && $LAST_MATCH_INFO.offset(0)[0]
453
456
 
454
- # Generate completions for this command
455
- #
456
- # @param [String] _search The line typed so far
457
- # @return [Array<String>] Completion words
458
- def complete(_search)
459
- []
460
- end
457
+ return unless block_index
461
458
 
462
- private
459
+ block_init_string = arg_string.slice!(block_index..-1)[1..-1]
460
+ prime_string = "proc #{block_init_string}\n"
461
+
462
+ block_string =
463
+ if !Pry::Code.complete_expression?(prime_string)
464
+ pry_instance.r(target, prime_string)
465
+ else
466
+ prime_string
467
+ end
468
+
469
+ begin
470
+ self.command_block = target.eval(block_string)
471
+ rescue Pry::RescuableException
472
+ raise CommandError, "Incomplete block definition."
473
+ end
474
+ end
463
475
 
464
476
  def find_hooks(event)
465
477
  event_name = "#{event}_#{command_name}"
466
- (self.hooks || self.class.hooks).get_hooks(event_name).values
478
+ (hooks || Pry.hooks || self.class.hooks).get_hooks(event_name).values
467
479
  end
468
480
 
469
481
  def before_hooks
@@ -489,230 +501,20 @@ WARN
489
501
  ret
490
502
  end
491
503
 
492
- # Fix the number of arguments we pass to a block to avoid arity warnings.
493
- # @param [Fixnum] arity The arity of the block
494
- # @param [Array] args The arguments to pass
495
- # @return [Array] A (possibly shorter) array of the arguments to pass
496
- def correct_arg_arity(arity, args)
497
- case
498
- when arity < 0
504
+ # Normalize method arguments according to its arity.
505
+ #
506
+ # @param [Integer] method
507
+ # @param [Array] args
508
+ # @return [Array] a (possibly shorter) array of the arguments to pass
509
+ def normalize_method_args(method, args)
510
+ case method.arity
511
+ when -1
499
512
  args
500
- when arity == 0
513
+ when 0
501
514
  []
502
- when arity > 0
503
- args.values_at(*(0..(arity - 1)).to_a)
504
- end
505
- end
506
- end
507
-
508
- # A super-class for Commands that are created with a single block.
509
- #
510
- # This class ensures that the block is called with the correct number of arguments
511
- # and the right context.
512
- #
513
- # Create subclasses using {Pry::CommandSet#command}.
514
- class BlockCommand < Command
515
- # backwards compatibility
516
- alias_method :opts, :context
517
-
518
- # Call the block that was registered with this command.
519
- # @param [Array<String>] args The arguments passed
520
- # @return [Object] The return value of the block
521
- def call(*args)
522
- instance_exec(*correct_arg_arity(block.arity, args), &block)
523
- end
524
-
525
- def help
526
- "#{command_options[:listing].to_s.ljust(18)} #{description}"
527
- end
528
- end
529
-
530
- # A super-class of Commands with structure.
531
- #
532
- # This class implements the bare-minimum functionality that a command should
533
- # have, namely a --help switch, and then delegates actual processing to its
534
- # subclasses.
535
- #
536
- # Create subclasses using {Pry::CommandSet#create_command}, and override the
537
- # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
538
- # method to actually run the command. If necessary, you can also override
539
- # `setup` which will be called before `options`, for example to require any
540
- # gems your command needs to run, or to set up state.
541
- class ClassCommand < Command
542
- class << self
543
- # Ensure that subclasses inherit the options, description and
544
- # match from a ClassCommand super class.
545
- def inherited(klass)
546
- klass.match match
547
- klass.description description
548
- klass.command_options options
549
- end
550
-
551
- def source
552
- source_object.source
553
- end
554
-
555
- def doc
556
- new.help
557
- end
558
-
559
- def source_location
560
- source_object.source_location
561
- end
562
-
563
- def source_file
564
- source_object.source_file
565
- end
566
- alias_method :file, :source_file
567
-
568
- def source_line
569
- source_object.source_line
570
- end
571
- alias_method :line, :source_line
572
-
573
- private
574
-
575
- # The object used to extract the source for the command.
576
- #
577
- # This should be a `Pry::Method(block)` for a command made with `create_command`
578
- # and a `Pry::WrappedModule(self)` for a command that's a standard class.
579
- # @return [Pry::WrappedModule, Pry::Method]
580
- def source_object
581
- @source_object ||= if name =~ /^[A-Z]/
582
- Pry::WrappedModule(self)
583
- else
584
- Pry::Method(block)
585
- end
586
- end
587
- end
588
-
589
- attr_accessor :opts
590
- attr_accessor :args
591
-
592
- # Set up `opts` and `args`, and then call `process`.
593
- #
594
- # This method will display help if necessary.
595
- #
596
- # @param [Array<String>] args The arguments passed
597
- # @return [Object] The return value of `process` or VOID_VALUE
598
- def call(*args)
599
- setup
600
-
601
- self.opts = slop
602
- self.args = self.opts.parse!(args)
603
-
604
- if opts.present?(:help)
605
- output.puts slop.help
606
- void
607
515
  else
608
- process(*correct_arg_arity(method(:process).arity, args))
516
+ args.values_at(*(0..(method.arity - 1)).to_a)
609
517
  end
610
518
  end
611
-
612
- # Return the help generated by Pry::Slop for this command.
613
- def help
614
- slop.help
615
- end
616
-
617
- # Return an instance of Pry::Slop that can parse either subcommands or the
618
- # options that this command accepts.
619
- def slop
620
- Pry::Slop.new do |opt|
621
- opt.banner(unindent(self.class.banner))
622
- subcommands(opt)
623
- options(opt)
624
- opt.on :h, :help, 'Show this message.'
625
- end
626
- end
627
-
628
- # Generate shell completions
629
- # @param [String] search The line typed so far
630
- # @return [Array<String>] the words to complete
631
- def complete(search)
632
- slop.flat_map do |opt|
633
- [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
634
- end.compact + super
635
- end
636
-
637
- # A method called just before `options(opt)` as part of `call`.
638
- #
639
- # This method can be used to set up any context your command needs to run,
640
- # for example requiring gems, or setting default values for options.
641
- #
642
- # @example
643
- # def setup
644
- # require 'gist'
645
- # @action = :method
646
- # end
647
- def setup; end
648
-
649
- # A method to setup Pry::Slop commands so it can parse the subcommands your
650
- # command expects. If you need to set up default values, use `setup`
651
- # instead.
652
- #
653
- # @example A minimal example
654
- # def subcommands(cmd)
655
- # cmd.command :download do |opt|
656
- # description 'Downloads a content from a server'
657
- #
658
- # opt.on :verbose, 'Use verbose output'
659
- #
660
- # run do |options, arguments|
661
- # ContentDownloader.download(options, arguments)
662
- # end
663
- # end
664
- # end
665
- #
666
- # @example Define the invokation block anywhere you want
667
- # def subcommands(cmd)
668
- # cmd.command :download do |opt|
669
- # description 'Downloads a content from a server'
670
- #
671
- # opt.on :verbose, 'Use verbose output'
672
- # end
673
- # end
674
- #
675
- # def process
676
- # # Perform calculations...
677
- # opts.fetch_command(:download).run do |options, arguments|
678
- # ContentDownloader.download(options, arguments)
679
- # end
680
- # # More calculations...
681
- # end
682
- def subcommands(cmd); end
683
-
684
- # A method to setup Pry::Slop so it can parse the options your command expects.
685
- #
686
- # @note Please don't do anything side-effecty in the main part of this
687
- # method, as it may be called by Pry at any time for introspection reasons.
688
- # If you need to set up default values, use `setup` instead.
689
- #
690
- # @example
691
- # def options(opt)
692
- # opt.banner "Gists methods or classes"
693
- # opt.on(:c, :class, "gist a class") do
694
- # @action = :class
695
- # end
696
- # end
697
- def options(opt); end
698
-
699
- # The actual body of your command should go here.
700
- #
701
- # The `opts` mehod can be called to get the options that Pry::Slop has passed,
702
- # and `args` gives the remaining, unparsed arguments.
703
- #
704
- # The return value of this method is discarded unless the command was
705
- # created with `:keep_retval => true`, in which case it is returned to the
706
- # repl.
707
- #
708
- # @example
709
- # def process
710
- # if opts.present?(:class)
711
- # gist_class
712
- # else
713
- # gist_method
714
- # end
715
- # end
716
- def process; raise CommandError, "command '#{command_name}' not implemented" end
717
519
  end
718
520
  end