Class: Solargraph::ApiMap

Inherits:
Object
  • Object
show all
Includes:
NodeMethods
Defined in:
lib/solargraph/api_map.rb

Constant Summary collapse

KEYWORDS =
[
  '__ENCODING__', '__LINE__', '__FILE__', 'BEGIN', 'END', 'alias', 'and',
  'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', 'else',
  'elsif', 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next',
  'nil', 'not', 'or', 'redo', 'rescue', 'retry', 'return', 'self', 'super',
  'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield'
]
MAPPABLE_METHODS =
[
  :include, :require, :autoload, :attr_reader, :attr_writer, :attr_accessor, :private, :public, :protected
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeMethods

#infer, #pack_name, #unpack_name

Constructor Details

#initialize(workspace = nil) ⇒ ApiMap

Returns a new instance of ApiMap.



22
23
24
25
26
# File 'lib/solargraph/api_map.rb', line 22

def initialize workspace = nil
  @workspace = workspace
  #process_workspace
  clear
end

Instance Attribute Details

#workspace ⇒ Object (readonly)

Returns the value of attribute workspace.



20
21
22
# File 'lib/solargraph/api_map.rb', line 20

def workspace
  @workspace
end

Class Method Details

.current ⇒ Object



402
403
404
405
406
407
408
# File 'lib/solargraph/api_map.rb', line 402

def self.current
  if @current.nil?
    @current = ApiMap.new
    @current.merge(Parser::CurrentRuby.parse(File.read("#{Solargraph::STUB_PATH}/ruby/2.3.0/core.rb")))
  end
  @current
end

.get_keywords(without_snippets: false) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/solargraph/api_map.rb', line 87

def self.get_keywords without_snippets: false
  result = []
  keywords = KEYWORDS
  keywords -= Snippets.keywords if without_snippets
  keywords.each { |k|
    result.push Suggestion.new(k, kind: Suggestion::KEYWORD, detail: 'Keyword')
  }
  result
end

Instance Method Details

#append_file(filename) ⇒ Object

def process_workspace clear return if @workspace.nil? process_files process_requires process_maps end



46
47
48
# File 'lib/solargraph/api_map.rb', line 46

def append_file filename
  append_source File.read(filename), filename
end

#append_node(node, comments, filename = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/solargraph/api_map.rb', line 55

def append_node node, comments, filename = nil
  mapified = mapify(node)
  root = AST::Node.new(:begin, [filename])
  mapified.children.each { |c|
    root = root.append c
  }
  @file_nodes[filename] = root
  @file_comments[filename] = associate_comments(mapified, comments)
  @required.uniq!
  process_maps
end

#append_source(text, filename = nil) ⇒ Object



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

def append_source text, filename = nil
  node, comments = Parser::CurrentRuby.parse_with_comments(text)
  append_node(node, comments, filename)
end

#associate_comments(node, comments) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/solargraph/api_map.rb', line 67

def associate_comments node, comments
  comment_hash = Parser::Source::Comment.associate(node, comments)
  yard_hash = {}
  comment_hash.each_pair { |k, v|
    #ctxt = v.map(&:text).join("\r\n")
    ctxt = ''
    v.each { |l|
      ctxt += l.text.gsub(/^#/, '') + "\n"
    }
    parser = YARD::DocstringParser.new
    yard_hash[k] = parser.parse(ctxt).to_docstring
  }
  yard_hash
end

#clear ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/solargraph/api_map.rb', line 28

def clear
  @file_nodes = {}
  @file_comments = {}
  @parent_stack = {}
  @namespace_map = {}
  @namespace_tree = {}
  #@pending_requires = []
  @required = []
end

#find_fully_qualified_namespace(name, root = '', skip = []) ⇒ Object



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
184
185
186
187
188
189
190
# File 'lib/solargraph/api_map.rb', line 158

def find_fully_qualified_namespace name, root = '', skip = []
  return nil if skip.include?(root)
  skip.push root
  if name == ''
    if root == ''
      return ''
    else
      return find_fully_qualified_namespace(root, '', skip)
    end
  else
    if (root == '')
      return name unless @namespace_map[name].nil?
      get_include_strings_from(*@file_nodes.values).each { |i|
        reroot = "#{root == '' ? '' : root + '::'}#{i}"
        recname = find_fully_qualified_namespace name, reroot, skip
        return recname unless recname.nil?
      }
    else
      roots = root.to_s.split('::')
      while roots.length > 0
        fqns = roots.join('::') + '::' + name
        return fqns unless @namespace_map[fqns].nil?
        roots.pop
      end
      return name unless @namespace_map[name].nil?
      get_include_strings_from(*@file_nodes.values).each { |i|
        recname = find_fully_qualified_namespace name, i, skip
        return recname unless recname.nil?
      }
    end
  end
  nil
end

#find_instance_variable_assignment(var, node, scope) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/solargraph/api_map.rb', line 251

def find_instance_variable_assignment(var, node, scope)
  node.children.each { |c|
    if c.kind_of?(AST::Node)
      is_inst = !find_parent(c, :def).nil?
      if c.type == :ivasgn and ( (scope == :instance and is_inst) or (scope != :instance and !is_inst) )
        if c.children[0].to_s == var
          return c
        end
      else
        inner = find_instance_variable_assignment(var, c, scope)
        return inner unless inner.nil?
      end
    end
  }
  nil
end

#find_parent(node, *types) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/solargraph/api_map.rb', line 206

def find_parent(node, *types)
  parents = @parent_stack[node]
  parents.each { |p|
    return p if types.include?(p.type)
  }
  nil
end

#get_comment_for(node) ⇒ Object



82
83
84
85
# File 'lib/solargraph/api_map.rb', line 82

def get_comment_for node
  filename = get_filename_for(node)
  @file_comments[filename][node] unless @file_comments[filename].nil?
end

#get_filename_for(node) ⇒ Object



218
219
220
221
# File 'lib/solargraph/api_map.rb', line 218

def get_filename_for(node)
  root = get_root_for(node)
  root.children[0]
end

#get_global_variables ⇒ Object



268
269
270
271
# File 'lib/solargraph/api_map.rb', line 268

def get_global_variables
  # TODO I bet these aren't getting mapped at all. Damn.
  []
end

#get_include_strings_from(*nodes) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
# File 'lib/solargraph/api_map.rb', line 410

def get_include_strings_from *nodes
  arr = []
  nodes.each { |node|
    next unless node.kind_of?(AST::Node)
    arr.push unpack_name(node.children[2]) if (node.type == :send and node.children[1] == :include)
    node.children.each { |n|
      arr += get_include_strings_from(n) if n.kind_of?(AST::Node) and n.type != :class and n.type != :module
    }
  }
  arr
end

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



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/solargraph/api_map.rb', line 325

def get_instance_methods(namespace, root = '')
  meths = inner_get_instance_methods(namespace, root, [])
  yard = YardMap.new(required: @required, workspace: @workspace)
  type = get_namespace_type(namespace, root)
  if type == :class
    meths += yard.get_instance_methods('Object')
  elsif type == :module
    meths += yard.get_instance_methods('Module')
  end
  meths += yard.get_instance_methods(namespace, root)
  sc = get_superclass(namespace, root)
  until sc.nil?
    meths += yard.get_instance_methods(sc, root)
    sc = get_superclass(sc)
  end
  meths
end

#get_instance_variables(namespace, scope = :instance) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/solargraph/api_map.rb', line 197

def get_instance_variables(namespace, scope = :instance)
  nodes = get_namespace_nodes(namespace) || @file_nodes.values
  arr = []
  nodes.each { |n|
    arr += inner_get_instance_variables(n, scope)
  }
  arr
end

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



283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/solargraph/api_map.rb', line 283

def get_methods(namespace, root = '')
  meths = inner_get_methods(namespace, root, [])
  yard = YardMap.new(required: @required, workspace: @workspace)
  meths += yard.get_methods(namespace, root)
  type = get_namespace_type(namespace, root)
  if type == :class
    meths += yard.get_instance_methods('Class')
  elsif type == :module
    meths += yard.get_methods('Module')
  end
  meths
end

#get_namespace_nodes(fqns) ⇒ Object



192
193
194
195
# File 'lib/solargraph/api_map.rb', line 192

def get_namespace_nodes(fqns)
  return @file_nodes.values if fqns == ''
  @namespace_map[fqns] || []
end

#get_namespace_type(namespace, root = '') ⇒ Object



273
274
275
276
277
278
279
280
281
# File 'lib/solargraph/api_map.rb', line 273

def get_namespace_type namespace, root = ''
  type = nil
  fqns = find_fully_qualified_namespace(namespace, root)
  nodes = get_namespace_nodes(fqns)
  unless nodes.nil? or nodes.empty? or !nodes[0].kind_of?(AST::Node)
    type = nodes[0].type if [:class, :module].include?(nodes[0].type)
  end
  type
end

#get_root_for(node) ⇒ Object



214
215
216
# File 'lib/solargraph/api_map.rb', line 214

def get_root_for(node)
  @parent_stack[node].last unless @parent_stack[node].nil?
end

#get_superclass(namespace, root = '') ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/solargraph/api_map.rb', line 343

def get_superclass(namespace, root = '')
  fqns = find_fully_qualified_namespace(namespace, root)
  nodes = get_namespace_nodes(fqns)
  nodes.each { |n|
    if n.kind_of?(AST::Node)
      if n.type == :class and !n.children[1].nil?
        return unpack_name(n.children[1])
      end
    end
  }
  return nil
end

#infer_instance_variable(var, namespace, scope = :instance) ⇒ Object



240
241
242
243
244
245
246
247
248
249
# File 'lib/solargraph/api_map.rb', line 240

def infer_instance_variable(var, namespace, scope = :instance)
  vn = nil
  if namespace_exists?(namespace)
    get_namespace_nodes(namespace).each { |node|
      vn = find_instance_variable_assignment(var, node, scope)
      break unless vn.nil?
    }
  end
  infer(vn.children[1]) unless vn.nil?
end

#inner_get_instance_methods(namespace, root, skip) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/solargraph/api_map.rb', line 356

def inner_get_instance_methods(namespace, root, skip)
  fqns = find_fully_qualified_namespace(namespace, root)
  meths = []
  return meths if skip.include?(fqns)
  skip.push fqns
  nodes = get_namespace_nodes(fqns)
  nodes.each { |n|
    if n.kind_of?(AST::Node)
      if n.type == :class and !n.children[1].nil?
        s = unpack_name(n.children[1])
        meths += inner_get_instance_methods(s, namespace, skip)
      end
      current_scope = :public
      n.children.each { |c|
        if c.kind_of?(AST::Node) and c.type == :send and [:public, :protected, :private].include?(c.children[1])
        # TODO: Determine the current scope so we can decide whether to
        # exclude protected or private methods. Right now we're just
        # assuming public only
        elsif current_scope == :public
          if c.kind_of?(AST::Node) and c.type == :def
            cmnt = get_comment_for(c)
            meths.push Suggestion.new(c.children[0], kind: Suggestion::METHOD, documentation: cmnt) if c.children[0].to_s[0].match(/[a-z]/i)
          elsif c.kind_of?(AST::Node) and c.type == :send and c.children[1] == :attr_reader
            c.children[2..-1].each { |x|
              meths.push Suggestion.new(x.children[0], kind: Suggestion::METHOD) if x.type == :sym
            }
          elsif c.kind_of?(AST::Node) and c.type == :send and c.children[1] == :attr_writer
            c.children[2..-1].each { |x|
              meths.push Suggestion.new("#{x.children[0]}=", kind: Suggestion::METHOD) if x.type == :sym
            }
          elsif c.kind_of?(AST::Node) and c.type == :send and c.children[1] == :attr_accessor
            c.children[2..-1].each { |x|
              meths.push Suggestion.new(x.children[0], kind: Suggestion::METHOD) if x.type == :sym
              meths.push Suggestion.new("#{x.children[0]}=", kind: Suggestion::METHOD) if x.type == :sym
            }
          end
        end
        get_include_strings_from(n).each { |i|
          meths += inner_get_instance_methods(i, fqns, skip)
        }
      }
    end
  }
  meths.uniq
end

#inner_get_instance_variables(node, scope) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/solargraph/api_map.rb', line 223

def inner_get_instance_variables(node, scope)
  arr = []
  if node.kind_of?(AST::Node)
    node.children.each { |c|
      if c.kind_of?(AST::Node)
        #next if [:class, :module].include?(c.type)
        is_inst = !find_parent(c, :def).nil?
        if c.type == :ivasgn and ( (scope == :instance and is_inst) or (scope != :instance and !is_inst) )
          arr.push Suggestion.new(c.children[0], kind: Suggestion::VARIABLE)
        end
        arr += inner_get_instance_variables(c, scope) unless [:class, :module].include?(c.type)
      end
    }
  end
  arr
end

#inner_get_methods(namespace, root = '', skip = []) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/solargraph/api_map.rb', line 296

def inner_get_methods(namespace, root = '', skip = [])
  meths = []
  return meths if skip.include?(namespace)
  skip.push namespace
  fqns = find_fully_qualified_namespace(namespace, root)
  return meths if fqns.nil?
  nodes = get_namespace_nodes(fqns)
  nodes.each { |n|
    if n.kind_of?(AST::Node)
      if n.type == :class and !n.children[1].nil?
        s = unpack_name(n.children[1])
        meths += inner_get_methods(s, root, skip)
      end
      n.children.each { |c|
        if c.kind_of?(AST::Node) and c.type == :defs
          docstring = get_comment_for(c)
          meths.push Suggestion.new(c.children[1], kind: Suggestion::METHOD, documentation: docstring) if c.children[1].to_s[0].match(/[a-z_]/i) and c.children[1] != :def
        elsif c.kind_of?(AST::Node) and c.type == :send and c.children[1] == :include
          # TODO This might not be right. Should we be getting singleton methods
          # from an include, or only from an extend?
          i = unpack_name(c.children[2])
          meths += inner_get_methods(i, root, skip) unless i == 'Kernel'
        end
      }
    end
  }
  meths.uniq
end

#inner_namespaces_in(name, root, skip) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/solargraph/api_map.rb', line 130

def inner_namespaces_in name, root, skip
  result = []
  fqns = find_fully_qualified_namespace(name, root)
  if fqns.nil?
    return result
  else
    return result if skip.include?(fqns)
    skip.push fqns
    cursor = @namespace_tree
    parts = fqns.split('::')
    parts.each { |p|
      cursor = cursor[p]
    }
    unless cursor.nil?
      cursor.keys.each { |k|
        result.push Suggestion.new(k, kind: Suggestion::CLASS)
      }
      nodes = get_namespace_nodes(fqns)
      nodes.each { |n|
        get_include_strings_from(n).each { |i|
          result += inner_namespaces_in(i, fqns, skip)
        }
      }
    end
  end
  result
end

#namespace_exists?(name, root = '') ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/solargraph/api_map.rb', line 111

def namespace_exists? name, root = ''
  !find_fully_qualified_namespace(name, root).nil?
end

#namespaces ⇒ Object



107
108
109
# File 'lib/solargraph/api_map.rb', line 107

def namespaces
  @namespace_map.keys
end

#namespaces_in(name, root = '') ⇒ Object

, skip = []



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/solargraph/api_map.rb', line 115

def namespaces_in name, root = '' #, skip = []
  result = []
  result += inner_namespaces_in(name, root, [])
  yard = YardMap.new(required: @required, workspace: @workspace)
  result += yard.get_constants name, root
  fqns = find_fully_qualified_namespace(name, root)
  unless fqns.nil?
    nodes = get_namespace_nodes(fqns)
    get_include_strings_from(*nodes).each { |i|
      result += yard.get_constants(i, root)
    }
  end
  result
end

#process_maps ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/solargraph/api_map.rb', line 97

def process_maps
  @parent_stack = {}
  @namespace_map = {}
  @namespace_tree = {}
  @file_nodes.values.each { |f|
    map_parents f #AST::Node.new(:tmp, @file_nodes.values)
    map_namespaces f #AST::Node.new(:tmp, @file_nodes.values)
  }
end