Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid showing private methods for external references #2783

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ruby_lsp/listeners/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,11 @@ def complete_methods(node, name)
return unless range

guessed_type = type.is_a?(TypeInferrer::GuessedType) && type.name
external_references = @node_context.fully_qualified_name != type.name

@index.method_completion_candidates(method_name, type.name).each do |entry|
next if entry.visibility != RubyIndexer::Entry::Visibility::PUBLIC && external_references

entry_name = entry.name
owner_name = entry.owner&.name

Expand Down
72 changes: 72 additions & 0 deletions test/requests/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,78 @@ def test_guessed_type_name_is_only_included_for_guessed_types
end
end

def test_completion_for_private_methods
vinistock marked this conversation as resolved.
Show resolved Hide resolved
source = +<<~RUBY
class Foo
def bar
b
end

private

def baz
end
end

foo = Foo.new
foo.b
RUBY

with_server(source) do |server, uri|
server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 2, character: 5 },
})

result = server.pop_response.response
assert_includes(result.map(&:label), "baz")

server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 12, character: 5 },
})

result = server.pop_response.response
refute_includes(result.map(&:label), "baz")
end
end

def test_completion_for_protected_methods
source = +<<~RUBY
class Foo
def bar
b
end

protected

def baz
end
end

foo = Foo.new
foo.b
RUBY

with_server(source) do |server, uri|
server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 2, character: 5 },
})

result = server.pop_response.response
assert_includes(result.map(&:label), "baz")

server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 12, character: 5 },
})

result = server.pop_response.response
refute_includes(result.map(&:label), "baz")
end
end

private

def with_file_structure(server, &block)
Expand Down
Loading