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]
@todo To make JSON strongly typed we’ll need a record syntax @return [Hash{String => undefined, nil}]
Public Class Methods
Source
# File lib/solargraph/workspace/config.rb, line 22 def initialize directory = '' @directory = File.absolute_path(directory) @raw_data = config_data included excluded end
@param directory [String]
Public Instance Methods
Source
# File lib/solargraph/workspace/config.rb, line 46 def allow? filename filename = File.absolute_path(filename, directory) filename.start_with?(directory) && !excluded.include?(filename) && excluded_directories.none? { |d| filename.start_with?(d) } end
@param filename [String]
Source
# File lib/solargraph/workspace/config.rb, line 56 def calculated Solargraph.logger.info "Indexing workspace files in #{directory}" unless @calculated || directory.empty? || directory == '*' @calculated ||= included - excluded end
The calculated array of (included - excluded) files in the workspace.
@return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 67 def domains raw_data['domains'] 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>] @sg-ignore Need to validate config
Source
# File lib/solargraph/workspace/config.rb, line 40 def excluded return [] if directory.empty? || directory == '*' @excluded ||= process_exclusions(@raw_data['exclude']) end
An array of files excluded from the workspace.
@return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 98 def formatter raw_data['formatter'] end
A hash of options supported by the formatter
@sg-ignore Need to validate config @return [Hash]
Source
# File lib/solargraph/workspace/config.rb, line 32 def included return [] if directory.empty? || directory == '*' @included ||= process_globs(@raw_data['include']) end
An array of files included in the workspace (before calculating excluded files).
@return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 114 def max_files raw_data['max_files'] end
The maximum number of files to parse from the workspace.
@sg-ignore Need to validate config @return [Integer]
Source
# File lib/solargraph/workspace/config.rb, line 106 def plugins raw_data['plugins'] end
An array of plugins to require.
@sg-ignore Need to validate config @return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 90 def reporters raw_data['reporters'] end
An array of reporters to use for diagnostics.
@sg-ignore Need to validate config @return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 82 def require_paths raw_data['require_paths'] || [] end
An array of load paths for required paths.
@return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 75 def required raw_data['require'] end
An array of required paths to add to the workspace.
@return [Array<String>] @sg-ignore Need to validate config
Source
# File lib/solargraph/workspace/config.rb, line 119 def type_checker_rules # @type [Hash{String => String}] raw_rules = raw_data.fetch('type_checker', {}).fetch('rules', {}) raw_rules.to_h do |k, v| [k.to_sym, v.to_sym] end end
@return [Hash{Symbol => Symbol}]
Private Instance Methods
Source
# File lib/solargraph/workspace/config.rb, line 142 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{String => undefined}]
Source
# File lib/solargraph/workspace/config.rb, line 165 def default_config { 'include' => ['Rakefile', 'Gemfile', '*.gemspec', '**/*.rb'], 'exclude' => ['spec/**/*', 'test/**/*', 'vendor/**/*', '.bundle/**/*'], 'require' => [], 'domains' => [], 'reporters' => %w[rubocop require_not_found], 'formatter' => { 'rubocop' => { 'cops' => 'safe', 'except' => [], 'only' => [], 'extra_args' =>[] } }, 'type_checker' => { 'rules' => { } }, 'require_paths' => [], 'plugins' => [], 'max_files' => MAX_FILES } end
@return [Hash{String => Array, Hash, Integer}]
Source
# File lib/solargraph/workspace/config.rb, line 246 def excluded_directories # @type [Array<String>] excluded = @raw_data['exclude'] excluded .select { |g| glob_is_directory?(g) } .map { |g| File.absolute_path(glob_to_directory(g), directory) } end
@return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 230 def glob_is_directory? glob File.directory?(glob) || File.directory?(glob_to_directory(glob)) 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]
Source
# File lib/solargraph/workspace/config.rb, line 241 def glob_to_directory glob glob.gsub(/(\/\*|\/\*\*\/\*\*?)$/, '') 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]
Source
# File lib/solargraph/workspace/config.rb, line 130 def global_config_path ENV['SOLARGRAPH_GLOBAL_CONFIG'] || File.join(Dir.home, '.config', 'solargraph', 'config.yml') end
@return [String]
Source
# File lib/solargraph/workspace/config.rb, line 207 def process_exclusions globs remainder = globs.select do |glob| if glob_is_directory?(glob) exdir = File.absolute_path(glob_to_directory(glob), directory) included.delete_if { |file| file.start_with?(exdir) } false else true end end process_globs remainder 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>]
Source
# File lib/solargraph/workspace/config.rb, line 193 def process_globs globs result = globs.flat_map do |glob| Dir[File.absolute_path(glob, directory)] .map{ |f| f.gsub(/\\/, '/') } .select { |f| File.file?(f) } end result end
Get an array of files from the provided globs.
@param globs [Array<String>] @return [Array<String>]
Source
# File lib/solargraph/workspace/config.rb, line 158 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
Read a .solargraph yaml config
@param config_path [String] @return [Hash{String => Array, Hash, Integer}, nil]
Source
# File lib/solargraph/workspace/config.rb, line 136 def workspace_config_path return '' if @directory.empty? File.join(@directory, '.solargraph.yml') end
@return [String]