Skip to content

Commit 089b09d

Browse files
author
Aryan Soni
committed
Implement document symbol for associations
1 parent 46b97ce commit 089b09d

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ There is no need to add the gem to your bundle.
1616

1717
* Hover over an ActiveRecord model to reveal its schema.
1818
* Run or debug a test by clicking on the code lens which appears above the test class, or an individual test.
19-
* Navigate to validations, callbacks and test cases using your editor's "Go to Symbol" feature, or outline view.
19+
* Navigate to associations, validations, callbacks and test cases using your editor's "Go to Symbol" feature, or outline view.
2020

2121
## Documentation
2222

lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Rails
66
# ![Document Symbol demo](../../document_symbol.gif)
77
#
88
# The [document symbol](https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol)
9-
# request allows users to navigate between ActiveSupport test cases with VS Code's "Go to Symbol" feature.
9+
# request allows users to navigate between associations, validations, callbacks and ActiveSupport test cases with
10+
# VS Code's "Go to Symbol" feature.
1011
class DocumentSymbol
1112
extend T::Sig
1213
include Requests::Support::Common
@@ -100,7 +101,7 @@ def on_call_node_enter(node)
100101
case message
101102
when *CALLBACKS, "validate"
102103
handle_all_arg_types(node, T.must(message))
103-
when "validates", "validates!", "validates_each"
104+
when "validates", "validates!", "validates_each", "belongs_to", "has_one", "has_many", "has_and_belongs_to_many"
104105
handle_symbol_and_string_arg_types(node, T.must(message))
105106
when "validates_with"
106107
handle_class_arg_types(node, T.must(message))

test/dummy/app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
class User < ApplicationRecord
55
before_create :foo_arg, -> () {}
66
validates :name, presence: true
7+
has_one :profile
78
end

test/ruby_lsp_rails/document_symbol_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,21 @@ class FooModel < ApplicationRecord
349349
assert_equal("validates_with(Foo::BarClass)", response[0].children[1].name)
350350
end
351351

352+
test "correctly handles association callbacks with string and symbol argument types" do
353+
response = generate_document_symbols_for_source(<<~RUBY)
354+
class FooModel < ApplicationRecord
355+
belongs_to :foo
356+
belongs_to "baz"
357+
end
358+
RUBY
359+
360+
assert_equal(1, response.size)
361+
assert_equal("FooModel", response[0].name)
362+
assert_equal(2, response[0].children.size)
363+
assert_equal("belongs_to(foo)", response[0].children[0].name)
364+
assert_equal("belongs_to(baz)", response[0].children[1].name)
365+
end
366+
352367
private
353368

354369
def generate_document_symbols_for_source(source)

0 commit comments

Comments
 (0)