pry 0.9.12.6-i386-mswin32 → 0.10.0-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +2 -2
  4. data/{README.markdown → README.md} +37 -31
  5. data/lib/pry.rb +38 -151
  6. data/lib/pry/cli.rb +35 -17
  7. data/lib/pry/code.rb +19 -63
  8. data/lib/pry/code/code_file.rb +103 -0
  9. data/lib/pry/code/code_range.rb +2 -1
  10. data/lib/pry/code/loc.rb +2 -2
  11. data/lib/pry/code_object.rb +40 -21
  12. data/lib/pry/color_printer.rb +55 -0
  13. data/lib/pry/command.rb +12 -9
  14. data/lib/pry/command_set.rb +81 -38
  15. data/lib/pry/commands.rb +1 -1
  16. data/lib/pry/commands/amend_line.rb +2 -2
  17. data/lib/pry/commands/bang.rb +1 -1
  18. data/lib/pry/commands/cat.rb +11 -2
  19. data/lib/pry/commands/cat/exception_formatter.rb +6 -7
  20. data/lib/pry/commands/cat/file_formatter.rb +15 -32
  21. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  22. data/lib/pry/commands/cd.rb +14 -3
  23. data/lib/pry/commands/change_inspector.rb +27 -0
  24. data/lib/pry/commands/change_prompt.rb +26 -0
  25. data/lib/pry/commands/code_collector.rb +4 -4
  26. data/lib/pry/commands/easter_eggs.rb +3 -3
  27. data/lib/pry/commands/edit.rb +10 -22
  28. data/lib/pry/commands/edit/exception_patcher.rb +2 -2
  29. data/lib/pry/commands/edit/file_and_line_locator.rb +0 -2
  30. data/lib/pry/commands/exit_program.rb +0 -1
  31. data/lib/pry/commands/find_method.rb +16 -22
  32. data/lib/pry/commands/gem_install.rb +5 -2
  33. data/lib/pry/commands/gem_open.rb +1 -1
  34. data/lib/pry/commands/gist.rb +10 -11
  35. data/lib/pry/commands/help.rb +14 -14
  36. data/lib/pry/commands/hist.rb +27 -8
  37. data/lib/pry/commands/install_command.rb +14 -12
  38. data/lib/pry/commands/list_inspectors.rb +35 -0
  39. data/lib/pry/commands/list_prompts.rb +35 -0
  40. data/lib/pry/commands/ls.rb +72 -296
  41. data/lib/pry/commands/ls/constants.rb +47 -0
  42. data/lib/pry/commands/ls/formatter.rb +49 -0
  43. data/lib/pry/commands/ls/globals.rb +48 -0
  44. data/lib/pry/commands/ls/grep.rb +21 -0
  45. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  46. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  47. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  48. data/lib/pry/commands/ls/local_names.rb +35 -0
  49. data/lib/pry/commands/ls/local_vars.rb +39 -0
  50. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  51. data/lib/pry/commands/ls/methods.rb +57 -0
  52. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  53. data/lib/pry/commands/ls/self_methods.rb +32 -0
  54. data/lib/pry/commands/play.rb +44 -10
  55. data/lib/pry/commands/pry_backtrace.rb +1 -2
  56. data/lib/pry/commands/raise_up.rb +2 -2
  57. data/lib/pry/commands/reload_code.rb +16 -19
  58. data/lib/pry/commands/ri.rb +7 -3
  59. data/lib/pry/commands/shell_command.rb +18 -13
  60. data/lib/pry/commands/shell_mode.rb +2 -4
  61. data/lib/pry/commands/show_doc.rb +5 -0
  62. data/lib/pry/commands/show_info.rb +8 -13
  63. data/lib/pry/commands/show_source.rb +15 -3
  64. data/lib/pry/commands/simple_prompt.rb +1 -1
  65. data/lib/pry/commands/toggle_color.rb +8 -4
  66. data/lib/pry/commands/watch_expression.rb +105 -0
  67. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  68. data/lib/pry/commands/whereami.rb +18 -10
  69. data/lib/pry/commands/wtf.rb +3 -3
  70. data/lib/pry/config.rb +20 -254
  71. data/lib/pry/config/behavior.rb +139 -0
  72. data/lib/pry/config/convenience.rb +26 -0
  73. data/lib/pry/config/default.rb +165 -0
  74. data/lib/pry/core_extensions.rb +31 -21
  75. data/lib/pry/editor.rb +107 -103
  76. data/lib/pry/exceptions.rb +77 -0
  77. data/lib/pry/helpers/base_helpers.rb +22 -109
  78. data/lib/pry/helpers/command_helpers.rb +10 -8
  79. data/lib/pry/helpers/documentation_helpers.rb +1 -2
  80. data/lib/pry/helpers/text.rb +4 -5
  81. data/lib/pry/history.rb +46 -45
  82. data/lib/pry/history_array.rb +6 -1
  83. data/lib/pry/hooks.rb +9 -29
  84. data/lib/pry/indent.rb +6 -6
  85. data/lib/pry/input_completer.rb +242 -0
  86. data/lib/pry/input_lock.rb +132 -0
  87. data/lib/pry/inspector.rb +27 -0
  88. data/lib/pry/last_exception.rb +61 -0
  89. data/lib/pry/method.rb +82 -87
  90. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +41 -38
  91. data/lib/pry/module_candidate.rb +4 -14
  92. data/lib/pry/object_path.rb +82 -0
  93. data/lib/pry/output.rb +50 -0
  94. data/lib/pry/pager.rb +193 -48
  95. data/lib/pry/plugins.rb +1 -1
  96. data/lib/pry/prompt.rb +26 -0
  97. data/lib/pry/pry_class.rb +149 -230
  98. data/lib/pry/pry_instance.rb +302 -413
  99. data/lib/pry/rbx_path.rb +1 -1
  100. data/lib/pry/repl.rb +202 -0
  101. data/lib/pry/repl_file_loader.rb +20 -26
  102. data/lib/pry/rubygem.rb +13 -5
  103. data/lib/pry/terminal.rb +2 -1
  104. data/lib/pry/test/helper.rb +26 -41
  105. data/lib/pry/version.rb +1 -1
  106. data/lib/pry/wrapped_module.rb +45 -59
  107. metadata +62 -225
  108. data/.document +0 -2
  109. data/.gitignore +0 -16
  110. data/.travis.yml +0 -25
  111. data/.yardopts +0 -1
  112. data/CHANGELOG +0 -534
  113. data/CONTRIBUTORS +0 -55
  114. data/Gemfile +0 -12
  115. data/Rakefile +0 -140
  116. data/TODO +0 -117
  117. data/lib/pry/completion.rb +0 -321
  118. data/lib/pry/custom_completions.rb +0 -6
  119. data/lib/pry/rbx_method.rb +0 -13
  120. data/man/pry.1 +0 -195
  121. data/man/pry.1.html +0 -204
  122. data/man/pry.1.ronn +0 -141
  123. data/pry.gemspec +0 -29
  124. data/spec/Procfile +0 -3
  125. data/spec/cli_spec.rb +0 -78
  126. data/spec/code_object_spec.rb +0 -277
  127. data/spec/code_spec.rb +0 -219
  128. data/spec/command_helpers_spec.rb +0 -29
  129. data/spec/command_integration_spec.rb +0 -644
  130. data/spec/command_set_spec.rb +0 -627
  131. data/spec/command_spec.rb +0 -821
  132. data/spec/commands/amend_line_spec.rb +0 -247
  133. data/spec/commands/bang_spec.rb +0 -19
  134. data/spec/commands/cat_spec.rb +0 -164
  135. data/spec/commands/cd_spec.rb +0 -250
  136. data/spec/commands/disable_pry_spec.rb +0 -25
  137. data/spec/commands/edit_spec.rb +0 -727
  138. data/spec/commands/exit_all_spec.rb +0 -34
  139. data/spec/commands/exit_program_spec.rb +0 -19
  140. data/spec/commands/exit_spec.rb +0 -34
  141. data/spec/commands/find_method_spec.rb +0 -70
  142. data/spec/commands/gem_list_spec.rb +0 -26
  143. data/spec/commands/gist_spec.rb +0 -79
  144. data/spec/commands/help_spec.rb +0 -56
  145. data/spec/commands/hist_spec.rb +0 -181
  146. data/spec/commands/jump_to_spec.rb +0 -15
  147. data/spec/commands/ls_spec.rb +0 -181
  148. data/spec/commands/play_spec.rb +0 -140
  149. data/spec/commands/raise_up_spec.rb +0 -56
  150. data/spec/commands/save_file_spec.rb +0 -177
  151. data/spec/commands/show_doc_spec.rb +0 -510
  152. data/spec/commands/show_input_spec.rb +0 -17
  153. data/spec/commands/show_source_spec.rb +0 -782
  154. data/spec/commands/whereami_spec.rb +0 -203
  155. data/spec/completion_spec.rb +0 -241
  156. data/spec/control_d_handler_spec.rb +0 -58
  157. data/spec/documentation_helper_spec.rb +0 -73
  158. data/spec/editor_spec.rb +0 -79
  159. data/spec/exception_whitelist_spec.rb +0 -21
  160. data/spec/fixtures/candidate_helper1.rb +0 -11
  161. data/spec/fixtures/candidate_helper2.rb +0 -8
  162. data/spec/fixtures/example.erb +0 -5
  163. data/spec/fixtures/example_nesting.rb +0 -33
  164. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  165. data/spec/fixtures/testrc +0 -2
  166. data/spec/fixtures/testrcbad +0 -2
  167. data/spec/fixtures/whereami_helper.rb +0 -6
  168. data/spec/helper.rb +0 -34
  169. data/spec/helpers/bacon.rb +0 -86
  170. data/spec/helpers/mock_pry.rb +0 -43
  171. data/spec/helpers/table_spec.rb +0 -105
  172. data/spec/history_array_spec.rb +0 -67
  173. data/spec/hooks_spec.rb +0 -522
  174. data/spec/indent_spec.rb +0 -301
  175. data/spec/input_stack_spec.rb +0 -90
  176. data/spec/method_spec.rb +0 -482
  177. data/spec/prompt_spec.rb +0 -60
  178. data/spec/pry_defaults_spec.rb +0 -419
  179. data/spec/pry_history_spec.rb +0 -99
  180. data/spec/pry_output_spec.rb +0 -95
  181. data/spec/pry_spec.rb +0 -515
  182. data/spec/run_command_spec.rb +0 -25
  183. data/spec/sticky_locals_spec.rb +0 -157
  184. data/spec/syntax_checking_spec.rb +0 -81
  185. data/spec/wrapped_module_spec.rb +0 -261
  186. data/wiki/Customizing-pry.md +0 -397
  187. data/wiki/Home.md +0 -4
