pry 0.10.4 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +360 -16
  3. data/LICENSE +1 -1
  4. data/README.md +352 -306
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +81 -74
  10. data/lib/pry/code/code_file.rb +37 -26
  11. data/lib/pry/code/code_range.rb +7 -5
  12. data/lib/pry/code/loc.rb +26 -13
  13. data/lib/pry/code.rb +48 -31
  14. data/lib/pry/code_object.rb +53 -28
  15. data/lib/pry/color_printer.rb +46 -35
  16. data/lib/pry/command.rb +197 -369
  17. data/lib/pry/command_set.rb +89 -114
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -72
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -46
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +62 -54
  27. data/lib/pry/commands/cd.rb +40 -35
  28. data/lib/pry/commands/change_inspector.rb +29 -22
  29. data/lib/pry/commands/change_prompt.rb +48 -23
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  36. data/lib/pry/commands/edit.rb +185 -157
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -16
  40. data/lib/pry/commands/find_method.rb +168 -162
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +151 -149
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +35 -28
  47. data/lib/pry/commands/ls/constants.rb +59 -31
  48. data/lib/pry/commands/ls/formatter.rb +42 -36
  49. data/lib/pry/commands/ls/globals.rb +38 -36
  50. data/lib/pry/commands/ls/grep.rb +17 -15
  51. data/lib/pry/commands/ls/instance_vars.rb +29 -28
  52. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  53. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  54. data/lib/pry/commands/ls/local_names.rb +26 -24
  55. data/lib/pry/commands/ls/local_vars.rb +38 -30
  56. data/lib/pry/commands/ls/ls_entity.rb +47 -52
  57. data/lib/pry/commands/ls/methods.rb +49 -51
  58. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  59. data/lib/pry/commands/ls/self_methods.rb +23 -21
  60. data/lib/pry/commands/ls.rb +124 -103
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +92 -82
  63. data/lib/pry/commands/pry_backtrace.rb +24 -17
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +60 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -42
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +56 -29
  71. data/lib/pry/commands/shell_mode.rb +22 -18
  72. data/lib/pry/commands/show_doc.rb +81 -70
  73. data/lib/pry/commands/show_info.rb +193 -160
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +109 -42
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +20 -16
  79. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  80. data/lib/pry/commands/watch_expression.rb +89 -84
  81. data/lib/pry/commands/whereami.rb +155 -146
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +317 -20
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +22 -9
  90. data/lib/pry/editor.rb +53 -33
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +13 -18
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +20 -62
  96. data/lib/pry/helpers/command_helpers.rb +52 -62
  97. data/lib/pry/helpers/documentation_helpers.rb +20 -12
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +60 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -85
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +81 -55
  104. data/lib/pry/hooks.rb +60 -110
  105. data/lib/pry/indent.rb +72 -66
  106. data/lib/pry/input_completer.rb +199 -158
  107. data/lib/pry/input_lock.rb +7 -10
  108. data/lib/pry/inspector.rb +36 -24
  109. data/lib/pry/last_exception.rb +45 -45
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/method/patcher.rb +14 -8
  112. data/lib/pry/method/weird_method_locator.rb +79 -45
  113. data/lib/pry/method.rb +177 -124
  114. data/lib/pry/object_path.rb +37 -28
  115. data/lib/pry/output.rb +102 -16
  116. data/lib/pry/pager.rb +187 -177
  117. data/lib/pry/plugins.rb +49 -13
  118. data/lib/pry/prompt.rb +213 -25
  119. data/lib/pry/pry_class.rb +106 -93
  120. data/lib/pry/pry_instance.rb +261 -224
  121. data/lib/pry/repl.rb +82 -27
  122. data/lib/pry/repl_file_loader.rb +27 -22
  123. data/lib/pry/ring.rb +89 -0
  124. data/lib/pry/slop/LICENSE +20 -0
  125. data/lib/pry/slop/commands.rb +190 -0
  126. data/lib/pry/slop/option.rb +210 -0
  127. data/lib/pry/slop.rb +672 -0
  128. data/lib/pry/syntax_highlighter.rb +26 -0
  129. data/lib/pry/system_command_handler.rb +17 -0
  130. data/lib/pry/testable/evalable.rb +24 -0
  131. data/lib/pry/testable/mockable.rb +22 -0
  132. data/lib/pry/testable/pry_tester.rb +88 -0
  133. data/lib/pry/testable/utility.rb +34 -0
  134. data/lib/pry/testable/variables.rb +52 -0
  135. data/lib/pry/testable.rb +68 -0
  136. data/lib/pry/version.rb +3 -1
  137. data/lib/pry/warning.rb +27 -0
  138. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +28 -27
  139. data/lib/pry/wrapped_module.rb +66 -57
  140. data/lib/pry.rb +134 -149
  141. metadata +49 -59
  142. data/lib/pry/commands/disabled_commands.rb +0 -2
  143. data/lib/pry/commands/gem_cd.rb +0 -26
  144. data/lib/pry/commands/gem_install.rb +0 -32
  145. data/lib/pry/commands/gem_list.rb +0 -33
  146. data/lib/pry/commands/gem_open.rb +0 -29
  147. data/lib/pry/commands/gist.rb +0 -101
  148. data/lib/pry/commands/install_command.rb +0 -53
  149. data/lib/pry/commands/list_prompts.rb +0 -35
  150. data/lib/pry/commands/simple_prompt.rb +0 -22
  151. data/lib/pry/commands.rb +0 -6
  152. data/lib/pry/config/behavior.rb +0 -139
  153. data/lib/pry/config/convenience.rb +0 -25
  154. data/lib/pry/config/default.rb +0 -161
  155. data/lib/pry/history_array.rb +0 -121
  156. data/lib/pry/rbx_path.rb +0 -22
  157. data/lib/pry/rubygem.rb +0 -82
  158. data/lib/pry/terminal.rb +0 -79
  159. data/lib/pry/test/helper.rb +0 -170
