pry 0.9.8pre5-java → 0.9.8pre6-java

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -12,3 +12,4 @@ coverage/
12
12
  .rbx/
13
13
  .rvmrc
14
14
  Gemfile.lock
15
+ *.swp
data/CHANGELOG CHANGED
@@ -1,3 +1,39 @@
1
+ X/X/2012 version 0.9.8
2
+
3
+ MAJOR NEW FEATURES
4
+ - upgraded command api, https://github.com/pry/pry/wiki/Custom-commands
5
+ - added a system of hooks for customizing pry behaviour
6
+ - changed syntax checking to use eval() for improved accuracy
7
+
8
+
9
+ complete CHANGELOG:
10
+ * CommandError's no longer cause the current input to be disgarded
11
+ * Better syntax highlighting for rbx code code
12
+ * added cat --in to show pry input history
13
+ * prefixed temporary file names with 'pry'
14
+ * added a man page
15
+ * added CommandSet#{before_command,after_command} for enhancing builtin commands
16
+ * added checking for namespace collisions with pry commands, set Pry.config.collision_warning
17
+ * work around namespace collisions by ensuring lines starting with a space are executed as
18
+ * ruby.work around namespace collisions by prensuring lines starting with a space are executed as ruby
19
+ * added handlers for Ctrl+C (SIGINT) on jruby, these are now caught as in other ruby versions
20
+ * removed dependency on ruby_parser
21
+ * prevented colours leaking across the pry prompt
22
+ * fixed edge cases in Pry::Method, for methods with crazy names and methods that have been 'undef'd
23
+ * refactored history handling code for clarity and correctness
24
+ * added Pry::WrappedModule as a counterpart to Pry::Method
25
+ * made a trailing , cause pry to wait for further input
26
+ * removed gist-method command, added gist command
27
+ * added pry-backtrace command to show history of current session
28
+ * fixed whereami within 'super' methods
29
+ * replaced inline version guards by Pry::Helpers::BaseHelpers.{rbx?,jruby?,windows?} etc.
30
+ * removed the CommandProcessor, its functionality is part of the new Command class
31
+ * changed cd .. at the top level so it doesn't quit pry.
32
+ * changed edit-command to no-longer need a command set argument
33
+ * fixed empty lines so that they don't replace _ by nil
34
+ * fixed SyntaxErrors at the REPL level so they don't replace _ex_.
35
+ <TODO: everything that happened after f3262e32700709a6998579a9f71cdc6b8f4a695e>
36
+
1
37
  5/11/2011 version 0.9.7.4 hotfix
2
38
  * ls -M now works in modules (bugfix)
3
39
  * added exception msg for bad cd object/path
data/Rakefile CHANGED
@@ -20,7 +20,7 @@ def apply_spec_defaults(s)
20
20
  s.files = `git ls-files`.split("\n")
21
21
  s.test_files = `git ls-files -- test/*`.split("\n")
22
22
  s.add_dependency('coderay', '~> 1.0.5')
23
- s.add_dependency('slop', ['>= 2.4.1', '< 3'])
23
+ s.add_dependency('slop', ['>= 2.4.3', '< 3'])
24
24
  s.add_dependency('method_source','~> 0.7')
25
25
  s.add_development_dependency('bacon', '~> 1.1')
26
26
  s.add_development_dependency('open4', '~> 1.3')
@@ -116,7 +116,7 @@ end
116
116
  end
117
117
 
118
118
  desc "build all platform gems at once"
119
- task :gems => [:clean, :rmgems, :gemspec, 'ruby:gem', 'mswin32:gem', 'mingw32:gem', 'jruby:gem']
119
+ task :gems => [:clean, :rmgems, 'ruby:gem', 'mswin32:gem', 'mingw32:gem', 'jruby:gem']
120
120
 
121
121
  desc "remove all platform gems"
122
122
  task :rmgems => ['ruby:clobber_package']
