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

Implement document symbol for associations #293

Merged
merged 1 commit into from
Mar 15, 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
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
Loading