pry 0.10.pre.1-java → 0.10.0.pre2-java

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 +126 -182
  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
@@ -0,0 +1,41 @@
1
+ class Pry
2
+ class Command::Cd < Pry::ClassCommand
3
+ match 'cd'
4
+ group 'Context'
5
+ description 'Move into a new context (object or scope).'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: cd [OPTIONS] [--help]
9
+
10
+ Move into new context (object or scope). As in UNIX shells use `cd ..` to go
11
+ back, `cd /` to return to Pry top-level and `cd -` to toggle between last two
12
+ scopes. Complex syntax (e.g `cd ../@x/@y`) also supported.
13
+
14
+ cd @x
15
+ cd ..
16
+ cd /
17
+ cd -
18
+
19
+ https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
20
+ BANNER
21
+
22
+ def process
23
+ state.old_stack ||= []
24
+
25
+ if arg_string.strip == "-"
26
+ unless state.old_stack.empty?
27
+ _pry_.binding_stack, state.old_stack = state.old_stack, _pry_.binding_stack
28
+ end
29
+ else
30
+ stack = ObjectPath.new(arg_string, _pry_.binding_stack).resolve
31
+
32
+ if stack && stack != _pry_.binding_stack
33
+ state.old_stack = _pry_.binding_stack
34
+ _pry_.binding_stack = stack
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ Pry::Commands.add_command(Pry::Command::Cd)
41
+ end
@@ -0,0 +1,27 @@
1
+ class Pry::Command::ChangeInspector < Pry::ClassCommand
2
+ match 'change-inspector'
3
+ group 'Input and Output'
4
+ description 'Change the current inspector proc.'
5
+ command_options argument_required: true
6
+ banner <<-BANNER
7
+ Usage: change-inspector NAME
8
+
9
+ Change the proc used to print return values. See list-inspectors for a list
10
+ of available procs and a short description of what each one does.
11
+ BANNER
12
+
13
+ def process(inspector)
14
+ if inspector_map.key?(inspector)
15
+ _pry_.print = inspector_map[inspector][:value]
16
+ output.puts "Switched to the '#{inspector}' inspector!"
17
+ else
18
+ raise Pry::CommandError, "'#{inspector}' isn't a known inspector!"
19
+ end
20
+ end
21
+
22
+ private
23
+ def inspector_map
24
+ Pry::Inspector::MAP
25
+ end
26
+ Pry::Commands.add_command(self)
27
+ end
@@ -0,0 +1,26 @@
1
+ class Pry::Command::ChangePrompt < Pry::ClassCommand
2
+ match 'change-prompt'
3
+ group 'Input and Output'
4
+ description 'Change the current prompt.'
5
+ command_options argument_required: true
6
+ banner <<-BANNER
7
+ Usage: change-prompt NAME
8
+
9
+ Change the current prompt. See list-prompts for a list of available
10
+ prompts.
11
+ BANNER
12
+
13
+ def process(prompt)
14
+ if prompt_map.key?(prompt)
15
+ _pry_.prompt = prompt_map[prompt][:value]
16
+ else
17
+ raise Pry::CommandError, "'#{prompt}' isn't a known prompt!"
18
+ end
19
+ end
20
+
21
+ private
22
+ def prompt_map
23
+ Pry::Prompt::MAP
24
+ end
25
+ Pry::Commands.add_command(self)
26
+ end
@@ -0,0 +1,165 @@
1
+ class Pry
2
+ class Command::CodeCollector
3
+ include Helpers::CommandHelpers
4
+
5
+ attr_reader :args
6
+ attr_reader :opts
7
+ attr_reader :_pry_
8
+
9
+ # The name of the explicitly given file (if any).
10
+ attr_accessor :file
11
+
12
+ class << self
13
+ attr_accessor :input_expression_ranges
14
+ attr_accessor :output_result_ranges
15
+ end
16
+
17
+ @input_expression_ranges = []
18
+ @output_result_ranges = []
19
+
20
+ def initialize(args, opts, _pry_)
21
+ @args = args
22
+ @opts = opts
23
+ @_pry_ = _pry_
24
+ end
25
+
26
+ # Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
27
+ def self.inject_options(opt)
28
+ @input_expression_ranges = []
29
+ @output_result_ranges = []
30
+
31
+ opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number or range",
32
+ :optional_argument => true, :as => Range, :default => 1..-1
33
+ opt.on :o, :out, "Select lines from Pry's output result history. Takes an index or range",
34
+ :optional_argument => true, :as => Range, :default => -5..-1 do |r|
35
+ output_result_ranges << (r || (-5..-1))
36
+ end
37
+ opt.on :i, :in, "Select lines from Pry's input expression history. Takes an index or range",
38
+ :optional_argument => true, :as => Range, :default => -5..-1 do |r|
39
+ input_expression_ranges << (r || (-5..-1))
40
+ end
41
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors",
42
+ :as => :count
43
+ opt.on :d, :doc, "Select lines from the code object's documentation"
44
+ end
45
+
46
+ # The content (i.e code/docs) for the selected object.
47
+ # If the user provided a bare code object, it returns the source.
48
+ # If the user provided the `-i` or `-o` switches, it returns the
49
+ # selected input/output lines joined as a string. If the user used
50
+ # `-d CODE_OBJECT` it returns the docs for that code object.
51
+ #
52
+ # @return [String]
53
+ def content
54
+ return @content if @content
55
+ raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination?
56
+
57
+ content = case
58
+ when opts.present?(:o)
59
+ pry_output_content
60
+ when opts.present?(:i)
61
+ pry_input_content
62
+ when opts.present?(:d)
63
+ code_object_doc
64
+ else
65
+ code_object_source_or_file
66
+ end
67
+
68
+ @content ||= restrict_to_lines(content, line_range)
69
+ end
70
+
71
+ # The code object
72
+ #
73
+ # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
74
+ def code_object
75
+ Pry::CodeObject.lookup(obj_name, _pry_, :super => opts[:super])
76
+ end
77
+
78
+ # Given a string and a range, return the `range` lines of that
79
+ # string.
80
+ #
81
+ # @param [String] content
82
+ # @param [Range, Fixnum] range
83
+ # @return [String] The string restricted to the given range
84
+ def restrict_to_lines(content, range)
85
+ Array(content.lines.to_a[range]).join
86
+ end
87
+
88
+ # The selected `_pry_.output_array` as a string, as specified by
89
+ # the `-o` switch.
90
+ #
91
+ # @return [String]
92
+ def pry_output_content
93
+ pry_array_content_as_string(_pry_.output_array, self.class.output_result_ranges) do |v|
94
+ _pry_.config.gist.inspecter.call(v)
95
+ end
96
+ end
97
+
98
+ # The selected `_pry_.input_array` as a string, as specified by
99
+ # the `-i` switch.
100
+ #
101
+ # @return [String]
102
+ def pry_input_content
103
+ pry_array_content_as_string(_pry_.input_array, self.class.input_expression_ranges) { |v| v }
104
+ end
105
+
106
+ # The line range passed to `--lines`, converted to a 0-indexed range.
107
+ def line_range
108
+ opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
109
+ end
110
+
111
+ # Name of the object argument
112
+ def obj_name
113
+ @obj_name ||= args.empty? ? "" : args.join(" ")
114
+ end
115
+
116
+ private
117
+
118
+ def bad_option_combination?
119
+ [opts.present?(:in), opts.present?(:out),
120
+ !args.empty?].count(true) > 1
121
+ end
122
+
123
+ def pry_array_content_as_string(array, ranges, &block)
124
+ all = ''
125
+ ranges.each do |range|
126
+ raise CommandError, "Minimum value for range is 1, not 0." if convert_to_range(range).first == 0
127
+
128
+ ranged_array = Array(array[range]) || []
129
+ ranged_array.compact.each { |v| all << block.call(v) }
130
+ end
131
+
132
+ all
133
+ end
134
+
135
+ def code_object_doc
136
+ (code_object && code_object.doc) or could_not_locate(obj_name)
137
+ end
138
+
139
+ def code_object_source_or_file
140
+ (code_object && code_object.source) || file_content
141
+ end
142
+
143
+ def file_content
144
+ if File.exists?(obj_name)
145
+ # Set the file accessor.
146
+ self.file = obj_name
147
+ File.read(obj_name)
148
+ else
149
+ could_not_locate(obj_name)
150
+ end
151
+ end
152
+
153
+ def could_not_locate(name)
154
+ raise CommandError, "Cannot locate: #{name}!"
155
+ end
156
+
157
+ def convert_to_range(n)
158
+ if !n.is_a?(Range)
159
+ (n..n)
160
+ else
161
+ n
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,27 @@
1
+ class Pry
2
+ class Command::DisablePry < Pry::ClassCommand
3
+ match 'disable-pry'
4
+ group 'Navigating Pry'
5
+ description 'Stops all future calls to pry and exits the current session.'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: disable-pry
9
+
10
+ After this command is run any further calls to pry will immediately return `nil`
11
+ without interrupting the flow of your program. This is particularly useful when
12
+ you've debugged the problem you were having, and now wish the program to run to
13
+ the end.
14
+
15
+ As alternatives, consider using `exit!` to force the current Ruby process
16
+ to quit immediately; or using `edit-method -p` to remove the `binding.pry`
17
+ from the code.
18
+ BANNER
19
+
20
+ def process
21
+ ENV['DISABLE_PRY'] = 'true'
22
+ _pry_.run_command "exit"
23
+ end
24
+ end
25
+
26
+ Pry::Commands.add_command(Pry::Command::DisablePry)
27
+ end
@@ -0,0 +1,2 @@
1
+ Pry::Commands.disabled_command("edit-method", "Use `edit` instead.")
2
+ Pry::Commands.disabled_command("show-command", "Use show-source [command_name] instead.")
@@ -0,0 +1,112 @@
1
+ class Pry
2
+ Pry::Commands.instance_eval do
3
+ command "nyan-cat", "", :requires_gem => ["nyancat"] do
4
+ run ".nyancat"
5
+ end
6
+
7
+ command(/!s\/(.*?)\/(.*?)/, "") do |source, dest|
8
+ eval_string.gsub!(/#{source}/) { dest }
9
+ run "show-input"
10
+ end
11
+
12
+ command "get-naked", "" do
13
+ text = %{
14
+ --
15
+ We dont have to take our clothes off to have a good time.
16
+ We could dance & party all night And drink some cherry wine.
17
+ -- Jermaine Stewart }
18
+ output.puts text
19
+ text
20
+ end
21
+
22
+ command "east-coker", "" do
23
+ text = %{
24
+ --
25
+ Now the light falls
26
+ Across the open field, leaving the deep lane
27
+ Shuttered with branches, dark in the afternoon,
28
+ Where you lean against a bank while a van passes,
29
+ And the deep lane insists on the direction
30
+ Into the village, in the electric heat
31
+ Hypnotised. In a warm haze the sultry light
32
+ Is absorbed, not refracted, by grey stone.
33
+ The dahlias sleep in the empty silence.
34
+ Wait for the early owl.
35
+ -- T.S Eliot
36
+ }
37
+ output.puts text
38
+ text
39
+ end
40
+
41
+ command "cohen-poem", "" do
42
+ text = %{
43
+ --
44
+ When this American woman,
45
+ whose thighs are bound in casual red cloth,
46
+ comes thundering past my sitting place
47
+ like a forest-burning Mongol tribe,
48
+ the city is ravished
49
+ and brittle buildings of a hundred years
50
+ splash into the street;
51
+ and my eyes are burnt
52
+ for the embroidered Chinese girls,
53
+ already old,
54
+ and so small between the thin pines
55
+ on these enormous landscapes,
56
+ that if you turn your head
57
+ they are lost for hours.
58
+ -- Leonard Cohen
59
+ }
60
+ output.puts text
61
+ text
62
+ end
63
+
64
+ command "pessoa-poem", "" do
65
+ output.puts <<-TEXT
66
+ --
67
+ I've gone to bed with every feeling,
68
+ I've been the pimp of every emotion,
69
+ All felt sensations have bought me drinks,
70
+ I've traded glances with every motive for every act,
71
+ I've held hands with every urge to depart,
72
+ ..
73
+ Rage, foam, the vastness that doesn't fit in my handkerchief,
74
+ The dog in heat howling in the night,
75
+ The pond from the farm going in circles around my insomnia,
76
+ The woods as they were, on our late-afternoon walks, the rose,
77
+ The indifferent tuft of hair, the moss, the pines,
78
+ The rage of not containing all this, not retaining all this,
79
+ O abstract hunger for things, impotent libido for moments,
80
+ Intellectual orgy of feeling life!
81
+ -- Fernando Pessoa
82
+ TEXT
83
+ end
84
+
85
+ command "test-ansi", "" do
86
+ prev_color = _pry_.config.color
87
+ _pry_.config.color = true
88
+
89
+ picture = unindent <<-'EOS'.gsub(/[[:alpha:]!]/) { |s| text.red(s) }
90
+ ____ _______________________
91
+ / \ | A W G |
92
+ / O O \ | N I O N ! |
93
+ | | | S S R I ! |
94
+ \ \__/ / __| I K ! |
95
+ \____/ \________________________|
96
+ EOS
97
+
98
+ if windows_ansi?
99
+ move_up = proc { |n| "\e[#{n}F" }
100
+ else
101
+ move_up = proc { |n| "\e[#{n}A\e[0G" }
102
+ end
103
+
104
+ output.puts "\n" * 6
105
+ output.puts picture.lines.map(&:chomp).reverse.join(move_up[1])
106
+ output.puts "\n" * 6
107
+ output.puts "** ENV['TERM'] is #{ENV['TERM']} **\n\n"
108
+
109
+ _pry_.config.color = prev_color
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,195 @@
1
+ class Pry
2
+ class Command::Edit < Pry::ClassCommand
3
+ require 'pry/commands/edit/exception_patcher'
4
+ require 'pry/commands/edit/file_and_line_locator'
5
+
6
+ match 'edit'
7
+ group 'Editing'
8
+ description 'Invoke the default editor on a file.'
9
+
10
+ banner <<-'BANNER'
11
+ Usage: edit [--no-reload|--reload|--patch] [--line LINE] [--temp|--ex|FILE[:LINE]|OBJECT|--in N]
12
+
13
+ Open a text editor. When no FILE is given, edits the pry input buffer.
14
+ When a method/module/command is given, the code is opened in an editor.
15
+ Ensure `Pry.config.editor` or `_pry_.config.editor` is set to your editor of choice.
16
+
17
+ edit sample.rb edit -p MyClass#my_method
18
+ edit sample.rb --line 105 edit MyClass
19
+ edit MyClass#my_method edit --ex
20
+ edit --method edit --ex -p
21
+
22
+ https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_command
23
+ BANNER
24
+
25
+ def options(opt)
26
+ opt.on :e, :ex, "Open the file that raised the most recent exception (_ex_.file)",
27
+ :optional_argument => true, :as => Integer
28
+ opt.on :i, :in, "Open a temporary file containing the Nth input expression. N may be a range",
29
+ :optional_argument => true, :as => Range, :default => -1..-1
30
+ opt.on :t, :temp, "Open an empty temporary file"
31
+ opt.on :l, :line, "Jump to this line in the opened file",
32
+ :argument => true, :as => Integer
33
+ opt.on :n, :"no-reload", "Don't automatically reload the edited file"
34
+ opt.on :c, :current, "Open the current __FILE__ and at __LINE__ (as returned by `whereami`)"
35
+ opt.on :r, :reload, "Reload the edited code immediately (default for ruby files)"
36
+ opt.on :p, :patch, "Instead of editing the object's file, try to edit in a tempfile and apply as a monkey patch"
37
+ opt.on :m, :method, "Explicitly edit the _current_ method (when inside a method context)."
38
+ end
39
+
40
+ def process
41
+ if bad_option_combination?
42
+ raise CommandError, "Only one of --ex, --temp, --in, --method and FILE may be specified."
43
+ end
44
+
45
+ if repl_edit?
46
+ # code defined in pry, eval'd within pry.
47
+ repl_edit
48
+ elsif runtime_patch?
49
+ # patch code without persisting changes
50
+ apply_runtime_patch
51
+ else
52
+ # code stored in actual files, eval'd at top-level
53
+ file_edit
54
+ end
55
+ end
56
+
57
+ def repl_edit?
58
+ !opts.present?(:ex) && !opts.present?(:current) && !opts.present?(:method) &&
59
+ filename_argument.empty?
60
+ end
61
+
62
+ def repl_edit
63
+ content = Pry::Editor.new(_pry_).edit_tempfile_with_content(initial_temp_file_content,
64
+ initial_temp_file_content.lines.count)
65
+ silence_warnings do
66
+ eval_string.replace content
67
+ end
68
+ end
69
+
70
+ def file_based_exception?
71
+ opts.present?(:ex) && !opts.present?(:patch)
72
+ end
73
+
74
+ def runtime_patch?
75
+ !file_based_exception? && (opts.present?(:patch) || pry_method?(code_object))
76
+ end
77
+
78
+ def apply_runtime_patch
79
+ if patch_exception?
80
+ ExceptionPatcher.new(_pry_, state, file_and_line_for_current_exception).perform_patch
81
+ else
82
+ if code_object.is_a?(Pry::Method)
83
+ code_object.redefine Pry::Editor.new(_pry_).edit_tempfile_with_content(code_object.source)
84
+ else
85
+ raise NotImplementedError, "Cannot yet patch #{code_object} objects!"
86
+ end
87
+ end
88
+ end
89
+
90
+ def ensure_file_name_is_valid(file_name)
91
+ raise CommandError, "Cannot find a valid file for #{filename_argument}" if !file_name
92
+ raise CommandError, "#{file_name} is not a valid file name, cannot edit!" if not_a_real_file?(file_name)
93
+ end
94
+
95
+ def file_and_line_for_current_exception
96
+ FileAndLineLocator.from_exception(_pry_.last_exception, opts[:ex].to_i)
97
+ end
98
+
99
+ def file_and_line
100
+ file_name, line = if opts.present?(:current)
101
+ FileAndLineLocator.from_binding(target)
102
+ elsif opts.present?(:ex)
103
+ file_and_line_for_current_exception
104
+ elsif code_object
105
+ FileAndLineLocator.from_code_object(code_object, filename_argument)
106
+ else
107
+ # when file and line are passed as a single arg, e.g my_file.rb:30
108
+ FileAndLineLocator.from_filename_argument(filename_argument)
109
+ end
110
+
111
+ [file_name, opts.present?(:line) ? opts[:l].to_i : line]
112
+ end
113
+
114
+ def file_edit
115
+ file_name, line = file_and_line
116
+
117
+ ensure_file_name_is_valid(file_name)
118
+
119
+ Pry::Editor.new(_pry_).invoke_editor(file_name, line, reload?(file_name))
120
+ set_file_and_dir_locals(file_name)
121
+
122
+ if reload?(file_name)
123
+ silence_warnings do
124
+ load file_name
125
+ end
126
+ end
127
+ end
128
+
129
+ def filename_argument
130
+ args.join(' ')
131
+ end
132
+
133
+ def code_object
134
+ @code_object ||= !probably_a_file?(filename_argument) &&
135
+ Pry::CodeObject.lookup(filename_argument, _pry_)
136
+ end
137
+
138
+ def pry_method?(code_object)
139
+ code_object.is_a?(Pry::Method) &&
140
+ code_object.pry_method?
141
+ end
142
+
143
+ def patch_exception?
144
+ opts.present?(:ex) && opts.present?(:patch)
145
+ end
146
+
147
+ def bad_option_combination?
148
+ [opts.present?(:ex), opts.present?(:temp),
149
+ opts.present?(:in), opts.present?(:method), !filename_argument.empty?].count(true) > 1
150
+ end
151
+
152
+ def input_expression
153
+ case opts[:i]
154
+ when Range
155
+ (_pry_.input_array[opts[:i]] || []).join
156
+ when Fixnum
157
+ _pry_.input_array[opts[:i]] || ""
158
+ else
159
+ raise Pry::CommandError, "Not a valid range: #{opts[:i]}"
160
+ end
161
+ end
162
+
163
+ def reloadable?
164
+ opts.present?(:reload) || opts.present?(:ex)
165
+ end
166
+
167
+ def never_reload?
168
+ opts.present?(:'no-reload') || _pry_.config.disable_auto_reload
169
+ end
170
+
171
+ def reload?(file_name="")
172
+ (reloadable? || file_name.end_with?(".rb")) && !never_reload?
173
+ end
174
+
175
+ def initial_temp_file_content
176
+ case
177
+ when opts.present?(:temp)
178
+ ""
179
+ when opts.present?(:in)
180
+ input_expression
181
+ when eval_string.strip != ""
182
+ eval_string
183
+ else
184
+ _pry_.input_array.reverse_each.find { |x| x && x.strip != "" } || ""
185
+ end
186
+ end
187
+
188
+ def probably_a_file?(str)
189
+ [".rb", ".c", ".py", ".yml", ".gemspec"].include?(File.extname(str)) ||
190
+ str =~ /\/|\\/
191
+ end
192
+ end
193
+
194
+ Pry::Commands.add_command(Pry::Command::Edit)
195
+ end