data/lib/pry.rb CHANGED
@@ -146,8 +146,8 @@ class Pry
146
146
 
147
147
  # CommandErrors are caught by the REPL loop and displayed to the user. They
148
148
  # indicate an exceptional condition that's fatal to the current command.
149
- class CommandError < StandardError
150
- end
149
+ class CommandError < StandardError; end
150
+ class NonMethodContextError < CommandError; end
151
151
  end
152
152
 
153
153
  require "method_source"
@@ -179,6 +179,7 @@ end
179
179
  require "pry/version"
180
180
  require "pry/rbx_method"
181
181
  require "pry/rbx_path"
182
+ require "pry/code"
182
183
  require "pry/method"
183
184
  require "pry/wrapped_module"
184
185
  require "pry/history_array"
@@ -0,0 +1,344 @@
1
+ class Pry
2
+ class << self
3
+ # Convert the given object into an instance of `Pry::Code`, if it isn't
4
+ # already one.
5
+ #
6
+ # @param [Code, Method, UnboundMethod, Proc, Pry::Method, String, Array,
7
+ # IO] obj
8
+ def Code(obj)
9
+ case obj
10
+ when Code
11
+ obj
12
+ when ::Method, UnboundMethod, Proc, Pry::Method
13
+ Code.from_method(obj)
14
+ else
15
+ Code.new(obj)
16
+ end
17
+ end
18
+ end
19
+
20
+ # `Pry::Code` is a class that encapsulates lines of source code and their
21
+ # line numbers and formats them for terminal output. It can read from a file
22
+ # or method definition or be instantiated with a `String` or an `Array`.
23
+ #
24
+ # In general, the formatting methods in `Code` return a new `Code` object
25
+ # which will format the text as specified when `#to_s` is called. This allows
26
+ # arbitrary chaining of formatting methods without mutating the original
27
+ # object.
28
+ class Code
29
+ class << self
30
+ # Instantiate a `Code` object containing code loaded from a file or
31
+ # Pry's line buffer.
32
+ #
33
+ # @param [String] fn The name of a file, or "(pry)".
34
+ # @param [Symbol] code_type (:ruby) The type of code the file contains.
35
+ # @return [Code]
36
+ def from_file(fn, code_type=nil)
37
+ if fn == Pry.eval_path
38
+ f = Pry.line_buffer.drop(1)
39
+ else
40
+ if File.readable?(fn)
41
+ f = File.open(fn, 'r')
42
+ code_type = type_from_filename(fn)
43
+ else
44
+ raise CommandError, "Cannot open #{fn.inspect} for reading."
45
+ end
46
+ end
47
+ new(f, 1, code_type || :ruby)
48
+ ensure
49
+ f.close if f.respond_to?(:close)
50
+ end
51
+
52
+ # Instantiate a `Code` object containing code extracted from a
53
+ # `::Method`, `UnboundMethod`, `Proc`, or `Pry::Method` object.
54
+ #
55
+ # @param [::Method, UnboundMethod, Proc, Pry::Method] meth The method
56
+ # object.
57
+ # @param [Fixnum, nil] The line number to start on, or nil to use the
58
+ # method's original line numbers.
59
+ # @return [Code]
60
+ def from_method(meth, start_line=nil)
61
+ meth = Pry::Method(meth)
62
+ start_line ||= meth.source_line || 1
63
+ new(meth.source, start_line, meth.source_type)
64
+ end
65
+
66
+ protected
67
+ # Guess the CodeRay type of a file from its extension, or nil if
68
+ # unknown.
69
+ #
70
+ # @param [String] filename
71
+ # @return [Symbol, nil]
72
+ def type_from_filename(filename)
73
+ map = {
74
+ %w(.c .h) => :c,
75
+ %w(.cpp .hpp .cc .h cxx) => :cpp,
76
+ %w(.rb .ru .irbrc .gemspec .pryrc) => :ruby,
77
+ %w(.py) => :python,
78
+ %w(.diff) => :diff,
79
+ %w(.css) => :css,
80
+ %w(.html) => :html,
81
+ %w(.yaml .yml) => :yaml,
82
+ %w(.xml) => :xml,
83
+ %w(.php) => :php,
84
+ %w(.js) => :javascript,
85
+ %w(.java) => :java,
86
+ %w(.rhtml) => :rhtml,
87
+ %w(.json) => :json
88
+ }
89
+
90
+ _, type = map.find do |k, _|
91
+ k.any? { |ext| ext == File.extname(filename) }
92
+ end
93
+
94
+ type
95
+ end
96
+ end
97
+
98
+ attr_accessor :code_type
99
+
100
+ # Instantiate a `Code` object containing code from the given `Array`,
101
+ # `String`, or `IO`. The first line will be line 1 unless specified
102
+ # otherwise. If you need non-contiguous line numbers, you can create an
103
+ # empty `Code` object and then use `#push` to insert the lines.
104
+ #
105
+ # @param [Array<String>, String, IO] lines
106
+ # @param [Fixnum?] (1) start_line
107
+ # @param [Symbol?] (:ruby) code_type
108
+ def initialize(lines=[], start_line=1, code_type=:ruby)
109
+ if lines.is_a? String
110
+ lines = lines.lines
111
+ end
112
+
113
+ @lines = lines.each_with_index.map { |l, i| [l.chomp, i + start_line] }
114
+ @code_type = code_type
115
+ end
116
+
117
+ # Append the given line. `line_num` is one more than the last existing
118
+ # line, unless specified otherwise.
119
+ #
120
+ # @param [String] line
121
+ # @param [Fixnum?] line_num
122
+ # @return [String] The inserted line.
123
+ def push(line, line_num=nil)
124
+ line_num = @lines.last.last + 1 unless line_num
125
+ @lines.push([line, line_num])
126
+ line
127
+ end
128
+ alias << push
129
+
130
+ # Filter the lines using the given block.
131
+ #
132
+ # @yield [line]
133
+ # @return [Code]
134
+ def select(&blk)
135
+ alter do
136
+ @lines = @lines.select(&blk)
137
+ end
138
+ end
139
+
140
+ # Remove all lines that aren't in the given range, expressed either as a
141
+ # `Range` object or a first and last line number (inclusive). Negative
142
+ # indices count from the end of the array of lines.
143
+ #
144
+ # @param [Range, Fixnum] start_line
145
+ # @param [Fixnum?] end_line
146
+ # @return [Code]
147
+ def between(start_line, end_line=nil)
148
+ return self unless start_line
149
+
150
+ if start_line.is_a? Range
151
+ end_line = start_line.last
152
+ end_line -= 1 if start_line.exclude_end?
153
+
154
+ start_line = start_line.first
155
+ else
156
+ end_line ||= start_line
157
+ end
158
+
159
+ if start_line > 0
160
+ start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
161
+ else
162
+ start_idx = start_line
163
+ end
164
+
165
+ if end_line > 0
166
+ end_idx = (@lines.index { |l| l.last > end_line } || 0) - 1
167
+ else
168
+ end_idx = end_line
169
+ end
170
+
171
+ alter do
172
+ @lines = @lines[start_idx..end_idx] || []
173
+ end
174
+ end
175
+
176
+ # Remove all lines except for the `lines` up to and excluding `line_num`.
177
+ #
178
+ # @param [Fixnum] line_num
179
+ # @param [Fixnum] (1) lines
180
+ # @return [Code]
181
+ def before(line_num, lines=1)
182
+ return self unless line_num
183
+
184
+ select do |l, ln|
185
+ ln >= line_num - lines && ln < line_num
186
+ end
187
+ end
188
+
189
+ # Remove all lines except for the `lines` on either side of and including
190
+ # `line_num`.
191
+ #
192
+ # @param [Fixnum] line_num
193
+ # @param [Fixnum] (1) lines
194
+ # @return [Code]
195
+ def around(line_num, lines=1)
196
+ return self unless line_num
197
+
198
+ select do |l, ln|
199
+ ln >= line_num - lines && ln <= line_num + lines
200
+ end
201
+ end
202
+
203
+ # Remove all lines except for the `lines` after and excluding `line_num`.
204
+ #
205
+ # @param [Fixnum] line_num
206
+ # @param [Fixnum] (1) lines
207
+ # @return [Code]
208
+ def after(line_num, lines=1)
209
+ return self unless line_num
210
+
211
+ select do |l, ln|
212
+ ln > line_num && ln <= line_num + lines
213
+ end
214
+ end
215
+
216
+ # Remove all lines that don't match the given `pattern`.
217
+ #
218
+ # @param [Regexp] pattern
219
+ # @return [Code]
220
+ def grep(pattern)
221
+ return self unless pattern
222
+ pattern = Regexp.new(pattern)
223
+
224
+ select do |l, ln|
225
+ l =~ pattern
226
+ end
227
+ end
228
+
229
+ # Format output with line numbers next to it, unless `y_n` is falsy.
230
+ #
231
+ # @param [Boolean?] (true) y_n
232
+ # @return [Code]
233
+ def with_line_numbers(y_n=true)
234
+ alter do
235
+ @with_line_numbers = y_n
236
+ end
237
+ end
238
+
239
+ # Format output with a marker next to the given `line_num`, unless `line_num`
240
+ # is falsy.
241
+ #
242
+ # @param [Fixnum?] (1) line_num
243
+ # @return [Code]
244
+ def with_marker(line_num=1)
245
+ alter do
246
+ @with_marker = !!line_num
247
+ @marker_line_num = line_num
248
+ end
249
+ end
250
+
251
+ # Format output with the specified number of spaces in front of every line,
252
+ # unless `spaces` is falsy.
253
+ #
254
+ # @param [Fixnum?] (0) spaces
255
+ # @return [Code]
256
+ def with_indentation(spaces=0)
257
+ alter do
258
+ @with_indentation = !!spaces
259
+ @indentation_num = spaces
260
+ end
261
+ end
262
+
263
+ # @return [String]
264
+ def inspect
265
+ Object.instance_method(:to_s).bind(self).call
266
+ end
267
+
268
+ # Based on the configuration of the object, return a formatted String
269
+ # representation.
270
+ #
271
+ # @return [String]
272
+ def to_s
273
+ lines = @lines.map(&:dup)
274
+
275
+ if Pry.color
276
+ lines.each do |l|
277
+ l[0] = CodeRay.scan(l[0], @code_type).term
278
+ end
279
+ end
280
+
281
+ if @with_line_numbers
282
+ max_width = lines.last.last.to_s.length if lines.length > 0
283
+ lines.each do |l|
284
+ padded_line_num = l[1].to_s.rjust(max_width)
285
+ l[0] = "#{Pry::Helpers::Text.blue(padded_line_num)}: #{l[0]}"
286
+ end
287
+ end
288
+
289
+ if @with_marker
290
+ lines.each do |l|
291
+ if l[1] == @marker_line_num
292
+ l[0] = " => #{l[0]}"
293
+ else
294
+ l[0] = " #{l[0]}"
295
+ end
296
+ end
297
+ end
298
+
299
+ if @with_indentation
300
+ lines.each do |l|
301
+ l[0] = "#{' ' * @indentation_num}#{l[0]}"
302
+ end
303
+ end
304
+
305
+ lines.map { |l| "#{l.first}\n" }.join
306
+ end
307
+
308
+ # Return the number of lines stored.
309
+ #
310
+ # @return [Fixnum]
311
+ def length
312
+ @lines ? @lines.length : 0
313
+ end
314
+
315
+ # Two `Code` objects are equal if they contain the same lines with the same
316
+ # numbers. Otherwise, call `to_s` and `chomp` and compare as Strings.
317
+ #
318
+ # @param [Code, Object] other
319
+ # @return [Boolean]
320
+ def ==(other)
321
+ if other.is_a?(Code)
322
+ @other_lines = other.instance_variable_get(:@lines)
323
+ @lines.each_with_index.all? do |(l, ln), i|
324
+ l == @other_lines[i].first && ln == @other_lines[i].last
325
+ end
326
+ else
327
+ to_s.chomp == other.to_s.chomp
328
+ end
329
+ end
330
+
331
+ # Forward any missing methods to the output of `#to_s`.
332
+ def method_missing(name, *args, &blk)
333
+ to_s.send(name, *args, &blk)
334
+ end
335
+ undef =~
336
+
337
+ protected
338
+ # An abstraction of the `dup.instance_eval` pattern used throughout this
339
+ # class.
340
+ def alter(&blk)
341
+ dup.tap { |o| o.instance_eval(&blk) }
342
+ end
343
+ end
344
+ end
@@ -16,8 +16,8 @@ class Pry
16
16
  class << self
