Class: Solargraph::YardMap

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of YardMap.



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

def initialize required: [], workspace: nil
  #unless workspace.nil?
  #  wsy = File.join(workspace, '.yardoc')
  #  yardocs.push wsy if File.exist?(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)
        yardocs.push gy unless gy.nil?
      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!
end

Instance Method Details

#at(signature) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/solargraph/yard_map.rb', line 101

def at signature
  yardocs.each { |y|
    yard = YARD::Registry.load! y
    unless yard.nil?
      obj = yard.at(signature)
      return obj unless obj.nil?
    end
  }
  nil
end

#document(query) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/solargraph/yard_map.rb', line 54

def document query
  found = []
  yardocs.each { |y|
    yard = YARD::Registry.load! y
    unless yard.nil?
      obj = yard.at query
      #found.push YARD::Templates::Engine.render(format: :html, object: obj) unless obj.nil?
      unless obj.nil?
        # HACK: Fix return tags in core documentation
        fix_return! obj if obj.kind_of?(YARD::CodeObjects::MethodObject) and y.include?('.solargraph')
        found.push obj
      end
    end
  }
  found
end

#find_fully_qualified_namespace(namespace, scope) ⇒ Object



189
190
191
192
# File 'lib/solargraph/yard_map.rb', line 189

def find_fully_qualified_namespace namespace, scope
  obj = resolve(namespace, scope)
  return obj.path unless obj.nil?
end

#gem_namesObject



185
186
187
# File 'lib/solargraph/yard_map.rb', line 185

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

#get_constants(namespace, scope = '') ⇒ Object



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

def get_constants namespace, scope = ''
  consts = []
  result = []
  yardocs.each { |y|
    yard = YARD::Registry.load! 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)
  }
  result
end

#get_instance_methods(namespace, scope = '', visibility: [:public]) ⇒ Object



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

def get_instance_methods namespace, scope = '', visibility: [:public]
  meths = []
  yardocs.each { |y|
    yard = YARD::Registry.load! y
    unless yard.nil?
      ns = nil
      #if scope == ''
      #  ns = yard.at(namespace)
      #else
        ns = find_first_resolved_namespace(yard, namespace, scope)
      #end
      unless ns.nil?
        ns.meths(scope: :instance, visibility: visibility).each { |m|
          # HACK: Fix return tags in core documentation
          fix_return! m if y.include?('.solargraph')
          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}", 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'
          meths += get_instance_methods('Object')
        end
      end
    end
  }
  meths
end

#get_methods(namespace, scope = '', visibility: [:public]) ⇒ Object



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

def get_methods namespace, scope = '', visibility: [:public]
  meths = []
  yardocs.each { |y|
    yard = YARD::Registry.load! y
    unless yard.nil?
      ns = nil
      #if scope == ''
      #  ns = yard.at(namespace)
      #else
        ns = find_first_resolved_namespace(yard, namespace, scope)
      #end
      unless ns.nil? or !ns.kind_of?(YARD::CodeObjects::NamespaceObject)
        ns.meths(scope: :class, visibility: visibility).each { |m|
          # HACK: Fix return tags in core documentation
          fix_return! m if y.include?('.solargraph')
          n = m.to_s.split(/[\.#]/).last
          label = "#{n}"
          args = get_method_args(m)
          label += " #{args.join(', ')}" unless args.empty?
          meths.push Suggestion.new(label, insert: "#{n}", kind: Suggestion::METHOD, documentation: m.docstring, code_object: m, detail: "#{ns}", location: "#{m.file}:#{m.line}")
        }
        if ns.kind_of?(YARD::CodeObjects::ClassObject) and namespace != 'Class'
          meths += get_instance_methods('Class')
        end
      end
    end
  }
  meths
end

#resolve(signature, scope) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/solargraph/yard_map.rb', line 112

def resolve signature, scope
  yardocs.each { |y|
    yard = YARD::Registry.load! y
    unless yard.nil?
      obj = yard.resolve(P(scope), signature)
      return obj unless obj.nil?
    end
  }
  nil
end

#search(query) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solargraph/yard_map.rb', line 41

def search query
  found = []
  yardocs.each { |y|
    yard = YARD::Registry.load! y
    unless yard.nil?
      yard.paths.each { |p|
        found.push p if p.downcase.include?(query.downcase)
      }
    end
  }
  found.sort
end

#yardocsObject



37
38
39
# File 'lib/solargraph/yard_map.rb', line 37

def yardocs
  @yardocs ||= []
end