pry 0.9.10pre1-java → 0.9.11-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG +63 -2
  3. data/CONTRIBUTORS +43 -25
  4. data/Gemfile +7 -0
  5. data/Guardfile +62 -0
  6. data/README.markdown +4 -4
  7. data/Rakefile +34 -35
  8. data/lib/pry.rb +107 -54
  9. data/lib/pry/cli.rb +34 -11
  10. data/lib/pry/code.rb +165 -182
  11. data/lib/pry/code/code_range.rb +70 -0
  12. data/lib/pry/code/loc.rb +92 -0
  13. data/lib/pry/code_object.rb +153 -0
  14. data/lib/pry/command.rb +160 -22
  15. data/lib/pry/command_set.rb +37 -26
  16. data/lib/pry/commands.rb +4 -27
  17. data/lib/pry/commands/amend_line.rb +99 -0
  18. data/lib/pry/commands/bang.rb +20 -0
  19. data/lib/pry/commands/bang_pry.rb +17 -0
  20. data/lib/pry/commands/cat.rb +53 -0
  21. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  22. data/lib/pry/commands/cat/exception_formatter.rb +78 -0
  23. data/lib/pry/commands/cat/file_formatter.rb +84 -0
  24. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  25. data/lib/pry/commands/cd.rb +30 -0
  26. data/lib/pry/commands/code_collector.rb +165 -0
  27. data/lib/pry/commands/deprecated_commands.rb +2 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/easter_eggs.rb +112 -0
  30. data/lib/pry/commands/edit.rb +206 -0
  31. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  32. data/lib/pry/commands/edit/file_and_line_locator.rb +38 -0
  33. data/lib/pry/commands/edit/method_patcher.rb +122 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +24 -0
  37. data/lib/pry/commands/find_method.rb +199 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +29 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +95 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +161 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +51 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/ls.rb +339 -0
  50. data/lib/pry/commands/nesting.rb +25 -0
  51. data/lib/pry/commands/play.rb +69 -0
  52. data/lib/pry/commands/pry_backtrace.rb +26 -0
  53. data/lib/pry/commands/pry_version.rb +17 -0
  54. data/lib/pry/commands/raise_up.rb +32 -0
  55. data/lib/pry/commands/reload_code.rb +39 -0
  56. data/lib/pry/commands/reset.rb +18 -0
  57. data/lib/pry/commands/ri.rb +56 -0
  58. data/lib/pry/commands/save_file.rb +61 -0
  59. data/lib/pry/commands/shell_command.rb +43 -0
  60. data/lib/pry/commands/shell_mode.rb +27 -0
  61. data/lib/pry/commands/show_doc.rb +78 -0
  62. data/lib/pry/commands/show_info.rb +139 -0
  63. data/lib/pry/commands/show_input.rb +17 -0
  64. data/lib/pry/commands/show_source.rb +37 -0
  65. data/lib/pry/commands/simple_prompt.rb +22 -0
  66. data/lib/pry/commands/stat.rb +40 -0
  67. data/lib/pry/commands/switch_to.rb +23 -0
  68. data/lib/pry/commands/toggle_color.rb +20 -0
  69. data/lib/pry/commands/whereami.rb +114 -0
  70. data/lib/pry/commands/wtf.rb +57 -0
  71. data/lib/pry/completion.rb +120 -46
  72. data/lib/pry/config.rb +11 -0
  73. data/lib/pry/core_extensions.rb +30 -19
  74. data/lib/pry/editor.rb +129 -0
  75. data/lib/pry/helpers.rb +1 -0
  76. data/lib/pry/helpers/base_helpers.rb +89 -119
  77. data/lib/pry/helpers/command_helpers.rb +7 -122
  78. data/lib/pry/helpers/table.rb +100 -0
  79. data/lib/pry/helpers/text.rb +4 -4
  80. data/lib/pry/history_array.rb +5 -0
  81. data/lib/pry/hooks.rb +1 -3
  82. data/lib/pry/indent.rb +104 -30
  83. data/lib/pry/method.rb +66 -22
  84. data/lib/pry/module_candidate.rb +26 -15
  85. data/lib/pry/pager.rb +70 -0
  86. data/lib/pry/plugins.rb +1 -2
  87. data/lib/pry/pry_class.rb +63 -22
  88. data/lib/pry/pry_instance.rb +58 -37
  89. data/lib/pry/rubygem.rb +74 -0
  90. data/lib/pry/terminal_info.rb +43 -0
  91. data/lib/pry/test/helper.rb +185 -0
  92. data/lib/pry/version.rb +1 -1
  93. data/lib/pry/wrapped_module.rb +58 -24
  94. data/pry.gemspec +21 -37
  95. data/{test/test_cli.rb → spec/cli_spec.rb} +0 -0
  96. data/spec/code_object_spec.rb +277 -0
  97. data/{test/test_code.rb → spec/code_spec.rb} +19 -1
  98. data/{test/test_command_helpers.rb → spec/command_helpers_spec.rb} +0 -0
  99. data/{test/test_command_integration.rb → spec/command_integration_spec.rb} +38 -46
  100. data/{test/test_command_set.rb → spec/command_set_spec.rb} +18 -1
  101. data/{test/test_command.rb → spec/command_spec.rb} +250 -149
  102. data/spec/commands/amend_line_spec.rb +247 -0
  103. data/spec/commands/bang_spec.rb +19 -0
  104. data/spec/commands/cat_spec.rb +164 -0
  105. data/spec/commands/cd_spec.rb +250 -0
  106. data/spec/commands/disable_pry_spec.rb +25 -0
  107. data/spec/commands/edit_spec.rb +727 -0
  108. data/spec/commands/exit_all_spec.rb +34 -0
  109. data/spec/commands/exit_program_spec.rb +19 -0
  110. data/spec/commands/exit_spec.rb +34 -0
  111. data/{test/test_default_commands/test_find_method.rb → spec/commands/find_method_spec.rb} +27 -7
  112. data/spec/commands/gem_list_spec.rb +26 -0
  113. data/spec/commands/gist_spec.rb +75 -0
  114. data/{test/test_default_commands/test_help.rb → spec/commands/help_spec.rb} +8 -9
  115. data/spec/commands/hist_spec.rb +181 -0
  116. data/spec/commands/jump_to_spec.rb +15 -0
  117. data/spec/commands/ls_spec.rb +177 -0
  118. data/spec/commands/play_spec.rb +140 -0
  119. data/spec/commands/raise_up_spec.rb +56 -0
  120. data/spec/commands/save_file_spec.rb +177 -0
  121. data/spec/commands/show_doc_spec.rb +378 -0
  122. data/spec/commands/show_input_spec.rb +17 -0
  123. data/spec/commands/show_source_spec.rb +597 -0
  124. data/spec/commands/whereami_spec.rb +154 -0
  125. data/spec/completion_spec.rb +233 -0
  126. data/spec/control_d_handler_spec.rb +58 -0
  127. data/spec/editor_spec.rb +79 -0
  128. data/{test/test_exception_whitelist.rb → spec/exception_whitelist_spec.rb} +0 -0
  129. data/{test → spec/fixtures}/candidate_helper1.rb +0 -0
  130. data/{test → spec/fixtures}/candidate_helper2.rb +0 -0
  131. data/{test/test_default_commands → spec/fixtures}/example.erb +0 -0
  132. data/spec/fixtures/example_nesting.rb +33 -0
  133. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  134. data/{test → spec/fixtures}/testrc +0 -0
  135. data/{test → spec/fixtures}/testrcbad +0 -0
  136. data/spec/helper.rb +34 -0
  137. data/spec/helpers/bacon.rb +86 -0
  138. data/spec/helpers/mock_pry.rb +43 -0
  139. data/spec/helpers/table_spec.rb +83 -0
  140. data/{test/test_history_array.rb → spec/history_array_spec.rb} +21 -19
  141. data/{test/test_hooks.rb → spec/hooks_spec.rb} +0 -0
  142. data/{test/test_indent.rb → spec/indent_spec.rb} +24 -0
  143. data/{test/test_input_stack.rb → spec/input_stack_spec.rb} +4 -0
  144. data/{test/test_method.rb → spec/method_spec.rb} +65 -1
  145. data/{test/test_prompt.rb → spec/prompt_spec.rb} +0 -0
  146. data/{test/test_pry_defaults.rb → spec/pry_defaults_spec.rb} +14 -14
  147. data/{test/test_pry_history.rb → spec/pry_history_spec.rb} +15 -0
  148. data/spec/pry_output_spec.rb +95 -0
  149. data/{test/test_pry.rb → spec/pry_spec.rb} +74 -32
  150. data/{test/test_sticky_locals.rb → spec/sticky_locals_spec.rb} +27 -25
  151. data/{test/test_syntax_checking.rb → spec/syntax_checking_spec.rb} +17 -1
  152. data/{test/test_wrapped_module.rb → spec/wrapped_module_spec.rb} +92 -5
  153. metadata +239 -115
  154. data/examples/example_basic.rb +0 -15
  155. data/examples/example_command_override.rb +0 -32
  156. data/examples/example_commands.rb +0 -36
  157. data/examples/example_hooks.rb +0 -9
  158. data/examples/example_image_edit.rb +0 -67
  159. data/examples/example_input.rb +0 -7
  160. data/examples/example_input2.rb +0 -29
  161. data/examples/example_output.rb +0 -11
  162. data/examples/example_print.rb +0 -6
  163. data/examples/example_prompt.rb +0 -9
  164. data/examples/helper.rb +0 -6
  165. data/lib/pry/default_commands/cd.rb +0 -81
  166. data/lib/pry/default_commands/commands.rb +0 -62
  167. data/lib/pry/default_commands/context.rb +0 -98
  168. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  169. data/lib/pry/default_commands/editing.rb +0 -420
  170. data/lib/pry/default_commands/find_method.rb +0 -169
  171. data/lib/pry/default_commands/gems.rb +0 -84
  172. data/lib/pry/default_commands/gist.rb +0 -187
  173. data/lib/pry/default_commands/help.rb +0 -127
  174. data/lib/pry/default_commands/hist.rb +0 -120
  175. data/lib/pry/default_commands/input_and_output.rb +0 -306
  176. data/lib/pry/default_commands/introspection.rb +0 -410
  177. data/lib/pry/default_commands/ls.rb +0 -272
  178. data/lib/pry/default_commands/misc.rb +0 -38
  179. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  180. data/lib/pry/default_commands/whereami.rb +0 -92
  181. data/lib/pry/extended_commands/experimental.rb +0 -7
  182. data/test/helper.rb +0 -223
  183. data/test/test_completion.rb +0 -62
  184. data/test/test_control_d_handler.rb +0 -45
  185. data/test/test_default_commands/test_cd.rb +0 -321
  186. data/test/test_default_commands/test_context.rb +0 -288
  187. data/test/test_default_commands/test_documentation.rb +0 -315
  188. data/test/test_default_commands/test_gems.rb +0 -18
  189. data/test/test_default_commands/test_input.rb +0 -428
  190. data/test/test_default_commands/test_introspection.rb +0 -511
  191. data/test/test_default_commands/test_ls.rb +0 -151
  192. data/test/test_default_commands/test_shell.rb +0 -343
  193. data/test/test_default_commands/test_show_source.rb +0 -432
  194. data/test/test_pry_output.rb +0 -41