@@ -1,4 +1,3 @@
1
-
2
1
  class Pry
3
2
 
4
3
  # Manage the processing of command line options
@@ -11,7 +10,9 @@ class Pry
11
10
  # @return [Proc] The Proc defining the valid command line options.
12
11
  attr_accessor :options
13
12
 
14
- # @return [Array] The Procs that process the parsed options.
13
+ # @return [Array] The Procs that process the parsed options. Plugins can
14
+ # utilize this facility in order to add and process their own Pry
15
+ # options.
15
16
  attr_accessor :option_processors
16
17
 
17
18
  # @return [Array<String>] The input array of strings to process
@@ -43,7 +44,7 @@ class Pry
43
44
  end
44
45
 
45
46
  # Add a block responsible for processing parsed options.
46
- def process_options(&block)
47
+ def add_option_processor(&block)
47
48
  self.option_processors ||= []
48
49
  option_processors << block
49
50
 
@@ -56,14 +57,26 @@ class Pry
56
57
  self.option_processors = nil
57
58
  end
58
59
 
59
- def parse_options(args=ARGV.dup)
60
+ def parse_options(args=ARGV)
60
61
  unless options
61
62
  raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
62
63
  end
63
64
 
64
65
  self.input_args = args
65
66
 
66
- opts = Slop.parse!(args, :help => true, :multiple_switches => false, &options)
67
+ begin
68
+ opts = Slop.parse!(
69
+ args,
70
+ :help => true,
71
+ :multiple_switches => false,
72
+ :strict => true,
73
+ &options
74
+ )
75
+ rescue Slop::InvalidOptionError
76
+ # Display help message on unknown switches and exit.
77
+ puts Slop.new(&options)
78
+ exit
79
+ end
67
80
 
