Class: Solargraph::DocMap

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/solargraph/doc_map.rb

Overview

A collection of pins generated from required gems.

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

logger

Constructor Details

#initialize(requires, preferences, workspace = nil) ⇒ DocMap

Returns a new instance of DocMap.

Parameters:

  • requires (Array<String>)
  • preferences (Array<Gem::Specification>)
  • workspace (Workspace, nil) (defaults to: nil)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/solargraph/doc_map.rb', line 51

def initialize(requires, preferences, workspace = nil)
  @requires = requires.compact
  @preferences = preferences.compact
  @workspace = workspace
  @rbs_collection_path = workspace&.rbs_collection_path
  @rbs_collection_config_path = workspace&.rbs_collection_config_path
  @environ = Convention.for_global(self)
  @requires.concat @environ.requires if @environ
  load_serialized_gem_pins
  pins.concat @environ.pins
end

Instance Attribute Details

#environEnviron (readonly)

Returns:



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

def environ
  @environ
end

#pinsArray<Pin::Base> (readonly)

Returns:



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

def pins
  @pins
end

#preferencesArray<Gem::Specification> (readonly)

Returns:

  • (Array<Gem::Specification>)


18
19
20
# File 'lib/solargraph/doc_map.rb', line 18

def preferences
  @preferences
end

#rbs_collection_config_pathString? (readonly)

Returns:

  • (String, nil)


40
41
42
# File 'lib/solargraph/doc_map.rb', line 40

def rbs_collection_config_path
  @rbs_collection_config_path
end

#rbs_collection_pathString? (readonly)

Returns:

  • (String, nil)


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

def rbs_collection_path
  @rbs_collection_path
end

#requiresArray<String> (readonly) Also known as: required

Returns:

  • (Array<String>)


14
15
16
# File 'lib/solargraph/doc_map.rb', line 14

def requires
  @requires
end

#uncached_rbs_collection_gemspecsArray<Gem::Specification> (readonly)

Returns:

  • (Array<Gem::Specification>)


34
35
36
# File 'lib/solargraph/doc_map.rb', line 34

def uncached_rbs_collection_gemspecs
  @uncached_rbs_collection_gemspecs
end

#uncached_yard_gemspecsArray<Gem::Specification> (readonly)

Returns:

  • (Array<Gem::Specification>)


31
32
33
# File 'lib/solargraph/doc_map.rb', line 31

def uncached_yard_gemspecs
  @uncached_yard_gemspecs
end

#workspaceWorkspace? (readonly)

Returns:



43
44
45
# File 'lib/solargraph/doc_map.rb', line 43

def workspace
  @workspace
end

Class Method Details

.all_combined_pins_in_memoryHash{Array(String, String) => Array<Pin::Base>}

Returns Indexed by gemspec name and version.

Returns:

  • (Hash{Array(String, String) => Array<Pin::Base>})

    Indexed by gemspec name and version



152
153
154
# File 'lib/solargraph/doc_map.rb', line 152

def self.all_combined_pins_in_memory
  @combined_pins_in_memory ||= {}
end

.all_rbs_collection_gems_in_memoryHash{String => Hash{Array(String, String) => Array<Pin::Base>}}

Returns stored by RBS collection path.

Returns:

  • (Hash{String => Hash{Array(String, String) => Array<Pin::Base>}})

    stored by RBS collection path



137
138
139
# File 'lib/solargraph/doc_map.rb', line 137

def self.all_rbs_collection_gems_in_memory
  @rbs_collection_gems_in_memory ||= {}
end

.all_yard_gems_in_memoryHash{Array(String, String) => Array<Gem::Specification>}

Returns Indexed by gemspec name and version.

Returns:

  • (Hash{Array(String, String) => Array<Gem::Specification>})

    Indexed by gemspec name and version



132
133
134
# File 'lib/solargraph/doc_map.rb', line 132

def self.all_yard_gems_in_memory
  @yard_gems_in_memory ||= {}
end

Instance Method Details

#cache(gemspec, rebuild: false, out: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • gemspec (Gem::Specification)
  • rebuild (Boolean) (defaults to: false)

    whether to rebuild the pins even if they are cached

  • out (IO, nil) (defaults to: nil)

    output stream for logging



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/solargraph/doc_map.rb', line 108

def cache(gemspec, rebuild: false, out: nil)
  build_yard = uncached_yard_gemspecs.include?(gemspec) || rebuild
  build_rbs_collection = uncached_rbs_collection_gemspecs.include?(gemspec) || rebuild
  if build_yard || build_rbs_collection
    type = []
    type << 'YARD' if build_yard
    type << 'RBS collection' if build_rbs_collection
    out.puts("Caching #{type.join(' and ')} pins for gem #{gemspec.name}:#{gemspec.version}") if out
  end
  cache_yard_pins(gemspec, out) if build_yard
  cache_rbs_collection_pins(gemspec, out) if build_rbs_collection
end

#cache_all!(out) ⇒ void

This method returns an undefined value.

Parameters:

  • out (IO)


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

