pry 0.9.12.2 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +153 -132
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
@@ -1,9 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
-
4
5
  module BaseHelpers
5
-
6
- module_function
6
+ extend self
7
7
 
8
8
  def silence_warnings
9
9
  old_verbose = $VERBOSE
@@ -20,181 +20,52 @@ class Pry
20
20
  # This is required to introspect methods on objects like Net::HTTP::Get that
21
21
  # have overridden the `method` method.
22
22
  def safe_send(obj, method, *args, &block)
23
- (Module === obj ? Module : Object).instance_method(method).bind(obj).call(*args, &block)
23
+ (obj.is_a?(Module) ? Module : Object).instance_method(method)
24
+ .bind(obj).call(*args, &block)
24
25
  end
25
- public :safe_send
26
26
 
27
27
  def find_command(name, set = Pry::Commands)
28
28
  command_match = set.find do |_, command|
29
- (listing = command.options[:listing]) == name && listing != nil
29
+ (listing = command.options[:listing]) == name && !listing.nil?
30
30
  end
31
31
  command_match.last if command_match
32
32
  end
33
33
 
34
34
  def not_a_real_file?(file)
35
- file =~ /(\(.*\))|<.*>/ || file =~ /__unknown__/ || file == "" || file == "-e"
36
- end
37
-
38
- def command_dependencies_met?(options)
39
- return true if !options[:requires_gem]
40
- Array(options[:requires_gem]).all? do |g|
41
- Rubygem.installed?(g)
42
- end
43
- end
44
-
45
- def set_file_and_dir_locals(file_name, _pry_=_pry_(), target=target())
46
- return if !target or !file_name
47
- _pry_.last_file = File.expand_path(file_name)
48
- _pry_.inject_local("_file_", _pry_.last_file, target)
49
-
50
- _pry_.last_dir = File.dirname(_pry_.last_file)
51
- _pry_.inject_local("_dir_", _pry_.last_dir, target)
35
+ file =~ /^(\(.*\))$|^<.*>$/ || file =~ /__unknown__/ || file == "" || file == "-e"
52
36
  end
53
37
 
54
38
  def use_ansi_codes?
55
- windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
39
+ Pry::Helpers::Platform.windows_ansi? ||
40
+ ((term = Pry::Env['TERM']) && term != "dumb")
56
41
  end
57
42
 
58
43
  def colorize_code(code)
59
- if Pry.color
60
- CodeRay.scan(code, :ruby).term
61
- else
62
- code
63
- end
44
+ SyntaxHighlighter.highlight(code)
64
45
  end
65
46
 
66
- def highlight(string, regexp, highlight_color=:bright_yellow)
67
- string.gsub(regexp) { |match| "<#{highlight_color}>#{match}</#{highlight_color}>" }
47
+ def highlight(string, regexp, highlight_color = :bright_yellow)
48
+ string.gsub(regexp) do |match|
49
+ "<#{highlight_color}>#{match}</#{highlight_color}>"
50
+ end
68
51
  end
69
52
 
70
53
  # formatting
71
54
  def heading(text)
72
55
  text = "#{text}\n--"
73
- Pry.color ? "\e[1m#{text}\e[0m": text
74
- end
75
-
76
- # have fun on the Windows platform.
77
- def windows?
78
- RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
79
- end
80
-
81
- # are we able to use ansi on windows?
82
- def windows_ansi?
83
- defined?(Win32::Console) || ENV['ANSICON']
84
- end
85
-
86
- def jruby?
87
- RbConfig::CONFIG['ruby_install_name'] == 'jruby'
88
- end
89
-
90
- def jruby_19?
91
- RbConfig::CONFIG['ruby_install_name'] == 'jruby' &&
92
- RbConfig::CONFIG['ruby_version'] == '1.9'
93
- end
94
-
95
- def rbx?
96
- RbConfig::CONFIG['ruby_install_name'] == 'rbx'
97
- end
98
-
99
- def mri_18?
100
- RUBY_VERSION =~ /1.8/ && RbConfig::CONFIG['ruby_install_name'] == 'ruby'
56
+ "\e[1m#{text}\e[0m"
101
57
  end
102
58
 
