From 7808ae29324a782fe9d9e050bfe8a68be2ff56a0 Mon Sep 17 00:00:00 2001 From: Hungle2911 <122929293+Hungle2911@users.noreply.github.com> Date: Thu, 12 Dec 2024 19:21:20 -0500 Subject: [PATCH 1/2] feat: Handle instance variable target nodes in document symbol --- lib/ruby_lsp/listeners/document_symbol.rb | 11 +++++++++ .../document_symbol_expectations_test.rb | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/ruby_lsp/listeners/document_symbol.rb b/lib/ruby_lsp/listeners/document_symbol.rb index 6e08c532e..7dfff122c 100644 --- a/lib/ruby_lsp/listeners/document_symbol.rb +++ b/lib/ruby_lsp/listeners/document_symbol.rb @@ -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, @@ -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( diff --git a/test/requests/document_symbol_expectations_test.rb b/test/requests/document_symbol_expectations_test.rb index e96c2523a..217a8f494 100644 --- a/test/requests/document_symbol_expectations_test.rb +++ b/test/requests/document_symbol_expectations_test.rb @@ -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 From cf61d5a67a529b176213cb74298657f08d93eb82 Mon Sep 17 00:00:00 2001 From: Hungle2911 <122929293+Hungle2911@users.noreply.github.com> Date: Fri, 13 Dec 2024 15:04:55 -0500 Subject: [PATCH 2/2] fix: use location method for instance variable target nodes range --- lib/ruby_lsp/listeners/document_symbol.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ruby_lsp/listeners/document_symbol.rb b/lib/ruby_lsp/listeners/document_symbol.rb index 7dfff122c..49a38b746 100644 --- a/lib/ruby_lsp/listeners/document_symbol.rb +++ b/lib/ruby_lsp/listeners/document_symbol.rb @@ -278,8 +278,8 @@ 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, + range_location: node.location, + selection_range_location: node.location, ) end