Module: Solargraph::NodeMethods

Included in:
ApiMap, CodeMap
Defined in:
lib/solargraph/node_methods.rb

Instance Method Summary collapse

Instance Method Details

#const_from(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/solargraph/node_methods.rb', line 25

def const_from node
  if node.kind_of?(AST::Node) and node.type == :const
    result = ''
    unless node.children[0].nil?
      result = const_from(node.children[0])
    end
    if result == ''
      result = node.children[1].to_s
    else
      result = result + '::' + node.children[1].to_s
    end
    result
  else
    nil
  end
end

#drill_signature(node, signature) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/solargraph/node_methods.rb', line 42

def drill_signature node, signature
  return signature unless node.kind_of?(AST::Node)
  if node.type == :const or node.type == :cbase
    unless node.children[0].nil?
      signature += drill_signature(node.children[0], signature)
    end
    signature += '::' unless signature.empty?
    signature += node.children[1].to_s
  elsif node.type == :lvar
    signature += '.' unless signature.empty?
    signature += node.children[0].to_s
  elsif node.type == :send
    unless node.children[0].nil?
      signature += drill_signature(node.children[0], signature)
    end
    signature += '.' unless signature.empty?
    signature += node.children[1].to_s
  end
  signature
end

#infer(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/solargraph/node_methods.rb', line 63

def infer node
  if node.type == :str
    return 'String'
  elsif node.type == :array
    return 'Array'
  elsif node.type == :hash
    return 'Hash'
  elsif node.type == :int
    return 'Integer'
  elsif node.type == :float
    return 'Float'
  end
  nil
end

#pack_name(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/solargraph/node_methods.rb', line 7

def pack_name(node)
  parts = []
  if node.kind_of?(AST::Node)
    node.children.each { |n|
      if n.kind_of?(AST::Node)
        if n.type == :cbase
          parts = pack_name(n)
        else
          parts += pack_name(n)
        end
      else
        parts.push n unless n.nil?
      end
    }
  end
  parts
end

#resolve_node_signature(node) ⇒ String

Get a call signature from a node. The result should be a string in the form of a method path, e.g., String.new or variable.method.

Returns:

  • (String)


83
84
85
86
# File 'lib/solargraph/node_methods.rb', line 83

def resolve_node_signature node
  #stack_node_signature(node).join('.')
  drill_signature node, ''
end

#stack_node_signature(node) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/solargraph/node_methods.rb', line 88

def stack_node_signature node
  parts = []
  if node.kind_of?(AST::Node)
    if node.type == :send
      unless node.children[0].nil?
        parts = [unpack_name(node.children[0])] + parts
      end
      parts += stack_node_signature(node.children[1])
    else
      parts = [unpack_name(node)] + stack_node_signature(node.children[1])
    end
  else
    parts.push node.to_s
  end
  parts
end

#unpack_name(node) ⇒ Object



3
4
5
# File 'lib/solargraph/node_methods.rb', line 3

def unpack_name(node)
  pack_name(node).join("::")
end

#yard_optionsObject



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

def yard_options
  if @yard_options.nil?
    @yard_options = {
      include: [],
      exclude: [],
      flags: []
    }
    unless workspace.nil?
      yardopts_file = File.join(workspace, '.yardopts')
      if File.exist?(yardopts_file)
        yardopts = File.read(yardopts_file)
        yardopts.lines.each { |line|
          arg = line.strip
          if arg.start_with?('-')
            @yard_options[:flags].push arg
          else
            @yard_options[:include].push arg
          end
        }
      end
    end
    @yard_options[:include].concat ['app/**/*.rb', 'lib/**/*.rb'] if @yard_options[:include].empty?
  end
  @yard_options
end