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
# 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
  required.each { |r|
    gy = YARD::Registry.yardoc_file_for_gem(r)
    yardocs.push gy unless gy.nil?
  }
  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')
end

Instance Method Details

#at(signature) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/solargraph/yard_map.rb', line 46

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

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



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 25

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|
    result.push Suggestion.new(c.to_s.split('::').last, kind: Suggestion::CLASS)
  }
  result
end

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/solargraph/yard_map.rb', line 93

def get_instance_methods namespace, scope = ''
  meths = []
  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
      unless ns.nil?
        ns.meths(scope: :instance, visibility: [:public]).each { |m|
          n = m.to_s.split('#').last
          meths.push Suggestion.new("#{n}", kind: Suggestion::METHOD, documentation: m.docstring, code_object: m) if n.to_s.match(/^[a-z]/i) and !m.to_s.start_with?('Kernel#') and !m.docstring.to_s.include?(':nodoc:')
        }
        if ns.kind_of?(YARD::CodeObjects::ClassObject) and namespace != 'Object'
          meths += get_instance_methods('Object')
        end
      end
    end
  }
  meths
end

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/solargraph/yard_map.rb', line 68

def get_methods namespace, scope = ''
  meths = []
  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
      unless ns.nil?
        ns.meths(scope: :class, visibility: [:public]).each { |m|
          n = m.to_s.split('.').last
          meths.push Suggestion.new("#{n}", kind: Suggestion::METHOD) if n.to_s.match(/^[a-z]/i)
        }
        if ns.kind_of?(YARD::CodeObjects::ClassObject) and namespace != 'Class'
          meths += get_instance_methods('Class')
        end
      end
    end
  }
  meths
end

#resolve(signature, scope) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/solargraph/yard_map.rb', line 57

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

#yardocs ⇒ Object



21
22
23
# File 'lib/solargraph/yard_map.rb', line 21

def yardocs
  @yardocs ||= []
end