103
- def mri_19?
104
- RUBY_VERSION =~ /1.9/ && RbConfig::CONFIG['ruby_install_name'] == 'ruby'
105
- end
106
-
107
- # Try to use `less` for paging, if it fails then use
108
- # simple_pager. Also do not page if Pry.pager is falsey
109
- def stagger_output(text, out = nil)
110
- out ||= case
111
- when respond_to?(:output)
112
- # Mixin.
113
- output
114
- when Pry.respond_to?(:output)
115
- # Parent.
116
- Pry.output
117
- else
118
- # Sys.
119
- $stdout
120
- end
121
-
122
- if text.lines.count < Pry::Pager.page_size || !Pry.pager
123
- out.puts text
59
+ # Send the given text through the best available pager (if Pry.config.pager is
60
+ # enabled). Infers where to send the output if used as a mixin.
61
+ # DEPRECATED.
62
+ def stagger_output(text, _out = nil)
63
+ if defined?(pry_instance) && pry_instance
64
+ pry_instance.pager.page text
124
65
  else
125
- Pry::Pager.page(text)
66
+ Pry.new.pager.page text
126
67
  end
127
- rescue Errno::ENOENT
128
- Pry::Pager.page(text, :simple)
129
- rescue Errno::EPIPE
130
68
  end
