From b24d3c0e6286e075a81b612e46fe9a03a840867d Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Fri, 19 Jul 2024 15:20:31 -0400 Subject: [PATCH] Unify class and module handling for RBS --- .../lib/ruby_indexer/rbs_indexer.rb | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb index d728261ec7..27c15109bd 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb @@ -37,45 +37,33 @@ def process_signature(pathname, declarations) sig { params(declaration: RBS::AST::Declarations::Base, pathname: Pathname).void } def process_declaration(declaration, pathname) case declaration - when RBS::AST::Declarations::Class - handle_class_declaration(declaration, pathname) - when RBS::AST::Declarations::Module - handle_module_declaration(declaration, pathname) + when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module + handle_class_or_module_declaration(declaration, pathname) else # rubocop:disable Style/EmptyElse # Other kinds not yet handled end end - sig { params(declaration: RBS::AST::Declarations::Class, pathname: Pathname).void } - def handle_class_declaration(declaration, pathname) - nesting = [declaration.name.name.to_s] - file_path = pathname.to_s - location = to_ruby_indexer_location(declaration.location) - comments = Array(declaration.comment&.string) - parent_class = declaration.super_class&.name&.name&.to_s - class_entry = Entry::Class.new(nesting, file_path, location, location, comments, parent_class) - add_declaration_mixins_to_entry(declaration, class_entry) - @index.add(class_entry) - declaration.members.each do |member| - next unless member.is_a?(RBS::AST::Members::MethodDefinition) - - handle_method(member, class_entry) - end + sig do + params(declaration: T.any(RBS::AST::Declarations::Class, RBS::AST::Declarations::Module), pathname: Pathname).void end - - sig { params(declaration: RBS::AST::Declarations::Module, pathname: Pathname).void } - def handle_module_declaration(declaration, pathname) + def handle_class_or_module_declaration(declaration, pathname) nesting = [declaration.name.name.to_s] file_path = pathname.to_s location = to_ruby_indexer_location(declaration.location) comments = Array(declaration.comment&.string) - module_entry = Entry::Module.new(nesting, file_path, location, location, comments) - add_declaration_mixins_to_entry(declaration, module_entry) - @index.add(module_entry) + if declaration.is_a?(RBS::AST::Declarations::Class) + parent_class = declaration.super_class&.name&.name&.to_s + entry = Entry::Class.new(nesting, file_path, location, location, comments, parent_class) + else + entry = Entry::Module.new(nesting, file_path, location, location, comments) + end + add_declaration_mixins_to_entry(declaration, entry) + @index.add(entry) declaration.members.each do |member| next unless member.is_a?(RBS::AST::Members::MethodDefinition) - handle_method(member, module_entry) + handle_method(member, entry) end end