Skip to content

Commit

Permalink
Introduce .ruby-lsp.yml config file
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Feb 29, 2024
1 parent 4d5bc2e commit ad1b277
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .index.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .ruby-lsp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
indexing:
excluded_patterns:
- "**/test/fixtures/**/*.rb"
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ See the [documentation](https://shopify.github.io/ruby-lsp) for more in-depth de
For creating rich themes for Ruby using the semantic highlighting information, see the [semantic highlighting
documentation](SEMANTIC_HIGHLIGHTING.md).

## Configuration

Configuration is read from an optional `.ruby-lsp.yml` file in the root of your project.

### Configuring code indexing

By default, the Ruby LSP indexes all Ruby files defined in the current project and all of its dependencies, including
Expand All @@ -76,26 +80,30 @@ default gems, except for
- Gems that only appear under the `:development` group
- All Ruby files under `test/**/*.rb`

By creating a `.index.yml` file, these configurations can be overridden and tuned. Note that indexing dependent behavior, such as definition, hover, completion or workspace symbol will be impacted by the configurations placed here.
Within the `.ruby-lsp.yml` file, these configurations can be overridden and tuned. Note that indexing dependent behavior, such as definition, hover, completion or workspace symbol will be impacted by the configurations placed here.

```yaml
# Exclude files based on a given pattern. Often used to exclude test files or fixtures
excluded_patterns:
- "**/spec/**/*.rb"
indexing:
excluded_patterns:
- "**/spec/**/*.rb"

# Include files based on a given pattern. Can be used to index Ruby files that use different extensions
included_patterns:
- "**/bin/*"
indexing:
included_patterns:
- "**/bin/*"

# Exclude gems by name. If a gem is never referenced in the project's code and is only used as a tool, excluding it will
# speed up indexing and reduce the amount of results in features like definition or completion
excluded_gems:
- rubocop
- pathname
indexing:
excluded_gems:
- rubocop
- pathname

# Include gems by name. Normally used to include development gems that are excluded by default
included_gems:
- prism
indexing:
included_gems:
- prism
```
### Addons
Expand Down
24 changes: 20 additions & 4 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,32 @@ def initialize

sig { void }
def load_config
return unless File.exist?(".index.yml")
config_file = if File.exist?(".ruby-lsp.yml")
warn("The .index.yml configuration file is deprecated. Please rename it to .ruby-lsp.yml and update the
structure as described in the README: https://github.com/Shopify/ruby-lsp?tab=readme-ov-file#configuration")
".ruby-lsp.yml"
elsif File.exist?(".index.yml") # previously used for configuration
".index.yml"
else
return
end

config = YAML.parse_file(".index.yml")
config = YAML.parse_file(config_file)
return unless config

config_hash = config.to_ruby
config_hash = case config_file
when ".ruby-lsp.yml"
config.to_ruby["indexing"]
when ".index.yml"
config.to_ruby
else
raise "Invalid config file: #{config_file}" # Should never be reached
end

validate_config!(config_hash)
apply_config(config_hash)
rescue Psych::SyntaxError => e
raise e, "Syntax error while loading .index.yml configuration: #{e.message}"
raise e, "Syntax error while loading #{config_file} configuration: #{e.message}"
end

sig { returns(T::Array[IndexablePath]) }
Expand Down
21 changes: 19 additions & 2 deletions lib/ruby_indexer/test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ def test_load_configuration_executes_configure_block
assert(indexables.none? { |indexable| indexable.full_path == __FILE__ })
end

def test_supports_older_index_configuration
FileUtils.mv(".ruby-lsp.yml", ".ruby-lsp.yml.tmp")
s = <<~YAML
excluded_patterns:
- "**/test/fixtures/**/*.rb"
YAML
File.write(".index.yml", s)

@config.load_config
indexables = @config.indexables

assert(indexables.none? { |indexable| indexable.full_path.include?("test/fixtures") })
ensure
FileUtils.rm_f(".index.yml")
FileUtils.mv(".ruby-lsp.yml.tmp", ".ruby-lsp.yml")
end

def test_indexables_only_includes_gem_require_paths
@config.load_config
indexables = @config.indexables
Expand Down Expand Up @@ -101,8 +118,8 @@ def test_paths_are_unique
assert_equal(indexables.uniq.length, indexables.length)
end

def test_configuration_raises_for_unknown_keys
Psych::Nodes::Document.any_instance.expects(:to_ruby).returns({ "unknown_config" => 123 })
def test_configuration_raises_for_unknown_keys_within_indexing
Psych::Nodes::Document.any_instance.expects(:to_ruby).returns({ "indexing" => { "unknown_config" => 123 } })

assert_raises(ArgumentError) do
@config.load_config
Expand Down

0 comments on commit ad1b277

Please sign in to comment.