68
81
  # Option processors are optional.
69
82
  if option_processors
@@ -91,19 +104,19 @@ Slop.new do
91
104
  end.parse(ARGV.dup)
92
105
 
93
106
  if Pry.config.should_load_plugins
94
- Pry::CLI.add_plugin_options
107
+ Pry::CLI.add_plugin_options
95
108
  end
96
109
 
97
110
  # The default Pry command line options (before plugin options are included)
98
111
  Pry::CLI.add_options do
99
112
  banner %{Usage: pry [OPTIONS]
100
113
  Start a Pry session.
101
- See: `https://github.com/pry` for more information.
114
+ See http://pryrepl.org/ for more information.
102
115
  Copyright (c) 2013 John Mair (banisterfiend)
103
116
  --
104
117
  }
105
118
  on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
106
- exec_string << input + "\n"
119
+ exec_string << input << "\n"
107
120
  end
108
121
 
109
122
  on "no-pager", "Disable pager for long output" do
@@ -115,7 +128,7 @@ Copyright (c) 2013 John Mair (banisterfiend)
115
128
  end
116
129
 
117
130
  on "no-color", "Disable syntax highlighting for session" do
118
- Pry.color = false
131
+ Pry.config.color = false
119
132
  end
120
133
 
121
134
  on :f, "Suppress loading of ~/.pryrc and ./.pryrc" do
