pry 0.9.6-i386-mswin32 → 0.9.6.1-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,8 +61,8 @@ class Pry
61
61
  start_line = (ex_line - 1) - window_size
62
62
  start_line = start_line < 0 ? 0 : start_line
63
63
  end_line = (ex_line - 1) + window_size
64
- if ex_file && is_core_rbx_path?(ex_file)
65
- file_name = rbx_convert_path_to_full(ex_file)
64
+ if ex_file && RbxPath.is_core_path?(ex_file)
65
+ file_name = RbxPath.convert_path_to_full(ex_file)
66
66
  else
67
67
  file_name = ex_file
68
68
  end
@@ -4,12 +4,8 @@ class Pry
4
4
  Experimental = Pry::CommandSet.new do
5
5
 
6
6
  command "reload-method", "Reload the source specifically for a method", :requires_gem => "method_reload" do |meth_name|
7
- if (meth = get_method_object(meth_name, target, {})).nil?
8
- output.puts "Invalid method name: #{meth_name}."
9
- next
10
- end
11
-
12
- meth.reload
7
+ meth = get_method_or_print_error(meth_name, target, {}, :no_cmd)
8
+ meth.reload if meth
13
9
  end
14
10
  end
15
11
  end
@@ -35,7 +35,7 @@ class Pry
35
35
  end
36
36
 
37
37
  def set_file_and_dir_locals(file_name)
38
- return if !target
38
+ return if !target or !file_name
39
39
  _pry_.last_file = File.expand_path(file_name)
40
40
  _pry_.inject_local("_file_", _pry_.last_file, target)
41
41
 
@@ -5,15 +5,6 @@ class Pry
5
5
 
6
6
  module_function
7
7
 
8
- def meth_name_from_binding(b)
9
- meth_name = b.eval('__method__')
10
- if [:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name)
11
- nil
12
- else
13
- meth_name
14
- end
15
- end
16
-
17
8
  # if start_line is not false then add line numbers starting with start_line
18
9
  def render_output(should_flood, start_line, text, color=:blue)
19
10
  if start_line
@@ -27,18 +18,6 @@ class Pry
27
18
  end
28
19
  end
29
20
 
30
- def is_a_dynamically_defined_method?(meth)
31
- file, _ = meth.source_location
32
- !!(file =~ /(\(.*\))|<.*>/)
33
- end
34
-
35
- def check_for_dynamically_defined_method(meth)
36
- file, _ = meth.source_location
37
- if file =~ /(\(.*\))|<.*>/ && file != Pry.eval_path
38
- raise "Cannot retrieve source for dynamically defined method."
39
- end
40
- end
41
-
42
21
  # Open a temp file and yield it to the block, closing it after
43
22
  # @return [String] The path of the temp file
44
23
  def temp_file
@@ -48,225 +27,28 @@ class Pry
48
27
  file.close
49
28
  end
50
29
 
