From 5a3f368c142dc7c6c4f367cbdfa702c3d7a534b1 Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Fri, 30 Sep 2022 10:14:11 +0900 Subject: [PATCH] Fix highlight about ivar, cvar, and gvar --- ...ext_document_document_highlight_handler.rb | 21 ++++++++++++--- lib/rucoa/node_concerns.rb | 1 + lib/rucoa/node_concerns/variable.rb | 26 +++++++++++++++++++ lib/rucoa/nodes/cvar_node.rb | 1 + lib/rucoa/nodes/cvasgn_node.rb | 1 + lib/rucoa/nodes/gvar_node.rb | 1 + lib/rucoa/nodes/gvasgn_node.rb | 1 + lib/rucoa/nodes/ivar_node.rb | 1 + lib/rucoa/nodes/ivasgn_node.rb | 1 + lib/rucoa/nodes/lvar_node.rb | 19 +------------- lib/rucoa/nodes/lvasgn_node.rb | 5 +--- ...ocument_document_highlight_handler_spec.rb | 13 ++++++++++ 12 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 lib/rucoa/node_concerns/variable.rb diff --git a/lib/rucoa/handlers/text_document_document_highlight_handler.rb b/lib/rucoa/handlers/text_document_document_highlight_handler.rb index cc3aee9..4d8a5b5 100644 --- a/lib/rucoa/handlers/text_document_document_highlight_handler.rb +++ b/lib/rucoa/handlers/text_document_document_highlight_handler.rb @@ -298,7 +298,11 @@ def global_variable_scopable_node # @return [Enumerable] def nodes - global_variable_scopable_node&.each_descendant(:gvar, :gvasgn) || [] + return [] unless global_variable_scopable_node + + global_variable_scopable_node.each_descendant(:gvar, :gvasgn).select do |node| + node.name == @node.name + end end end @@ -319,12 +323,17 @@ def call # @return [Rucoa::Nodes::Base, nil] def instance_variable_scopable_node - @node.each_ancestor(:class, :module).first + @instance_variable_scopable_node ||= @node.each_ancestor(:class, :module).first end + # @todo Stop descendant searching if scope boundary node is found (e.g. class, def, etc.). # @return [Enumerable] def nodes - instance_variable_scopable_node&.each_descendant(:ivar, :ivasgn) || [] + return [] unless instance_variable_scopable_node + + instance_variable_scopable_node.each_descendant(:ivar, :ivasgn).select do |node| + node.name == @node.name + end end end @@ -350,7 +359,11 @@ def instance_variable_scopable_node # @return [Enumerable] def nodes - instance_variable_scopable_node&.each_descendant(:cvar, :cvasgn) || [] + return [] unless instance_variable_scopable_node + + instance_variable_scopable_node.each_descendant(:cvar, :cvasgn).select do |node| + node.name == @node.name + end end end diff --git a/lib/rucoa/node_concerns.rb b/lib/rucoa/node_concerns.rb index 56f916e..30761a4 100644 --- a/lib/rucoa/node_concerns.rb +++ b/lib/rucoa/node_concerns.rb @@ -6,5 +6,6 @@ module NodeConcerns autoload :Modifier, 'rucoa/node_concerns/modifier' autoload :QualifiedName, 'rucoa/node_concerns/qualified_name' autoload :Rescue, 'rucoa/node_concerns/rescue' + autoload :Variable, 'rucoa/node_concerns/variable' end end diff --git a/lib/rucoa/node_concerns/variable.rb b/lib/rucoa/node_concerns/variable.rb new file mode 100644 index 0000000..3d7a19f --- /dev/null +++ b/lib/rucoa/node_concerns/variable.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Rucoa + module NodeConcerns + module Variable + # @return [String] + # @example returns variable name + # node = Rucoa::Source.new( + # content: <<~RUBY, + # foo = 1 + # foo + # RUBY + # uri: 'file:///path/to/example.rb' + # ).node_at( + # Rucoa::Position.new( + # column: 0, + # line: 2 + # ) + # ) + # expect(node.name).to eq('foo') + def name + children[0].to_s + end + end + end +end diff --git a/lib/rucoa/nodes/cvar_node.rb b/lib/rucoa/nodes/cvar_node.rb index 1c58b5e..ed6c1f1 100644 --- a/lib/rucoa/nodes/cvar_node.rb +++ b/lib/rucoa/nodes/cvar_node.rb @@ -3,6 +3,7 @@ module Rucoa module Nodes class CvarNode < Base + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/cvasgn_node.rb b/lib/rucoa/nodes/cvasgn_node.rb index f472002..8f050fc 100644 --- a/lib/rucoa/nodes/cvasgn_node.rb +++ b/lib/rucoa/nodes/cvasgn_node.rb @@ -3,6 +3,7 @@ module Rucoa module Nodes class CvasgnNode < Base + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/gvar_node.rb b/lib/rucoa/nodes/gvar_node.rb index 6fe4ba2..b21783b 100644 --- a/lib/rucoa/nodes/gvar_node.rb +++ b/lib/rucoa/nodes/gvar_node.rb @@ -3,6 +3,7 @@ module Rucoa module Nodes class GvarNode < Base + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/gvasgn_node.rb b/lib/rucoa/nodes/gvasgn_node.rb index bbfc04e..e1b3ec4 100644 --- a/lib/rucoa/nodes/gvasgn_node.rb +++ b/lib/rucoa/nodes/gvasgn_node.rb @@ -3,6 +3,7 @@ module Rucoa module Nodes class GvasgnNode < Base + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/ivar_node.rb b/lib/rucoa/nodes/ivar_node.rb index 60d837f..1966d76 100644 --- a/lib/rucoa/nodes/ivar_node.rb +++ b/lib/rucoa/nodes/ivar_node.rb @@ -3,6 +3,7 @@ module Rucoa module Nodes class IvarNode < Base + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/ivasgn_node.rb b/lib/rucoa/nodes/ivasgn_node.rb index d03c2b9..39439ce 100644 --- a/lib/rucoa/nodes/ivasgn_node.rb +++ b/lib/rucoa/nodes/ivasgn_node.rb @@ -3,6 +3,7 @@ module Rucoa module Nodes class IvasgnNode < Base + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/lvar_node.rb b/lib/rucoa/nodes/lvar_node.rb index dc7d37a..20b3dd8 100644 --- a/lib/rucoa/nodes/lvar_node.rb +++ b/lib/rucoa/nodes/lvar_node.rb @@ -3,24 +3,7 @@ module Rucoa module Nodes class LvarNode < Base - # @return [String] - # @example returns local variable name - # node = Rucoa::Source.new( - # content: <<~RUBY, - # foo = 1 - # foo - # RUBY - # uri: 'file:///path/to/example.rb' - # ).node_at( - # Rucoa::Position.new( - # column: 2, - # line: 2 - # ) - # ) - # expect(node.name).to eq('foo') - def name - children[0].to_s - end + include NodeConcerns::Variable end end end diff --git a/lib/rucoa/nodes/lvasgn_node.rb b/lib/rucoa/nodes/lvasgn_node.rb index 071b9e8..d73645d 100644 --- a/lib/rucoa/nodes/lvasgn_node.rb +++ b/lib/rucoa/nodes/lvasgn_node.rb @@ -3,10 +3,7 @@ module Rucoa module Nodes class LvasgnNode < Base - # @return [String] - def name - children[0].to_s - end + include NodeConcerns::Variable end end end diff --git a/spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb b/spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb index 2999755..5be954e 100644 --- a/spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb @@ -858,6 +858,10 @@ def foo def bar @x ||= 2 end + + def baz + @y = 3 + end end class B @@ -905,6 +909,10 @@ def foo def bar @@x ||= 2 end + + def baz + @@y = 3 + end end class B @@ -952,6 +960,10 @@ def foo def bar $x ||= 2 end + + def baz + $y = 3 + end end class B @@ -1097,6 +1109,7 @@ def b def a foo = 1 foo + baz end def b