@@ -136,11 +149,11 @@ Copyright (c) 2013 John Mair (banisterfiend)
136
149
  Pry.config.should_load_plugins = false
137
150
  end
138
151
 
139
- on "installed-plugins", "List installed plugins." do
152
+ on "plugins", "List installed plugins." do
140
153
  puts "Installed Plugins:"
141
154
  puts "--"
142
155
  Pry.locate_plugins.each do |plugin|
143
- puts "#{plugin.name}".ljust(18) + plugin.spec.summary
156
+ puts "#{plugin.name}".ljust(18) << plugin.spec.summary
144
157
  end
145
158
  exit
146
159
  end
@@ -149,6 +162,10 @@ Copyright (c) 2013 John Mair (banisterfiend)
149
162
  Pry.config.prompt = Pry::SIMPLE_PROMPT
150
163
  end
151
164
 
165
+ on "noprompt", "No prompt mode" do
166
+ Pry.config.prompt = Pry::NO_PROMPT
167
+ end
168
+
152
169
  on :r, :require=, "`require` a Ruby script at startup" do |file|
153
170
  Pry.config.requires << file
154
171
  end
@@ -177,7 +194,7 @@ Copyright (c) 2013 John Mair (banisterfiend)
177
194
  "Start the session in the specified context. Equivalent to `context.pry` in a session.",
178
195
  :default => "Pry.toplevel_binding"
179
196
  )
180
- end.process_options do |opts|
197
+ end.add_option_processor do |opts|
181
198
 
182
199
  exit if opts.help?
183
200
 
@@ -185,7 +202,12 @@ end.process_options do |opts|
185
202
  Pry.cli = true
186
203
 
187
204
  # create the actual context
188
- context = Pry.binding_for(eval(opts[:context]))
205
+ if opts[:context]
206
+ Pry.initial_session_setup
207
+ context = Pry.binding_for(eval(opts[:context]))
208
+ else
209
+ context = Pry.toplevel_binding
210
+ end
189
211
 
190
212
  if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
191
213
  full_name = File.expand_path(Pry::CLI.input_args.first)
@@ -193,10 +215,6 @@ end.process_options do |opts|
193
215
  exit
194
216
  end
195
217
 
196
- if Pry.config.should_load_plugins
197
- parser = Slop.new
198
- end
199
-
200
218
  # Start the session (running any code passed with -e, if there is any)
201
219
  Pry.start(context, :input => StringIO.new(exec_string))
202
220
  end
@@ -1,5 +1,6 @@
1
1
  require 'pry/code/loc'
2
2
  require 'pry/code/code_range'
3
+ require 'pry/code/code_file'
3
4
 
4
5
  class Pry
5
6
  class << self
@@ -29,26 +30,6 @@ class Pry
29
30
  # arbitrary chaining of formatting methods without mutating the original
30
31
  # object.
31
32
  class Code
32
-
33
- # List of all supported languages.
34
- # @return [Hash]
35
- EXTENSIONS = {
36
- %w(.py) => :python,
37
- %w(.js) => :javascript,
38
- %w(.css) => :css,
39
- %w(.xml) => :xml,
40
- %w(.php) => :php,
41
- %w(.html) => :html,
42
- %w(.diff) => :diff,
43
- %w(.java) => :java,
44
- %w(.json) => :json,
45
- %w(.c .h) => :c,
46
- %w(.rhtml) => :rhtml,
47
- %w(.yaml .yml) => :yaml,
48
- %w(.cpp .hpp .cc .h cxx) => :cpp,
49
- %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
50
- }
51
-
52
33
  class << self
