class Solargraph::Workspace::Config
Configuration data for a workspace.
Constants
- MAX_FILES
The maximum number of files that can be added to a workspace. The workspace's .solargraph.yml can override this value.
Attributes
@return [String]
@return [Hash]
Public Class Methods
@param directory [String]
# File lib/solargraph/workspace/config.rb, line 21 def initialize directory = '' @directory = directory @raw_data = config_data included excluded end
Public Instance Methods
# File lib/solargraph/workspace/config.rb, line 44 def allow? filename filename.start_with?(directory) && !excluded.include?(filename) && excluded_directories.none? { |d| filename.start_with?(d) } end
The calculated array of (included - excluded) files in the workspace.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 53 def calculated Solargraph.logger.info "Indexing workspace files in #{directory}" unless @calculated || directory.empty? || directory == '*' @calculated ||= included - excluded end
An array of domains configured for the workspace. A domain is a namespace that the ApiMap
should include in the global namespace. It's typically used to identify available DSLs.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 63 def domains raw_data['domains'] end
An array of files excluded from the workspace.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 39 def excluded return [] if directory.empty? || directory == '*' @excluded ||= process_exclusions(@raw_data['exclude']) end
A hash of options supported by the formatter
@return [Hash]
# File lib/solargraph/workspace/config.rb, line 91 def formatter raw_data['formatter'] end
An array of files included in the workspace (before calculating excluded files).
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 31 def included return [] if directory.empty? || directory == '*' @included ||= process_globs(@raw_data['include']) end
The maximum number of files to parse from the workspace.
@return [Integer]
# File lib/solargraph/workspace/config.rb, line 105 def max_files raw_data['max_files'] end
An array of plugins to require.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 98 def plugins raw_data['plugins'] end
An array of reporters to use for diagnostics.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 84 def reporters raw_data['reporters'] end
An array of load paths for required paths.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 77 def require_paths raw_data['require_paths'] || [] end
An array of required paths to add to the workspace.
@return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 70 def required raw_data['require'] end
Private Instance Methods
@return [Hash]
# File lib/solargraph/workspace/config.rb, line 124 def config_data workspace_config = read_config(workspace_config_path) global_config = read_config(global_config_path) defaults = default_config defaults.merge({'exclude' => []}) unless workspace_config.nil? defaults .merge(global_config || {}) .merge(workspace_config || {}) end
@return [Hash]
# File lib/solargraph/workspace/config.rb, line 147 def default_config { 'include' => ['**/*.rb'], 'exclude' => ['spec/**/*', 'test/**/*', 'vendor/**/*', '.bundle/**/*'], 'require' => [], 'domains' => [], 'reporters' => %w[rubocop require_not_found], 'formatter' => { 'rubocop' => { 'cops' => 'safe', 'except' => [], 'only' => [], 'extra_args' =>[] } }, 'require_paths' => [], 'plugins' => [], 'max_files' => MAX_FILES } end
# File lib/solargraph/workspace/config.rb, line 223 def excluded_directories @raw_data['exclude'] .select { |g| glob_is_directory?(g) } .map { |g| File.join(directory, glob_to_directory(g)) } end
True if the glob translates to a whole directory.
@example
glob_is_directory?('path/to/dir') # => true glob_is_directory?('path/to/dir/**/*) # => true glob_is_directory?('path/to/file.txt') # => false glob_is_directory?('path/to/*.txt') # => false
@param glob [String] @return [Boolean]
# File lib/solargraph/workspace/config.rb, line 208 def glob_is_directory? glob File.directory?(glob) || File.directory?(glob_to_directory(glob)) end
Translate a glob to a base directory if applicable
@example
glob_to_directory('path/to/dir/**/*') # => 'path/to/dir'
@param glob [String] @return [String]
# File lib/solargraph/workspace/config.rb, line 219 def glob_to_directory glob glob.gsub(/(\/\*|\/\*\*\/\*\*?)$/, '') end
@return [String]
# File lib/solargraph/workspace/config.rb, line 112 def global_config_path ENV['SOLARGRAPH_GLOBAL_CONFIG'] || File.join(Dir.home, '.config', 'solargraph', 'config.yml') end
Modify the included files based on excluded directories and get an array of additional files to exclude.
@param globs [Array<String>] @return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 185 def process_exclusions globs remainder = globs.select do |glob| if glob_is_directory?(glob) exdir = File.join(directory, glob_to_directory(glob)) included.delete_if { |file| file.start_with?(exdir) } false else true end end process_globs remainder end
Get an array of files from the provided globs.
@param globs [Array<String>] @return [Array<String>]
# File lib/solargraph/workspace/config.rb, line 172 def process_globs globs result = [] globs.each do |glob| result.concat Dir[File.join directory, glob].map{ |f| f.gsub(/\\/, '/') } end result end
Read a .solargraph yaml config
@param directory [String] @return [Hash, nil]
# File lib/solargraph/workspace/config.rb, line 140 def read_config config_path = '' return nil if config_path.empty? return nil unless File.file?(config_path) YAML.safe_load(File.read(config_path)) end
@return [String]
# File lib/solargraph/workspace/config.rb, line 118 def workspace_config_path return '' if @directory.empty? File.join(@directory, '.solargraph.yml') end