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

Fix highlight about ivar, cvar, and gvar #92

Merged
merged 1 commit into from
Sep 30, 2022
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
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