class Solargraph::Workspace
A workspace consists of the files in a project’s directory and the project’s configuration. It provides a Source for each file to be used in an associated Library or ApiMap.
A workspace consists of the files in a project’s directory and the project’s configuration. It provides a Source for each file to be used in an associated Library or ApiMap.
Attributes
@return [String]
@return [Array<String>]
The language server configuration (or an empty hash if the workspace was not initialized from a server).
@return [Hash]
@return [Array<String>]
Public Class Methods
Source
# File lib/solargraph/workspace.rb, line 25 def initialize directory = '', config = nil, server = {} raise ArgumentError, 'directory must be a String' unless directory.is_a?(String) @directory = if ['*', ''].include?(directory) directory else File.absolute_path(directory) end @config = config @server = server load_sources @gemnames = [] require_plugins end
@param directory [String] TODO: Remove ” and ‘*’ special cases @param config [Config, nil] @param server [Hash]
Public Instance Methods
Source
# File lib/solargraph/workspace.rb, line 153 def command_path server['commandPath'] || 'solargraph' end
@return [String]
Source
# File lib/solargraph/workspace.rb, line 49 def config @config ||= Solargraph::Workspace::Config.new(directory) end
@return [Solargraph::Workspace::Config]
Source
# File lib/solargraph/workspace.rb, line 158 def directory_or_nil return nil if directory.empty? || directory == '*' directory end
@return [String, nil]
Source
# File lib/solargraph/workspace.rb, line 94 def filenames source_hash.keys end
@return [Array<String>]
Source
# File lib/solargraph/workspace.rb, line 167 def gemfile? directory && File.file?(File.join(directory, 'Gemfile')) end
True if the workspace has a root Gemfile.
@todo Handle projects with custom Bundler/Gemfile setups (see DocMap#gemspecs_required_from_bundler)
Source
# File lib/solargraph/workspace.rb, line 105 def has_file? filename source_hash.key?(filename) end
@param filename [String] @return [Boolean]
Source
# File lib/solargraph/workspace.rb, line 65 def merge *sources unless directory == '*' || sources.all? { |source| source_hash.key?(source.filename) } # Reload the config to determine if a new source should be included @config = Solargraph::Workspace::Config.new(directory) end includes_any = false sources.each do |source| if directory == "*" || config.calculated.include?(source.filename) source_hash[source.filename] = source includes_any = true end end includes_any end
Merge the source. A merge will update the existing source for the file or add it to the sources if the workspace is configured to include it. The source is ignored if the configuration excludes it.
@param sources [Array<Solargraph::Source>] @return [Boolean] True if the source was added to the workspace
Source
# File lib/solargraph/workspace.rb, line 135 def rbs_collection_config_path @rbs_collection_config_path ||= begin unless directory.empty? || directory == '*' yaml_file = File.join(directory, 'rbs_collection.yaml') yaml_file if File.file?(yaml_file) end end end
@return [String, nil]
Source
# File lib/solargraph/workspace.rb, line 130 def rbs_collection_path @gem_rbs_collection ||= read_rbs_collection_path end
@return [String, nil]
Source
# File lib/solargraph/workspace.rb, line 87 def remove filename return false unless source_hash.key?(filename) source_hash.delete filename true end
Remove a source from the workspace. The source will not be removed if its file exists and the workspace is configured to include it.
@param filename [String] @return [Boolean] True if the source was removed from the workspace
Source
# File lib/solargraph/workspace.rb, line 43 def require_paths # @todo are the semantics of '*' the same as '', meaning 'don't send back any require paths'? @require_paths ||= RequirePaths.new(directory_or_nil, config).generate end
The require paths associated with the workspace.
@return [Array<String>]
Source
# File lib/solargraph/workspace.rb, line 55 def rules(level) @rules ||= TypeChecker::Rules.new(level, config.type_checker_rules) end
@param level [Symbol] @return [TypeChecker::Rules]
Source
# File lib/solargraph/workspace.rb, line 113 def source filename source_hash[filename] end
Get a source by its filename.
@param filename [String] @return [Solargraph::Source]
Source
# File lib/solargraph/workspace.rb, line 99 def sources source_hash.values end
@return [Array<Solargraph::Source>]
Source
# File lib/solargraph/workspace.rb, line 148 def synchronize! updater source_hash[updater.filename] = source_hash[updater.filename].synchronize(updater) end
Synchronize the workspace from the provided updater.
@param updater [Source::Updater] @return [void]
Source
# File lib/solargraph/workspace.rb, line 121 def would_require? path require_paths.each do |rp| full = File.join rp, path return true if File.file?(full) || File.file?(full << ".rb") end false end
True if the path resolves to a file in the workspace’s require paths.
@param path [String] @return [Boolean]
Private Instance Methods
Source
# File lib/solargraph/workspace.rb, line 185 def load_sources source_hash.clear unless directory.empty? || directory == '*' size = config.calculated.length raise WorkspaceTooLargeError, "The workspace is too large to index (#{size} files, #{config.max_files} max)" if config.max_files > 0 and size > config.max_files config.calculated.each do |filename| begin source_hash[filename] = Solargraph::Source.load(filename) rescue Errno::ENOENT => e Solargraph.logger.warn("Error loading #{filename}: [#{e.class}] #{e.message}") end end end end
@return [void]
Source
# File lib/solargraph/workspace.rb, line 212 def read_rbs_collection_path return unless rbs_collection_config_path path = YAML.load_file(rbs_collection_config_path)&.fetch('path') # make fully qualified File.expand_path(path, directory) end
@return [String, nil]
Source
# File lib/solargraph/workspace.rb, line 201 def require_plugins config.plugins.each do |plugin| begin require plugin rescue LoadError Solargraph.logger.warn "Failed to load plugin '#{plugin}'" end end end
@return [void]
Source
# File lib/solargraph/workspace.rb, line 180 def source_hash @source_hash ||= {} end
@return [Hash{String => Solargraph::Source}]