51
- ########### RBX HELPERS #############
52
- def is_core_rbx_path?(path)
53
- rbx? &&
54
- path.start_with?("kernel")
55
- end
56
-
57
- def rbx_core?(meth)
58
- meth.source_location &&
59
- is_core_rbx_path?(meth.source_location.first)
60
- end
61
-
62
- def rvm_ruby?(path)
63
- !!(path =~ /\.rvm/)
64
- end
65
-
66
- def rbx_core_code_for(meth)
67
- rbx_core_code_or_doc_for(meth, :code)
68
- end
69
-
70
- def rbx_core_doc_for(meth)
71
- rbx_core_code_or_doc_for(meth, :doc)
72
- end
73
-
74
- def rbx_core_code_or_doc_for(meth, code_or_doc)
75
- path_line = rbx_core_path_line_for(meth)
76
-
77
- case code_or_doc
78
- when :code
79
- MethodSource.source_helper(path_line)
80
- when :doc
81
- MethodSource.comment_helper(path_line)
82
- end
83
- end
84
-
85
- def rbx_convert_path_to_full(path)
86
- if rvm_ruby?(Rubinius::BIN_PATH)
87
- rbx_rvm_convert_path_to_full(path)
88
- else
89
- rbx_std_convert_path_to_full(path)
90
- end
91
- end
92
-
93
- def rbx_rvm_convert_path_to_full(path)
94
- ruby_name = File.dirname(Rubinius::BIN_PATH).split("/").last
95
- source_path = File.join(File.dirname(File.dirname(File.dirname(Rubinius::BIN_PATH))), "src", ruby_name)
96
- file_name = File.join(source_path, path)
97
- raise "Cannot find rbx core source" if !File.exists?(file_name)
98
- file_name
99
- end
100
-
101
- def rbx_std_convert_path_to_full(path)
102
- file_name = File.join(Rubinius::BIN_PATH, "..", path)
103
- raise "Cannot find rbx core source" if !File.exists?(file_name)
104
- file_name
105
- end
106
-
107
- def rbx_core_path_line_for(meth)
108
- if rvm_ruby?(Rubinius::BIN_PATH)
109
- rvm_rbx_core_path_line_for(meth)
110
- else
111
- std_rbx_core_path_line_for(meth)
112
- end
113
- end
114
-
115
- def std_rbx_core_path_line_for(meth)
116
- file_name = rbx_std_convert_path_to_full(meth.source_location.first)
117
- start_line = meth.source_location.last
118
-
119
- [file_name, start_line]
120
- end
121
-
122
- def rvm_rbx_core_path_line_for(meth)
123
- file_name = rbx_rvm_convert_path_to_full(meth.source_location.first)
124
- start_line = meth.source_location.last
125
-
126
- [file_name, start_line]
127
- end
128
-
129
- ######### END RBX HELPERS ###############
130
-
131
- def code_and_code_type_for(meth)
132
- case code_type = code_type_for(meth)
133
- when nil
134
- return nil
135
- when :c
136
- code = Pry::MethodInfo.info_for(meth).source
137
- code = strip_comments_from_c_code(code)
138
- when :ruby
139
- if meth.source_location.first == Pry.eval_path
140
- start_line = meth.source_location.last
141
-
142
- # FIXME this line below needs to be refactored, WAY too
143
- # much of a hack. We pass nothing to prompt because if
144
- # prompt uses #inspect (or #pretty_inspect) on the context
145
- # it can hang the session if the object being inspected on
146
- # is enormous see: https://github.com/pry/pry/issues/245
147
- p = Pry.new(:input => StringIO.new(Pry.line_buffer[start_line..-1].join), :prompt => proc {""}, :hooks => {}).r(target)
148
- code = strip_leading_whitespace(p)
149
- else
150
- if rbx_core?(meth)
151
- code = strip_leading_whitespace(rbx_core_code_for(meth))
152
- else
153
- code = strip_leading_whitespace(meth.source)
154
- end
155
- end
156
- set_file_and_dir_locals(path_line_for(meth).first)
157
- end
158
-
159
- [code, code_type]
160
- end
161
-
162
- def doc_and_code_type_for(meth)
163
- case code_type = code_type_for(meth)
164
- when nil
165
- return nil
166
- when :c
167
- doc = Pry::MethodInfo.info_for(meth).docstring
168
- when :ruby
169
- if rbx_core?(meth)
170
- doc = strip_leading_hash_and_whitespace_from_ruby_comments(rbx_core_doc_for(meth))
171
- else
172
- doc = strip_leading_hash_and_whitespace_from_ruby_comments(meth.comment)
173
- end
174
- set_file_and_dir_locals(path_line_for(meth).first)
175
- end
176
-
177
- [doc, code_type]
178
- end
179
-
180
- def get_method_object(meth_name, target=nil, options={})
181
- get_method_object_from_target(*get_method_attributes(meth_name, target, options)) rescue nil
182
- end
183
-
184
- def get_method_attributes(meth_name, target=nil, options={})
185
- if meth_name
186
- if meth_name =~ /(\S+)\#(\S+)\Z/
187
- context, meth_name = $1, $2
188
- target = Pry.binding_for(target.eval(context))
189
- type = :instance
190
- elsif meth_name =~ /(\S+)\.(\S+)\Z/
191
- context, meth_name = $1, $2
192
- target = Pry.binding_for(target.eval(context))
193
- type = :singleton
194
- elsif options["instance_methods"]
195
- type = :instance
196
- elsif options[:methods]
197
- type = :singleton
198
- else
199
- type = nil
200
- end
30
+ def get_method_or_print_error(name, target, opts={}, omit_cmd=false)
31
+ if (meth = Pry::Method.from_str(name, target, opts))
32
+ set_file_and_dir_locals(meth.source_file)
33
+ meth
201
34
  else
202
- meth_name = meth_name_from_binding(target)
203
- type = nil
35
+ # FIXME: better/more accurate error handling
36
+ output.print "Invalid method name: #{name}."
37
+ output.print " Type `#{command_name} --help` for help." unless omit_cmd
38
+ output.puts
204
39
  end
205
- [meth_name, target, type]
206
40
  end
207
41
 
