Skip to content

Commit

Permalink
Build locations before creating entries
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 9, 2024
1 parent 7626704 commit 52cc3f8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 147 deletions.
62 changes: 27 additions & 35 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize(index, dispatcher, parse_result, file_path, collect_comments: fal
T::Hash[Integer, Prism::Comment],
)
@inside_def = T.let(false, T::Boolean)
@encoding = T.let(@index.configuration.encoding, Encoding)

# The nesting stack we're currently inside. Used to determine the fully qualified name of constants, but only
# stored by unresolved aliases which need the original nesting to be lazily resolved
Expand Down Expand Up @@ -106,10 +107,9 @@ def on_class_node_enter(node)
entry = Entry::Class.new(
nesting,
@file_path,
node.location,
constant_path.location,
Location.from_prism_location(node.location, @encoding),
Location.from_prism_location(constant_path.location, @encoding),
comments,
@index.configuration.encoding,
parent_class,
)

Expand All @@ -136,10 +136,9 @@ def on_module_node_enter(node)
entry = Entry::Module.new(
actual_nesting(name),
@file_path,
node.location,
constant_path.location,
Location.from_prism_location(node.location, @encoding),
Location.from_prism_location(constant_path.location, @encoding),
comments,
@index.configuration.encoding,
)

@owner_stack << entry
Expand Down Expand Up @@ -170,19 +169,17 @@ def on_singleton_class_node_enter(node)
if existing_entries
entry = T.must(existing_entries.first)
entry.update_singleton_information(
node.location,
expression.location,
Location.from_prism_location(node.location, @encoding),
Location.from_prism_location(expression.location, @encoding),
collect_comments(node),
@index.configuration.encoding,
)
else
entry = Entry::SingletonClass.new(
real_nesting,
@file_path,
node.location,
expression.location,
Location.from_prism_location(node.location, @encoding),
Location.from_prism_location(expression.location, @encoding),
collect_comments(node),
@index.configuration.encoding,
nil,
)
@index.add(entry, skip_prefix_tree: true)
Expand Down Expand Up @@ -340,10 +337,9 @@ def on_def_node_enter(node)
@index.add(Entry::Method.new(
method_name,
@file_path,
node.location,
node.name_loc,
Location.from_prism_location(node.location, @encoding),
Location.from_prism_location(node.name_loc, @encoding),
comments,
@index.configuration.encoding,
[Entry::Signature.new(list_params(node.parameters))],
current_visibility,
@owner_stack.last,
Expand All @@ -357,10 +353,9 @@ def on_def_node_enter(node)
@index.add(Entry::Method.new(
method_name,
@file_path,
node.location,
node.name_loc,
Location.from_prism_location(node.location, @encoding),
Location.from_prism_location(node.name_loc, @encoding),
comments,
@index.configuration.encoding,
[Entry::Signature.new(list_params(node.parameters))],
current_visibility,
singleton,
Expand Down Expand Up @@ -417,9 +412,8 @@ def on_alias_method_node_enter(node)
node.old_name.slice,
@owner_stack.last,
@file_path,
node.new_name.location,
Location.from_prism_location(node.new_name.location, @encoding),
comments,
@index.configuration.encoding,
),
)
end
Expand Down Expand Up @@ -453,9 +447,8 @@ def handle_instance_variable(node, loc)
@index.add(Entry::InstanceVariable.new(
name,
@file_path,
loc,
Location.from_prism_location(loc, @encoding),
collect_comments(node),
@index.configuration.encoding,
owner,
))
end
Expand Down Expand Up @@ -518,9 +511,8 @@ def handle_alias_method(node)
old_name_value,
@owner_stack.last,
@file_path,
new_name.location,
Location.from_prism_location(new_name.location, @encoding),
comments,
@index.configuration.encoding,
),
)
end
Expand Down Expand Up @@ -555,9 +547,8 @@ def add_constant(node, name, value = nil)
@stack.dup,
name,
@file_path,
node.location,
Location.from_prism_location(node.location, @encoding),
comments,
@index.configuration.encoding,
)
when Prism::ConstantWriteNode, Prism::ConstantAndWriteNode, Prism::ConstantOrWriteNode,
Prism::ConstantOperatorWriteNode
Expand All @@ -569,9 +560,8 @@ def add_constant(node, name, value = nil)
@stack.dup,
name,
@file_path,
node.location,
Location.from_prism_location(node.location, @encoding),
comments,
@index.configuration.encoding,
)
when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathOperatorWriteNode,
Prism::ConstantPathAndWriteNode
Expand All @@ -581,12 +571,16 @@ def add_constant(node, name, value = nil)
@stack.dup,
name,
@file_path,
node.location,
Location.from_prism_location(node.location, @encoding),
comments,
@index.configuration.encoding,
)
else
Entry::Constant.new(name, @file_path, node.location, comments, @index.configuration.encoding)
Entry::Constant.new(
name,
@file_path,
Location.from_prism_location(node.location, @encoding),
comments,
)
end,
)
end
Expand Down Expand Up @@ -652,9 +646,8 @@ def handle_attribute(node, reader:, writer:)
@index.add(Entry::Accessor.new(
name,
@file_path,
loc,
Location.from_prism_location(loc, @encoding),
comments,
@index.configuration.encoding,
current_visibility,
@owner_stack.last,
))
Expand All @@ -665,9 +658,8 @@ def handle_attribute(node, reader:, writer:)
@index.add(Entry::Accessor.new(
"#{name}=",
@file_path,
loc,
Location.from_prism_location(loc, @encoding),
comments,
@index.configuration.encoding,
current_visibility,
@owner_stack.last,
))
Expand Down
Loading

0 comments on commit 52cc3f8

Please sign in to comment.