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

directory[R]

@return [String]

raw_data[R]

@return [Hash]

Public Class Methods

new(directory = '') click to toggle source

@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

allow?(filename) click to toggle source
# 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
calculated() click to toggle source

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
domains() click to toggle source

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
excluded() click to toggle source

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
formatter() click to toggle source

A hash of options supported by the formatter

@return [Hash]

# File lib/solargraph/workspace/config.rb, line 91
def formatter
  raw_data['formatter']
end
included() click to toggle source

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
max_files() click to toggle source

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
plugins() click to toggle source

An array of plugins to require.

@return [Array<String>]

# File lib/solargraph/workspace/config.rb, line 98
def plugins
  raw_data['plugins']
end
reporters() click to toggle source

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
require_paths() click to toggle source

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
required() click to toggle source

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

config_data() click to toggle source

@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
default_config() click to toggle source

@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
excluded_directories() click to toggle source
# File lib/solargraph/workspace/config.rb, line 224
def excluded_directories
  @raw_data['exclude']
    .select { |g| glob_is_directory?(g) }
    .map { |g| File.join(directory, glob_to_directory(g)) }
end
glob_is_directory?(glob) click to toggle source

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 209
def glob_is_directory? glob
  File.directory?(glob) || File.directory?(glob_to_directory(glob))
end
glob_to_directory(glob) click to toggle source

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 220
def glob_to_directory glob
  glob.gsub(/(\/\*|\/\*\*\/\*\*?)$/, '')
end
global_config_path() click to toggle source

@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
process_exclusions(globs) click to toggle source

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 186
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
process_globs(globs) click to toggle source

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.flat_map do |glob|
    Dir[File.join directory, glob]
      .map{ |f| f.gsub(/\\/, '/') }
      .select { |f| File.file?(f) }
  end
  result
end
read_config(config_path = '') click to toggle source

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
workspace_config_path() click to toggle source

@return [String]

# File lib/solargraph/workspace/config.rb, line 118
def workspace_config_path
  return '' if @directory.empty?
  File.join(@directory, '.solargraph.yml')
end