Skip to content

Commit

Permalink
Index global variables (#2656)
Browse files Browse the repository at this point in the history
* feat: handle global variable in RBSIndexer

* style: use super short syntax and oneline signature

* style: remove unnecessary initialize declaration in GlobalVariable
  • Loading branch information
snutij authored Oct 4, 2024
1 parent eca9613 commit 9a737cb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
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 @@ -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)) }
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

0 comments on commit 9a737cb

Please sign in to comment.