Skip to content

Commit 9c4d411

Browse files
committed
Use NodeContext from RubyLSP to fix Definition targetting
1 parent 036cfd8 commit 9c4d411

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/ruby_lsp/ruby_lsp_rails/definition.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,26 @@ class Definition
4343
def initialize(client, response_builder, node_context, index, dispatcher)
4444
@client = client
4545
@response_builder = response_builder
46+
@node_context = node_context
4647
@nesting = T.let(node_context.nesting, T::Array[String])
4748
@index = index
4849

49-
dispatcher.register(self, :on_call_node_enter)
50+
dispatcher.register(self, :on_call_node_enter, :on_symbol_node_enter, :on_string_node_enter)
5051
end
5152

52-
sig { params(node: Prism::CallNode).void }
53-
def on_call_node_enter(node)
53+
sig { params(node: Prism::SymbolNode).void }
54+
def on_symbol_node_enter(node)
55+
handle_dsl(node) if @node_context.call_node
56+
end
57+
58+
sig { params(node: Prism::StringNode).void }
59+
def on_string_node_enter(node)
60+
handle_dsl(node) if @node_context.call_node
61+
end
62+
63+
sig { params(node: T.any(Prism::SymbolNode, Prism::StringNode)).void }
64+
def handle_dsl(node)
65+
node = T.must(@node_context.call_node)
5466
return unless self_receiver?(node)
5567

5668
message = node.message
@@ -61,7 +73,18 @@ def on_call_node_enter(node)
6173
handle_association(node)
6274
elsif Support::Callbacks::ALL.include?(message)
6375
handle_callback(node)
64-
elsif message.end_with?("_path") || message.end_with?("_url")
76+
end
77+
end
78+
79+
sig { params(node: Prism::CallNode).void }
80+
def on_call_node_enter(node)
81+
return unless self_receiver?(node)
82+
83+
message = node.message
84+
85+
return unless message
86+
87+
if message.end_with?("_path") || message.end_with?("_url")
6588
handle_route(node)
6689
end
6790
end

test/ruby_lsp_rails/definition_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module RubyLsp
77
module Rails
88
class DefinitionTest < ActiveSupport::TestCase
99
test "recognizes model callback with multiple symbol arguments" do
10-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 10 })
10+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 18 })
1111
# typed: false
1212
1313
class TestModel
@@ -34,7 +34,7 @@ def baz; end
3434
end
3535

3636
test "recognizes has_many model associations" do
37-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 4 })
37+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 14 })
3838
# typed: false
3939
4040
class Organization < ActiveRecord::Base
@@ -53,7 +53,7 @@ class Organization < ActiveRecord::Base
5353
end
5454

5555
test "recognizes belongs_to model associations" do
56-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 4 })
56+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 14 })
5757
# typed: false
5858
5959
class Membership < ActiveRecord::Base
@@ -72,7 +72,7 @@ class Membership < ActiveRecord::Base
7272
end
7373

7474
test "recognizes has_one model associations" do
75-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 4 })
75+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 11 })
7676
# typed: false
7777
7878
class User < ActiveRecord::Base
@@ -91,7 +91,7 @@ class User < ActiveRecord::Base
9191
end
9292

9393
test "recognizes has_and_belongs_to_many model associations" do
94-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 4 })
94+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 27 })
9595
# typed: false
9696
9797
class Profile < ActiveRecord::Base
@@ -110,7 +110,7 @@ class Profile < ActiveRecord::Base
110110
end
111111

112112
test "handles class_name argument for associations" do
113-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 4 })
113+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 11 })
114114
# typed: false
115115
116116
class User < ActiveRecord::Base
@@ -129,7 +129,7 @@ class User < ActiveRecord::Base
129129
end
130130

131131
test "recognizes controller callback with string argument" do
132-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 10 })
132+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 17 })
133133
# typed: false
134134
135135
class TestController
@@ -149,7 +149,7 @@ def foo; end
149149
end
150150

151151
test "recognizes job callback with string and symbol arguments" do
152-
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 10 })
152+
response = generate_definitions_for_source(<<~RUBY, { line: 3, character: 18 })
153153
# typed: false
154154
155155
class TestJob

0 commit comments

Comments
 (0)