Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Nov 24, 2023
1 parent c1e2880 commit ae6a2cc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
7 changes: 5 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ def handle_attribute(node, reader:, writer:)
arguments = node.arguments&.arguments
return unless arguments

receiver = node.receiver
return unless receiver.nil? || receiver.is_a?(Prism::SelfNode)

comments = collect_comments(node)
arguments.each do |argument|
name, loc = case argument
Expand All @@ -339,8 +342,8 @@ def handle_attribute(node, reader:, writer:)

next unless name && loc

@index << Entry::Attribute.new(name, @file_path, loc, comments, @current_owner) if reader
@index << Entry::Attribute.new("#{name}=", @file_path, loc, comments, @current_owner) if writer
@index << Entry::Accessor.new(name, @file_path, loc, comments, @current_owner) if reader
@index << Entry::Accessor.new("#{name}=", @file_path, loc, comments, @current_owner) if writer
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,12 @@ def initialize(name:)
class RequiredParameter < Parameter
end

class Attribute < Entry
class Accessor < Entry
extend T::Sig

sig { returns(T.nilable(Entry::Namespace)) }
attr_reader :owner

sig { returns(T::Array[Parameter]) }
attr_reader :parameters

sig do
params(
name: String,
Expand All @@ -114,9 +111,13 @@ class Attribute < Entry
def initialize(name, file_path, location, comments, owner)
super(name, file_path, location, comments)
@owner = owner
end

@parameters = T.let([], T::Array[Parameter])
@parameters << RequiredParameter.new(name: name.delete_suffix("=").to_sym) if name.end_with?("=")
sig { returns(T::Array[Parameter]) }
def parameters
params = []
params << RequiredParameter.new(name: name.delete_suffix("=").to_sym) if name.end_with?("=")
params
end
end

Expand Down
21 changes: 16 additions & 5 deletions lib/ruby_indexer/test/method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,24 @@ class Foo
end
RUBY

assert_entry("bar", Entry::Attribute, "/fake/path/foo.rb:2-15:2-18")
assert_entry("bar", Entry::Accessor, "/fake/path/foo.rb:2-15:2-18")
assert_equal("Hello there", @index["bar"].first.comments.join("\n"))
assert_entry("other", Entry::Attribute, "/fake/path/foo.rb:2-21:2-26")
assert_entry("other", Entry::Accessor, "/fake/path/foo.rb:2-21:2-26")
assert_equal("Hello there", @index["other"].first.comments.join("\n"))
assert_entry("baz=", Entry::Attribute, "/fake/path/foo.rb:3-15:3-18")
assert_entry("qux", Entry::Attribute, "/fake/path/foo.rb:4-17:4-20")
assert_entry("qux=", Entry::Attribute, "/fake/path/foo.rb:4-17:4-20")
assert_entry("baz=", Entry::Accessor, "/fake/path/foo.rb:3-15:3-18")
assert_entry("qux", Entry::Accessor, "/fake/path/foo.rb:4-17:4-20")
assert_entry("qux=", Entry::Accessor, "/fake/path/foo.rb:4-17:4-20")
end

def test_ignores_attributes_invoked_on_constant
index(<<~RUBY)
class Foo
end
Foo.attr_reader :bar
RUBY

assert_no_entry("bar")
end
end
end
4 changes: 2 additions & 2 deletions lib/ruby_lsp/requests/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def on_call_node_enter(node)

candidates = T.cast(
@index.prefix_search(name),
T::Array[T.any(T::Array[RubyIndexer::Entry::Method], T::Array[RubyIndexer::Entry::Attribute])],
T::Array[T.any(T::Array[RubyIndexer::Entry::Method], T::Array[RubyIndexer::Entry::Accessor])],
)
candidates.each do |entries|
entry = entries.find { |e| e.owner&.name == receiver.name }
Expand All @@ -153,7 +153,7 @@ def on_call_node_enter(node)

sig do
params(
entry: T.any(RubyIndexer::Entry::Method, RubyIndexer::Entry::Attribute),
entry: T.any(RubyIndexer::Entry::Method, RubyIndexer::Entry::Accessor),
node: Prism::CallNode,
).returns(Interface::CompletionItem)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/workspace_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def kind_for_entry(entry)
Constant::SymbolKind::CONSTANT
when RubyIndexer::Entry::Method
entry.name == "initialize" ? Constant::SymbolKind::CONSTRUCTOR : Constant::SymbolKind::METHOD
when RubyIndexer::Entry::Attribute
when RubyIndexer::Entry::Accessor
Constant::SymbolKind::PROPERTY
end
end
Expand Down

0 comments on commit ae6a2cc

Please sign in to comment.