Skip to content

Commit

Permalink
Merge pull request #90 from r7kamura/feature/highglight-shadowing
Browse files Browse the repository at this point in the history
Support shadowing on highlight
  • Loading branch information
r7kamura authored Sep 30, 2022
2 parents ec0eb2a + 79c47fb commit 78c54e9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/rucoa/handlers/text_document_document_highlight_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ def nodes
].compact
end

# @todo Support shadowing.
# @return [Enumerable<Rucoa::Nodes::LvarNode>]
def reference_nodes
return [] unless assignment_node
Expand All @@ -446,13 +445,30 @@ def reference_nodes
node,
*node.descendants
]
end.take_while do |node| # FIXME: flat_map and take_while are not correct solution for shadowing.
case node
when Nodes::ArgNode, Nodes::LvasgnNode
node.equal?(assignment_node) || node.name != assignment_node.name
else
true
end
end.select do |node|
case node
when Nodes::LvarNode
node.name == @node.name
end
end
end

class UnshadowedNodeVisitor
def initialize(
node:,
&block
)
@block = block
@node = node
end
end
end

class ModuleMapper < Base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1335,5 +1335,71 @@ def a
)
end
end

context 'when local variable is shadowed by local variable assignment' do
let(:content) do
<<~RUBY
a = 1
b = 2
a
a = 2
a
RUBY
end

let(:position) do
Rucoa::Position.new(
column: 0,
line: 1
)
end

it 'returns highlights' do
subject
expect(server.responses).to match(
[
hash_including(
'id' => 1,
'result' => Array.new(2) do
a_kind_of(Hash)
end
)
]
)
end
end

context 'when local variable is shadowed by block argument' do
let(:content) do
<<~RUBY
a = 1
a
foo do |a|
a
end
RUBY
end

let(:position) do
Rucoa::Position.new(
column: 0,
line: 1
)
end

it 'returns highlights' do
subject
expect(server.responses).to match(
[
hash_including(
'id' => 1,
'result' => Array.new(2) do
a_kind_of(Hash)
end
)
]
)
end
end
end
end

0 comments on commit 78c54e9

Please sign in to comment.