def cache_all!(out)
  # if we log at debug level:
  if logger.info?
    gem_desc = uncached_gemspecs.map { |gemspec| "#{gemspec.name}:#{gemspec.version}" }.join(', ')
    logger.info "Caching pins for gems: #{gem_desc}" unless uncached_gemspecs.empty?
  end
  logger.debug { "Caching for YARD: #{uncached_yard_gemspecs.map(&:name)}" }
  logger.debug { "Caching for RBS collection: #{uncached_rbs_collection_gemspecs.map(&:name)}" }
  load_serialized_gem_pins
  uncached_gemspecs.each do |gemspec|
    cache(gemspec, out: out)
  end
  load_serialized_gem_pins
  @uncached_rbs_collection_gemspecs = []
  @uncached_yard_gemspecs = []
end

#cache_rbs_collection_pins(gemspec, out) ⇒ void

This method returns an undefined value.

Parameters:

  • gemspec (Gem::Specification)
  • out (IO)


94
95
96
97
98
99
100
101
102
# File 'lib/solargraph/doc_map.rb', line 94

def cache_rbs_collection_pins(gemspec, out)
  rbs_map = RbsMap.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path)
  pins = rbs_map.pins
  rbs_version_cache_key = rbs_map.cache_key
  # cache pins even if result is zero, so we don't retry building pins
  pins ||= []
  PinCache.serialize_rbs_collection_gem(gemspec, rbs_version_cache_key, pins)
  logger.info { "Cached #{pins.length} RBS collection pins for gem #{gemspec.name} #{gemspec.version} with cache_key #{rbs_version_cache_key.inspect}" unless pins.empty? }
end

#cache_yard_pins(gemspec, out) ⇒ void

This method returns an undefined value.

Parameters:

  • gemspec (Gem::Specification)
  • out (IO)


85
86
87
88
89
# File 'lib/solargraph/doc_map.rb', line 85

def cache_yard_pins(gemspec, out)
  pins = GemPins.build_yard_pins(yard_plugins, gemspec)
  PinCache.serialize_yard_gem(gemspec, pins)
  logger.info { "Cached #{pins.length} YARD pins for gem #{gemspec.name}:#{gemspec.version}" } unless pins.empty?
end

#combined_pins_in_memoryHash{Array(String, String) => Array<Pin::Base>}

TODO:

this should also include an index by the hash of the RBS collection

Returns Indexed by gemspec name and version.

Returns:

  • (Hash{Array(String, String) => Array<Pin::Base>})

    Indexed by gemspec name and version



158
159
160
# File 'lib/solargraph/doc_map.rb', line 158

def combined_pins_in_memory
  self.class.all_combined_pins_in_memory
end

#dependenciesSet<Gem::Specification>

Returns:

  • (Set<Gem::Specification>)


168
169
170
# File 'lib/solargraph/doc_map.rb', line 168

def dependencies
  @dependencies ||= (gemspecs.flat_map { |spec| fetch_dependencies(spec) } - gemspecs).to_set
end

#gemspecsArray<Gem::Specification>

Returns:

  • (Array<Gem::Specification>)


122
123
124
# File 'lib/solargraph/doc_map.rb', line 122

def gemspecs
  @gemspecs ||= required_gems_map.values.compact.flatten
end

#rbs_collection_pins_in_memoryHash{Array(String, String) => Array<Pin::Base>}

Returns Indexed by gemspec name and version.

Returns:

  • (Hash{Array(String, String) => Array<Pin::Base>})

    Indexed by gemspec name and version



147
148
149
# File 'lib/solargraph/doc_map.rb', line 147

def rbs_collection_pins_in_memory
  self.class.all_rbs_collection_gems_in_memory[rbs_collection_path] ||= {}
end

#uncached_gemspecsArray<Gem::Specification>

Returns:

  • (Array<Gem::Specification>)


24
25
26
27
28
# File 'lib/solargraph/doc_map.rb', line 24

def uncached_gemspecs
  uncached_yard_gemspecs.concat(uncached_rbs_collection_gemspecs)
                        .sort
                        .uniq { |gemspec| "#{gemspec.name}:#{gemspec.version}" }
end

#unresolved_requiresArray<String>

Returns:

  • (Array<String>)


127
128
129
# File 'lib/solargraph/doc_map.rb', line 127

def unresolved_requires
  @unresolved_requires ||= required_gems_map.select { |_, gemspecs| gemspecs.nil? }.keys
end

#yard_pins_in_memoryHash{Array(String, String) => Array<Pin::Base>}

Returns Indexed by gemspec name and version.

Returns:

  • (Hash{Array(String, String) => Array<Pin::Base>})

    Indexed by gemspec name and version



142
143
144
# File 'lib/solargraph/doc_map.rb', line 142

def yard_pins_in_memory
  self.class.all_yard_gems_in_memory
end

#yard_pluginsArray<String>

Returns:

  • (Array<String>)


163
164
165
# File 'lib/solargraph/doc_map.rb', line 163

def yard_plugins
  @environ.yard_plugins
end