Class: Solargraph::RbsMap

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/solargraph/rbs_map.rb,
lib/solargraph/rbs_map/core_map.rb,
lib/solargraph/rbs_map/core_fills.rb,
lib/solargraph/rbs_map/stdlib_map.rb,
lib/solargraph/rbs_map/conversions.rb

Direct Known Subclasses

StdlibMap

Defined Under Namespace

Modules: CoreFills Classes: Conversions, CoreMap, StdlibMap

Constant Summary collapse

@@rbs_maps_hash =
{}

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(library, version = nil, rbs_collection_config_path: nil, rbs_collection_paths: []) ⇒ RbsMap

Returns a new instance of RbsMap.

Parameters:

  • library (String)
  • version (String, nil) (defaults to: nil)

    ersion [String, nil

  • rbs_collection_config_path (String, Pathname, nil) (defaults to: nil)
  • rbs_collection_paths (Array<Pathname, String>) (defaults to: [])


29
30
31
32
33
34
35
36
37
38
# File 'lib/solargraph/rbs_map.rb', line 29

def initialize library, version = nil, rbs_collection_config_path: nil, rbs_collection_paths: []
  if rbs_collection_config_path.nil? && !rbs_collection_paths.empty?
    raise 'Please provide rbs_collection_config_path if you provide rbs_collection_paths'
  end
  @library = library
  @version = version
  @rbs_collection_config_path = rbs_collection_config_path
  @rbs_collection_paths = rbs_collection_paths
  add_library loader, library, version
end

Instance Attribute Details

#libraryObject (readonly)

Returns the value of attribute library.



19
20
21
# File 'lib/solargraph/rbs_map.rb', line 19

def library
  @library
end

#rbs_collection_config_pathObject (readonly)

Returns the value of attribute rbs_collection_config_path.



23
24
25
# File 'lib/solargraph/rbs_map.rb', line 23

def rbs_collection_config_path
  @rbs_collection_config_path
end

#rbs_collection_pathsObject (readonly)

Returns the value of attribute rbs_collection_paths.



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

def rbs_collection_paths
  @rbs_collection_paths
end

Class Method Details

.from_gemspec(gemspec, rbs_collection_path, rbs_collection_config_path) ⇒ RbsMap

Parameters:

  • gemspec (Gem::Specification)
  • rbs_collection_path (String, Pathname, nil)
  • rbs_collection_config_path (String, Pathname, nil)

Returns:



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/solargraph/rbs_map.rb', line 79

def self.from_gemspec gemspec, rbs_collection_path, rbs_collection_config_path
  rbs_map = RbsMap.new(gemspec.name, gemspec.version,
                       rbs_collection_paths: [rbs_collection_path].compact,
                       rbs_collection_config_path: rbs_collection_config_path)
  return rbs_map if rbs_map.resolved?

  # try any version of the gem in the collection
  RbsMap.new(gemspec.name, nil,
             rbs_collection_paths: [rbs_collection_path].compact,
             rbs_collection_config_path: rbs_collection_config_path)
end

.load(library) ⇒ RbsMap

Parameters:

  • library (String)

Returns:



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

def self.load library
  @@rbs_maps_hash[library] ||= RbsMap.new(library)
end

Instance Method Details

#cache_keyString

Returns representing the version of the RBS info fetched for the given library. Must change when the RBS info is updated upstream for the same library and version. May change if the config for where information comes form changes.

Returns:

  • (String)

    representing the version of the RBS info fetched for the given library. Must change when the RBS info is updated upstream for the same library and version. May change if the config for where information comes form changes.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/solargraph/rbs_map.rb', line 49

def cache_key
  @hextdigest ||= begin
    # @type [String, nil]
    data = nil
    if rbs_collection_config_path
      lockfile_path = RBS::Collection::Config.to_lockfile_path(Pathname.new(rbs_collection_config_path))
      if lockfile_path.exist?
        collection_config = RBS::Collection::Config.from_path lockfile_path
        gem_config = collection_config.gem(library)
        data = gem_config&.to_s
      end
    end
    if data.nil? || data.empty?
      if resolved?
        # definitely came from the gem itself and not elsewhere -
        # only one version per gem
        'gem-export'
      else
        'unresolved'
      end
    else
      Digest::SHA1.hexdigest(data)
    end
  end
end

#path_pin(path, klass = Pin::Base) ⇒ generic<T>?

Parameters:

  • path (String)
  • klass (Class<generic<T>>) (defaults to: Pin::Base)

Returns:

  • (generic<T>, nil)


100
101
102
103
# File 'lib/solargraph/rbs_map.rb', line 100

def path_pin path, klass = Pin::Base
  pin = pins.find { |p| p.path == path }
  pin if pin&.is_a?(klass)
end

#path_pins(path) ⇒ Array<Pin::Base>

Parameters:

  • path (String)

Returns:



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

def path_pins path
  pins.select { |p| p.path == path }
end

#pinsArray<Pin::Base>

Returns:



92
93
94
# File 'lib/solargraph/rbs_map.rb', line 92

def pins
  @pins ||= resolved? ? conversions.pins : []
end

#repositoryRBS::Repository

Returns:

  • (RBS::Repository)


116
117
118
119
120
121
122
123
# File 'lib/solargraph/rbs_map.rb', line 116

def repository
  @repository ||= RBS::Repository.new(no_stdlib: false).tap do |repo|
    @rbs_collection_paths.each do |dir|
      dir_path = Pathname.new(dir)
      repo.add(dir_path) if dir_path.exist? && dir_path.directory?
    end
  end
end

#resolved?Boolean

Returns:

  • (Boolean)


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

def resolved?
  @resolved
end