Class: Solargraph::LiveMap

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/live_map.rb,
lib/solargraph/live_map/cache.rb

Overview

The LiveMap allows extensions to add their own completion suggestions.

Defined Under Namespace

Classes: Cache

Constant Summary collapse

@@plugins =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_map) ⇒ LiveMap

Returns a new instance of LiveMap.



12
13
14
15
# File 'lib/solargraph/live_map.rb', line 12

def initialize api_map
  @api_map = api_map
  runners
end

Instance Attribute Details

#api_mapSolargraph::ApiMap (readonly)

Returns:



10
11
12
# File 'lib/solargraph/live_map.rb', line 10

def api_map
  @api_map
end

Class Method Details

.install(cls) ⇒ Object

Register a plugin for LiveMap to use when generating suggestions.

Parameters:



73
74
75
# File 'lib/solargraph/live_map.rb', line 73

def self.install cls
  @@plugins.push cls unless @@plugins.include?(cls)
end

.pluginsObject



81
82
83
# File 'lib/solargraph/live_map.rb', line 81

def self.plugins
  @@plugins.clone
end

.uninstall(cls) ⇒ Object



77
78
79
# File 'lib/solargraph/live_map.rb', line 77

def self.uninstall cls
  @@plugins.delete cls
end

Instance Method Details

#get_constants(namespace, root = '') ⇒ Array<Solargraph::Suggestion>

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/live_map.rb', line 35

def get_constants(namespace, root = '')
  cached = cache.get_constants(namespace, root)
  return cached unless cached.nil?
  did_runtime = false
  result = []
  runners.each do |p|
    next if did_runtime and p.runtime?
    result.concat p.get_constants(namespace, root)
    did_runtime = true if p.runtime?
  end
  suggestions = []
  result.uniq.each do |r|
    kind = Suggestion::CONSTANT
    if r['class'] == 'Class'
      kind = Suggestion::CLASS
    elsif r['class'] == 'Module'
      kind = Suggestion::MODULE
    end
    suggestions.push(Suggestion.new(r['name'], kind: kind))
  end
  cache.set_constants(namespace, root, suggestions)
  suggestions
end

#get_fqns(namespace, root) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/solargraph/live_map.rb', line 59

def get_fqns(namespace, root)
  did_runtime = false
  runners.each do |p|
    next if did_runtime and p.runtime?
    result = p.get_fqns(namespace, root)
    return result unless result.nil?
    did_runtime = true if p.runtime?
  end
  nil
end

#get_methods(namespace, root = '', scope = 'instance', with_private = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/solargraph/live_map.rb', line 17

def get_methods(namespace, root = '', scope = 'instance', with_private = false)
  params = {
    namespace: namespace, root: root, scope: scope, with_private: with_private
  }
  cached = cache.get_methods(params)
  return cached unless cached.nil?
  did_runtime = false
  result = []
  runners.each do |p|
    next if did_runtime and p.runtime?
    result.concat p.get_methods(namespace: namespace, root: root, scope: scope, with_private: with_private)
    did_runtime = true if p.runtime?
  end
  cache.set_methods(params, result)
  result
end

#refreshObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/solargraph/live_map.rb', line 85

def refresh
  changed = false
  runners.each do |p|
    changed ||= p.refresh
  end
  if changed
    STDERR.puts "Resetting LiveMap cache"
    cache.clear
    get_constants('')
    get_methods('', '', 'class')
    get_methods('', '', 'instance')
    get_methods('Kernel', '', 'class')
    get_methods('Kernel', '', 'instance')
  end
end