From 02200672a97be47f84b835eca39fe4b7f78ef65e Mon Sep 17 00:00:00 2001 From: snutij Date: Wed, 2 Oct 2024 22:04:51 +0200 Subject: [PATCH 1/3] feat: handle global variable in RBSIndexer --- lib/ruby_indexer/lib/ruby_indexer/entry.rb | 16 ++++++++++++++ .../lib/ruby_indexer/rbs_indexer.rb | 22 +++++++++++++++++++ lib/ruby_indexer/test/rbs_indexer_test.rb | 14 ++++++++++++ 3 files changed, 52 insertions(+) diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index 291f5dcdf..cd6d3a112 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -525,6 +525,22 @@ def initialize(target, unresolved_alias, encoding) end end + # Represents a global variable e.g.: $DEBUG + class GlobalVariable < Entry + sig do + params( + name: String, + file_path: String, + location: RubyIndexer::Location, + comments: T.nilable(String), + encoding: Encoding, + ).void + end + def initialize(name, file_path, location, comments, encoding) + super(name, file_path, location, comments, encoding) + end + 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..b608c76e7 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,25 @@ def handle_constant(declaration, nesting, file_path) )) end + sig do + params(declaration: RBS::AST::Declarations::Global, pathname: Pathname).void + end + 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 +315,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) From 5c46def6f878e8deaaada2a364cbb9593c4c5ee2 Mon Sep 17 00:00:00 2001 From: snutij Date: Thu, 3 Oct 2024 15:56:46 +0200 Subject: [PATCH 2/3] style: use super short syntax and oneline signature --- lib/ruby_indexer/lib/ruby_indexer/entry.rb | 2 +- lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index cd6d3a112..f6e38a4df 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -537,7 +537,7 @@ class GlobalVariable < Entry ).void end def initialize(name, file_path, location, comments, encoding) - super(name, file_path, location, comments, encoding) + super end end diff --git a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb index b608c76e7..4c7f2acd2 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb @@ -273,9 +273,7 @@ def handle_constant(declaration, nesting, file_path) )) end - sig do - params(declaration: RBS::AST::Declarations::Global, pathname: Pathname).void - 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 From 6d0623d9ac5e74d3d955cbadc2092feb92149eda Mon Sep 17 00:00:00 2001 From: snutij Date: Fri, 4 Oct 2024 21:37:47 +0200 Subject: [PATCH 3/3] style: remove unnecessary initialize declaration in GlobalVariable --- lib/ruby_indexer/lib/ruby_indexer/entry.rb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index f6e38a4df..41d9442b3 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -526,20 +526,7 @@ def initialize(target, unresolved_alias, encoding) end # Represents a global variable e.g.: $DEBUG - class GlobalVariable < Entry - sig do - params( - name: String, - file_path: String, - location: RubyIndexer::Location, - comments: T.nilable(String), - encoding: Encoding, - ).void - end - def initialize(name, file_path, location, comments, encoding) - super - end - end + class GlobalVariable < Entry; end # Represents an instance variable e.g.: @a = 1 class InstanceVariable < Entry