208
- def get_method_object_from_target(meth_name, target, type=nil)
209
- case type
210
- when :instance
211
- target.eval("instance_method(:#{meth_name})") rescue nil
212
- when :singleton
213
- target.eval("method(:#{meth_name})") rescue nil
214
- else
215
- get_method_object_from_target(meth_name, target, :instance) ||
216
- get_method_object_from_target(meth_name, target, :singleton)
217
- end
218
- end
219
-
220
- def path_line_for(meth)
221
- if rbx_core?(meth)
222
- rbx_core_path_line_for(meth)
223
- else
224
- meth.source_location
225
- end
226
- end
227
-
228
- def make_header(meth, code_type, content)
42
+ def make_header(meth, content=meth.source)
43
+ code_type = meth.source_type
229
44
  num_lines = "Number of lines: #{Pry::Helpers::Text.bold(content.each_line.count.to_s)}"
230
45
  case code_type
231
- when :ruby
232
- file, line = path_line_for(meth)
233
- "\n#{Pry::Helpers::Text.bold('From:')} #{file} @ line #{line}:\n#{num_lines}\n\n"
234
- else
46
+ when :c
235
47
  file = Pry::MethodInfo.info_for(meth).file
236
48
  "\n#{Pry::Helpers::Text.bold('From:')} #{file} in Ruby Core (C Method):\n#{num_lines}\n\n"
237
49
  end
238
50
  end
239
51
 
240
- def is_a_c_method?(meth)
241
- meth.source_location.nil?
242
- end
243
-
244
- def should_use_pry_doc?(meth)
245
- Pry.config.has_pry_doc && is_a_c_method?(meth)
246
- end
247
-
248
- def code_type_for(meth)
249
- # only C methods
250
- if should_use_pry_doc?(meth)
251
- info = Pry::MethodInfo.info_for(meth)
252
- if info && info.source
253
- code_type = :c
254
- else
255
- output.puts "Cannot find C method: #{meth.name}"
256
- code_type = nil
257
- end
258
- else
259
- if is_a_c_method?(meth)
260
- output.puts "Cannot locate this method: #{meth.name}. Try `gem install pry-doc` to get access to Ruby Core documentation."
261
- code_type = nil
262
- else
263
- check_for_dynamically_defined_method(meth)
264
- code_type = :ruby
265
- end
266
- end
267
- code_type
268
- end
269
-
270
52
  def file_map