17
17
  attr_accessor :block
18
18
  attr_accessor :name
19
- attr_accessor :description
20
- attr_accessor :command_options
19
+ attr_writer :description
20
+ attr_writer :command_options
21
21
 
22
22
  # Define or get the command's description
23
23
  def description(arg=nil)
@@ -27,7 +27,8 @@ class Pry
27
27
 
28
28
  # Define or get the command's options
29
29
  def command_options(arg=nil)
30
- @command_options = arg if arg
30
+ @command_options ||= {}
31
+ @command_options.merge!(arg) if arg
31
32
  @command_options
32
33
  end
33
34
  # backward compatibility
@@ -278,7 +279,7 @@ class Pry
278
279
  instance_exec(*args, &block)
279
280
  end
280
281
 
281
- ret = call *args
282
+ ret = call(*args)
282
283
 
283
284
  self.class.hooks[:after].each do |block|
284
285
  ret = instance_exec(*args, &block)
@@ -286,6 +287,22 @@ class Pry
286
287
 
287
288
  ret
288
289
  end
290
+
291
+ # Fix the number of arguments we pass to a block to avoid arity warnings.
292
+ #
293
+ # @param Number the arity of the block
294
+ # @param Array the arguments to pass
295
+ # @return Array a (possibly shorter) array of the arguments to pass
296
+ def correct_arg_arity(arity, args)
297
+ case
298
+ when arity < 0
299
+ args
300
+ when arity == 0
301
+ []
302
+ when arity > 0
303
+ args.values_at(*(0..(arity - 1)).to_a)
304
+ end
305
+ end
289
306
  end
290
307
 
291
308
  # A super-class for Commands that are created with a single block.
@@ -306,22 +323,6 @@ class Pry
306
323
  instance_exec(*correct_arg_arity(block.arity, args), &block)
307
324
  end
308
325
 
309
- # Fix the number of arguments we pass to a block to avoid arity warnings.
310
- #
311
- # @param Number the arity of the block
312
- # @param Array the arguments to pass
313
- # @return Array a (possibly shorter) array of the arguments to pass
314
- def correct_arg_arity(arity, args)
315
- case
316
- when arity < 0
317
- args
318
- when arity == 0
319
- []
320
- when arity > 0
321
- args.values_at *(0..(arity - 1)).to_a
322
- end
323
- end
324
-
325
326
  def help; description; end
326
327
  end
327
328
 
@@ -355,7 +356,7 @@ class Pry
355
356
  output.puts slop.help
356
357
  void
357
358
  else
358
- process
359
+ process(*correct_arg_arity(method(:process).arity, args))
359
360
  end
360
361
  end
361
362