131
-
132
- # @param [String] arg_string The object path expressed as a string.
133
- # @param [Pry] _pry_ The relevant Pry instance.
134
- # @param [Array<Binding>] old_stack The state of the old binding stack
135
- # @return [Array<Array<Binding>, Array<Binding>>] An array
136
- # containing two elements: The new `binding_stack` and the old `binding_stack`.
137
- def context_from_object_path(arg_string, _pry_=nil, old_stack=[])
138
-
139
- # Extract command arguments. Delete blank arguments like " ", but
140
- # don't delete empty strings like "".
141
- path = arg_string.split(/\//).delete_if { |a| a =~ /\A\s+\z/ }
142
- stack = _pry_.binding_stack.dup
143
- state_old_stack = old_stack
144
-
145
- # Special case when we only get a single "/", return to root.
146
- if path.empty?
147
- state_old_stack = stack.dup unless old_stack.empty?
148
- stack = [stack.first]
149
- end
150
-
151
- path.each_with_index do |context, i|
152
- begin
153
- case context.chomp
154
- when ""
155
- state_old_stack = stack.dup
156
- stack = [stack.first]
157
- when "::"
158
- state_old_stack = stack.dup
159
- stack.push(TOPLEVEL_BINDING)
160
- when "."
161
- next
162
- when ".."
163
- unless stack.size == 1
164
- # Don't rewrite old_stack if we're in complex expression
165
- # (e.g.: `cd 1/2/3/../4).
166
- state_old_stack = stack.dup if path.first == ".."
167
- stack.pop
168
- end
169
- when "-"
170
- unless old_stack.empty?
171
- # Interchange current stack and old stack with each other.
172
- stack, state_old_stack = state_old_stack, stack
173
- end
174
- else
175
- state_old_stack = stack.dup if i == 0
176
- stack.push(Pry.binding_for(stack.last.eval(context)))
177
- end
178
-
179
- rescue RescuableException => e
180
- # Restore old stack to its initial values.
181
- state_old_stack = old_stack
182
-
183
- msg = [
184
- "Bad object path: #{arg_string}.",
185
- "Failed trying to resolve: #{context}.",
186
- e.inspect
187
- ].join(' ')
188
-
189
- CommandError.new(msg).tap do |err|
190
- err.set_backtrace e.backtrace
191
- raise err
192
- end
193
- end
194
- end
195
- return stack, state_old_stack
196
- end
197
-
198
69
  end
199
70
  end
200
71
  end
@@ -1,98 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+
1
5
  class Pry
2
6
  module Helpers
3
-
4
7
  module CommandHelpers
5
8
  include OptionsHelpers
6
9
 
7
- module_function
10
+ extend self
8
11
 
9
12
  # Open a temp file and yield it to the block, closing it after
10
13
  # @return [String] The path of the temp file
11
- def temp_file(ext='.rb')
14
+ def temp_file(ext = '.rb')
12
15
  file = Tempfile.new(['pry', ext])
13
- yield file
16
+ yield(file)
14
17
  ensure
15
- file.close(true) if file
16
- end
17
-
18
- def render_output(str, opts={})
19
- if opts[:flood]
20
- output.puts str
21
- else
22
- stagger_output str
23
- end
18
+ file.close(true)
24
19
  end
25
20
 
26
- def internal_binding?(target)
27
- m = target.eval("::Kernel.__method__").to_s
21
+ def internal_binding?(context)
22
+ method_name = context.eval("::Kernel.__method__").to_s
28
23
  # class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
29
- ["__binding__", "__pry__", "class_eval"].include?(m)
24
+ %w[__binding__ __pry__ class_eval].include?(method_name)
25
+ # TODO: codehaus is dead, there was no test for this and the
26
+ # description for the commit doesn't exist. Probably a candidate for
27
+ # removal so we have a chance to introduce a regression and document it
28
+ # properly.
30
29
  end
31
30
 
32
- def get_method_or_raise(name, target, opts={}, omit_help=false)
33
- meth = Pry::Method.from_str(name, target, opts)
34
-
35
- if name && !meth
36
- command_error("The method '#{name}' could not be found.", omit_help, MethodNotFound)
31
+ def get_method_or_raise(method_name, context, opts = {})
32
+ method = Pry::Method.from_str(method_name, context, opts)
33
+ if !method && method_name
34
+ raise Pry::MethodNotFound, "method '#{method_name}' could not be found."
37
35
  end
38
36
 
39
37
  (opts[:super] || 0).times do
40
- if meth.super
41
- meth = meth.super
38
+ if method.super
39
+ method = method.super
42
40
  else
43
- command_error("'#{meth.name_with_owner}' has no super method.", omit_help, MethodNotFound)
41
+ raise Pry::MethodNotFound,
42
+ "'#{method.name_with_owner}' has no super method"
44
43
  end
45
44
  end
46
45
 
47
- if !meth || (!name && internal_binding?(target))
48
- command_error("No method name given, and context is not a method.", omit_help, MethodNotFound)
46
+ if !method || (!method_name && internal_binding?(context))
47
+ raise Pry::MethodNotFound,
48
+ 'no method name given, and context is not a method'
49
49
  end
50
50
 
51
- set_file_and_dir_locals(meth.source_file)
52
- meth
53
- end
54
-
55
- def command_error(message, omit_help, klass=CommandError)
56
- message += " Type `#{command_name} --help` for help." unless omit_help
57
- raise klass, message
51
+ set_file_and_dir_locals(method.source_file)
52
+ method
58
53
  end
59
54
 
60
- # Remove any common leading whitespace from every line in `text`.
61
- #
62
- # This can be used to make a HEREDOC line up with the left margin, without
55
+ # Remove any common leading whitespace from every line in `text`. This
56
+ # can be used to make a HEREDOC line up with the left margin, without
63
57
  # sacrificing the indentation level of the source code.
64
58
  #
65
- # e.g.
66
- # opt.banner unindent <<-USAGE
59
+ # @example
60
+ # opt.banner(unindent(<<-USAGE))
67
61
  # Lorem ipsum dolor sit amet, consectetur adipisicing elit,
68
62
  # sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
69
63
  # "Ut enim ad minim veniam."
70
64
  # USAGE
71
65
  #
72
- # Heavily based on textwrap.dedent from Python, which is:
73
- # Copyright (C) 1999-2001 Gregory P. Ward.
74
- # Copyright (C) 2002, 2003 Python Software Foundation.
75
- # Written by Greg Ward <gward@python.net>
76
- #
77
- # Licensed under <http://docs.python.org/license.html>
78
- # From <http://hg.python.org/cpython/file/6b9f0a6efaeb/Lib/textwrap.py>
79
- #
80
- # @param [String] text The text from which to remove indentation
81
- # @return [String] The text with indentation stripped.
82
- def unindent(text, left_padding = 0)
83
- # Empty blank lines
84
- text = text.sub(/^[ \t]+$/, '')
85
-
86
- # Find the longest common whitespace to all indented lines
87
- # Ignore lines containing just -- or ++ as these seem to be used by
88
- # comment authors as delimeters.
89
- margin = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/).inject do |current_margin, next_indent|
66
+ # @param [String] dirty_text The text from which to remove indentation
67
+ # @return [String] the text with indentation stripped
68
+ def unindent(dirty_text, left_padding = 0)
69
+ text = dirty_text.sub(/\A[ \t]+\z/, '') # Empty blank lines.
70
+
71
+ # Find the longest common whitespace to all indented lines. Ignore lines
72
+ # containing just -- or ++ as these seem to be used by comment authors
73
+ # as delimeters.
74
+ scanned_text = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/)
75
+ margin = scanned_text.inject do |current_margin, next_indent|
90
76
  if next_indent.start_with?(current_margin)
