From ad1b27747aabada13523ad2d4957ee6d353fb6f4 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Thu, 29 Feb 2024 16:15:11 -0500 Subject: [PATCH] Introduce `.ruby-lsp.yml` config file --- .index.yml | 2 -- .ruby-lsp.yml | 3 ++ README.md | 28 ++++++++++++------- .../lib/ruby_indexer/configuration.rb | 24 +++++++++++++--- lib/ruby_indexer/test/configuration_test.rb | 21 ++++++++++++-- 5 files changed, 60 insertions(+), 18 deletions(-) delete mode 100644 .index.yml create mode 100644 .ruby-lsp.yml diff --git a/.index.yml b/.index.yml deleted file mode 100644 index 1c4e7b84c..000000000 --- a/.index.yml +++ /dev/null @@ -1,2 +0,0 @@ -excluded_patterns: - - "**/test/fixtures/**/*.rb" diff --git a/.ruby-lsp.yml b/.ruby-lsp.yml new file mode 100644 index 000000000..30f9f985e --- /dev/null +++ b/.ruby-lsp.yml @@ -0,0 +1,3 @@ +indexing: + excluded_patterns: + - "**/test/fixtures/**/*.rb" diff --git a/README.md b/README.md index 855e36ac0..6a6ac6a38 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb index 9e00e5d4e..c9a0d3169 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/configuration.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/configuration.rb @@ -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]) } diff --git a/lib/ruby_indexer/test/configuration_test.rb b/lib/ruby_indexer/test/configuration_test.rb index 31722a06c..880431414 100644 --- a/lib/ruby_indexer/test/configuration_test.rb +++ b/lib/ruby_indexer/test/configuration_test.rb @@ -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 @@ -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