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

feat: Handle instance variable target nodes in document symbol #2981

Merged
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
11 changes: 11 additions & 0 deletions lib/ruby_lsp/listeners/document_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def initialize(response_builder, uri, dispatcher)
:on_module_node_enter,
:on_module_node_leave,
:on_instance_variable_write_node_enter,
:on_instance_variable_target_node_enter,
:on_instance_variable_operator_write_node_enter,
:on_instance_variable_or_write_node_enter,
:on_instance_variable_and_write_node_enter,
Expand Down Expand Up @@ -272,6 +273,16 @@ def on_instance_variable_write_node_enter(node)
)
end

sig { params(node: Prism::InstanceVariableTargetNode).void }
def on_instance_variable_target_node_enter(node)
create_document_symbol(
name: node.name.to_s,
kind: Constant::SymbolKind::FIELD,
range_location: node.location,
selection_range_location: node.location,
)
end

sig { params(node: Prism::InstanceVariableOperatorWriteNode).void }
def on_instance_variable_operator_write_node_enter(node)
create_document_symbol(
Expand Down
23 changes: 23 additions & 0 deletions test/requests/document_symbol_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ def test_instance_variable_with_shorthand_assignment
assert_equal("@quux", T.must(response[4]).name)
end

def test_instance_variable_with_destructuring_assignment
source = <<~RUBY
@a, @b = [1, 2]
@c, @d, @e = [3, 4, 5]
RUBY
uri = URI("file:///fake.rb")

document = RubyLsp::RubyDocument.new(source: source, version: 1, uri: uri)

dispatcher = Prism::Dispatcher.new
listener = RubyLsp::Requests::DocumentSymbol.new(uri, dispatcher)
dispatcher.dispatch(document.parse_result.value)
response = listener.perform

assert_equal(5, response.size)

assert_equal("@a", T.must(response[0]).name)
assert_equal("@b", T.must(response[1]).name)
assert_equal("@c", T.must(response[2]).name)
assert_equal("@d", T.must(response[3]).name)
assert_equal("@e", T.must(response[4]).name)
end

def test_labels_blank_names
source = <<~RUBY
def
Expand Down
Loading