Skip to content

Commit

Permalink
Merge pull request #92 from r7kamura/feature/highlight-fix
Browse files Browse the repository at this point in the history
Fix highlight about ivar, cvar, and gvar
  • Loading branch information
r7kamura authored Sep 30, 2022
2 parents 78c54e9 + 5a3f368 commit bb8cb82
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 26 deletions.
21 changes: 17 additions & 4 deletions lib/rucoa/handlers/text_document_document_highlight_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ def global_variable_scopable_node

# @return [Enumerable<Rucoa::Nodes::Base>]
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

Expand All @@ -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<Rucoa::Nodes::Base>]
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

Expand All @@ -350,7 +359,11 @@ def instance_variable_scopable_node

# @return [Enumerable<Rucoa::Nodes::Base>]
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

Expand Down
1 change: 1 addition & 0 deletions lib/rucoa/node_concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions lib/rucoa/node_concerns/variable.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/rucoa/nodes/cvar_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Rucoa
module Nodes
class CvarNode < Base
include NodeConcerns::Variable
end
end
end
1 change: 1 addition & 0 deletions lib/rucoa/nodes/cvasgn_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Rucoa
module Nodes
class CvasgnNode < Base
include NodeConcerns::Variable
end
end
end
1 change: 1 addition & 0 deletions lib/rucoa/nodes/gvar_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Rucoa
module Nodes
class GvarNode < Base
include NodeConcerns::Variable
end
end
end
1 change: 1 addition & 0 deletions lib/rucoa/nodes/gvasgn_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Rucoa
module Nodes
class GvasgnNode < Base
include NodeConcerns::Variable
end
end
end
1 change: 1 addition & 0 deletions lib/rucoa/nodes/ivar_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Rucoa
module Nodes
class IvarNode < Base
include NodeConcerns::Variable
end
end
end
1 change: 1 addition & 0 deletions lib/rucoa/nodes/ivasgn_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Rucoa
module Nodes
class IvasgnNode < Base
include NodeConcerns::Variable
end
end
end
19 changes: 1 addition & 18 deletions lib/rucoa/nodes/lvar_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions lib/rucoa/nodes/lvasgn_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,10 @@ def foo
def bar
@x ||= 2
end
def baz
@y = 3
end
end
class B
Expand Down Expand Up @@ -905,6 +909,10 @@ def foo
def bar
@@x ||= 2
end
def baz
@@y = 3
end
end
class B
Expand Down Expand Up @@ -952,6 +960,10 @@ def foo
def bar
$x ||= 2
end
def baz
$y = 3
end
end
class B
Expand Down Expand Up @@ -1097,6 +1109,7 @@ def b
def a
foo = 1
foo
baz
end
def b
Expand Down

0 comments on commit bb8cb82

Please sign in to comment.