53
34
  include MethodSource::CodeHelpers
54
35
 
@@ -58,13 +39,9 @@ class Pry
58
39
  # @param [String] filename The name of a file, or "(pry)".
59
40
  # @param [Symbol] code_type The type of code the file contains.
60
41
  # @return [Code]
61
- def from_file(filename, code_type = type_from_filename(filename))
62
- code = if filename == Pry.eval_path
63
- Pry.line_buffer.drop(1)
64
- else
65
- File.read(abs_path(filename))
66
- end
67
- new(code, 1, code_type)
42
+ def from_file(filename, code_type = nil)
43
+ code_file = CodeFile.new(filename, code_type)
44
+ new(code_file.code, 1, code_file.code_type)
68
45
  end
69
46
 
70
47
  # Instantiate a `Code` object containing code extracted from a
@@ -94,35 +71,6 @@ class Pry
94
71
  start_line ||= candidate.line
95
72
  new(candidate.source, start_line, :ruby)
96
73
  end
97
-
98
- protected
99
-
100
- # Guess the CodeRay type of a file from its extension, or nil if
101
- # unknown.
102
- #
103
- # @param [String] filename
104
- # @param [Symbol] default (:ruby) the file type to assume if none could be
105
- # detected.
106
- # @return [Symbol, nil]
107
- def type_from_filename(filename, default = :ruby)
108
- _, type = Pry::Code::EXTENSIONS.find do |k, _|
109
- k.any? { |ext| ext == File.extname(filename) }
110
- end
111
-
112
- type || default
113
- end
114
-
115
- # @param [String] filename
116
- # @raise [MethodSource::SourceNotFoundError] if the +filename+ is not
117
- # readable for some reason.
118
- # @return [String] absolute path for the given +filename+.
119
- def abs_path(filename)
120
- abs_path = [File.expand_path(filename, Dir.pwd),
121
- File.expand_path(filename, Pry::INITIAL_PWD)
122
- ].detect { |abs_path| File.readable?(abs_path) }
123
- abs_path or raise MethodSource::SourceNotFoundError,
124
- "Cannot open #{filename.inspect} for reading."
125
- end
126
74
  end
127
75
 
128
76
  # @return [Symbol] The type of code stored in this wrapper.
@@ -197,7 +145,7 @@ class Pry
197
145
  if start_line >= 0
198
146
  @lines.index { |loc| loc.lineno >= start_line } || @lines.length
199
147
  else
200
- @lines.length + start_line
148
+ [@lines.length + start_line, 0].max
201
149
  end
202
150
 
203
151
  alter do
@@ -305,14 +253,22 @@ class Pry
305
253
  # @return [String] a formatted representation (based on the configuration of
306
254
  # the object).
307
255
  def to_s
308
- @lines.map { |loc|
256
+ print_to_output("")
257
+ end
258
+
259
+ # Writes a formatted representation (based on the configuration of the
260
+ # object) to the given output, which must respond to `#<<`.
261
+ def print_to_output(output, color=false)
262
+ @lines.each do |loc|
309
263
  loc = loc.dup
310
- loc.colorize(@code_type) if Pry.color
311
- loc.add_line_number(max_lineno_width) if @with_line_numbers
264
+ loc.colorize(@code_type)
265
+ loc.add_line_number(max_lineno_width, color) if @with_line_numbers
312
266
  loc.add_marker(@marker_lineno) if @with_marker
313
267
  loc.indent(@indentation_num) if @with_indentation
314
- loc.line
315
- }.join("\n") + "\n"
268
+ output << loc.line
269
+ output << "\n"
270
+ end
271
+ output
316
272
  end
317
273
 
318
274
  # Get the comment that describes the expression on the given line number.
@@ -344,7 +300,7 @@ class Pry
344
300
  #
345
301
  # @return [String]
346
302
  def raw
347
- @lines.map(&:line).join("\n") + "\n"
303
+ @lines.map(&:line).join("\n") << "\n"
348
304
  end
349
305
 
350
306
  # Return the number of lines stored.
