diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index fc51c0bcc..5f68b46bb 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -529,6 +529,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)) } diff --git a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb index e8bb79566..4c7f2acd2 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb @@ -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 @@ -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 @@ -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)) diff --git a/lib/ruby_indexer/test/rbs_indexer_test.rb b/lib/ruby_indexer/test/rbs_indexer_test.rb index 34a652bbc..8c2a99ed7 100644 --- a/lib/ruby_indexer/test/rbs_indexer_test.rb +++ b/lib/ruby_indexer/test/rbs_indexer_test.rb @@ -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)