data/lib/pry/plugins.rb CHANGED
@@ -1,6 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ostruct'
4
+
1
5
  class Pry
2
6
  class PluginManager
3
- PRY_PLUGIN_PREFIX = /^pry-/
7
+ PRY_PLUGIN_PREFIX = /^pry-/.freeze
4
8
 
5
9
  # Placeholder when no associated gem found, displays warning
6
10
  class NoPlugin
@@ -8,8 +12,13 @@ class Pry
8
12
  @name = name
9
13
  end
10
14
 
11
- def method_missing(*args)
15
+ def method_missing(*)
12
16
  warn "Warning: The plugin '#{@name}' was not found! (no gem found)"
17
+ super
18
+ end
19
+
20
+ def respond_to_missing?(*)
21
+ false
13
22
  end
14
23
  end
15
24
 
@@ -17,7 +26,10 @@ class Pry
17
26
  attr_accessor :name, :gem_name, :enabled, :spec, :active
18
27
 
19
28
  def initialize(name, gem_name, spec, enabled)
20
- @name, @gem_name, @enabled, @spec = name, gem_name, enabled, spec
29
+ @name = name
30
+ @gem_name = gem_name
31
+ @enabled = enabled
32
+ @spec = spec
21
33
  end
22
34
 
23
35
  # Disable a plugin. (prevents plugin from being loaded, cannot
@@ -35,22 +47,28 @@ class Pry
35
47
  # Load the Command line options defined by this plugin (if they exist)
36
48
  def load_cli_options
37
49
  cli_options_file = File.join(spec.full_gem_path, "lib/#{spec.name}/cli.rb")
38
- require cli_options_file if File.exist?(cli_options_file)
50
+ return unless File.exist?(cli_options_file)
51
+
52
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4.4")
53
+ cli_options_file = File.realpath(cli_options_file)
54
+ end
55
+ require cli_options_file
39
56
  end
57
+
40
58
  # Activate the plugin (require the gem - enables/loads the
