Skip to content

Support shadowing on highlight #90

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

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
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