Class: Solargraph::Workspace
- Inherits:
-
Object
- Object
- Solargraph::Workspace
- Defined in:
- lib/solargraph/workspace.rb,
lib/solargraph/workspace/config.rb,
lib/solargraph/workspace/gemspecs.rb,
lib/solargraph/workspace/require_paths.rb
Overview
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.
Defined Under Namespace
Classes: Config, Gemspecs, RequirePaths
Instance Attribute Summary collapse
- #directory ⇒ String readonly
- #gemnames ⇒ Array<String> (also: #source_gems) readonly
Instance Method Summary collapse
-
#command_path ⇒ String
@sg-ignore return type could not be inferred.
- #config ⇒ Solargraph::Workspace::Config
- #directory_or_nil ⇒ String?
- #filenames ⇒ Array<String>
- #find_gem(name, version = nil, out: nil) ⇒ Gem::Specification?
-
#gemfile? ⇒ Boolean
True if the workspace has a root Gemfile.
-
#gemspec? ⇒ Boolean
True if the workspace contains at least one gemspec file.
-
#gemspec_files ⇒ Array<String>
Get an array of all gemspec files in the workspace.
- #has_file?(filename) ⇒ Boolean
-
#initialize(directory = '', config = nil, server = {}) ⇒ Workspace
constructor
A new instance of Workspace.
-
#merge(*sources) ⇒ Boolean
Merge the source.
- #rbs_collection_config_path ⇒ String?
- #rbs_collection_path ⇒ String?
-
#remove(filename) ⇒ Boolean
Remove a source from the workspace.
-
#require_paths ⇒ Array<String>
The require paths associated with the workspace.
- #rules(level) ⇒ TypeChecker::Rules
-
#source(filename) ⇒ Solargraph::Source
Get a source by its filename.
- #sources ⇒ Array<Solargraph::Source>
-
#synchronize!(updater) ⇒ void
Synchronize the workspace from the provided updater.
-
#would_require?(path) ⇒ Boolean
True if the path resolves to a file in the workspace’s require paths.
Constructor Details
#initialize(directory = '', config = nil, server = {}) ⇒ Workspace
Returns a new instance of Workspace.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/solargraph/workspace.rb', line 26 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 |
Instance Attribute Details
#directory ⇒ String (readonly)
17 18 19 |
# File 'lib/solargraph/workspace.rb', line 17 def directory @directory end |
#gemnames ⇒ Array<String> (readonly) Also known as: source_gems
20 21 22 |
# File 'lib/solargraph/workspace.rb', line 20 def gemnames @gemnames end |
Instance Method Details
#command_path ⇒ String
@sg-ignore return type could not be inferred
163 164 165 |
# File 'lib/solargraph/workspace.rb', line 163 def command_path server['commandPath'] || 'solargraph' end |
#config ⇒ Solargraph::Workspace::Config
50 51 52 |
# File 'lib/solargraph/workspace.rb', line 50 def config @config ||= Solargraph::Workspace::Config.new(directory) end |
#directory_or_nil ⇒ String?
168 169 170 171 |
# File 'lib/solargraph/workspace.rb', line 168 def directory_or_nil return nil if directory.empty? || directory == '*' directory end |
#filenames ⇒ Array<String>
96 97 98 |
# File 'lib/solargraph/workspace.rb', line 96 def filenames source_hash.keys end |
#find_gem(name, version = nil, out: nil) ⇒ Gem::Specification?
149 150 151 |
# File 'lib/solargraph/workspace.rb', line 149 def find_gem name, version = nil, out: nil Gem::Specification.find_by_name(name, version) end |
#gemfile? ⇒ Boolean
Handle projects with custom Bundler/Gemfile setups (see DocMap#gemspecs_required_from_bundler)
True if the workspace has a root Gemfile.
177 178 179 |
# File 'lib/solargraph/workspace.rb', line 177 def gemfile? directory && File.file?(File.join(directory, 'Gemfile')) end |
#gemspec? ⇒ Boolean
True if the workspace contains at least one gemspec file.
184 185 186 |
# File 'lib/solargraph/workspace.rb', line 184 def gemspec? !gemspec_files.empty? end |
#gemspec_files ⇒ Array<String>
Get an array of all gemspec files in the workspace.
191 192 193 194 195 196 |
# File 'lib/solargraph/workspace.rb', line 191 def gemspec_files return [] if directory.empty? || directory == '*' @gemspec_files ||= Dir[File.join(directory, '**/*.gemspec')].select do |gs| config.allow? gs end end |
#has_file?(filename) ⇒ Boolean
107 108 109 |
# File 'lib/solargraph/workspace.rb', line 107 def has_file? filename source_hash.key?(filename) end |
#merge(*sources) ⇒ Boolean
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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/solargraph/workspace.rb', line 66 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| next unless directory == '*' || config.calculated.include?(source.filename) # @sg-ignore Wrong argument type for Hash#[]=: arg0 expected String, received String, nil source_hash[source.filename] = source includes_any = true end includes_any end |
#rbs_collection_config_path ⇒ String?
137 138 139 140 141 142 |
# File 'lib/solargraph/workspace.rb', line 137 def rbs_collection_config_path @rbs_collection_config_path ||= unless directory.empty? || directory == '*' yaml_file = File.join(directory, 'rbs_collection.yaml') yaml_file if File.file?(yaml_file) end end |
#rbs_collection_path ⇒ String?
132 133 134 |
# File 'lib/solargraph/workspace.rb', line 132 def rbs_collection_path @rbs_collection_path ||= read_rbs_collection_path end |
#remove(filename) ⇒ Boolean
Remove a source from the workspace. The source will not be removed if its file exists and the workspace is configured to include it.
89 90 91 92 93 |
# File 'lib/solargraph/workspace.rb', line 89 def remove filename return false unless source_hash.key?(filename) source_hash.delete filename true end |
#require_paths ⇒ Array<String>
The require paths associated with the workspace.
44 45 46 47 |
# File 'lib/solargraph/workspace.rb', line 44 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 |
#rules(level) ⇒ TypeChecker::Rules
56 57 58 |
# File 'lib/solargraph/workspace.rb', line 56 def rules level @rules ||= TypeChecker::Rules.new(level, config.type_checker_rules) end |
#source(filename) ⇒ Solargraph::Source
Get a source by its filename.
115 116 117 |
# File 'lib/solargraph/workspace.rb', line 115 def source filename source_hash[filename] end |
#sources ⇒ Array<Solargraph::Source>
101 102 103 |
# File 'lib/solargraph/workspace.rb', line 101 def sources source_hash.values end |
#synchronize!(updater) ⇒ void
This method returns an undefined value.
Synchronize the workspace from the provided updater.
157 158 159 |
# File 'lib/solargraph/workspace.rb', line 157 def synchronize! updater source_hash[updater.filename] = source_hash[updater.filename].synchronize(updater) end |
#would_require?(path) ⇒ Boolean
True if the path resolves to a file in the workspace’s require paths.
123 124 125 126 127 128 129 |
# File 'lib/solargraph/workspace.rb', line 123 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 |