Skip to content

Commit

Permalink
Merge pull request #293 from Shopify/as/document-symbol-associations
Browse files Browse the repository at this point in the history
Implement document symbol for associations
  • Loading branch information
aryan-soni authored Mar 15, 2024
2 parents d17f921 + 089b09d commit 7d12d96
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ There is no need to add the gem to your bundle.

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

## Documentation

Expand Down
5 changes: 3 additions & 2 deletions lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Rails
# ![Document Symbol demo](../../document_symbol.gif)
#
# The [document symbol](https://microsoft.github.io/language-server-protocol/specification#textDocument_documentSymbol)
# request allows users to navigate between ActiveSupport test cases with VS Code's "Go to Symbol" feature.
# request allows users to navigate between associations, validations, callbacks and ActiveSupport test cases with
# VS Code's "Go to Symbol" feature.
class DocumentSymbol
extend T::Sig
include Requests::Support::Common
Expand Down Expand Up @@ -100,7 +101,7 @@ def on_call_node_enter(node)
case message
when *CALLBACKS, "validate"
handle_all_arg_types(node, T.must(message))
when "validates", "validates!", "validates_each"
when "validates", "validates!", "validates_each", "belongs_to", "has_one", "has_many", "has_and_belongs_to_many"
handle_symbol_and_string_arg_types(node, T.must(message))
when "validates_with"
handle_class_arg_types(node, T.must(message))
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
class User < ApplicationRecord
before_create :foo_arg, -> () {}
validates :name, presence: true
has_one :profile
end
15 changes: 15 additions & 0 deletions test/ruby_lsp_rails/document_symbol_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ class FooModel < ApplicationRecord
assert_equal("validates_with(Foo::BarClass)", response[0].children[1].name)
end

test "correctly handles association callbacks with string and symbol argument types" do
response = generate_document_symbols_for_source(<<~RUBY)
class FooModel < ApplicationRecord
belongs_to :foo
belongs_to "baz"
end
RUBY

assert_equal(1, response.size)
assert_equal("FooModel", response[0].name)
assert_equal(2, response[0].children.size)
assert_equal("belongs_to(foo)", response[0].children[0].name)
assert_equal("belongs_to(baz)", response[0].children[1].name)
end

private

def generate_document_symbols_for_source(source)
Expand Down

0 comments on commit 7d12d96

Please sign in to comment.