pry 0.10.pre.1-i386-mingw32 → 0.10.0.pre3-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +2 -2
  4. data/{README.markdown → README.md} +41 -35
  5. data/lib/pry.rb +82 -139
  6. data/lib/pry/cli.rb +77 -30
  7. data/lib/pry/code.rb +122 -183
  8. data/lib/pry/code/code_file.rb +103 -0
  9. data/lib/pry/code/code_range.rb +71 -0
  10. data/lib/pry/code/loc.rb +92 -0
  11. data/lib/pry/code_object.rb +172 -0
  12. data/lib/pry/color_printer.rb +55 -0
  13. data/lib/pry/command.rb +184 -28
  14. data/lib/pry/command_set.rb +113 -59
  15. data/lib/pry/commands.rb +4 -27
  16. data/lib/pry/commands/amend_line.rb +99 -0
  17. data/lib/pry/commands/bang.rb +20 -0
  18. data/lib/pry/commands/bang_pry.rb +17 -0
  19. data/lib/pry/commands/cat.rb +62 -0
  20. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  21. data/lib/pry/commands/cat/exception_formatter.rb +77 -0
  22. data/lib/pry/commands/cat/file_formatter.rb +67 -0
  23. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  24. data/lib/pry/commands/cd.rb +41 -0
  25. data/lib/pry/commands/change_inspector.rb +27 -0
  26. data/lib/pry/commands/change_prompt.rb +26 -0
  27. data/lib/pry/commands/code_collector.rb +165 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/disabled_commands.rb +2 -0
  30. data/lib/pry/commands/easter_eggs.rb +112 -0
  31. data/lib/pry/commands/edit.rb +195 -0
  32. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +36 -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 +23 -0
  37. data/lib/pry/commands/find_method.rb +193 -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 +32 -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 +101 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +180 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +53 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/list_inspectors.rb +35 -0
  50. data/lib/pry/commands/list_prompts.rb +35 -0
  51. data/lib/pry/commands/ls.rb +114 -0
  52. data/lib/pry/commands/ls/constants.rb +47 -0
  53. data/lib/pry/commands/ls/formatter.rb +49 -0
  54. data/lib/pry/commands/ls/globals.rb +48 -0
  55. data/lib/pry/commands/ls/grep.rb +21 -0
  56. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  57. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  58. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  59. data/lib/pry/commands/ls/local_names.rb +35 -0
  60. data/lib/pry/commands/ls/local_vars.rb +39 -0
  61. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  62. data/lib/pry/commands/ls/methods.rb +57 -0
  63. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  64. data/lib/pry/commands/ls/self_methods.rb +32 -0
  65. data/lib/pry/commands/nesting.rb +25 -0
  66. data/lib/pry/commands/play.rb +103 -0
  67. data/lib/pry/commands/pry_backtrace.rb +25 -0
  68. data/lib/pry/commands/pry_version.rb +17 -0
  69. data/lib/pry/commands/raise_up.rb +32 -0
  70. data/lib/pry/commands/reload_code.rb +62 -0
  71. data/lib/pry/commands/reset.rb +18 -0
  72. data/lib/pry/commands/ri.rb +60 -0
  73. data/lib/pry/commands/save_file.rb +61 -0
  74. data/lib/pry/commands/shell_command.rb +48 -0
  75. data/lib/pry/commands/shell_mode.rb +25 -0
  76. data/lib/pry/commands/show_doc.rb +83 -0
  77. data/lib/pry/commands/show_info.rb +195 -0
  78. data/lib/pry/commands/show_input.rb +17 -0
  79. data/lib/pry/commands/show_source.rb +50 -0
  80. data/lib/pry/commands/simple_prompt.rb +22 -0
  81. data/lib/pry/commands/stat.rb +40 -0
  82. data/lib/pry/commands/switch_to.rb +23 -0
  83. data/lib/pry/commands/toggle_color.rb +24 -0
  84. data/lib/pry/commands/watch_expression.rb +105 -0
  85. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  86. data/lib/pry/commands/whereami.rb +190 -0
  87. data/lib/pry/commands/wtf.rb +57 -0
  88. data/lib/pry/config.rb +20 -229
  89. data/lib/pry/config/behavior.rb +139 -0
  90. data/lib/pry/config/convenience.rb +26 -0
  91. data/lib/pry/config/default.rb +165 -0
  92. data/lib/pry/core_extensions.rb +59 -38
  93. data/lib/pry/editor.rb +133 -0
  94. data/lib/pry/exceptions.rb +77 -0
  95. data/lib/pry/helpers.rb +1 -0
  96. data/lib/pry/helpers/base_helpers.rb +40 -154
  97. data/lib/pry/helpers/command_helpers.rb +19 -130
  98. data/lib/pry/helpers/documentation_helpers.rb +21 -11
  99. data/lib/pry/helpers/table.rb +109 -0
  100. data/lib/pry/helpers/text.rb +8 -9
  101. data/lib/pry/history.rb +61 -45
  102. data/lib/pry/history_array.rb +11 -1
  103. data/lib/pry/hooks.rb +10 -32
  104. data/lib/pry/indent.rb +110 -38
  105. data/lib/pry/input_completer.rb +242 -0
  106. data/lib/pry/input_lock.rb +132 -0
  107. data/lib/pry/inspector.rb +27 -0
  108. data/lib/pry/last_exception.rb +61 -0
  109. data/lib/pry/method.rb +199 -200
  110. data/lib/pry/method/disowned.rb +53 -0
  111. data/lib/pry/method/patcher.rb +125 -0
  112. data/lib/pry/method/weird_method_locator.rb +186 -0
  113. data/lib/pry/module_candidate.rb +39 -33
  114. data/lib/pry/object_path.rb +82 -0
  115. data/lib/pry/output.rb +50 -0
  116. data/lib/pry/pager.rb +234 -0
  117. data/lib/pry/plugins.rb +4 -3
  118. data/lib/pry/prompt.rb +26 -0
  119. data/lib/pry/pry_class.rb +199 -227
  120. data/lib/pry/pry_instance.rb +344 -403
  121. data/lib/pry/rbx_path.rb +1 -1
  122. data/lib/pry/repl.rb +202 -0
  123. data/lib/pry/repl_file_loader.rb +20 -26
  124. data/lib/pry/rubygem.rb +82 -0
  125. data/lib/pry/terminal.rb +79 -0
  126. data/lib/pry/test/helper.rb +170 -0
  127. data/lib/pry/version.rb +1 -1
  128. data/lib/pry/wrapped_module.rb +133 -48
  129. metadata +132 -197
  130. data/.document +0 -2
  131. data/.gemtest +0 -0
  132. data/.gitignore +0 -16
  133. data/.travis.yml +0 -17
  134. data/.yardopts +0 -1
  135. data/CHANGELOG +0 -387
  136. data/CONTRIBUTORS +0 -36
  137. data/Gemfile +0 -2
  138. data/Rakefile +0 -137
  139. data/TODO +0 -117
  140. data/examples/example_basic.rb +0 -15
  141. data/examples/example_command_override.rb +0 -32
  142. data/examples/example_commands.rb +0 -36
  143. data/examples/example_hooks.rb +0 -9
  144. data/examples/example_image_edit.rb +0 -67
  145. data/examples/example_input.rb +0 -7
  146. data/examples/example_input2.rb +0 -29
  147. data/examples/example_output.rb +0 -11
  148. data/examples/example_print.rb +0 -6
  149. data/examples/example_prompt.rb +0 -9
  150. data/examples/helper.rb +0 -6
  151. data/lib/pry/completion.rb +0 -221
  152. data/lib/pry/custom_completions.rb +0 -6
  153. data/lib/pry/default_commands/cd.rb +0 -81
  154. data/lib/pry/default_commands/commands.rb +0 -62
  155. data/lib/pry/default_commands/context.rb +0 -98
  156. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  157. data/lib/pry/default_commands/editing.rb +0 -420
  158. data/lib/pry/default_commands/find_method.rb +0 -169
  159. data/lib/pry/default_commands/gems.rb +0 -84
  160. data/lib/pry/default_commands/gist.rb +0 -187
  161. data/lib/pry/default_commands/help.rb +0 -127
  162. data/lib/pry/default_commands/hist.rb +0 -120
  163. data/lib/pry/default_commands/input_and_output.rb +0 -306
  164. data/lib/pry/default_commands/introspection.rb +0 -410
  165. data/lib/pry/default_commands/ls.rb +0 -272
  166. data/lib/pry/default_commands/misc.rb +0 -38
  167. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  168. data/lib/pry/default_commands/whereami.rb +0 -92
  169. data/lib/pry/extended_commands/experimental.rb +0 -7
  170. data/lib/pry/rbx_method.rb +0 -13
  171. data/man/pry.1 +0 -195
  172. data/man/pry.1.html +0 -204
  173. data/man/pry.1.ronn +0 -141
  174. data/pry.gemspec +0 -46
  175. data/test/candidate_helper1.rb +0 -11
  176. data/test/candidate_helper2.rb +0 -8
  177. data/test/helper.rb +0 -223
  178. data/test/test_cli.rb +0 -78
  179. data/test/test_code.rb +0 -201
  180. data/test/test_command.rb +0 -712
  181. data/test/test_command_helpers.rb +0 -9
  182. data/test/test_command_integration.rb +0 -668
  183. data/test/test_command_set.rb +0 -610
  184. data/test/test_completion.rb +0 -62
  185. data/test/test_control_d_handler.rb +0 -45
  186. data/test/test_default_commands/example.erb +0 -5
  187. data/test/test_default_commands/test_cd.rb +0 -318
  188. data/test/test_default_commands/test_context.rb +0 -280
  189. data/test/test_default_commands/test_documentation.rb +0 -314
  190. data/test/test_default_commands/test_find_method.rb +0 -50
  191. data/test/test_default_commands/test_gems.rb +0 -18
  192. data/test/test_default_commands/test_help.rb +0 -57
  193. data/test/test_default_commands/test_input.rb +0 -428
  194. data/test/test_default_commands/test_introspection.rb +0 -511
  195. data/test/test_default_commands/test_ls.rb +0 -151
  196. data/test/test_default_commands/test_shell.rb +0 -343
  197. data/test/test_default_commands/test_show_source.rb +0 -432
  198. data/test/test_exception_whitelist.rb +0 -21
  199. data/test/test_history_array.rb +0 -65
  200. data/test/test_hooks.rb +0 -521
  201. data/test/test_indent.rb +0 -277
  202. data/test/test_input_stack.rb +0 -86
  203. data/test/test_method.rb +0 -401
  204. data/test/test_pry.rb +0 -463
  205. data/test/test_pry_defaults.rb +0 -419
  206. data/test/test_pry_history.rb +0 -84
  207. data/test/test_pry_output.rb +0 -41
  208. data/test/test_sticky_locals.rb +0 -155
  209. data/test/test_syntax_checking.rb +0 -65
  210. data/test/test_wrapped_module.rb +0 -174
  211. data/test/testrc +0 -2
  212. data/test/testrcbad +0 -2
  213. data/wiki/Customizing-pry.md +0 -397
  214. data/wiki/Home.md +0 -4
@@ -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, :ruby)
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