-
Notifications
You must be signed in to change notification settings - Fork 26
/
pry-doc.rb
executable file
·215 lines (182 loc) · 6.73 KB
/
pry-doc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# pry-doc.rb
# (C) John Mair (banisterfiend); MIT license
direc = File.dirname(__FILE__)
require "#{direc}/pry-doc/version"
require "yard"
if RUBY_VERSION =~ /1.9/
YARD::Registry.load_yardoc("#{File.dirname(__FILE__)}/pry-doc/core_docs_19")
else
YARD::Registry.load_yardoc("#{File.dirname(__FILE__)}/pry-doc/core_docs_18")
end
class Pry
# do not use pry-doc if rbx is active
if !Object.const_defined?(:RUBY_ENGINE) || RUBY_ENGINE !~ /rbx/
self.config.has_pry_doc = true
end
module MethodInfo
# Convert a method object into the `Class#method` string notation.
# @param [Method, UnboundMethod] meth
# @return [String] The method in string receiver notation.
def self.receiver_notation_for(meth)
if is_singleton?(meth)
"#{meth.owner.to_s[/#<.+?:(.+?)>/, 1]}.#{meth.name}"
else
"#{meth.owner.name}##{meth.name}"
end
end
# Retrives aliases of a method
# @param [Method, UnboundMethod] meth The method object.
# @return [Array] The aliases of a method if it exists
# otherwise, return empty array
def self.aliases(meth)
host = method_host(meth)
method_type = is_singleton?(meth) ? :method : :instance_method
methods = Pry::Method.send(:all_from_common, host, method_type, false).
map { |m| m.instance_variable_get(:@method) }
methods.select { |m| host.send(method_type,m.name) == host.send(method_type,meth.name) }.
reject { |m| m.name == meth.name }.
map { |m| host.send(method_type,m.name) }
end
# Checks whether method is a singleton (i.e class method)
# @param [Method, UnboundMethod] meth
# @param [Boolean] true if singleton
def self.is_singleton?(meth)
meth.owner.ancestors.first != meth.owner
end
# Check whether the file containing the method is already cached.
# @param [Method, UnboundMethod] meth The method object.
# @return [Boolean] Whether the method is cached.
def self.cached?(meth)
!!registry_lookup(meth)
end
def self.registry_lookup(meth)
obj = YARD::Registry.at(receiver_notation_for(meth))
if obj.nil?
if !(aliases = aliases(meth)).empty?
obj = YARD::Registry.at(receiver_notation_for(aliases.first))
elsif meth.owner == Kernel
# YARD thinks that some methods are on Object when
# they're actually on Kernel; so try again on Object if Kernel fails.
obj = YARD::Registry.at("Object##{meth.name}")
end
end
obj
end
# Retrieve the YARD object that contains the method data.
# @param [Method, UnboundMethod] meth The method object.
# @return [YARD::CodeObjects::MethodObject] The YARD data for the method.
def self.info_for(meth)
cache(meth)
registry_lookup(meth)
end
# Determine whether a method is an eval method.
# @return [Boolean] Whether the method is an eval method.
def self.is_eval_method?(meth)
file, _ = meth.source_location
if file =~ /(\(.*\))|<.*>/
true
else
false
end
end
# Attempts to find the c source files if method belongs to a gem
# and use YARD to parse and cache the source files for display
#
# @param [Method, UnboundMethod] meth The method object.
def self.parse_and_cache_if_gem_cext(meth)
if gem_dir = find_gem_dir(meth)
if c_files_found?(gem_dir)
warn "Scanning and caching *.c files..."
YARD.parse("#{gem_dir}/**/*.c")
end
end
end
# @param [String] root directory path of gem that method belongs to
# @return [Boolean] true if c files exist?
def self.c_files_found?(gem_dir)
Dir.glob("#{gem_dir}/**/*.c").count > 0
end
# @return [Object] The host of the method (receiver or owner).
def self.method_host(meth)
is_singleton?(meth) ? meth.receiver : meth.owner
end
# FIXME: this is unnecessarily limited to ext/ and lib/ folders
# @return [String] The root folder of a given gem directory.
def self.gem_root(dir)
dir.split(/\/(?:lib|ext)(?:\/|$)/).first
end
# @param [Method, UnboundMethod] meth The method object.
# @return [String] root directory path of gem that method belongs to,
# nil if could not be found
def self.find_gem_dir(meth)
host = method_host(meth)
begin
host_source_location, _ = WrappedModule.new(host).source_location
break if host_source_location != nil
host = eval(host.namespace_name)
end while host
# we want to exclude all source_locations that aren't gems (i.e
# stdlib)
if host_source_location && host_source_location =~ %r{/gems/}
gem_root(host_source_location)
else
# the WrappedModule approach failed, so try our backup approach
gem_dir_from_method(meth)
end
end
# Try to guess what the gem name will be based on the name of the module.
# We try a few approaches here depending on the `guess` parameter.
# @param [String] name The name of the module.
# @param [Fixnum] guess The current guessing approach to use.
# @return [String, nil] The guessed gem name, or `nil` if out of guesses.
def self.guess_gem_name_from_module_name(name, guess)
case guess
when 0
name.downcase
when 1
name.scan(/[A-Z][a-z]+/).map(&:downcase).join('_')
when 2
name.scan(/[A-Z][a-z]+/).map(&:downcase).join('_').sub("_", "-")
when 3
name.scan(/[A-Z][a-z]+/).map(&:downcase).join('-')
when 4
name
else
nil
end
end
# Try to recover the gem directory of a gem based on a method object.
# @param [Method, UnboundMethod] meth The method object.
# @return [String, nil] The located gem directory.
def self.gem_dir_from_method(meth)
guess = 0
host = method_host(meth)
root_module_name = host.name.split("::").first
while gem_name = guess_gem_name_from_module_name(root_module_name, guess)
matches = $LOAD_PATH.grep %r{/gems/#{gem_name}} if !gem_name.empty?
if matches && matches.any?
return gem_root(matches.first)
else
guess += 1
end
end
nil
end
# Cache the file that holds the method or return immediately if file is
# already cached. Return if the method cannot be cached -
# i.e is a C stdlib method.
# @param [Method, UnboundMethod] meth The method object.
def self.cache(meth)
file, _ = meth.source_location
return if is_eval_method?(meth)
return if cached?(meth)
if !file
parse_and_cache_if_gem_cext(meth)
return
end
log.enter_level(Logger::FATAL) do
YARD.parse(file)
end
end
end
end