271
53
  {
272
54
  [".c", ".h"] => :c,
@@ -357,24 +139,6 @@ class Pry
357
139
  process_yardoc process_rdoc(comment, code_type)
358
140
  end
359
141
 
360
- # strip leading whitespace but preserve indentation
361
- def strip_leading_whitespace(text)
362
- return text if text.empty?
363
- leading_spaces = text.lines.first[/^(\s+)/, 1]
364
- text.gsub(/^#{leading_spaces}/, '')
365
- end
366
-
367
- def strip_leading_hash_and_whitespace_from_ruby_comments(comment)
368
- comment = comment.dup
369
- comment.gsub!(/\A\#+?$/, '')
370
- comment.gsub!(/^\s*#/, '')
371
- strip_leading_whitespace(comment)
372
- end
373
-
374
- def strip_comments_from_c_code(code)
375
- code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
376
- end
377
-
378
142
  def invoke_editor(file, line)
379
143
  if Pry.config.editor.respond_to?(:call)
380
144
  editor_invocation = Pry.config.editor.call(file, line)
data/lib/pry/method.rb ADDED
@@ -0,0 +1,184 @@
1
+ class Pry
2
+ class Method
3
+ include RbxMethod if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
4
+
5
+ class << self
6
+ def from_str(str, target=TOPLEVEL_BINDING, options={})
7
+ if str.nil?
8
+ from_binding(target)
9
+ elsif str.to_s =~ /(\S+)\#(\S+)\Z/
10
+ context, meth_name = $1, $2
11
+ from_module(target.eval(context), meth_name)
12
+ elsif str.to_s =~ /(\S+)\.(\S+)\Z/
13
+ context, meth_name = $1, $2
14
+ from_obj(target.eval(context), meth_name)
15
+ elsif options[:instance]
16
+ new(target.eval("instance_method(:#{str})")) rescue nil
17
+ elsif options[:methods]
18
+ new(target.eval("method(:#{str})")) rescue nil
19
+ else
20
+ from_str(str, target, :instance => true) ||
21
+ from_str(str, target, :methods => true)
22
+ end
23
+ end
24
+
25
+ def from_binding(b)
26
+ meth_name = b.eval('__method__')
27
+ if [:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name)
28
+ nil
29
+ else
30
+ new(b.eval("method(:#{meth_name})"))
31
+ end
32
+ end
33
+
34
+ def from_module(nodule, name)
35
+ new(nodule.instance_method(name)) rescue nil
36
+ end
37
+ alias from_class from_module
38
+
39
+ def from_obj(obj, name)
40
+ new(obj.method(name)) rescue nil
41
+ end
42
+ end
43
+
44
+ def initialize(method)
45
+ @method = method
46
+ end
47
+
48
+ def source
49
+ @source ||= case source_type
50
+ when :c
51
+ info = pry_doc_info
52
+ if info and info.source
53
+ code = strip_comments_from_c_code(info.source)
54
+ end
55
+ when :rbx
56
+ strip_leading_whitespace(core_code)
57
+ when :ruby
58
+ if pry_method?
59
+ code = Pry.new(:input => StringIO.new(Pry.line_buffer[source_line..-1].join), :prompt => proc {""}, :hooks => {}).r
60
+ else
61
+ code = @method.source
62
+ end
63
+ strip_leading_whitespace(code)
64
+ end
65
+ end
66
+
67
+ def doc
68
+ @doc ||= case source_type
69
+ when :c
70
+ info = pry_doc_info
71
+ info.docstring if info
72
+ when :rbx
73
+ strip_leading_hash_and_whitespace_from_ruby_comments(core_doc)
74
+ when :ruby
75
+ if pry_method?
76
+ raise "Error: Can't view doc for a REPL-defined method."
77
+ else
78
+ strip_leading_hash_and_whitespace_from_ruby_comments(@method.comment)
79
+ end
80
+ end
81
+ end
82
+
83
+ def source_type
84
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
85
+ if core? then :rbx else :ruby end
86
+ else
87
+ if source_location.nil? then :c else :ruby end
88
+ end
89
+ end
90
+
91
+ def source_file
92
+ source_location.nil? ? nil : source_location.first
93
+ end
94
+
95
+ def source_line
96
+ source_location.nil? ? nil : source_location.last
97
+ end
98
+
99
+ def visibility
100
+ if owner.public_instance_methods.include?(name)
101
+ :public
102
+ elsif owner.protected_instance_methods.include?(name)
103
+ :protected
104
+ elsif owner.private_instance_methods.include?(name)
105
+ :private
106
+ else
107
+ :none
108
+ end
109
+ end
110
+
111
+ # paraphrased from awesome_print gem
112
+ def signature
113
+ if respond_to?(:parameters)
114
+ args = parameters.inject([]) do |arr, (type, name)|
115
+ name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
116
+ arr << case type
117
+ when :req then name.to_s
118
+ when :opt, :rest then "*#{name}"
119
+ when :block then "&#{name}"
120
+ else '?'
121
+ end
122
+ end
123
+ else
124
+ args = (1..arity.abs).map { |i| "arg#{i}" }
125
+ args[-1] = "*#{args[-1]}" if arity < 0
126
+ end
127
+
128
+ "#{name}(#{args.join(', ')})"
129
+ end
130
+
131
+ def dynamically_defined?
132
+ source_file ? !!(source_file =~ /(\(.*\))|<.*>/) : nil
133
+ end
134
+
135
+ def pry_method?
136
+ source_file == Pry.eval_path
137
+ end
138
+
139
+ def ==(obj)
140
+ if obj.is_a? Pry::Method
141
+ super
142
+ else
143
+ @method == obj
144
+ end
145
+ end
146
+
147
+ def is_a?(klass)
148
+ klass == Pry::Method or @method.is_a?(klass)
149
+ end
150
+ alias kind_of? is_a?
151
+
152
+ def respond_to?(method_name)
153
+ super or @method.respond_to?(method_name)
154
+ end
155
+
156
+ def method_missing(method_name, *args, &block)
157
+ @method.send(method_name, *args, &block)
158
+ end
159
+
160
+ private
161
+ def pry_doc_info
162
+ if Pry.config.has_pry_doc
163
+ Pry::MethodInfo.info_for(@method) or output.puts "Cannot find C method: #{name}"
164
+ else
165
+ output.puts "Cannot locate this method: #{name}. Try `gem install pry-doc` to get access to Ruby Core documentation."
166
+ end
167
+ end
168
+
169
+ def strip_comments_from_c_code(code)
170
+ code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
171
+ end
172
+
173
+ def strip_leading_hash_and_whitespace_from_ruby_comments(comment)
174
+ comment = comment.dup
175
+ comment.gsub!(/\A\#+?$/, '')
176
+ comment.gsub!(/^\s*#/, '')
177
+ strip_leading_whitespace(comment)
178
+ end
179
+
180
+ def strip_leading_whitespace(text)
181
+ Pry::Helpers::CommandHelpers.unindent(text)
182
+ end
183
+ end
184
+ end