Class: Solargraph::YardMap
- Inherits:
-
Object
- Object
- Solargraph::YardMap
- Defined in:
- lib/solargraph/yard_map.rb
Defined Under Namespace
Classes: Cache
Instance Method Summary collapse
- #at(signature) ⇒ Object
- #document(query) ⇒ Object
- #find_fully_qualified_namespace(namespace, scope) ⇒ Object
- #gem_names ⇒ Object
- #get_constants(namespace, scope = '') ⇒ Array<Suggestion>
- #get_instance_methods(namespace, scope = '', visibility: [:public]) ⇒ Array<Suggestion>
- #get_methods(namespace, scope = '', visibility: [:public]) ⇒ Array<Suggestion>
-
#initialize(required: [], workspace: nil) ⇒ YardMap
constructor
A new instance of YardMap.
- #load_yardoc(y) ⇒ Object
- #resolve(signature, scope) ⇒ Object
- #search(query) ⇒ Object
- #yardocs ⇒ Object
Constructor Details
#initialize(required: [], workspace: nil) ⇒ YardMap
Returns a new instance of YardMap.
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 |
# File 'lib/solargraph/yard_map.rb', line 10 def initialize required: [], workspace: nil unless workspace.nil? wsy = File.join(workspace, '.yardoc') yardocs.push wsy if File.exist?(wsy) #wsy = Dir[File.join workspace, '**/*.rb'] #yardocs.push(wsy) end used = [] required.each { |r| if workspace.nil? or !File.exist?(File.join workspace, 'lib', "#{r}.rb") g = r.split('/').first unless used.include?(g) used.push g gy = YARD::Registry.yardoc_file_for_gem(g) if gy.nil? STDERR.puts "Required path not found: #{r}" else yardocs.push gy end end end } # TODO: Experimental loading of all gems #Bundler.load.specs.each { |s| # unless used.include?(s.name) # used.push s.name # gy = YARD::Registry.yardoc_file_for_gem(s.name) # yardocs.push gy unless gy.nil? # end #} yardocs.push File.join(Dir.home, '.solargraph', 'cache', '2.0.0', 'yardoc') #yardocs.push File.join(Dir.home, '.solargraph', 'cache', '2.0.0', 'yardoc-stdlib') yardocs.uniq! cache_core end |
Instance Method Details
#at(signature) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/solargraph/yard_map.rb', line 117 def at signature yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? obj = yard.at(signature) return obj unless obj.nil? end } nil end |
#document(query) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/solargraph/yard_map.rb', line 71 def document query found = [] yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? obj = yard.at query found.push obj unless obj.nil? end } found end |
#find_fully_qualified_namespace(namespace, scope) ⇒ Object
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/solargraph/yard_map.rb', line 218 def find_fully_qualified_namespace namespace, scope yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? obj = find_first_resolved_namespace(yard, namespace, scope) return obj.path unless obj.nil? or !obj.kind_of?(YARD::CodeObjects::NamespaceObject) end } nil end |
#gem_names ⇒ Object
214 215 216 |
# File 'lib/solargraph/yard_map.rb', line 214 def gem_names Gem::Specification.map{ |s| s.name }.uniq end |
#get_constants(namespace, scope = '') ⇒ Array<Suggestion>
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 |
# File 'lib/solargraph/yard_map.rb', line 84 def get_constants namespace, scope = '' cached = cache.get_constants(namespace, scope) return cached unless cached.nil? consts = [] result = [] yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? ns = nil if scope == '' ns = yard.at(namespace) else ns = yard.resolve(P(scope), namespace) end consts += ns.children unless ns.nil? end } consts.each { |c| detail = nil kind = nil if c.kind_of?(YARD::CodeObjects::ClassObject) detail = 'Class' kind = Suggestion::CLASS elsif c.kind_of?(YARD::CodeObjects::ModuleObject) detail = 'Module' kind = Suggestion::MODULE end result.push Suggestion.new(c.to_s.split('::').last, detail: detail, kind: kind) } cache.set_constants(namespace, scope, result) result end |
#get_instance_methods(namespace, scope = '', visibility: [:public]) ⇒ Array<Suggestion>
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 |
# File 'lib/solargraph/yard_map.rb', line 183 def get_instance_methods namespace, scope = '', visibility: [:public] cached = cache.get_instance_methods(namespace, scope, visibility) return cached unless cached.nil? meths = [] yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? ns = nil ns = find_first_resolved_namespace(yard, namespace, scope) unless ns.nil? ns.meths(scope: :instance, visibility: visibility).each { |m| n = m.to_s.split(/[\.#]/).last if n.to_s.match(/^[a-z]/i) and (namespace == 'Kernel' or !m.to_s.start_with?('Kernel#')) and !m.docstring.to_s.include?(':nodoc:') label = "#{n}" args = get_method_args(m) label += " #{args.join(', ')}" unless args.empty? meths.push Suggestion.new(label, insert: "#{n.gsub(/=/, ' = ')}", kind: Suggestion::METHOD, documentation: m.docstring, code_object: m, detail: "#{ns}", location: "#{m.file}:#{m.line}") end } if ns.kind_of?(YARD::CodeObjects::ClassObject) and namespace != 'Object' if ns.superclass.kind_of?(YARD::CodeObjects::Proxy) meths += get_instance_methods(ns.superclass.to_s) end end end end } cache.set_instance_methods(namespace, scope, visibility, meths) meths end |
#get_methods(namespace, scope = '', visibility: [:public]) ⇒ Array<Suggestion>
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 |
# File 'lib/solargraph/yard_map.rb', line 140 def get_methods namespace, scope = '', visibility: [:public] cached = cache.get_methods(namespace, scope, visibility) return cached unless cached.nil? meths = [] binds = [] yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? ns = nil ns = find_first_resolved_namespace(yard, namespace, scope) if namespace == '' and scope == '' # Check for a bind tag in the yardoc root. If it exists, treat # workspace code as a DSL that uses public instance methods in the # specified namespaces. b = yard.root.tag(:bind) binds.concat b.types unless b.nil? end unless ns.nil? ns.meths(scope: :class, visibility: visibility).each { |m| n = m.to_s.split(/[\.#]/).last.gsub(/=/, ' = ') label = "#{n}" args = get_method_args(m) label += " #{args.join(', ')}" unless args.empty? meths.push Suggestion.new(label, insert: "#{n.gsub(/=/, ' = ')}", kind: Suggestion::METHOD, documentation: m.docstring, code_object: m, detail: "#{ns}", location: "#{m.file}:#{m.line}") } # Collect superclass methods if ns.kind_of?(YARD::CodeObjects::ClassObject) and !ns.superclass.nil? meths += get_methods ns.superclass.to_s, '', visibility: [:public, :protected] unless ['Object', 'BasicObject', ''].include?(ns.superclass.to_s) end if ns.kind_of?(YARD::CodeObjects::ClassObject) and namespace != 'Class' meths += get_instance_methods('Class') end end end } binds.each { |b| meths += get_instance_methods(b, scope, visibility: [:public]) } cache.set_methods(namespace, scope, visibility, meths) meths end |
#load_yardoc(y) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/solargraph/yard_map.rb', line 50 def load_yardoc y if y.kind_of?(Array) YARD::Registry.load y, true else YARD::Registry.load! y end end |
#resolve(signature, scope) ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/solargraph/yard_map.rb', line 128 def resolve signature, scope yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? obj = yard.resolve(P(scope), signature) return obj unless obj.nil? end } nil end |
#search(query) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/solargraph/yard_map.rb', line 58 def search query found = [] yardocs.each { |y| yard = load_yardoc(y) unless yard.nil? yard.paths.each { |p| found.push p if p.downcase.include?(query.downcase) } end } found.sort end |
#yardocs ⇒ Object
46 47 48 |
# File 'lib/solargraph/yard_map.rb', line 46 def yardocs @yardocs ||= [] end |