Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Mar 22, 2024
1 parent ee23b2e commit 2378bda
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 31 deletions.
2 changes: 0 additions & 2 deletions .index.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .ruby-lsp.yml
@@ -0,0 +1,3 @@
indexing:
excluded_patterns:
- "**/test/fixtures/**/*.rb"
26 changes: 17 additions & 9 deletions README.md
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:
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
35 changes: 35 additions & 0 deletions lib/ruby_lsp/indexing_config.rb
@@ -0,0 +1,35 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
class IndexingConfig
extend T::Sig
class << self
extend T::Sig

# TODO: signature
sig { params(workspace_uri: URI::Generic).returns(T::Hash[String, T.untyped]) }
def call(workspace_uri)
# Need to use the workspace URI, otherwise, this will fail for people working on a project that is a symlink.
index_path = File.join(workspace_uri.to_standardized_path, ".index.yml")
ruby_lsp_path = File.join(workspace_uri.to_standardized_path, ".ruby-lsp.yml")

if File.exist?(index_path)
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#configuration")
".ruby-lsp.yml"
end

# begin
indexing_config = if File.exist?(index_path)
YAML.parse_file(index_path).to_ruby
elsif File.exist?(ruby_lsp_path)
YAML.parse_file(ruby_lsp_path).to_ruby.fetch("indexing") # TODO: handle exception
else
{}
# end
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/ruby_lsp/internal.rb
Expand Up @@ -20,6 +20,7 @@
require "language_server-protocol"

require "ruby-lsp"
require "ruby_lsp/indexing_config"
require "ruby_lsp/base_server"
require "ruby_indexer/ruby_indexer"
require "core_ext/uri"
Expand Down
34 changes: 14 additions & 20 deletions lib/ruby_lsp/server.rb
Expand Up @@ -236,29 +236,23 @@ def run_initialized

RubyVM::YJIT.enable if defined?(RubyVM::YJIT.enable)

indexing_config = {}
# indexing_config = {}
indexing_config = IndexingConfig.call(@store.workspace_uri)

# Need to use the workspace URI, otherwise, this will fail for people working on a project that is a symlink.
index_path = File.join(@store.workspace_uri.to_standardized_path, ".index.yml")

if File.exist?(index_path)
begin
indexing_config = YAML.parse_file(".index.yml").to_ruby
rescue Psych::SyntaxError => e
message = "Syntax error while loading .index.yml configuration: #{e.message}"
send_message(
Notification.new(
method: "window/showMessage",
params: Interface::ShowMessageParams.new(
type: Constant::MessageType::WARNING,
message: message,
),
begin
perform_initial_indexing(indexing_config)
rescue Psych::SyntaxError => e
message = "Syntax error while loading configuration: #{e.message}"
send_message(
Notification.new(
method: "window/showMessage",
params: Interface::ShowMessageParams.new(
type: Constant::MessageType::WARNING,
message: message,
),
)
end
),
)
end

perform_initial_indexing(indexing_config)
check_formatter_is_available
end

Expand Down
43 changes: 43 additions & 0 deletions test/indexing_config_test.rb
@@ -0,0 +1,43 @@
# typed: true
# frozen_string_literal: true

require "test_helper"

module RubyLsp
class IndexConfigTest < Minitest::Test
def test_returns_empty_hash_when_no_configuration_files_exist
FileUtils.mv(".ruby-lsp.yml", ".ruby-lsp.yml.tmp")
workspace_uri = URI::Generic.build(scheme: "file", host: nil, path: "/path/to/workspace")

result = RubyLsp::IndexingConfig.call(workspace_uri)

assert_empty(result)
ensure
FileUtils.mv(".ruby-lsp.yml.tmp", ".ruby-lsp.yml")
end

def test_supports_depecated_index_configuration_file
FileUtils.mv(".ruby-lsp.yml", ".ruby-lsp.yml.tmp")
File.write(".index.yml", <<~YAML)
excluded_patterns:
- "**/test/fixtures/**/*.rb"
YAML
workspace_uri = URI::Generic.build(scheme: "file", host: nil, path: Dir.pwd)

result = RubyLsp::IndexingConfig.call(workspace_uri)

assert_equal({ "excluded_patterns" => ["**/test/fixtures/**/*.rb"] }, result)
ensure
FileUtils.mv(".ruby-lsp.yml.tmp", ".ruby-lsp.yml")
FileUtils.rm_f(".index.yml")
end

def test_supports_newer_configuration
workspace_uri = URI::Generic.build(scheme: "file", host: nil, path: Dir.pwd)

result = RubyLsp::IndexingConfig.call(workspace_uri)

assert_equal({ "excluded_patterns" => ["**/test/fixtures/**/*.rb"] }, result)
end
end
end

0 comments on commit 2378bda

Please sign in to comment.