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

CACHE_KEY_GEM_EXPORT =
'gem-export'
CACHE_KEY_UNRESOLVED =
'unresolved'
CACHE_KEY_STDLIB =
'stdlib'
CACHE_KEY_LOCAL =
'local'
@@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

log_level, logger

Constructor Details

#initialize(library, version = nil, rbs_collection_config_path: nil, rbs_collection_paths: [], out: $stderr) ⇒ RbsMap

Returns a new instance of RbsMap.

Parameters:

  • library (String)
  • version (String, nil) (defaults to: nil)
  • rbs_collection_config_path (String, Pathname, nil) (defaults to: nil)
  • rbs_collection_paths (Array<Pathname, String>) (defaults to: [])
  • out (StringIO, IO, nil) (defaults to: $stderr)

    where to log messages



26
27
28
29
30
31
32
33
34
35
# File 'lib/solargraph/rbs_map.rb', line 26

def initialize library, version = nil, rbs_collection_config_path: nil, rbs_collection_paths: [], out: $stderr
  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.



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

def rbs_collection_config_path
  @rbs_collection_config_path
end

#rbs_collection_pathsObject (readonly)

Returns the value of attribute rbs_collection_paths.



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

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, Bundler::LazySpecification)
  • rbs_collection_path (String, Pathname, nil)
  • rbs_collection_config_path (String, Pathname, nil)

Returns:



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

def self.from_gemspec gemspec, rbs_collection_path, rbs_collection_config_path
  # prefers stdlib RBS if available
  rbs_map = RbsMap::StdlibMap.new(gemspec.name)
  return rbs_map if rbs_map.resolved?

  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:



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

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

.rbs_source_desc(cache_key) ⇒ String?

Returns a description of the source of the RBS info.

Parameters:

  • cache_key (String, nil)

Returns:

  • (String, nil)

    a description of the source of the RBS info



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/rbs_map.rb', line 44

def self.rbs_source_desc cache_key
  case cache_key
  when CACHE_KEY_GEM_EXPORT
    'RBS gem export'
  when CACHE_KEY_UNRESOLVED
    nil
  when CACHE_KEY_STDLIB
    'RBS standard library'
  when CACHE_KEY_LOCAL
    'local RBS shims'
  else
    'RBS collection'
  end
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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/solargraph/rbs_map.rb', line 68

def cache_key
  return CACHE_KEY_UNRESOLVED unless resolved?

  @cache_key ||= begin
    # @type [String, nil]
    data = nil
    # @type gem_config [nil, Hash{String => Hash{String => String}}]
    gem_config = nil
    if rbs_collection_config_path
      # @sg-ignore rbs_collection_config_path is not nil here
      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 gem_config.nil?
      CACHE_KEY_STDLIB
    else
      # @type [String]
      source = gem_config.dig('source', 'type')
      case source
      when 'rubygems'
        CACHE_KEY_GEM_EXPORT
      when 'local'
        CACHE_KEY_LOCAL
      when 'stdlib'
        CACHE_KEY_STDLIB
      else
        Digest::SHA1.hexdigest(data || '')
      end
    end
  end
end

#loaderRBS::EnvironmentLoader

Returns:

  • (RBS::EnvironmentLoader)


60
61
62
# File 'lib/solargraph/rbs_map.rb', line 60

def loader
  @loader ||= RBS::EnvironmentLoader.new(core_root: nil, repository: repository)
end

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

@sg-ignore Need to be able to resolve generics based on a

Class<generic<T>> param

Parameters:

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

Returns:

  • (generic<T>, nil)


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

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:



148
149
150
# File 'lib/solargraph/rbs_map.rb', line 148

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

#pins(out: $stderr) ⇒ Array<Pin::Base>

Parameters:

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

    where to log messages

Returns:



126
127
128
129
130
131
132
# File 'lib/solargraph/rbs_map.rb', line 126

def pins out: $stderr
  @pins ||= if resolved?
              conversions.pins
            else
              []
            end
end

#repositoryRBS::Repository

Returns:

  • (RBS::Repository)


157
158
159
160
161
162
163
164
# File 'lib/solargraph/rbs_map.rb', line 157

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)


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

def resolved?
  @resolved
end