Skip to content

Commit

Permalink
Build locations before creating entries (#2698)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Oct 10, 2024
1 parent b6cfb22 commit 98461f1
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 @@ -111,10 +112,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 @@ -141,10 +141,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 @@ -175,19 +174,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 @@ -345,10 +342,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 @@ -362,10 +358,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 @@ -447,9 +442,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 @@ -508,9 +502,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 @@ -573,9 +566,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 @@ -610,9 +602,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 @@ -624,9 +615,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 @@ -636,12 +626,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 @@ -707,9 +701,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 @@ -720,9 +713,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 98461f1

Please sign in to comment.