Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concern indexing enhancement #422

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PATH
remote: .
specs:
ruby-lsp-rails (0.3.11)
ruby-lsp (>= 0.17.11, < 0.18.0)
ruby-lsp (>= 0.17.12, < 0.18.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -234,7 +234,7 @@ GEM
rubocop (~> 1.51)
rubocop-sorbet (0.8.3)
rubocop (>= 0.90.0)
ruby-lsp (0.17.11)
ruby-lsp (0.17.12)
language_server-protocol (~> 3.17.0)
prism (>= 0.29.0, < 0.31)
rbs (>= 3, < 4)
Expand Down
3 changes: 3 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative "code_lens"
require_relative "document_symbol"
require_relative "definition"
require_relative "indexing_enhancement"

module RubyLsp
module Rails
Expand All @@ -35,6 +36,8 @@ def activate(global_state, message_queue)
# Start booting the real client in a background thread. Until this completes, the client will be a NullClient
Thread.new { @client = RunnerClient.create_client }
register_additional_file_watchers(global_state: global_state, message_queue: message_queue)

T.must(@global_state).index.register_enhancement(IndexingEnhancement.new)
end

sig { override.void }
Expand Down
63 changes: 63 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/indexing_enhancement.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# typed: strict
# frozen_string_literal: true

module RubyLsp
module Rails
class IndexingEnhancement
extend T::Sig
include RubyIndexer::Enhancement

sig do
override.params(
index: RubyIndexer::Index,
owner: T.nilable(RubyIndexer::Entry::Namespace),
node: Prism::CallNode,
file_path: String,
).void
end
def on_call_node(index, owner, node, file_path)
return unless owner

name = node.name

case name
when :extend
handle_concern_extend(index, owner, node)
end
end

private

sig do
params(
index: RubyIndexer::Index,
owner: RubyIndexer::Entry::Namespace,
node: Prism::CallNode,
).void
end
def handle_concern_extend(index, owner, node)
arguments = node.arguments&.arguments
return unless arguments

arguments.each do |node|
next unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)

module_name = node.full_name
next unless module_name == "ActiveSupport::Concern"

index.register_included_hook(owner.name) do |index, base|
class_methods_name = "#{owner.name}::ClassMethods"

if index.indexed?(class_methods_name)
singleton = index.existing_or_new_singleton_class(base.name)
singleton.mixin_operations << RubyIndexer::Entry::Include.new(class_methods_name)
end
end
rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError,
Prism::ConstantPathNode::MissingNodesInConstantPathError
# Do nothing
end
end
end
end
end
2 changes: 1 addition & 1 deletion ruby-lsp-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
Dir["lib/**/*", "LICENSE.txt", "Rakefile", "README.md"]
end

spec.add_dependency("ruby-lsp", ">= 0.17.11", "< 0.18.0")
spec.add_dependency("ruby-lsp", ">= 0.17.12", "< 0.18.0")
end
Loading
Loading