@@ -0,0 +1,103 @@
1
+ class Pry
2
+ class CodeFile
3
+ DEFAULT_EXT = '.rb'
4
+
5
+ # List of all supported languages.
6
+ # @return [Hash]
7
+ EXTENSIONS = {
8
+ %w(.py) => :python,
9
+ %w(.js) => :javascript,
10
+ %w(.css) => :css,
11
+ %w(.xml) => :xml,
12
+ %w(.php) => :php,
13
+ %w(.html) => :html,
14
+ %w(.diff) => :diff,
15
+ %w(.java) => :java,
16
+ %w(.json) => :json,
17
+ %w(.c .h) => :c,
18
+ %w(.rhtml) => :rhtml,
19
+ %w(.yaml .yml) => :yaml,
20
+ %w(.cpp .hpp .cc .h cxx) => :cpp,
21
+ %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
22
+ }
23
+
24
+ # @return [Symbol] The type of code stored in this wrapper.
25
+ attr_reader :code_type
26
+
27
+ # @param [String] filename The name of a file with code to be detected
28
+ # @param [Symbol] code_type The type of code the `filename` contains
29
+ def initialize(filename, code_type = type_from_filename(filename))
30
+ @filename = filename
31
+ @code_type = code_type
32
+ end
33
+
34
+ # @return [String] The code contained in the current `@filename`.
35
+ def code
36
+ if @filename == Pry.eval_path
37
+ Pry.line_buffer.drop(1)
38
+ elsif Pry::Method::Patcher.code_for(@filename)
39
+ Pry::Method::Patcher.code_for(@filename)
40
+ elsif RbxPath.is_core_path?(@filename)
41
+ File.read(RbxPath.convert_path_to_full(@filename))
42
+ else
43
+ path = abs_path
44
+ @code_type = type_from_filename(path)
45
+ File.read(path)
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ # @raise [MethodSource::SourceNotFoundError] if the `filename` is not
52
+ # readable for some reason.
53
+ # @return [String] absolute path for the given `filename`.
54
+ def abs_path
55
+ code_path.detect { |path| readable?(path) } or
56
+ raise MethodSource::SourceNotFoundError,
57
+ "Cannot open #{ @filename.inspect } for reading."
58
+ end
59
+
60
+ # @param [String] path
61
+ # @return [Boolean] if the path, with or without the default ext,
62
+ # is a readable file then `true`, otherwise `false`.
63
+ def readable?(path)
64
+ File.readable?(path) && !File.directory?(path) or
65
+ File.readable?(path << DEFAULT_EXT)
66
+ end
67
+
68
+ # @return [Array] All the paths that contain code that Pry can use for its
69
+ # API's. Skips directories.
70
+ def code_path
71
+ [from_pwd, from_pry_init_pwd, *from_load_path]
72
+ end
73
+
74
+ # @param [String] filename
75
+ # @param [Symbol] default (:unknown) the file type to assume if none could be
76
+ # detected.
77
+ # @return [Symbol, nil] The CodeRay type of a file from its extension, or
78
+ # `nil` if `:unknown`.
79
+ def type_from_filename(filename, default = :unknown)
80
+ _, @code_type = EXTENSIONS.find do |k, _|
81
+ k.any? { |ext| ext == File.extname(filename) }
82
+ end
83
+
84
+ code_type || default
85
+ end
86
+
87
+ # @return [String]
88
+ def from_pwd
89
+ File.expand_path(@filename, Dir.pwd)
90
+ end
91
+
92
+ # @return [String]
93
+ def from_pry_init_pwd
94
+ File.expand_path(@filename, Pry::INITIAL_PWD)
95
+ end
96
+
97
+ # @return [String]
98
+ def from_load_path
99
+ $LOAD_PATH.map { |path| File.expand_path(@filename, path) }
100
+ end
101
+
102
+ end
103
+ end
@@ -22,7 +22,8 @@ class Pry
22
22
 
23
23
  private
24
24
 
25
- attr_reader :start_line, :end_line
25
+ def start_line; @start_line; end
26
+ def end_line; @end_line; end
26
27
 
27
28
  # If `end_line` is equal to `nil`, then calculate it from the first
28
29
  # parameter, `start_line`. Otherwise, leave it as it is.
@@ -59,9 +59,9 @@ class Pry
59
59
  #
60
60
  # @param [Integer] max_width
61
61
  # @return [void]
62
- def add_line_number(max_width = 0)
62
+ def add_line_number(max_width = 0, color = false)
63
63
  padded = lineno.to_s.rjust(max_width)
64
- colorized_lineno = Pry::Helpers::BaseHelpers.colorize_code(padded)
64
+ colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
65
65
  tuple[0] = "#{ colorized_lineno }: #{ line }"
66
66
  end
67
67
 