41
59
  # plugin immediately at point of call, even if plugin is
42
60
  # disabled)
43
61
  # Does not reload plugin if it's already active.
44
62
  def activate!
45
63
  # Create the configuration object for the plugin.
46
- Pry.config.send("#{gem_name.gsub('-', '_')}=", Pry::Config.from_hash({}))
64
+ Pry.config.send("#{gem_name.tr('-', '_')}=", OpenStruct.new)
47
65
 
48
66
  begin
49
- require gem_name if !active?
67
+ require gem_name unless active?
50
68
  rescue LoadError => e
51
69
  warn "Found plugin #{gem_name}, but could not require '#{gem_name}'"
52
70
  warn e
53
- rescue => e
71
+ rescue StandardError => e
54
72
  warn "require '#{gem_name}' # Failed, saying: #{e}"
55
73
  end
56
74
 
@@ -60,6 +78,16 @@ class Pry
60
78
 
61
79
  alias active? active
62
80
  alias enabled? enabled
81
+
82
+ def supported?
83
+ pry_version = Gem::Version.new(VERSION)
84
+ spec.dependencies.each do |dependency|
85
+ if dependency.name == "pry"
86
+ return dependency.requirement.satisfied_by?(pry_version)
87
+ end
88
+ end
89
+ true
90
+ end
63
91
  end
64
92
 
65
93
  def initialize
@@ -68,11 +96,12 @@ class Pry
68
96
 
69
97
  # Find all installed Pry plugins and store them in an internal array.
70
98
  def locate_plugins
71
- Gem.refresh
72
- (Gem::Specification.respond_to?(:each) ? Gem::Specification : Gem.source_index.find_name('')).each do |gem|
99
+ gem_list.each do |gem|
73
100
  next if gem.name !~ PRY_PLUGIN_PREFIX
101
+
74
102
  plugin_name = gem.name.split('-', 2).last
75
- @plugins << Plugin.new(plugin_name, gem.name, gem, true) if !gem_located?(gem.name)
103
+ plugin = Plugin.new(plugin_name, gem.name, gem, false)
104
+ @plugins << plugin.tap(&:enable!) if plugin.supported? && !plugin_located?(plugin)
76
105
  end
77
106
  @plugins
78
107
  end
@@ -95,9 +124,16 @@ class Pry
95
124
  end
96
125
 
97
126
  private
98
- def gem_located?(gem_name)
99
- @plugins.any? { |plugin| plugin.gem_name == gem_name }
127
+
128
+ def plugin_located?(plugin)
129
+ @plugins.any? { |existing| existing.gem_name == plugin.gem_name }
100
130
  end
101
- end
102
131
 
132
+ def gem_list
133
+ Gem.refresh
134
+ return Gem::Specification if Gem::Specification.respond_to?(:each)
135
+
136
+ Gem.source_index.find_name('')
137
+ end
138
+ end
103
139
  end
