Skip to content

Commit

Permalink
feat: Handle instance variable target nodes in document symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Hungle2911 committed Dec 13, 2024
1 parent e5bb238 commit 7808ae2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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.name_loc,
selection_range_location: node.name_loc,
)
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

0 comments on commit 7808ae2

Please sign in to comment.