Class: Solargraph::YardMap

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/yard_map.rb,
lib/solargraph/yard_map/core_docs.rb

Defined Under Namespace

Modules: CoreDocs Classes: Cache

Constant Summary collapse

@@stdlib_yardoc =
CoreDocs.yard_stdlib_file
@@stdlib_namespaces =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(required: [], workspace: nil) ⇒ YardMap

Returns a new instance of YardMap.



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
# File 'lib/solargraph/yard_map.rb', line 18

def initialize required: [], workspace: nil
  @workspace = workspace
  used = []
  @required = required
  @namespace_yardocs = {}
  @required.each do |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.unshift gy
          add_gem_dependencies g
        end
      end
    end
  end
  yardocs.push CoreDocs.yardoc_file
  yardocs.uniq!
  yardocs.each do |y|
    load_yardoc y
    YARD::Registry.all(:class, :module).each do |ns|
      @namespace_yardocs[ns.path] ||= []
      @namespace_yardocs[ns.path].push y
    end
  end
  cache_core
end

Instance Attribute Details

#required ⇒ Object (readonly)

Returns the value of attribute required.



16
17
18
# File 'lib/solargraph/yard_map.rb', line 16

def required
  @required
end

#workspace ⇒ Object (readonly)

Returns the value of attribute workspace.



15
16
17
# File 'lib/solargraph/yard_map.rb', line 15

def workspace
  @workspace
end

Instance Method Details

#bundled_gem_yardocs ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/solargraph/yard_map.rb', line 283

def bundled_gem_yardocs
  result = []
  unless workspace.nil?
    Bundler.with_clean_env do
      Bundler.environment.chdir(workspace) do
        glfn = File.join(workspace, 'Gemfile.lock')
        spec_versions = {}
        if File.file?(glfn)
          lockfile = Bundler::LockfileParser.new(Bundler.read_file(glfn))
          lockfile.specs.each do |s|
            spec_versions[s.name] = s.version.to_s
          end
        end
        Bundler.environment.dependencies.each do |s|
          if s.type == :runtime
            ver = spec_versions[s.name]
            y = YARD::Registry.yardoc_file_for_gem(s.name, ver)
            if y.nil?
              STDERR.puts "Bundled gem not found: #{s.name}, #{ver}"
            else
              #STDERR.puts "Adding #{y}"
              result.push y
              add_gem_dependencies(s.name)
            end
          end
        end
      end
    end
  end
  result.uniq
end

#document(query) ⇒ Object

Parameters:

  • query (String)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/solargraph/yard_map.rb', line 91

def document query
  found = []
  (yardocs + [@@stdlib_yardoc]).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



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/solargraph/yard_map.rb', line 225

def find_fully_qualified_namespace namespace, scope
  unless scope.empty?
    parts = scope.split('::')
    while parts.length > 0
      here = "#{parts.join('::')}::#{namespace}"
      return here unless yardocs_documenting(here).empty?
      parts.pop
    end
  end
  return namespace unless yardocs_documenting(namespace).empty?
  nil
end

#gem_names ⇒ Object



221
222
223
# File 'lib/solargraph/yard_map.rb', line 221

def gem_names
  Gem::Specification.map{ |s| s.name }.uniq
end

#get_constants(namespace, scope = '') ⇒ Array<Suggestion>

Returns:



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
# File 'lib/solargraph/yard_map.rb', line 104

def get_constants namespace , scope = ''
  cached = cache.get_constants(namespace, scope)
  return cached unless cached.nil?
  consts = []
  result = []
  combined_namespaces(namespace, scope).each do |ns|
    yardocs_documenting(ns).each do |y|
      yard = load_yardoc(y)
      unless yard.nil?
        found = yard.at(ns)
        consts.concat found.children unless found.nil?
      end
    end
  end
  consts.each { |c|
    detail = nil
    kind = nil
    return_type = nil
    if c.kind_of?(YARD::CodeObjects::ClassObject)
      detail = 'Class'
      kind = Suggestion::CLASS
      return_type = "Class<#{c.to_s}>"
    elsif c.kind_of?(YARD::CodeObjects::ModuleObject)
      detail = 'Module'
      kind = Suggestion::MODULE
      return_type = "Module<#{c.to_s}>"
    elsif c.kind_of?(YARD::CodeObjects::ConstantObject)
      detail = 'Constant'
      kind = Suggestion::CONSTANT
    else
      next
    end
    result.push Suggestion.new(c.to_s.split('::').last, detail: c.to_s, kind: kind, docstring: c.docstring, return_type: return_type)
  }
  cache.set_constants(namespace, scope, result)
  result
end

#get_instance_methods(namespace, scope = '', visibility: [:public]) ⇒ Array<Suggestion>

Returns:



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
215
216
217
218
219
# File 'lib/solargraph/yard_map.rb', line 185

