Skip to content

Commit

Permalink
Update for changes to enhancements API
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Nov 19, 2024
1 parent e77740c commit 0f39fa0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 54 deletions.
1 change: 0 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 27 additions & 51 deletions lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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|
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/dummy/app/models/concerns/verifiable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Verifiable
extend ActiveSupport::Concern

included do
# checks if a user is verified
def verified?
true
end
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class User < ApplicationRecord

attr_readonly :last_name

include Verifiable # an ActiveSupport::Concern

private

def foo
Expand Down
2 changes: 0 additions & 2 deletions test/ruby_lsp_rails/indexing_enhancement_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f39fa0

Please sign in to comment.