91
77
  current_margin
92
78
  elsif current_margin.start_with?(next_indent)
93
79
  next_indent
94
80
  else
95
- ""
81
+ ''
96
82
  end
97
83
  end
98
84
 
@@ -101,7 +87,7 @@ class Pry
101
87
 
102
88
  # Restrict a string to the given range of lines (1-indexed)
103
89
  # @param [String] content The string.
104
- # @param [Range, Fixnum] lines The line(s) to restrict it to.
90
+ # @param [Range, Integer] lines The line(s) to restrict it to.
105
91
  # @return [String] The resulting string.
106
92
  def restrict_to_lines(content, lines)
107
93
  line_range = one_index_range_or_number(lines)
@@ -109,11 +95,7 @@ class Pry
109
95
  end
110
96
 
111
97
  def one_index_number(line_number)
112
- if line_number > 0
113
- line_number - 1
114
- else
115
- line_number
116
- end
98
+ line_number > 0 ? line_number - 1 : line_number
117
99
  end
118
100
 
119
101
  # convert a 1-index range to a 0-indexed one
@@ -149,6 +131,16 @@ class Pry
149
131
 
150
132
  Range.new(a, b)
151
133
  end
134
+
135
+ def set_file_and_dir_locals(file_name, pry = pry_instance, ctx = target)
136
+ return if !ctx || !file_name
137
+
138
+ pry.last_file = File.expand_path(file_name)
139
+ pry.inject_local("_file_", pry.last_file, ctx)
140
+
141
+ pry.last_dir = File.dirname(pry.last_file)
142
+ pry.inject_local("_dir_", pry.last_dir, ctx)
143
+ end
152
144
  end
153
145
  end
154
146
  end
@@ -1,21 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
-
4
5
  # This class contains methods useful for extracting
5
6
  # documentation from methods and classes.
6
7
  module DocumentationHelpers
8
+ YARD_TAGS = %w[
9
+ param return option yield attr attr_reader attr_writer deprecate example
10
+ raise
11
+ ].freeze
7
12
 
8
13
  module_function
9
14
 
10
15
  def process_rdoc(comment)
11
- return comment unless Pry.color
12
16
  comment = comment.dup
