Skip to content

Commit

Permalink
Introduce Entry::Signature
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jun 21, 2024
1 parent 8830833 commit 8b20e39
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ def on_def_node_enter(node)
method_name = node.name.to_s
comments = collect_comments(node)

signature = Entry::Signature.new(list_params(node.parameters))

case node.receiver
when nil
@index.add(Entry::Method.new(
Expand All @@ -309,7 +311,7 @@ def on_def_node_enter(node)
node.location,
node.name_loc,
comments,
list_params(node.parameters),
[signature],
current_visibility,
@owner_stack.last,
))
Expand All @@ -322,7 +324,7 @@ def on_def_node_enter(node)
node.location,
node.name_loc,
comments,
list_params(node.parameters),
[signature],
current_visibility,
singleton,
))
Expand Down
39 changes: 28 additions & 11 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ class Member < Entry
sig { returns(T.nilable(Entry::Namespace)) }
attr_reader :owner

sig { returns(T::Array[RubyIndexer::Entry::Parameter]) }
def parameters
T.must(signatures.first).parameters
end

sig do
params(
name: String,
Expand All @@ -310,32 +315,32 @@ def initialize(name, file_path, location, comments, visibility, owner) # rubocop
@owner = owner
end

sig { abstract.returns(T::Array[Parameter]) }
def parameters; end
sig { abstract.returns(T::Array[Entry::Signature]) }
def signatures; end

# Returns a string with the decorated names of the parameters of this member. E.g.: `(a, b = 1, c: 2)`
sig { returns(String) }
def decorated_parameters
"(#{parameters.map(&:decorated_name).join(", ")})"
"(#{T.must(signatures.first).parameters.map(&:decorated_name).join(", ")})"
end
end

class Accessor < Member
extend T::Sig

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

class Method < Member
extend T::Sig

sig { override.returns(T::Array[Parameter]) }
attr_reader :parameters
sig { override.returns(T::Array[Signature]) }
attr_reader :signatures

# Returns the location of the method name, excluding parameters or the body
sig { returns(Location) }
Expand All @@ -348,14 +353,14 @@ class Method < Member
location: T.any(Prism::Location, RubyIndexer::Location),
name_location: T.any(Prism::Location, Location),
comments: T::Array[String],
parameters: T::Array[Parameter],
signatures: T::Array[Signature],
visibility: Visibility,
owner: T.nilable(Entry::Namespace),
).void
end
def initialize(name, file_path, location, name_location, comments, parameters, visibility, owner) # rubocop:disable Metrics/ParameterLists
def initialize(name, file_path, location, name_location, comments, signatures, visibility, owner) # rubocop:disable Metrics/ParameterLists
super(name, file_path, location, comments, visibility, owner)
@parameters = parameters
@signatures = signatures
@name_location = T.let(
if name_location.is_a?(Prism::Location)
Location.new(
Expand Down Expand Up @@ -515,5 +520,17 @@ def decorated_parameters
@target.decorated_parameters
end
end

class Signature
extend T::Sig

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

sig { params(parameters: T::Array[Parameter]).void }
def initialize(parameters)
@parameters = parameters
end
end
end
end

0 comments on commit 8b20e39

Please sign in to comment.