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 21, 2024
1 parent e77740c commit 1bbc5ce
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 189 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,3 @@ platforms :mingw, :x64_mingw, :mswin, :jruby do
gem "tzinfo"
gem "tzinfo-data"
end

gem "ruby-lsp", github: "Shopify/ruby-lsp", branch: "11-14-allow_indexing_enhancements_to_create_namespaces"
23 changes: 6 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
GIT
remote: https://github.com/Shopify/ruby-lsp.git
revision: bb3a5b25da4ba2931278fdeb1f0fbdf40b8545c3
branch: 11-14-allow_indexing_enhancements_to_create_namespaces
specs:
ruby-lsp (0.21.4)
language_server-protocol (~> 3.17.0)
prism (>= 1.2, < 2.0)
rbs (>= 3, < 4)
sorbet-runtime (>= 0.5.10782)

PATH
remote: .
specs:
ruby-lsp-rails (0.3.26)
ruby-lsp (>= 0.21.2, < 0.22.0)
ruby-lsp (>= 0.22.0, < 0.23.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -140,8 +129,6 @@ GEM
nio4r (2.7.4)
nokogiri (1.16.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x64-mingw-ucrt)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-linux)
Expand Down Expand Up @@ -225,6 +212,11 @@ GEM
rubocop (~> 1.51)
rubocop-sorbet (0.8.7)
rubocop (>= 1)
ruby-lsp (0.22.0)
language_server-protocol (~> 3.17.0)
prism (>= 1.2, < 2.0)
rbs (>= 3, < 4)
sorbet-runtime (>= 0.5.10782)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
securerandom (0.3.1)
Expand All @@ -242,7 +234,6 @@ GEM
sorbet-static-and-runtime (>= 0.5.10187)
thor (>= 0.19.2)
sqlite3 (2.1.0-arm64-darwin)
sqlite3 (2.1.0-x64-mingw-ucrt)
sqlite3 (2.1.0-x86_64-darwin)
sqlite3 (2.1.0-x86_64-linux-gnu)
stringio (3.1.2)
Expand Down Expand Up @@ -275,7 +266,6 @@ GEM

PLATFORMS
arm64-darwin
x64-mingw-ucrt
x86_64-darwin
x86_64-linux

Expand All @@ -289,7 +279,6 @@ DEPENDENCIES
rubocop-rake (~> 0.6.0)
rubocop-shopify (~> 2.15)
rubocop-sorbet (~> 0.8)
ruby-lsp!
ruby-lsp-rails!
sorbet-static-and-runtime
sqlite3
Expand Down
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
2 changes: 1 addition & 1 deletion ruby-lsp-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
Dir["lib/**/*", "LICENSE.txt", "Rakefile", "README.md"]
end

spec.add_dependency("ruby-lsp", ">= 0.21.2", "< 0.22.0")
spec.add_dependency("ruby-lsp", ">= 0.22.0", "< 0.23.0")
end
Loading

0 comments on commit 1bbc5ce

Please sign in to comment.