13
- comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
14
- gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
15
- gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{$1}\e[0m" }.
16
- gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
17
- gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
18
- gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
17
+ last_match_ruby = proc do
18
+ SyntaxHighlighter.highlight(Regexp.last_match(1))
19
+ end
20
+
21
+ comment.gsub(%r{<code>(?:\s*\n)?(.*?)\s*</code>}m, &last_match_ruby)
22
+ .gsub(%r{<em>(?:\s*\n)?(.*?)\s*</em>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
23
+ .gsub(%r{<i>(?:\s*\n)?(.*?)\s*</i>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
24
+ .gsub(%r{<tt>(?:\s*\n)?(.*?)\s*</tt>}m, &last_match_ruby)
25
+ .gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{Regexp.last_match(1)}\e[0m" }
26
+ .gsub(/((?:^[ \t]+(?:(?!.+\e\[)).+(?:\n+|\Z))+)/, &last_match_ruby)
27
+ .gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{last_match_ruby.call}`" }
19
28
  end
20
29
 
21
30
  def process_yardoc_tag(comment, tag)
@@ -34,10 +43,9 @@ class Pry
34
43
  end
35
44
 
36
45
  def process_yardoc(comment)
37
- yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
38
- "deprecate", "example", "raise"]
39
- (yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
40
- gsub(/^@(#{yard_tags.join("|")})/) { Pry.color ? "\e[33m#{$1}\e[0m": $1 }
46
+ (YARD_TAGS - %w[example])
47
+ .inject(comment) { |a, v| process_yardoc_tag(a, v) }
48
+ .gsub(/^@(#{YARD_TAGS.join("|")})/) { "\e[33m#{Regexp.last_match(1)}\e[0m" }
41
49
  end
42
50
 
43
51
  def process_comment_markup(comment)
@@ -47,7 +55,7 @@ class Pry
47
55
  # @param [String] code
48
56
  # @return [String]
49
57
  def strip_comments_from_c_code(code)
50
- code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
58
+ code.sub(%r{\A\s*/\*.*?\*/\s*}m, '')
51
59
  end
52
60
 
53
61
  # Given a string that makes up a comment in a source-code file parse out the content
@@ -1,26 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
5
  module OptionsHelpers
4
6
  module_function
5
7
 
6
- # Add method options to the Slop instance
8
+ # Add method options to the Pry::Slop instance
7
9
  def method_options(opt)
8
10
  @method_target = target
9
11
  opt.on :M, "instance-methods", "Operate on instance methods."
10
12
  opt.on :m, :methods, "Operate on methods."
11
- opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
12
- opt.on :c, :context, "Select object context to run under.", :argument => true do |context|
13
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to " \
14
+ "traverse the ancestors.", as: :count
15
+ opt.on(
16
+ :c, :context, "Select object context to run under.", argument: true
17
+ ) do |context|
13
18
  @method_target = Pry.binding_for(target.eval(context))
14
19
  end
15
20
  end
16
21
 
17
22
  # Get the method object parsed by the slop instance
18
23
  def method_object
19
- @method_object ||= get_method_or_raise(args.empty? ? nil : args.join(" "), @method_target,
20
- :super => opts[:super],
21
- :instance => opts.present?(:'instance-methods') && !opts.present?(:'methods'),
22
- :methods => opts.present?(:'methods') && !opts.present?(:'instance-methods')
23
- )
24
+ @method_object ||= get_method_or_raise(
25
+ args.empty? ? nil : args.join(" "),
26
+ @method_target,
27
+ super: opts[:super],
28
+ instance: opts.present?(:'instance-methods') && !opts.present?(:methods),
29
+ methods: opts.present?(:methods) && !opts.present?(:'instance-methods')
30
+ )
24
31
  end
25
32
  end
26
33
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbconfig'
4
+
5
+ class Pry
6
+ module Helpers
7
+ # Contains methods for querying the platform that Pry is running on
8
+ # @api public
9
+ # @since v0.12.0
10
+ module Platform
11
+ # @return [Boolean]
12
+ def self.mac_osx?
13
+ !!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
14
+ end
15
+
16
+ # @return [Boolean]
17
+ def self.linux?
18
+ !!(RbConfig::CONFIG['host_os'] =~ /linux/i)
19
+ end
20
+
21
+ # @return [Boolean] true when Pry is running on Windows with ANSI support,
22
+ # false otherwise
23
+ def self.windows?
24
+ !!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
25
+ end
26
+
27
+ # @return [Boolean]
28
+ def self.windows_ansi?
29
+ return false unless windows?
30
+
31
+ !!(defined?(Win32::Console) || Pry::Env['ANSICON'] || mri?)
32
+ end
33
+
34
+ # @return [Boolean]
35
+ def self.jruby?
36
+ RbConfig::CONFIG['ruby_install_name'] == 'jruby'
37
+ end
38
+
39
+ # @return [Boolean]
40
+ def self.jruby_19?
41
+ jruby? && RbConfig::CONFIG['ruby_version'] == '1.9'
42
+ end
43
+
44
+ # @return [Boolean]
45
+ def self.mri?
46
+ RbConfig::CONFIG['ruby_install_name'] == 'ruby'
47
+ end
48
+
49
+ # @return [Boolean]
50
+ def self.mri_2?
51
+ mri? && RUBY_VERSION.start_with?('2')
52
+ end
53
+ end
54
+ end
55
+ end