pry 0.9.10pre1-i386-mingw32 → 0.9.11-i386-mingw32

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,120 +0,0 @@
1
- class Pry
2
- module DefaultCommands
3
- Hist = Pry::CommandSet.new do
4
-
5
- create_command "hist", "Show and replay Readline history. Aliases: history" do
6
- group "Editing"
7
- banner <<-USAGE
8
- Usage: hist
9
- hist --head N
10
- hist --tail N
11
- hist --show START..END
12
- hist --grep PATTERN
13
- hist --clear
14
- hist --replay START..END
15
- hist --save [START..END] FILE
16
- USAGE
17
-
18
- def options(opt)
19
- opt.on :H, :head, "Display the first N items.", :optional_argument => true, :as => Integer
20
- opt.on :T, :tail, "Display the last N items.", :optional_argument => true, :as => Integer
21
- opt.on :s, :show, "Show the given range of lines.", :optional_argument => true, :as => Range
22
- opt.on :G, :grep, "Show lines matching the given pattern.", :argument => true, :as => String
23
- opt.on :c, :clear, "Clear the current session's history."
24
- opt.on :r, :replay, "Replay a line or range of lines.", :argument => true, :as => Range
25
- opt.on :save, "Save history to a file.", :argument => true, :as => Range
26
-
27
- opt.on :e, :'exclude-pry', "Exclude Pry commands from the history."
28
- opt.on :n, :'no-numbers', "Omit line numbers."
29
- opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
30
- end
31
-
32
- def process
33
- @history = Pry::Code(Pry.history.to_a)
34
-
35
- if opts.present?(:show)
36
- @history = @history.between(opts[:show])
37
- end
38
-
39
- if opts.present?(:grep)
40
- @history = @history.grep(opts[:grep])
41
- end
42
-
43
- @history = case
44
- when opts.present?(:head)
45
- @history.take_lines(1, opts[:head] || 10)
46
- when opts.present?(:tail)
47
- @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
48
- when opts.present?(:show)
49
- @history.between(opts[:show])
50
- else
51
- @history
52
- end
53
-
54
- if opts.present?(:'exclude-pry')
55
- @history = @history.select { |l, ln| !command_set.valid_command?(l) }
56
- end
57
-
58
- if opts.present?(:save)
59
- process_save
60
- elsif opts.present?(:clear)
61
- process_clear
62
- elsif opts.present?(:replay)
63
- process_replay
64
- else
65
- process_display
66
- end
67
- end
68
-
69
- def process_display
70
- unless opts.present?(:'no-numbers')
71
- @history = @history.with_line_numbers
72
- end
73
-
74
- render_output(@history, opts)
75
- end
76
-
77
- def process_save
78
- case opts[:save]
79
- when Range
80
- @history = @history.between(opts[:save])
81
-
82
- unless args.first
83
- raise CommandError, "Must provide a file name."
84
- end
85
-
86
- file_name = File.expand_path(args.first)
87
- when String
88
- file_name = File.expand_path(opts[:save])
89
- end
90
-
91
- output.puts "Saving history in #{file_name}..."
92
-
93
- File.open(file_name, 'w') { |f| f.write(@history.raw) }
94
-
95
- output.puts "History saved."
96
- end
97
-
98
- def process_clear
99
- Pry.history.clear
100
- output.puts "History cleared."
101
- end
102
-
103
- def process_replay
104
- @history = @history.between(opts[:r])
105
-
106
- _pry_.input_stack.push _pry_.input
107
- _pry_.input = StringIO.new(@history.raw)
108
- # eval_string << "#{@history.raw}\n"
109
- # run "show-input" unless _pry_.complete_expression?(eval_string)
110
- end
111
- end
112
-
113
- alias_command "history", "hist"
114
-
115
-
116
-
117
-
118
- end
119
- end
120
- end
@@ -1,306 +0,0 @@
1
- require 'tempfile'
2
- require 'pry/default_commands/gist'
3
-
4
- class Pry
5
- module DefaultCommands
6
-
7
- InputAndOutput = Pry::CommandSet.new do
8
- import Gist
9
- command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>", :use_prefix => false, :takes_block => true) do |cmd|
10
- if cmd =~ /^cd\s+(.+)/i
11
- dest = $1
12
- begin
13
- Dir.chdir File.expand_path(dest)
14
- rescue Errno::ENOENT
15
- raise CommandError, "No such directory: #{dest}"
16
- end
17
- else
18
- pass_block(cmd)
19
-
20
- if command_block
21
- command_block.call `#{cmd}`
22
- else
23
- Pry.config.system.call(output, cmd, _pry_)
24
- end
25
- end
26
- end
27
-
28
- command "shell-mode", "Toggle shell mode. Bring in pwd prompt and file completion." do
29
- case _pry_.prompt
30
- when Pry::SHELL_PROMPT
31
- _pry_.pop_prompt
32
- _pry_.custom_completions = Pry::DEFAULT_CUSTOM_COMPLETIONS
33
- else
34
- _pry_.push_prompt Pry::SHELL_PROMPT
35
- _pry_.custom_completions = Pry::FILE_COMPLETIONS
36
- Readline.completion_proc = Pry::InputCompleter.build_completion_proc target,
37
- _pry_.instance_eval(&Pry::FILE_COMPLETIONS)
38
- end
39
- end
40
- alias_command "file-mode", "shell-mode"
41
-
42
- create_command "save-file", "Export to a file using content from the REPL." do
43
- banner <<-USAGE
44
- Usage: save-file [OPTIONS] [FILE]
45
- Save REPL content to a file.
46
- e.g: save-file -m my_method -m my_method2 ./hello.rb
47
- e.g: save-file -i 1..10 ./hello.rb --append
48
- e.g: save-file -k show-method ./my_command.rb
49
- e.g: save-file -f sample_file --lines 2..10 ./output_file.rb
50
- USAGE
51
-
52
- attr_accessor :content
53
- attr_accessor :file_name
54
-
55
- def setup
56
- self.content = ""
57
- end
58
-
59
- def convert_to_range(n)
60
- if !n.is_a?(Range)
61
- (n..n)
62
- else
63
- n
64
- end
65
- end
66
-
67
- def options(opt)
68
- opt.on :m, :method, "Save a method's source.", :argument => true do |meth_name|
69
- meth = get_method_or_raise(meth_name, target, {})
70
- self.content << meth.source
71
- end
72
- opt.on :c, :class, "Save a class's source.", :argument => true do |class_name|
73
- mod = Pry::WrappedModule.from_str(class_name, target)
74
- self.content << mod.source
75
- end
76
- opt.on :k, :command, "Save a command's source.", :argument => true do |command_name|
77
- command = find_command(command_name)
78
- block = Pry::Method.new(command.block)
79
- self.content << block.source
80
- end
81
- opt.on :f, :file, "Save a file.", :argument => true do |file|
82
- self.content << File.read(File.expand_path(file))
83
- end
84
- opt.on :l, :lines, "Only save a subset of lines.", :optional_argument => true, :as => Range, :default => 1..-1
85
- opt.on :o, :out, "Save entries from Pry's output result history. Takes an index or range.", :optional_argument => true,
86
- :as => Range, :default => -5..-1 do |range|
87
- range = convert_to_range(range)
88
-
89
- range.each do |v|
90
- self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
91
- end
92
-
93
- self.content << "\n"
94
- end
95
- opt.on :i, :in, "Save entries from Pry's input expression history. Takes an index or range.", :optional_argument => true,
96
- :as => Range, :default => -5..-1 do |range|
97
- input_expressions = _pry_.input_array[range] || []
98
- Array(input_expressions).each { |v| self.content << v }
99
- end
100
- opt.on :a, :append, "Append to the given file instead of overwriting it."
101
- end
102
-
103
- def process
104
- if args.empty?
105
- raise CommandError, "Must specify a file name."
106
- end
107
-
108
- self.file_name = File.expand_path(args.first)
109
-
110
- save_file
111
- end
112
-
113
- def save_file
114
- if self.content.empty?
115
- raise CommandError, "Found no code to save."
116
- end
117
-
118
- File.open(file_name, mode) do |f|
119
- if opts.present?(:lines)
120
- f.puts restrict_to_lines(content, opts[:l])
121
- else
122
- f.puts content
123
- end
124
- end
125
- end
126
-
127
- def mode
128
- if opts.present?(:append)
129
- "a"
130
- else
131
- "w"
132
- end
133
- end
134
- end
135
-
136
- create_command "cat", "Show code from a file, Pry's input buffer, or the last exception." do
137
- banner <<-USAGE
138
- Usage: cat FILE
139
- cat --ex [STACK_INDEX]
140
- cat --in [INPUT_INDEX_OR_RANGE]
141
-
142
- cat is capable of showing part or all of a source file, the context of the
143
- last exception, or an expression from Pry's input history.
144
-
145
- cat --ex defaults to showing the lines surrounding the location of the last
146
- exception. Invoking it more than once travels up the exception's backtrace,
147
- and providing a number shows the context of the given index of the backtrace.
148
- USAGE
149
-
150
- def options(opt)
151
- opt.on :ex, "Show the context of the last exception.", :optional_argument => true, :as => Integer
152
- opt.on :i, :in, "Show one or more entries from Pry's expression history.", :optional_argument => true, :as => Range, :default => -5..-1
153
-
154
- opt.on :s, :start, "Starting line (defaults to the first line).", :optional_argument => true, :as => Integer
155
- opt.on :e, :end, "Ending line (defaults to the last line).", :optional_argument => true, :as => Integer
156
- opt.on :l, :'line-numbers', "Show line numbers."
157
- opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python').", :argument => true, :as => Symbol
158
-
159
- opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
160
- end
161
-
162
- def process
163
- handler = case
164
- when opts.present?(:ex)
165
- method :process_ex
166
- when opts.present?(:in)
167
- method :process_in
168
- else
169
- method :process_file
170
- end
171
-
172
- output = handler.call do |code|
173
- code.code_type = opts[:type] || :ruby
174
-
175
- code.between(opts[:start] || 1, opts[:end] || -1).
176
- with_line_numbers(opts.present?(:'line-numbers') || opts.present?(:ex))
177
- end
178
-
179
- render_output(output, opts)
180
- end
181
-
182
- def process_ex
183
- window_size = Pry.config.default_window_size || 5
184
- ex = _pry_.last_exception
185
-
186
- raise CommandError, "No exception found." unless ex
187
-
188
- if opts[:ex].nil?
189
- bt_index = ex.bt_index
190
- ex.inc_bt_index
191
- else
192
- bt_index = opts[:ex]
193
- ex.bt_index = bt_index
194
- ex.inc_bt_index
195
- end
196
-
197
- ex_file, ex_line = ex.bt_source_location_for(bt_index)
198
-
199
- raise CommandError, "The given backtrace level is out of bounds." unless ex_file
200
-
201
- if RbxPath.is_core_path?(ex_file)
202
- ex_file = RbxPath.convert_path_to_full(ex_file)
203
- end
204
-
205
- set_file_and_dir_locals(ex_file)
206
-
207
- start_line = ex_line - window_size
208
- start_line = 1 if start_line < 1
209
- end_line = ex_line + window_size
210
-
211
- header = unindent <<-HEADER
212
- #{text.bold 'Exception:'} #{ex.class}: #{ex.message}
213
- --
214
- #{text.bold('From:')} #{ex_file} @ line #{ex_line} @ #{text.bold("level: #{bt_index}")} of backtrace (of #{ex.backtrace.size - 1}).
215
-
216
- HEADER
217
-
218
- code = yield(Pry::Code.from_file(ex_file).
219
- between(start_line, end_line).
220
- with_marker(ex_line))
221
-
222
- "#{header}#{code}"
223
- end
224
-
225
- def process_in
226
- normalized_range = absolute_index_range(opts[:i], _pry_.input_array.length)
227
- input_items = _pry_.input_array[normalized_range] || []
228
-
229
- zipped_items = normalized_range.zip(input_items).reject { |_, s| s.nil? || s == "" }
230
-
231
- unless zipped_items.length > 0
232
- raise CommandError, "No expressions found."
233
- end
234
-
235
- if zipped_items.length > 1
236
- contents = ""
237
- zipped_items.each do |i, s|
238
- contents << "#{text.bold(i.to_s)}:\n"
239
- contents << yield(Pry::Code(s).with_indentation(2)).to_s
240
- end
241
- else
242
- contents = yield(Pry::Code(zipped_items.first.last))
243
- end
244
-
245
- contents
246
- end
247
-
248
- def process_file
249
- file_name = args.shift
250
-
251
- unless file_name
252
- raise CommandError, "Must provide a filename, --in, or --ex."
253
- end
254
-
255
- file_name, line_num = file_name.split(':')
256
- file_name = File.expand_path(file_name)
257
- set_file_and_dir_locals(file_name)
258
-
259
- code = yield(Pry::Code.from_file(file_name))
260
-
261
- code.code_type = opts[:type] || detect_code_type_from_file(file_name)
262
- if line_num
263
- code = code.around(line_num.to_i,
264
- Pry.config.default_window_size || 7)
265
- end
266
-
267
- code
268
- end
269
-
270
- def detect_code_type_from_file(file_name)
271
- name, ext = File.basename(file_name).split('.', 2)
272
-
273
- if ext
274
- case ext
275
- when "py"
276
- :python
277
- when "rb", "gemspec", "rakefile", "ru"
278
- :ruby
279
- when "js"
280
- return :javascript
281
- when "yml", "prytheme"
282
- :yaml
283
- when "groovy"
284
- :groovy
285
- when "c"
286
- :c
287
- when "cpp"
288
- :cpp
289
- when "java"
290
- :java
291
- else
292
- :text
293
- end
294
- else
295
- case name
296
- when "Rakefile", "Gemfile"
297
- :ruby
298
- else
299
- :text
300
- end
301
- end
302
- end
303
- end
304
- end
305
- end
306
- end
@@ -1,410 +0,0 @@
1
- require 'tempfile'
2
-
3
- class Pry
4
- module DefaultCommands
5
-
6
- # For show-doc and show-source
7
- module ModuleIntrospectionHelpers
8
- attr_accessor :module_object
9
-
10
- def module?(name)
11
- self.module_object = Pry::WrappedModule.from_str(name, target)
12
- end
13
-
14
- def method?
15
- !!method_object
16
- rescue CommandError
17
- false
18
- end
19
-
20
- def process(name)
21
- if module?(name)
22
- code_or_doc = process_module
23
- elsif method?
24
- code_or_doc = process_method
25
- else
26
- code_or_doc = process_alternatives
27
- end
28
-
29
- render_output(code_or_doc, opts)
30
- end
31
-
32
- def process_alternatives
33
- if args.empty? && internal_binding?(target)
34
- mod = target_self.is_a?(Module) ? target_self : target_self.class
35
- self.module_object = Pry::WrappedModule(mod)
36
-
37
- process_module
38
- else
39
- process_method
40
- end
41
- end
42
-
43
- def module_start_line(mod, candidate_rank=0)
44
- if opts.present?(:'base-one')
45
- 1
46
- else
47
- mod.candidate(candidate_rank).line
48
- end
49
- end
50
-
51
- def use_line_numbers?
52
- opts.present?(:b) || opts.present?(:l)
53
- end
54
-
55
- def attempt
56
- rank = 0
57
- begin
58
- yield(rank)
59
- rescue Pry::CommandError
60
- raise if rank > (module_object.number_of_candidates - 1)
61
- rank += 1
62
- retry
63
- end
64
- end
65
- end
66
-
67
- Introspection = Pry::CommandSet.new do
68
-
69
- create_command "show-doc", "Show the documentation for a method or class. Aliases: \?", :shellwords => false do
70
- include ModuleIntrospectionHelpers
71
- include Helpers::DocumentationHelpers
72
- extend Helpers::BaseHelpers
73
-
74
- banner <<-BANNER
75
- Usage: show-doc [OPTIONS] [METH]
76
- Aliases: ?
77
-
78
- Show the documentation for a method or class. Tries instance methods first and then methods by default.
79
- e.g show-doc hello_method # docs for hello_method
80
- e.g show-doc Pry # docs for Pry class
81
- e.g show-doc Pry -a # docs for all definitions of Pry class (all monkey patches)
82
- BANNER
83
-
84
- options :requires_gem => "ruby18_source_location" if mri_18?
85
-
86
- def setup
87
- require 'ruby18_source_location' if mri_18?
88
- end
89
-
90
- def options(opt)
91
- method_options(opt)
92
- opt.on :l, "line-numbers", "Show line numbers."
93
- opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
94
- opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
95
- opt.on :a, :all, "Show docs for all definitions and monkeypatches of the module/class"
96
- end
97
-
98
- def process_module
99
- if opts.present?(:all)
100
- all_modules
101
- else
102
- normal_module
103
- end
104
- end
105
-
106
- def normal_module
107
- doc = ""
108
- if module_object.yard_docs?
109
- file_name, line = module_object.yard_file, module_object.yard_line
110
- doc << module_object.yard_doc
111
- start_line = 1
112
- else
113
- attempt do |rank|
114
- file_name, line = module_object.candidate(rank).source_location
115
- set_file_and_dir_locals(file_name)
116
- doc << module_object.candidate(rank).doc
117
- start_line = module_start_line(module_object, rank)
118
- end
119
- end
120
-
121
- doc = Code.new(doc, start_line, :text).
122
- with_line_numbers(use_line_numbers?).to_s
123
-
124
- doc.insert(0, "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line ? line : "N/A"}:\n\n")
125
- end
126
-
127
- def all_modules
128
- doc = ""
129
- doc << "Found #{module_object.number_of_candidates} candidates for `#{module_object.name}` definition:\n"
130
- module_object.number_of_candidates.times do |v|
131
- candidate = module_object.candidate(v)
132
- begin
133
- doc << "\nCandidate #{v+1}/#{module_object.number_of_candidates}: #{candidate.file} @ #{candidate.line}:\n\n"
134
- doc << candidate.doc
135
- rescue Pry::RescuableException
136
- doc << "No documentation found.\n"
137
- next
138
- end
139
- end
140
- doc
141
- end
142
-
143
- def process_method
144
- raise Pry::CommandError, "No documentation found." if method_object.doc.nil? || method_object.doc.empty?
145
-
146
- doc = process_comment_markup(method_object.doc)
147
- output.puts make_header(method_object, doc)
148
- output.puts "#{text.bold("Owner:")} #{method_object.owner || "N/A"}"
149
- output.puts "#{text.bold("Visibility:")} #{method_object.visibility}"
150
- output.puts "#{text.bold("Signature:")} #{method_object.signature}"
151
- output.puts
152
-
153
- if use_line_numbers?
154
- doc = Code.new(doc, start_line, :text).
155
- with_line_numbers(true).to_s
156
- end
157
-
158
- doc
159
- end
160
-
161
- def module_start_line(mod, candidate=0)
162
- if opts.present?(:'base-one')
163
- 1
164
- else
165
- if mod.candidate(candidate).line
166
- mod.candidate(candidate).line - mod.candidate(candidate).doc.lines.count
167
- else
168
- 1
169
- end
170
- end
171
- end
172
-
173
- def start_line
174
- if opts.present?(:'base-one')
175
- 1
176
- else
177
- (method_object.source_line - method_object.doc.lines.count) || 1
178
- end
179
- end
180
- end
181
-
182
- alias_command "?", "show-doc"
183
-
184
- create_command "stat", "View method information and set _file_ and _dir_ locals.", :shellwords => false do
185
- banner <<-BANNER
186
- Usage: stat [OPTIONS] [METH]
187
- Show method information for method METH and set _file_ and _dir_ locals.
188
- e.g: stat hello_method
189
- BANNER
190
-
191
- def options(opt)
192
- method_options(opt)
193
- end
194
-
195
- def process
196
- meth = method_object
197
- output.puts unindent <<-EOS
198
- Method Information:
199
- --
200
- Name: #{meth.name}
201
- Owner: #{meth.owner ? meth.owner : "Unknown"}
202
- Visibility: #{meth.visibility}
203
- Type: #{meth.is_a?(::Method) ? "Bound" : "Unbound"}
204
- Arity: #{meth.arity}
205
- Method Signature: #{meth.signature}
206
- Source Location: #{meth.source_location ? meth.source_location.join(":") : "Not found."}
207
- EOS
208
- end
209
- end
210
-
211
- create_command "show-source" do
212
- include ModuleIntrospectionHelpers
213
- extend Helpers::BaseHelpers
214
-
215
- description "Show the source for a method or class. Aliases: $, show-method"
216
-
217
- banner <<-BANNER
218
- Usage: show-source [OPTIONS] [METH|CLASS]
219
- Aliases: $, show-method
220
-
221
- Show the source for a method or class. Tries instance methods first and then methods by default.
222
-
223
- e.g: `show-source hello_method`
224
- e.g: `show-source -m hello_method`
225
- e.g: `show-source Pry#rep` # source for Pry#rep method
226
- e.g: `show-source Pry` # source for Pry class
227
- e.g: `show-source Pry -a` # source for all Pry class definitions (all monkey patches)
228
-
229
- https://github.com/pry/pry/wiki/Source-browsing#wiki-Show_method
230
- BANNER
231
-
232
- options :shellwords => false
233
- options :requires_gem => "ruby18_source_location" if mri_18?
234
-
235
- def setup
236
- require 'ruby18_source_location' if mri_18?
237
- end
238
-
239
- def options(opt)
240
- method_options(opt)
241
- opt.on :l, "line-numbers", "Show line numbers."
242
- opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
243
- opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
244
- opt.on :a, :all, "Show source for all definitions and monkeypatches of the module/class"
245
- end
246
-
247
- def process_method
248
- raise CommandError, "Could not find method source" unless method_object.source
249
-
250
- code = ""
251
- code << make_header(method_object)
252
- code << "#{text.bold("Owner:")} #{method_object.owner || "N/A"}\n"
253
- code << "#{text.bold("Visibility:")} #{method_object.visibility}\n"
254
- code << "\n"
255
-
256
- code << Code.from_method(method_object, start_line).
257
- with_line_numbers(use_line_numbers?).to_s
258
- end
259
-
260
- def process_module
261
- if opts.present?(:all)
262
- all_modules
263
- else
264
- normal_module
265
- end
266
- end
267
-
268
- def normal_module
269
- file_name = line = code = nil
270
- attempt do |rank|
271
- file_name, line = module_object.candidate(rank).source_location
272
- set_file_and_dir_locals(file_name)
273
- code = Code.from_module(module_object, module_start_line(module_object, rank), rank).
274
- with_line_numbers(use_line_numbers?).to_s
275
- end
276
-
277
- result = ""
278
- result << "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n"
279
- result << "#{Pry::Helpers::Text.bold('Number of lines:')} #{code.lines.count}\n\n"
280
- result << code
281
- end
282
-
283
- def all_modules
284
- mod = module_object
285
-
286
- result = ""
287
- result << "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
288
- mod.number_of_candidates.times do |v|
289
- candidate = mod.candidate(v)
290
- begin
291
- result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.file} @ line #{candidate.line}:\n"
292
- code = Code.from_module(mod, module_start_line(mod, v), v).
293
- with_line_numbers(use_line_numbers?).to_s
294
- result << "Number of lines: #{code.lines.count}\n\n"
295
- result << code
296
- rescue Pry::RescuableException
297
- result << "\nNo code found.\n"
298
- next
299
- end
300
- end
301
- result
302
- end
303
-
304
- def use_line_numbers?
305
- opts.present?(:b) || opts.present?(:l)
306
- end
307
-
308
- def start_line
309
- if opts.present?(:'base-one')
310
- 1
311
- else
312
- method_object.source_line || 1
313
- end
314
- end
315
- end
316
-
317
- alias_command "show-method", "show-source"
318
- alias_command "$", "show-source"
319
-
320
- command "show-command", "Show the source for CMD." do |*args|
321
- target = target()
322
-
323
- opts = Slop.parse!(args) do |opt|
324
- opt.banner unindent <<-USAGE
325
- Usage: show-command [OPTIONS] [CMD]
326
- Show the source for command CMD.
327
- e.g: show-command show-method
328
- USAGE
329
-
330
- opt.on :l, "line-numbers", "Show line numbers."
331
- opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
332
- opt.on :h, :help, "This message." do
333
- output.puts opt.help
334
- end
335
- end
336
-
337
- next if opts.present?(:help)
338
-
339
- command_name = args.shift
340
- if !command_name
341
- raise CommandError, "You must provide a command name."
342
- end
343
-
344
- if find_command(command_name)
345
- block = Pry::Method.new(find_command(command_name).block)
346
-
347
- next unless block.source
348
- set_file_and_dir_locals(block.source_file)
349
-
350
- output.puts make_header(block)
351
- output.puts
352
-
353
- code = Code.from_method(block).with_line_numbers(opts.present?(:'line-numbers')).to_s
354
-
355
- render_output(code, opts)
356
- else
357
- raise CommandError, "No such command: #{command_name}."
358
- end
359
- end
360
-
361
- create_command "ri", "View ri documentation. e.g `ri Array#each`" do
362
- banner <<-BANNER
363
- Usage: ri [spec]
364
- e.g. ri Array#each
365
-
366
- Relies on the rdoc gem being installed. See also: show-doc.
367
- BANNER
368
-
369
- def process(spec)
370
- # Lazily load RI
371
- require 'rdoc/ri/driver'
372
-
373
- unless defined? RDoc::RI::PryDriver
374
-
375
- # Subclass RI so that it formats its output nicely, and uses `lesspipe`.
376
- subclass = Class.new(RDoc::RI::Driver) # the hard way.
377
-
378
- subclass.class_eval do
379
- def page
380
- Pry::Helpers::BaseHelpers.lesspipe {|less| yield less}
381
- end
382
-
383
- def formatter(io)
384
- if @formatter_klass then
385
- @formatter_klass.new
386
- else
387
- RDoc::Markup::ToAnsi.new
388
- end
389
- end
390
- end
391
-
392
- RDoc::RI.const_set :PryDriver, subclass # hook it up!
393
- end
394
-
395
- # Spin-up an RI insance.
396
- ri = RDoc::RI::PryDriver.new :use_stdout => true, :interactive => false
397
-
398
- begin
399
- ri.display_names [spec] # Get the documentation (finally!)
400
- rescue RDoc::RI::Driver::NotFoundError => e
401
- output.puts "error: '#{e.name}' not found"
402
- end
403
- end
404
-
405
- end
406
-
407
- end
408
- end
409
- end
410
-