@@ -1,95 +0,0 @@
1
- class Pry
2
- module DefaultCommands
3
-
4
- EasterEggs = Pry::CommandSet.new do
5
-
6
- command "nyan-cat", "", :requires_gem => ["nyancat"] do
7
- run ".nyancat"
8
- end
9
-
10
- command(/!s\/(.*?)\/(.*?)/, "") do |source, dest|
11
- eval_string.gsub!(/#{source}/) { dest }
12
- run "show-input"
13
- end
14
-
15
- command "get-naked", "" do
16
- text = %{
17
- --
18
- We dont have to take our clothes off to have a good time.
19
- We could dance & party all night And drink some cherry wine.
20
- -- Jermaine Stewart }
21
- output.puts text
22
- text
23
- end
24
-
25
- command "east-coker", "" do
26
- text = %{
27
- --
28
- Now the light falls
29
- Across the open field, leaving the deep lane
30
- Shuttered with branches, dark in the afternoon,
31
- Where you lean against a bank while a van passes,
32
- And the deep lane insists on the direction
33
- Into the village, in the electric heat
34
- Hypnotised. In a warm haze the sultry light
35
- Is absorbed, not refracted, by grey stone.
36
- The dahlias sleep in the empty silence.
37
- Wait for the early owl.
38
- -- T.S Eliot
39
- }
40
- output.puts text
41
- text
42
- end
43
-
44
- command "cohen-poem", "" do
45
- text = %{
46
- --
47
- When this American woman,
48
- whose thighs are bound in casual red cloth,
49
- comes thundering past my sitting place
50
- like a forest-burning Mongol tribe,
51
- the city is ravished
52
- and brittle buildings of a hundred years
53
- splash into the street;
54
- and my eyes are burnt
55
- for the embroidered Chinese girls,
56
- already old,
57
- and so small between the thin pines
58
- on these enormous landscapes,
59
- that if you turn your head
60
- they are lost for hours.
61
- -- Leonard Cohen
62
- }
63
- output.puts text
64
- text
65
- end
66
-
67
- command "test-ansi", "" do
68
- prev_color = Pry.color
69
- Pry.color = true
70
-
71
- picture = unindent <<-'EOS'.gsub(/[[:alpha:]!]/) { |s| text.red(s) }
72
- ____ _______________________
73
- / \ | A W G |
74
- / O O \ | N I O N ! |
75
- | | | S S R I ! |
76
- \ \__/ / __| I K ! |
77
- \____/ \________________________|
78
- EOS
79
-
80
- if windows_ansi?
81
- move_up = proc { |n| "\e[#{n}F" }
82
- else
83
- move_up = proc { |n| "\e[#{n}A\e[0G" }
84
- end
85
-
86
- output.puts "\n" * 6
87
- output.puts picture.lines.map(&:chomp).reverse.join(move_up[1])
88
- output.puts "\n" * 6
89
- output.puts "** ENV['TERM'] is #{ENV['TERM']} **\n\n"
90
-
91
- Pry.color = prev_color
92
- end
93
- end
94
- end
95
- end
@@ -1,420 +0,0 @@
1
- require 'tempfile'
2
- require 'shellwords'
3
- require 'pry/default_commands/hist'
4
-
5
- class Pry
6
- module DefaultCommands
7
-
8
- Editing = Pry::CommandSet.new do
9
- import Hist
10
-
11
- create_command "!", "Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop.", :use_prefix => false do
12
- def process
13
- output.puts "Input buffer cleared!"
14
- eval_string.replace("")
15
- end
16
- end
17
-
18
- create_command "show-input", "Show the contents of the input buffer for the current multi-line expression." do
19
- def process
20
- output.puts Code.new(eval_string).with_line_numbers
21
- end
22
- end
23
-
24
- create_command "edit" do
25
- description "Invoke the default editor on a file."
26
-
27
- banner <<-BANNER
28
- Usage: edit [--no-reload|--reload] [--line LINE] [--temp|--ex|FILE[:LINE]|--in N]
29
-
30
- Open a text editor. When no FILE is given, edits the pry input buffer.
31
- Ensure Pry.config.editor is set to your editor of choice.
32
-
33
- e.g: `edit sample.rb`
34
- e.g: `edit sample.rb --line 105`
35
- e.g: `edit --ex`
36
-
37
- https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_command
38
- BANNER
39
-
40
- def options(opt)
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
43
- opt.on :t, :temp, "Open an empty temporary file"
44
- opt.on :l, :line, "Jump to this line in the opened file", :argument => true, :as => Integer
45
- opt.on :n, :"no-reload", "Don't automatically reload the edited code"
46
- opt.on :c, :"current", "Open the current __FILE__ and at __LINE__ (as returned by `whereami`)."
47
- opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
48
- end
49
-
50
- def process
51
- if [opts.present?(:ex), opts.present?(:temp), opts.present?(:in), !args.empty?].count(true) > 1
52
- raise CommandError, "Only one of --ex, --temp, --in and FILE may be specified."
53
- end
54
-
55
- if !opts.present?(:ex) && !opts.present?(:current) && args.empty?
56
- # edit of local code, eval'd within pry.
57
- process_local_edit
58
- else
59
- # edit of remote code, eval'd at top-level
60
- process_remote_edit
61
- end
62
- end
63
-
64
- def process_i
65
- case opts[:i]
66
- when Range
67
- (_pry_.input_array[opts[:i]] || []).join
68
- when Fixnum
69
- _pry_.input_array[opts[:i]] || ""
70
- else
71
- return output.puts "Not a valid range: #{opts[:i]}"
72
- end
73
- end
74
-
75
- def process_local_edit
76
- content = case
77
- when opts.present?(:temp)
78
- ""
79
- when opts.present?(:in)
80
- process_i
81
- when eval_string.strip != ""
82
- eval_string
83
- else
84
- _pry_.input_array.reverse_each.find{ |x| x && x.strip != "" } || ""
85
- end
86
-
87
- line = content.lines.count
88
-
89
- temp_file do |f|
90
- f.puts(content)
91
- f.flush
92
- reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
93
- f.close(false)
94
- invoke_editor(f.path, line, reload)
95
- if reload
96
- silence_warnings do
97
- eval_string.replace(File.read(f.path))
98
- end
99
- end
100
- end
101
- end
102
-
103
- def process_remote_edit
104
- if opts.present?(:ex)
105
- if _pry_.last_exception.nil?
106
- raise CommandError, "No exception found."
107
- end
108
-
109
- ex = _pry_.last_exception
110
- bt_index = opts[:ex].to_i
111
-
112
- ex_file, ex_line = ex.bt_source_location_for(bt_index)
113
- if ex_file && RbxPath.is_core_path?(ex_file)
114
- file_name = RbxPath.convert_path_to_full(ex_file)
115
- else
116
- file_name = ex_file
117
- end
118
-
119
- line = ex_line
120
-
121
- if file_name.nil?
122
- raise CommandError, "Exception has no associated file."
123
- end
124
-
125
- if Pry.eval_path == file_name
126
- raise CommandError, "Cannot edit exceptions raised in REPL."
127
- end
128
- elsif opts.present?(:current)
129
- file_name = target.eval("__FILE__")
130
- line = target.eval("__LINE__")
131
- else
132
-
133
- # break up into file:line
134
- file_name = File.expand_path(args.first)
135
- line = file_name.sub!(/:(\d+)$/, "") ? $1.to_i : 1
136
- end
137
-
138
- if not_a_real_file?(file_name)
139
- raise CommandError, "#{file_name} is not a valid file name, cannot edit!"
140
- end
141
-
142
- line = opts[:l].to_i if opts.present?(:line)
143
-
144
- reload = opts.present?(:reload) || ((opts.present?(:ex) || file_name.end_with?(".rb")) && !opts.present?(:'no-reload')) && !Pry.config.disable_auto_reload
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)
151
-
152
- if reload
153
- silence_warnings do
154
- TOPLEVEL_BINDING.eval(File.read(file_name), file_name)
155
- end
156
- end
157
- end
158
- end
159
-
160
- create_command "edit-method" do
161
- description "Edit the source code for a method."
162
-
163
- banner <<-BANNER
164
- Usage: edit-method [OPTIONS] [METH]
165
-
166
- Edit the method METH in an editor.
167
- Ensure Pry.config.editor is set to your editor of choice.
168
-
169
- e.g: `edit-method hello_method`
170
- e.g: `edit-method Pry#rep`
171
- e.g: `edit-method`
172
-
173
- https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_method
174
- BANNER
175
-
176
- command_options :shellwords => false
177
-
178
- def options(opt)
179
- method_options(opt)
180
- opt.on :n, "no-reload", "Do not automatically reload the method's file after editing."
181
- opt.on "no-jump", "Do not fast forward editor to first line of method."
182
- opt.on :p, :patch, "Instead of editing the method's file, try to edit in a tempfile and apply as a monkey patch."
183
- end
184
-
185
- def process
186
- if !Pry.config.editor
187
- raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
188
- end
189
-
190
- begin
191
- @method = method_object
192
- rescue NonMethodContextError => err
193
- end
194
-
195
- if opts.present?(:patch) || (@method && @method.dynamically_defined?)
196
- if err
197
- raise err # can't patch a non-method
198
- end
199
-
200
- process_patch
201
- else
202
- if err && !File.exist?(target.eval('__FILE__'))
203
- raise err # can't edit a non-file
204
- end
205
-
206
- process_file
207
- end
208
- end
209
-
210
- def process_patch
211
- lines = @method.source.lines.to_a
212
-
213
- lines[0] = definition_line_for_owner(lines[0])
214
-
215
- temp_file do |f|
216
- f.puts lines.join
217
- f.flush
218
- f.close(false)
219
- invoke_editor(f.path, 0, true)
220
-
221
- if @method.alias?
222
- with_method_transaction(original_name, @method.owner) do
223
- Pry.new(:input => StringIO.new(File.read(f.path))).rep(@method.owner)
224
- Pry.binding_for(@method.owner).eval("alias #{@method.name} #{original_name}")
225
- end
226
- else
227
- Pry.new(:input => StringIO.new(File.read(f.path))).rep(@method.owner)
228
- end
229
- end
230
- end
231
-
232
- def process_file
233
- file, line = extract_file_and_line
234
-
235
- reload = !opts.present?(:'no-reload') && !Pry.config.disable_auto_reload
236
- invoke_editor(file, opts["no-jump"] ? 0 : line, reload)
237
- silence_warnings do
238
- load file if reload
239
- end
240
- end
241
-
242
- protected
243
- def extract_file_and_line
244
- if @method
245
- if @method.source_type == :c
246
- raise CommandError, "Can't edit a C method."
247
- else
248
- [@method.source_file, @method.source_line]
249
- end
250
- else
251
- [target.eval('__FILE__'), target.eval('__LINE__')]
252
- end
253
- end
254
-
255
- def with_method_transaction(meth_name, target=TOPLEVEL_BINDING)
256
- target = Pry.binding_for(target)
257
- temp_name = "__pry_#{meth_name}__"
258
-
259
- target.eval("alias #{temp_name} #{meth_name}")
260
- yield
261
- target.eval("alias #{meth_name} #{temp_name}")
262
- ensure
263
- target.eval("undef #{temp_name}") rescue nil
264
- end
265
-
266
- # The original name of the method, if it's not present raise an error telling
267
- # the user why we don't work.
268
- #
269
- def original_name
270
- @method.original_name or raise CommandError, "Pry can only patch methods created with the `def` keyword."
271
- end
272
-
273
- # Update the definition line so that it can be eval'd directly on the Method's
274
- # owner instead of from the original context.
275
- #
276
- # In particular this takes `def self.foo` and turns it into `def foo` so that we
277
- # don't end up creating the method on the singleton class of the singleton class
278
- # by accident.
279
- #
280
- # This is necessarily done by String manipulation because we can't find out what
281
- # syntax is needed for the argument list by ruby-level introspection.
282
- #
283
- # @param String The original definition line. e.g. def self.foo(bar, baz=1)
284
- # @return String The new definition line. e.g. def foo(bar, baz=1)
285
- #
286
- def definition_line_for_owner(line)
287
- if line =~ /^def (?:.*?\.)?#{Regexp.escape(original_name)}(?=[\(\s;]|$)/
288
- "def #{original_name}#{$'}"
289
- else
290
- raise CommandError, "Could not find original `def #{original_name}` line to patch."
291
- end
292
- end
293
- end
294
-
295
- create_command(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/) do
296
- description "Amend a line of input in multi-line mode."
297
- command_options :interpolate => false, :listing => "amend-line"
298
-
299
- banner <<-'BANNER'
300
- Amend a line of input in multi-line mode. `amend-line N`, where the N in `amend-line N` represents line to replace.
301
-
302
- Can also specify a range of lines using `amend-line N..M` syntax. Passing '!' as replacement content deletes the line(s) instead.
303
- e.g amend-line 1 puts 'hello world! # replace line 1'
304
- e.g amend-line 1..4 ! # delete lines 1..4
305
- e.g amend-line 3 >puts 'goodbye' # insert before line 3
306
- e.g amend-line puts 'hello again' # no line number modifies immediately preceding line
307
- BANNER
308
-
309
- def process
310
- start_line_number, end_line_number, replacement_line = *args
311
-
312
- if eval_string.empty?
313
- raise CommandError, "No input to amend."
314
- end
315
-
316
- replacement_line = "" if !replacement_line
317
- input_array = eval_string.each_line.to_a
318
-
319
- end_line_number = start_line_number.to_i if !end_line_number
320
- line_range = start_line_number ? (one_index_number(start_line_number.to_i)..one_index_number(end_line_number.to_i)) : input_array.size - 1
321
-
322
- # delete selected lines if replacement line is '!'
323
- if arg_string == "!"
324
- input_array.slice!(line_range)
325
- elsif arg_string.start_with?(">")
326
- insert_slot = Array(line_range).first
327
- input_array.insert(insert_slot, arg_string[1..-1] + "\n")
328
- else
329
- input_array[line_range] = arg_string + "\n"
330
- end
331
- eval_string.replace input_array.join
332
- run "show-input"
333
- end
334
- end
335
-
336
- create_command "play" do
337
- include Helpers::DocumentationHelpers
338
-
339
- description "Play back a string variable or a method or a file as input."
340
-
341
- banner <<-BANNER
342
- Usage: play [OPTIONS] [--help]
343
-
344
- The play command enables you to replay code from files and methods as
345
- if they were entered directly in the Pry REPL. Default action (no
346
- options) is to play the provided string variable
347
-
348
- e.g: `play -i 20 --lines 1..3`
349
- e.g: `play -m Pry#repl --lines 1..-1`
350
- e.g: `play -f Rakefile --lines 5`
351
-
352
- https://github.com/pry/pry/wiki/User-Input#wiki-Play
353
- BANNER
354
-
355
- attr_accessor :content
356
-
357
- def setup
358
- self.content = ""
359
- end
360
-
361
- def options(opt)
362
- opt.on :m, :method, "Play a method's source.", :argument => true do |meth_name|
363
- meth = get_method_or_raise(meth_name, target, {})
364
- self.content << meth.source
365
- end
366
- opt.on :d, :doc, "Play a method's documentation.", :argument => true do |meth_name|
367
- meth = get_method_or_raise(meth_name, target, {})
368
- text.no_color do
369
- self.content << process_comment_markup(meth.doc)
370
- end
371
- end
372
- opt.on :c, :command, "Play a command's source.", :argument => true do |command_name|
373
- command = find_command(command_name)
374
- block = Pry::Method.new(command.block)
375
- self.content << block.source
376
- end
377
- opt.on :f, :file, "Play a file.", :argument => true do |file|
378
- self.content << File.read(File.expand_path(file))
379
- end
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,
382
- :as => Range, :default => -5..-1 do |range|
383
- input_expressions = _pry_.input_array[range] || []
384
- Array(input_expressions).each { |v| self.content << v }
385
- end
386
- opt.on :o, "open", 'When used with the -m switch, it plays the entire method except the last line, leaving the method definition "open". `amend-line` can then be used to modify the method.'
387
- end
388
-
389
- def process
390
- perform_play
391
- run "show-input" unless Pry::Code.complete_expression?(eval_string)
392
- end
393
-
394
- def process_non_opt
395
- args.each do |arg|
396
- begin
397
- self.content << target.eval(arg)
398
- rescue Pry::RescuableException
399
- raise CommandError, "Problem when evaling #{arg}."
400
- end
401
- end
402
- end
403
-
404
- def perform_play
405
- process_non_opt
406
-
407
- if opts.present?(:lines)
408
- self.content = restrict_to_lines(self.content, opts[:l])
409
- end
410
-
411
- if opts.present?(:open)
412
- self.content = restrict_to_lines(self.content, 1..-2)
413
- end
414
-
415
- eval_string << self.content
416
- end
417
- end
418
- end
419
- end
420
- end