Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index global variables #2656

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,9 @@ def initialize(target, unresolved_alias, encoding)
end
end

# Represents a global variable e.g.: $DEBUG
class GlobalVariable < Entry; end

# Represents an instance variable e.g.: @a = 1
class InstanceVariable < Entry
sig { returns(T.nilable(Entry::Namespace)) }
Expand Down
20 changes: 20 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def process_declaration(declaration, pathname)
when RBS::AST::Declarations::Constant
namespace_nesting = declaration.name.namespace.path.map(&:to_s)
handle_constant(declaration, namespace_nesting, pathname.to_s)
when RBS::AST::Declarations::Global
handle_global_variable(declaration, pathname)
else # rubocop:disable Style/EmptyElse
# Other kinds not yet handled
end
Expand Down Expand Up @@ -271,6 +273,23 @@ def handle_constant(declaration, nesting, file_path)
))
end

sig { params(declaration: RBS::AST::Declarations::Global, pathname: Pathname).void }
def handle_global_variable(declaration, pathname)
name = declaration.name.to_s
file_path = pathname.to_s
location = to_ruby_indexer_location(declaration.location)
comments = comments_to_string(declaration)
encoding = @index.configuration.encoding

@index.add(Entry::GlobalVariable.new(
name,
file_path,
location,
comments,
encoding,
))
end

sig { params(member: RBS::AST::Members::Alias, owner_entry: Entry::Namespace).void }
def handle_signature_alias(member, owner_entry)
file_path = member.location.buffer.name
Expand All @@ -294,6 +313,7 @@ def handle_signature_alias(member, owner_entry)
RBS::AST::Declarations::Class,
RBS::AST::Declarations::Module,
RBS::AST::Declarations::Constant,
RBS::AST::Declarations::Global,
RBS::AST::Members::MethodDefinition,
RBS::AST::Members::Alias,
)).returns(T.nilable(String))
Expand Down
14 changes: 14 additions & 0 deletions lib/ruby_indexer/test/rbs_indexer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def test_index_methods
assert_operator(entry.location.end_column, :>, 0)
end

def test_index_global_declaration
entries = @index["$DEBUG"]
refute_nil(entries)
assert_equal(1, entries.length)

entry = entries.first

assert_instance_of(Entry::GlobalVariable, entry)
assert_equal("$DEBUG", entry.name)
assert_match(%r{/gems/rbs-.*/core/global_variables.rbs}, entry.file_path)
assert_operator(entry.location.start_column, :<, entry.location.end_column)
assert_equal(entry.location.start_line, entry.location.end_line)
end

def test_attaches_correct_owner_to_singleton_methods
entries = @index["basename"]
refute_nil(entries)
Expand Down
Loading