Skip to content

Commit

Permalink
Avoid showing private methods for external references
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 25, 2024
1 parent d21d40f commit ff8460c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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::PRIVATE && external_references

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

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

def test_completion_for_private_methods
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

private

def with_file_structure(server, &block)
Expand Down

0 comments on commit ff8460c

Please sign in to comment.