@@ -1,4 +1,23 @@
1
1
  class Pry
2
+
3
+ # This class is responsible for taking a string (identifying a
4
+ # command/class/method/etc) and returning the relevant type of object.
5
+ # For example, if the user looks up "show-source" then a
6
+ # `Pry::Command` will be returned. Alternatively, if the user passes in "Pry#repl" then
7
+ # a `Pry::Method` object will be returned.
8
+ #
9
+ # The `CodeObject.lookup` method is responsible for 1. figuring out what kind of
10
+ # object the user wants (applying precedence rules in doing so -- i.e methods
11
+ # get precedence over commands with the same name) and 2. Returning
12
+ # the appropriate object. If the user fails to provide a string
13
+ # identifer for the object (i.e they pass in `nil` or "") then the
14
+ # object looked up will be the 'current method' or 'current class'
15
+ # associated with the Binding.
16
+ #
17
+ # TODO: This class is a clusterfuck. We need a much more robust
18
+ # concept of what a "Code Object" really is. Currently
19
+ # commands/classes/candidates/methods and so on just share a very
20
+ # ill-defined interface.
2
21
  class CodeObject
3
22
  module Helpers
4
23
  # we need this helper as some Pry::Method objects can wrap Procs
@@ -33,7 +52,7 @@ class Pry
33
52
 
34
53
  attr_accessor :str
35
54
  attr_accessor :target
36
- attr_accessor :pry
55
+ attr_accessor :_pry_
37
56
  attr_accessor :super_level
38
57
 
39
58
  def initialize(str, _pry_, options={})
@@ -42,7 +61,7 @@ class Pry
42
61
  }.merge!(options)
43
62
 
44
63
  @str = str
45
- @pry = _pry_
64
+ @_pry_ = _pry_
46
65
  @target = _pry_.current_context
47
66
  @super_level = options[:super]
48
67
  end
@@ -50,18 +69,24 @@ class Pry
50
69
  def command_lookup
51
70
  # TODO: just make it so find_command_by_match_or_listing doesn't
52
71
  # raise?
53
- pry.commands.find_command_by_match_or_listing(str) rescue nil
72
+ _pry_.commands.find_command_by_match_or_listing(str) rescue nil
54
73
  end
55
74
 
75
+ # when no paramter is given (i.e CodeObject.lookup(nil)), then we
76
+ # lookup the 'current object' from the binding.
56
77
  def empty_lookup
57
78
  return nil if str && !str.empty?
58
79
 
59
- if internal_binding?(target)
60
- mod = target_self.is_a?(Module) ? target_self : target_self.class
61
- Pry::WrappedModule(mod)
62
- else
63
- Pry::Method.from_binding(target)
64
- end
80
+ obj = if internal_binding?(target)
81
+ mod = target_self.is_a?(Module) ? target_self : target_self.class
82
+ Pry::WrappedModule(mod)
83
+ else
84
+ Pry::Method.from_binding(target)
85
+ end
86
+
87
+ # respect the super level (i.e user might have specified a
88
+ # --super flag to show-source)
89
+ lookup_super(obj, super_level)
65
90
  end
66
91
 
67
92
  # lookup variables and constants and `self` that are not modules
@@ -87,17 +112,12 @@ class Pry
87
112
  end
88
113
 
89
114
  def method_or_class_lookup
90
- # we need this here because stupid Pry::Method.from_str() does a
91
- # Pry::Method.from_binding when str is nil.
92
- # Once we refactor Pry::Method.from_str() so it doesnt lookup
93
- # from bindings, we can get rid of this check
94
- return nil if str.to_s.empty?
95
-
96
- obj = if str =~ /::(?:\S+)\Z/
97
- Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
98
- else
99
- Pry::Method.from_str(str,target) || Pry::WrappedModule.from_str(str, target)
100
- end
115
+ obj = case str
116
+ when /\S+\(\)\z/
117
+ Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target)
118
+ else
119
+ Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
120
+ end
101
121
 
102
122
  lookup_super(obj, super_level)
103
123
  end
@@ -108,7 +128,6 @@ class Pry
108
128
  [::Proc, ::Method, ::UnboundMethod].any? { |o| obj.is_a?(o) }
109
129
  end
110
130
 
111
-
112
131
  # Returns true if `str` looks like a method, i.e Klass#method
113
132
  # We need to consider this case because method lookups should fall
114
133
  # through to the `method_or_class_lookup()` method but a