def get_instance_methods namespace, scope = '', visibility: [:public]
  cached = cache.get_instance_methods(namespace, scope, visibility)
  return cached unless cached.nil?
  meths = []
  combined_namespaces(namespace, scope).each do |ns|
    yardocs_documenting(ns).each do |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)
              kind = (m.is_attribute? ? Suggestion::FIELD : Suggestion::METHOD)
              meths.push Suggestion.new(label, insert: "#{n.gsub(/=/, ' = ')}", kind: kind, docstring: m.docstring, code_object: m, detail: m.namespace, location: "#{m.file}:#{m.line}", arguments: args)
            end
          }
          if ns.kind_of?(YARD::CodeObjects::ClassObject) and namespace != 'Object'
            unless ns.nil?
              meths += get_instance_methods(ns.superclass.to_s)
            end
          end
          ns.instance_mixins.each do |m|
            meths += get_instance_methods(m.to_s) unless m.to_s == 'Kernel'
          end
        end
      end
    end
  end
  cache.set_instance_methods(namespace, scope, visibility, meths)
  meths
end

#get_methods(namespace, scope = '', visibility: [:public]) ⇒ Array<Suggestion>

Returns:



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
# File 'lib/solargraph/yard_map.rb', line 143

def get_methods namespace, scope = '', visibility: [:public]
  cached = cache.get_methods(namespace, scope, visibility)
  return cached unless cached.nil?
  meths = []
  combined_namespaces(namespace, scope).each do |ns|
    yardocs_documenting(ns).each do |y|
      yard = load_yardoc(y)
      unless yard.nil?
        ns = nil
        ns = find_first_resolved_namespace(yard, namespace, scope)
        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)
            kind = (m.is_attribute? ? Suggestion::FIELD : Suggestion::METHOD)
            meths.push Suggestion.new(label, insert: "#{n.gsub(/=/, ' = ')}", kind: kind, docstring: m.docstring, code_object: m, detail: "#{ns}", location: "#{m.file}:#{m.line}", arguments: args)
          }
          # 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')
            yard = load_yardoc(y)
            i = yard.at("#{ns}#initialize")
            unless i.nil?
              meths.delete_if{|m| m.label == 'new'}
              label = "#{i}"
              args = get_method_args(i)
              meths.push Suggestion.new('new', kind: Suggestion::METHOD, docstring: i.docstring, code_object: i, detail: "#{ns}", location: "#{i.file}:#{i.line}", arguments: args)
            end
          end
        end
      end
    end
  end
  cache.set_methods(namespace, scope, visibility, meths)
  meths
end

#get_namespace_type(fqns) ⇒ Symbol

Returns :class, :module, or nil.

Returns:

  • (Symbol) —

    :class, :module, or nil



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/solargraph/yard_map.rb', line 268

def get_namespace_type(fqns)
  yardocs_documenting(fqns).each do |y|
    yard = load_yardoc y
    unless yard.nil?
      obj = yard.at(fqns)
      unless obj.nil?
        return :class if obj.kind_of?(YARD::CodeObjects::ClassObject)
        return :module if obj.kind_of?(YARD::CodeObjects::ModuleObject)
        return nil
      end
    end
  end
  nil
end

#live_map ⇒ Solargraph::LiveMap

Returns:



51
52
53
# File 'lib/solargraph/yard_map.rb', line 51

def live_map
  @live_map ||= Solargraph::LiveMap.new
end

#load_yardoc(y) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/solargraph/yard_map.rb', line 60

def load_yardoc y
  begin
    if y.kind_of?(Array)
      YARD::Registry.load y, true
    else
      YARD::Registry.load! y
    end
  rescue Exception => e
    STDERR.puts "Error loading yardoc '#{y}' #{e.class} #{e.message}"
    yardocs.delete y
    nil
  end
end

#objects(path, space = '') ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/solargraph/yard_map.rb', line 238

def objects path, space = ''
  result = []
  yardocs.each { |y|
    yard = load_yardoc(y)
    unless yard.nil?
      obj = find_first_resolved_namespace(yard, path, space)
      if obj.nil? and path.include?('#')
        parts = path.split('#')
        obj = yard.at(parts[0])
        unless obj.nil?
          meths = obj.meths(scope: [:instance]).keep_if{|m| m.name.to_s == parts[1]}
          meths.each do |m|
            args = get_method_args(m)
            result.push Solargraph::Suggestion.new(m.name, kind: 'Method', detail: m.path, code_object: m, arguments: args)
          end
        end
      else
        unless obj.nil?
          args = []
          args = get_method_args(obj) if obj.kind_of?(YARD::CodeObjects::MethodObject)
          kind = kind_of_object(obj)
          result.push Solargraph::Suggestion.new(obj.name, kind: kind, detail: obj.path, code_object: obj, arguments: args)
        end
      end
    end
  }
  result
end

#search(query) ⇒ Object

Parameters:

  • query (String)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/solargraph/yard_map.rb', line 75

def search query
  found = []
  (yardocs + [@@stdlib_yardoc]).each { |y|
    yard = load_yardoc(y)
    unless yard.nil?
      yard.paths.each do |p|
        if found.empty? or (query.include?('.') or query.include?('#')) or !(p.include?('.') or p.include?('#'))
          found.push p if p.downcase.include?(query.downcase)
        end
      end
    end
  }
  found.uniq
end

#yardocs ⇒ Array<String>

Returns:

  • (Array<String>)


56
57
58
# File 'lib/solargraph/yard_map.rb', line 56

def yardocs
  @yardocs ||= []
end