Skip to content

Commit

Permalink
Exclude dependencies from workspace symbols (#2131)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Jun 5, 2024
1 parent 2dc2ea1 commit 852b629
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 26 deletions.
7 changes: 4 additions & 3 deletions lib/ruby_lsp/requests/workspace_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ def initialize(global_state, query)
sig { override.returns(T::Array[Interface::WorkspaceSymbol]) }
def perform
@index.fuzzy_search(@query).filter_map do |entry|
# If the project is using Sorbet, we let Sorbet handle symbols defined inside the project itself and RBIs, but
# we still return entries defined in gems to allow developers to jump directly to the source
file_path = entry.file_path
next if @global_state.typechecker && not_in_dependencies?(file_path)

# We only show symbols declared in the workspace
in_dependencies = !not_in_dependencies?(file_path)
next if in_dependencies

# We should never show private symbols when searching the entire workspace
next if entry.private?
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def run_initialize(message)
completion_provider: completion_provider,
code_lens_provider: code_lens_provider,
definition_provider: enabled_features["definition"],
workspace_symbol_provider: enabled_features["workspaceSymbol"],
workspace_symbol_provider: enabled_features["workspaceSymbol"] && !@global_state.typechecker,
signature_help_provider: signature_help_provider,
),
serverInfo: {
Expand Down
24 changes: 2 additions & 22 deletions test/requests/workspace_symbol_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,6 @@ module Bar; end
assert_equal(RubyLsp::Constant::SymbolKind::CONSTANT, T.must(result).kind)
end

def test_matches_only_gem_symbols_if_typechecker_is_present
# create a new global state so the stub is not used
@global_state = RubyLsp::GlobalState.new
@index = @global_state.index
indexable = RubyIndexer::IndexablePath.new(nil, "#{Dir.pwd}/workspace_symbol_foo.rb")

@index.index_single(indexable, <<~RUBY)
class Foo; end
RUBY

path = "#{Bundler.bundle_path}/gems/fake-gem-0.1.0/lib/gem_symbol_foo.rb"
@index.index_single(RubyIndexer::IndexablePath.new(nil, path), <<~RUBY)
class Foo; end
RUBY

result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Foo").perform
assert_equal(1, result.length)
assert_equal(URI::Generic.from_path(path: path).to_s, T.must(result.first).location.uri)
end

def test_symbols_include_container_name
@index.index_single(RubyIndexer::IndexablePath.new(nil, "/fake.rb"), <<~RUBY)
module Foo
Expand All @@ -85,11 +65,11 @@ class Bar; end
assert_equal("Foo", T.must(result).container_name)
end

def test_finds_default_gem_symbols
def test_does_not_include_symbols_from_dependencies
@index.index_single(RubyIndexer::IndexablePath.new(nil, "#{RbConfig::CONFIG["rubylibdir"]}/pathname.rb"))

result = RubyLsp::Requests::WorkspaceSymbol.new(@global_state, "Pathname").perform
refute_empty(result)
assert_empty(result)
end

def test_does_not_include_private_constants
Expand Down

0 comments on commit 852b629

Please sign in to comment.