data/lib/pry/prompt.rb CHANGED
@@ -1,26 +1,214 @@
1
- class Pry::Prompt
2
- MAP = {
3
- "default" => {
4
- value: Pry::DEFAULT_PROMPT,
5
- description: "The default Pry prompt. Includes information about the\n" \
6
- "current expression number, evaluation context, and nesting\n" \
7
- "level, plus a reminder that you're using Pry."
8
- },
9
-
10
- "simple" => {
11
- value: Pry::SIMPLE_PROMPT,
12
- description: "A simple '>>'."
13
- },
14
-
15
- "nav" => {
16
- value: Pry::NAV_PROMPT,
17
- description: "A prompt that displays the binding stack as a path and\n" \
18
- "includes information about _in_ and _out_."
19
- },
20
-
21
- "none" => {
22
- value: Pry::NO_PROMPT,
23
- description: "Wave goodbye to the Pry prompt."
24
- }
25
- }
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # Prompt represents the Pry prompt, which can be used with Readline-like
5
+ # libraries. It defines a few default prompts (default prompt, simple prompt,
6
+ # etc) and also provides an API for adding and implementing custom prompts.
7
+ #
8
+ # @example Registering a new Pry prompt
9
+ # Pry::Prompt.add(
10
+ # :ipython,
11
+ # 'IPython-like prompt', [':', '...:']
12
+ # ) do |_context, _nesting, pry_instance, sep|
13
+ # sep == ':' ? "In [#{pry_instance.input_ring.count}]: " : ' ...: '
14
+ # end
15
+ #
16
+ # # Produces:
17
+ # # In [3]: def foo
18
+ # # ...: puts 'foo'
19
+ # # ...: end
20
+ # # => :foo
21
+ # # In [4]:
22
+ #
23
+ # @example Manually instantiating the Prompt class
24
+ # prompt_procs = [
25
+ # proc { '#{rand(1)}>" },
26
+ # proc { "#{('a'..'z').to_a.sample}*" }
27
+ # ]
28
+ # prompt = Pry::Prompt.new(
29
+ # :random,
30
+ # 'Random number or letter prompt.',
31
+ # prompt_procs
32
+ # )
33
+ # prompt.wait_proc.call(...) #=>
34
+ # prompt.incomplete_proc.call(...)
35
+ #
36
+ # @since v0.11.0
37
+ # @api public
38
+ class Prompt
39
+ # A Hash that holds all prompts. The keys of the Hash are prompt
40
+ # names, the values are Hash instances of the format {:description, :value}.
41
+ @prompts = {}
42
+
43
+ class << self
44
+ # Retrieves a prompt.
45
+ #
46
+ # @example
47
+ # Prompt[:my_prompt]
48
+ #
49
+ # @param [Symbol] name The name of the prompt you want to access
50
+ # @return [Hash{Symbol=>Object}]
51
+ # @since v0.12.0
52
+ def [](name)
53
+ @prompts[name.to_s]
54
+ end
55
+
56
+ # @return [Hash{Symbol=>Hash}] the duplicate of the internal prompts hash
57
+ # @note Use this for read-only operations
58
+ # @since v0.12.0
59
+ def all
60
+ @prompts.dup
61
+ end
62
+
63
+ # Adds a new prompt to the prompt hash.
64
+ #
65
+ # @param [Symbol] name
66
+ # @param [String] description
67
+ # @param [Array<String>] separators The separators to differentiate
68
+ # between prompt modes (default mode and class/method definition mode).
69
+ # The Array *must* have a size of 2.
70
+ # @yield [context, nesting, pry_instance, sep]
71
+ # @yieldparam context [Object] the context where Pry is currently in
72
+ # @yieldparam nesting [Integer] whether the context is nested
73
+ # @yieldparam pry_instance [Pry] the Pry instance
74
+ # @yieldparam separator [String] separator string
75
+ # @return [nil]
76
+ # @raise [ArgumentError] if the size of `separators` is not 2
77
+ # @raise [ArgumentError] if `prompt_name` is already occupied
78
+ # @since v0.12.0
79
+ def add(name, description = '', separators = %w[> *])
80
+ name = name.to_s
81
+
82
+ unless separators.size == 2
83
+ raise ArgumentError, "separators size must be 2, given #{separators.size}"
84
+ end
85
+
86
+ if @prompts.key?(name)
87
+ raise ArgumentError, "the '#{name}' prompt was already added"
88
+ end
89
+
90
+ @prompts[name] = new(
91
+ name,
92
+ description,
93
+ separators.map do |sep|
94
+ proc do |context, nesting, pry_instance|
95
+ yield(context, nesting, pry_instance, sep)
96
+ end
97
+ end
98
+ )
99
+
100
+ nil
101
+ end
102
+ end
103
+
104
+ # @return [String]
105
+ attr_reader :name
106
+
107
+ # @return [String]
108
+ attr_reader :description
109
+
110
+ # @return [Array<Proc>] the array of procs that hold
111
+ # `[wait_proc, incomplete_proc]`
112
+ attr_reader :prompt_procs
113
+
114
+ # @param [String] name
115
+ # @param [String] description
116
+ # @param [Array<Proc>] prompt_procs
117
+ def initialize(name, description, prompt_procs)
118
+ @name = name
119
+ @description = description
120
+ @prompt_procs = prompt_procs
121
+ end
122
+
123
+ # @return [Proc] the proc which builds the wait prompt (`>`)
124
+ def wait_proc
125
+ @prompt_procs.first
126
+ end
127
+
128
+ # @return [Proc] the proc which builds the prompt when in the middle of an
129
+ # expression such as open method, etc. (`*`)
130
+ def incomplete_proc
131
+ @prompt_procs.last
132
+ end
133
+
134
+ # @deprecated Use a `Pry::Prompt` instance directly
135
+ def [](key)
136
+ key = key.to_s
137
+ if %w[name description].include?(key)
138
+ Pry::Warning.warn(
139
+ "`Pry::Prompt[:#{@name}][:#{key}]` is deprecated. " \
140
+ "Use `#{self.class}##{key}` instead"
141
+ )
142
+ public_send(key)
143
+ elsif key.to_s == 'value'
144
+ Pry::Warning.warn(
145
+ "`#{self.class}[:#{@name}][:value]` is deprecated. Use " \
146
+ "`#{self.class}#prompt_procs` instead or an instance of " \
147
+ "`#{self.class}` directly"
148
+ )
149
+ @prompt_procs
150
+ end
151
+ end
152
+
153
+ add(
154
+ :default,
155
+ "The default Pry prompt. Includes information about the current expression \n" \
156
+ "number, evaluation context, and nesting level, plus a reminder that you're \n" \
157
+ 'using Pry.'
158
+ ) do |context, nesting, pry_instance, sep|
159
+ format(
160
+ "[%<in_count>s] %<name>s(%<context>s)%<nesting>s%<separator>s ",
161
+ in_count: pry_instance.input_ring.count,
162
+ name: pry_instance.config.prompt_name,
163
+ context: Pry.view_clip(context),
164
+ nesting: (nesting > 0 ? ":#{nesting}" : ''),
165
+ separator: sep
166
+ )
167
+ end
168
+
169
+ add(
170
+ :simple,
171
+ "A simple `>>`.",
172
+ ['>> ', ' | ']
173
+ ) do |_, _, _, sep|
174
+ sep
175
+ end
176
+
177
+ add(
178
+ :nav,
179
+ "A prompt that displays the binding stack as a path and includes information \n" \
180
+ "about #{Helpers::Text.bold('_in_')} and #{Helpers::Text.bold('_out_')}.",
181
+ %w[> *]
182
+ ) do |_context, _nesting, pry_instance, sep|
183
+ tree = pry_instance.binding_stack.map { |b| Pry.view_clip(b.eval('self')) }
184
+ format(
185
+ "[%<in_count>s] (%<name>s) %<tree>s: %<stack_size>s%<separator>s ",
186
+ in_count: pry_instance.input_ring.count,
187
+ name: pry_instance.config.prompt_name,
188
+ tree: tree.join(' / '),
189
+ stack_size: pry_instance.binding_stack.size - 1,
190
+ separator: sep
191
+ )
192
+ end
193
+
194
+ add(
195
+ :shell,
196
+ 'A prompt that displays `$PWD` as you change it.',
197
+ %w[$ *]
198
+ ) do |context, _nesting, pry_instance, sep|
199
+ format(
200
+ "%<name>s %<context>s:%<pwd>s %<separator>s ",
201
+ name: pry_instance.config.prompt_name,
202
+ context: Pry.view_clip(context),
203
+ pwd: Dir.pwd,
204
+ separator: sep
205
+ )
206
+ end
207
+
208
+ add(
209
+ :none,
210
+ 'Wave goodbye to the Pry prompt.',
211
+ Array.new(2)
212
+ ) { '' }
213
+ end
26
214
  end