From 03bbd637bcc2422665056eac8c862f1848122a3a Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:24:16 -0500 Subject: [PATCH] Update for changes to enhancements API --- lib/ruby_lsp/ruby_lsp_rails/addon.rb | 1 - .../ruby_lsp_rails/indexing_enhancement.rb | 78 +++++++------------ test/dummy/app/models/concerns/verifiable.rb | 16 ++++ test/dummy/app/models/user.rb | 2 + .../indexing_enhancement_test.rb | 2 - 5 files changed, 45 insertions(+), 54 deletions(-) create mode 100644 test/dummy/app/models/concerns/verifiable.rb diff --git a/lib/ruby_lsp/ruby_lsp_rails/addon.rb b/lib/ruby_lsp/ruby_lsp_rails/addon.rb index d7b6461e..3fce20b0 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/addon.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/addon.rb @@ -56,7 +56,6 @@ def activate(global_state, outgoing_queue) @outgoing_queue << Notification.window_log_message("Activating Ruby LSP Rails add-on v#{VERSION}") register_additional_file_watchers(global_state: global_state, outgoing_queue: outgoing_queue) - @global_state.index.register_enhancement(IndexingEnhancement.new(@global_state.index)) # Start booting the real client in a background thread. Until this completes, the client will be a NullClient @client_mutex.unlock diff --git a/lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb b/lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb index d0aca528..b312807a 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb @@ -6,27 +6,27 @@ module Rails class IndexingEnhancement < RubyIndexer::Enhancement extend T::Sig + sig { params(listener: RubyIndexer::DeclarationListener).void } + def initialize(listener) + super + # We need this to prevent Sorbet from complaining that @listener is undeclared + @listener = listener + end + sig do override.params( - owner: T.nilable(RubyIndexer::Entry::Namespace), - node: Prism::CallNode, - file_path: String, - code_units_cache: T.any( - T.proc.params(arg0: Integer).returns(Integer), - Prism::CodeUnitsCache, - ), + call_node: Prism::CallNode, ).void end - def on_call_node_enter(owner, node, file_path, code_units_cache) + def on_call_node_enter(call_node) + owner = @listener.current_owner return unless owner - name = node.name - - case name + case call_node.name when :extend - handle_concern_extend(owner, node) + handle_concern_extend(owner, call_node) when :has_one, :has_many, :belongs_to, :has_and_belongs_to_many - handle_association(owner, node, file_path, code_units_cache) + handle_association(owner, call_node) end end @@ -35,16 +35,11 @@ def on_call_node_enter(owner, node, file_path, code_units_cache) sig do params( owner: RubyIndexer::Entry::Namespace, - node: Prism::CallNode, - file_path: String, - code_units_cache: T.any( - T.proc.params(arg0: Integer).returns(Integer), - Prism::CodeUnitsCache, - ), + call_node: Prism::CallNode, ).void end - def handle_association(owner, node, file_path, code_units_cache) - arguments = node.arguments&.arguments + def handle_association(owner, call_node) + arguments = call_node.arguments&.arguments return unless arguments name_arg = arguments.first @@ -58,41 +53,22 @@ def handle_association(owner, node, file_path, code_units_cache) return unless name - loc = RubyIndexer::Location.from_prism_location(name_arg.location, code_units_cache) + loc = name_arg.location # Reader - @index.add(RubyIndexer::Entry::Method.new( - name, - file_path, - loc, - loc, - nil, - [RubyIndexer::Entry::Signature.new([])], - RubyIndexer::Entry::Visibility::PUBLIC, - owner, - )) + reader_signatures = [RubyIndexer::Entry::Signature.new([])] + @listener.add_method(name, loc, reader_signatures) # Writer - @index.add(RubyIndexer::Entry::Method.new( - "#{name}=", - file_path, - loc, - loc, - nil, - [RubyIndexer::Entry::Signature.new([RubyIndexer::Entry::RequiredParameter.new(name: name.to_sym)])], - RubyIndexer::Entry::Visibility::PUBLIC, - owner, - )) + writer_signatures = [ + RubyIndexer::Entry::Signature.new([RubyIndexer::Entry::RequiredParameter.new(name: name.to_sym)]), + ] + @listener.add_method("#{name}=", loc, writer_signatures) end - sig do - params( - owner: RubyIndexer::Entry::Namespace, - node: Prism::CallNode, - ).void - end - def handle_concern_extend(owner, node) - arguments = node.arguments&.arguments + sig { params(owner: RubyIndexer::Entry::Namespace, call_node: Prism::CallNode).void } + def handle_concern_extend(owner, call_node) + arguments = call_node.arguments&.arguments return unless arguments arguments.each do |node| @@ -101,7 +77,7 @@ def handle_concern_extend(owner, node) module_name = node.full_name next unless module_name == "ActiveSupport::Concern" - @index.register_included_hook(owner.name) do |index, base| + @listener.register_included_hook do |index, base| class_methods_name = "#{owner.name}::ClassMethods" if index.indexed?(class_methods_name) diff --git a/test/dummy/app/models/concerns/verifiable.rb b/test/dummy/app/models/concerns/verifiable.rb new file mode 100644 index 00000000..4f01a0e6 --- /dev/null +++ b/test/dummy/app/models/concerns/verifiable.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Verifiable + extend ActiveSupport::Concern + + # checks if a user is verified + def verified? + true + end + + module ClassMethods + def all_verified + all.select(&:verified?) + end + end +end diff --git a/test/dummy/app/models/user.rb b/test/dummy/app/models/user.rb index f3b57411..c696c86d 100644 --- a/test/dummy/app/models/user.rb +++ b/test/dummy/app/models/user.rb @@ -9,6 +9,8 @@ class User < ApplicationRecord attr_readonly :last_name + include Verifiable # an ActiveSupport::Concern + private def foo diff --git a/test/ruby_lsp_rails/indexing_enhancement_test.rb b/test/ruby_lsp_rails/indexing_enhancement_test.rb index b6c7b66b..ba61730a 100644 --- a/test/ruby_lsp_rails/indexing_enhancement_test.rb +++ b/test/ruby_lsp_rails/indexing_enhancement_test.rb @@ -12,8 +12,6 @@ class << self def populated_index @index ||= begin index = RubyIndexer::Index.new - indexing_enhancement = IndexingEnhancement.new(index) - index.register_enhancement(indexing_enhancement) index.index_all index end