Skip to content

Add scope to list of symbols #333

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

Merged
merged 5 commits into from
Apr 18, 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
46 changes: 43 additions & 3 deletions lib/ruby_lsp/ruby_lsp_rails/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ class DocumentSymbol
end
def initialize(response_builder, dispatcher)
@response_builder = response_builder

dispatcher.register(self, :on_call_node_enter)
@namespace_stack = T.let([], T::Array[String])

dispatcher.register(
self,
:on_call_node_enter,
:on_class_node_enter,
:on_class_node_leave,
:on_module_node_enter,
:on_module_node_leave,
)
end

sig { params(node: Prism::CallNode).void }
def on_call_node_enter(node)
return if @namespace_stack.empty?

content = extract_test_case_name(node)

if content
Expand All @@ -45,15 +55,45 @@ def on_call_node_enter(node)
when *Support::Callbacks::ALL, "validate"
handle_all_arg_types(node, T.must(message))
when "validates", "validates!", "validates_each", "belongs_to", "has_one", "has_many",
"has_and_belongs_to_many", "attr_readonly"
"has_and_belongs_to_many", "attr_readonly", "scope"
handle_symbol_and_string_arg_types(node, T.must(message))
when "validates_with"
handle_class_arg_types(node, T.must(message))
end
end

sig { params(node: Prism::ClassNode).void }
def on_class_node_enter(node)
add_to_namespace_stack(node)
end

sig { params(node: Prism::ClassNode).void }
def on_class_node_leave(node)
remove_from_namespace_stack(node)
end

sig { params(node: Prism::ModuleNode).void }
def on_module_node_enter(node)
add_to_namespace_stack(node)
end

sig { params(node: Prism::ModuleNode).void }
def on_module_node_leave(node)
remove_from_namespace_stack(node)
end

private

sig { params(node: T.any(Prism::ClassNode, Prism::ModuleNode)).void }
def add_to_namespace_stack(node)
@namespace_stack << node.constant_path.slice
end

sig { params(node: T.any(Prism::ClassNode, Prism::ModuleNode)).void }
def remove_from_namespace_stack(node)
@namespace_stack.delete(node.constant_path.slice)
end

sig { params(node: Prism::CallNode, message: String).void }
def handle_all_arg_types(node, message)
block = node.block
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class User < ApplicationRecord
before_create :foo, -> () {}
validates :name, presence: true
has_one :profile
scope :adult, -> { where(age: 18..) }

attr_readonly :last_name

private

Expand Down
46 changes: 46 additions & 0 deletions test/ruby_lsp_rails/document_symbol_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,52 @@ class FooModel < ApplicationRecord
assert_equal("attr_readonly :foo", response[0].children[0].name)
end

test "correctly handles scope in a class" do
response = generate_document_symbols_for_source(<<~RUBY)
class FooModel < ApplicationRecord
scope :bar, ->{ where(b: 2).order(:c) }
scope :foo, ->{ where(a: 1).order(:b) }
end
RUBY

assert_equal(1, response.size)
assert_equal("FooModel", response[0].name)
assert_equal(2, response[0].children.size)
assert_equal("scope :bar", response[0].children[0].name)
assert_equal("scope :foo", response[0].children[1].name)
end

test "correctly handles scope in a concern" do
response = generate_document_symbols_for_source(<<~RUBY)
module Scopable
extend ActiveSupport::Concern

included do
scope :bar, ->{ where(b: 2).order(:c) }
scope :foo, ->{ where(a: 1).order(:b) }
end
end
RUBY

assert_equal(1, response.size)
assert_equal("Scopable", response[0].name)
assert_equal(2, response[0].children.size)
assert_equal("scope :bar", response[0].children[0].name)
assert_equal("scope :foo", response[0].children[1].name)
end

test "ignores scope in a routes file" do
response = generate_document_symbols_for_source(<<~RUBY)
Rails.application.routes.draw do
scope '/admin' do
resources :users
end
end
RUBY

assert_equal(0, response.size)
end

test "correctly handles model callbacks with multiple string arguments" do
response = generate_document_symbols_for_source(<<~RUBY)
class FooModel < ApplicationRecord
Expand Down