Class: Solargraph::Workspace
- Inherits:
-
Object
- Object
- Solargraph::Workspace
- Defined in:
- lib/solargraph/workspace.rb,
lib/solargraph/workspace/config.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, RequirePaths
Instance Attribute Summary collapse
- #directory ⇒ String readonly
- #gemnames ⇒ Array<String> (also: #source_gems) readonly
Instance Method Summary collapse
- #command_path ⇒ String
- #config ⇒ Solargraph::Workspace::Config
- #directory_or_nil ⇒ String?
- #filenames ⇒ Array<String>
-
#gemfile? ⇒ Boolean
True if the workspace has a root Gemfile.
-
#gemspec? ⇒ Boolean
True if the workspace contains at least one gemspec file.
-
#gemspecs ⇒ 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.
-
#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.
25 26 27 28 29 30 31 32 |
# File 'lib/solargraph/workspace.rb', line 25 def initialize directory = '', config = nil, server = {} @directory = directory @config = config @server = server load_sources @gemnames = [] require_plugins end |
Instance Attribute Details
#directory ⇒ String (readonly)
16 17 18 |
# File 'lib/solargraph/workspace.rb', line 16 def directory @directory end |
#gemnames ⇒ Array<String> (readonly) Also known as: source_gems
19 20 21 |
# File 'lib/solargraph/workspace.rb', line 19 def gemnames @gemnames end |
Instance Method Details
#command_path ⇒ String
158 159 160 |
# File 'lib/solargraph/workspace.rb', line 158 def command_path server['commandPath'] || 'solargraph' end |
#config ⇒ Solargraph::Workspace::Config
43 44 45 |
# File 'lib/solargraph/workspace.rb', line 43 def config @config ||= Solargraph::Workspace::Config.new(directory) end |
#directory_or_nil ⇒ String?
163 164 165 166 |
# File 'lib/solargraph/workspace.rb', line 163 def directory_or_nil return nil if directory.empty? || directory == '*' directory end |
#filenames ⇒ Array<String>
82 83 84 |
# File 'lib/solargraph/workspace.rb', line 82 def filenames source_hash.keys end |
#gemfile? ⇒ Boolean
Handle projects with custom Bundler/Gemfile setups (see DocMap#gemspecs_required_from_bundler)
True if the workspace has a root Gemfile.
172 173 174 |
# File 'lib/solargraph/workspace.rb', line 172 def gemfile? directory && File.file?(File.join(directory, 'Gemfile')) end |
#gemspec? ⇒ Boolean
True if the workspace contains at least one gemspec file.
120 121 122 |
# File 'lib/solargraph/workspace.rb', line 120 def gemspec? !gemspecs.empty? end |
#gemspecs ⇒ Array<String>
Get an array of all gemspec files in the workspace.
127 128 129 130 131 132 |
# File 'lib/solargraph/workspace.rb', line 127 def gemspecs return [] if directory.empty? || directory == '*' @gemspecs ||= Dir[File.join(directory, '**/*.gemspec')].select do |gs| config.allow? gs end end |
#has_file?(filename) ⇒ Boolean
93 94 95 |
# File 'lib/solargraph/workspace.rb', line 93 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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/solargraph/workspace.rb', line 53 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 |
#rbs_collection_config_path ⇒ String?
140 141 142 143 144 145 146 147 |
# File 'lib/solargraph/workspace.rb', line 140 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 |
#rbs_collection_path ⇒ String?
135 136 137 |
# File 'lib/solargraph/workspace.rb', line 135 def rbs_collection_path @gem_rbs_collection ||= 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.
75 76 77 78 79 |
# File 'lib/solargraph/workspace.rb', line 75 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.
37 38 39 40 |
# File 'lib/solargraph/workspace.rb', line 37 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 |
#source(filename) ⇒ Solargraph::Source
Get a source by its filename.
101 102 103 |
# File 'lib/solargraph/workspace.rb', line 101 def source filename source_hash[filename] end |
#sources ⇒ Array<Solargraph::Source>
87 88 89 |
# File 'lib/solargraph/workspace.rb', line 87 def sources source_hash.values end |
#synchronize!(updater) ⇒ void
This method returns an undefined value.
Synchronize the workspace from the provided updater.
153 154 155 |
# File 'lib/solargraph/workspace.rb', line 153 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.
109 110 111 112 113 114 115 |
# File 'lib/solargraph/workspace.rb', line 109 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 |