Skip to content

Commit 95a852e

Browse files
committed
Support shadowing on highlight
1 parent 937db17 commit 95a852e

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

lib/rucoa/handlers/text_document_document_highlight_handler.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ def nodes
427427
].compact
428428
end
429429

430-
# @todo Support shadowing.
431430
# @return [Enumerable<Rucoa::Nodes::LvarNode>]
432431
def reference_nodes
433432
return [] unless assignment_node
@@ -446,13 +445,30 @@ def reference_nodes
446445
node,
447446
*node.descendants
448447
]
448+
end.take_while do |node| # FIXME: flat_map and take_while are not correct solution for shadowing.
449+
case node
450+
when Nodes::ArgNode, Nodes::LvasgnNode
451+
node.equal?(assignment_node) || node.name != assignment_node.name
452+
else
453+
true
454+
end
449455
end.select do |node|
450456
case node
451457
when Nodes::LvarNode
452458
node.name == @node.name
453459
end
454460
end
455461
end
462+
463+
class UnshadowedNodeVisitor
464+
def initialize(
465+
node:,
466+
&block
467+
)
468+
@block = block
469+
@node = node
470+
end
471+
end
456472
end
457473

458474
class ModuleMapper < Base

spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,5 +1335,108 @@ def a
13351335
)
13361336
end
13371337
end
1338+
1339+
context 'when local variable is shadowed by local variable assignment' do
1340+
let(:content) do
1341+
<<~RUBY
1342+
a = 1
1343+
b = 2
1344+
a
1345+
a = 2
1346+
a
1347+
RUBY
1348+
end
1349+
1350+
let(:position) do
1351+
Rucoa::Position.new(
1352+
column: 0,
1353+
line: 1
1354+
)
1355+
end
1356+
1357+
it 'returns highlights' do
1358+
subject
1359+
expect(server.responses).to match(
1360+
[
1361+
hash_including(
1362+
'id' => 1,
1363+
'result' => Array.new(2) do
1364+
a_kind_of(Hash)
1365+
end
1366+
)
1367+
]
1368+
)
1369+
end
1370+
end
1371+
1372+
context 'when local variable is shadowed by block argument' do
1373+
let(:content) do
1374+
<<~RUBY
1375+
a = 1
1376+
a
1377+
foo do |a|
1378+
a
1379+
end
1380+
RUBY
1381+
end
1382+
1383+
let(:position) do
1384+
Rucoa::Position.new(
1385+
column: 0,
1386+
line: 1
1387+
)
1388+
end
1389+
1390+
it 'returns highlights' do
1391+
subject
1392+
expect(server.responses).to match(
1393+
[
1394+
hash_including(
1395+
'id' => 1,
1396+
'result' => Array.new(2) do
1397+
a_kind_of(Hash)
1398+
end
1399+
)
1400+
]
1401+
)
1402+
end
1403+
end
1404+
1405+
context 'when local variable is shadowed but restored' do
1406+
before do
1407+
pending 'not implemented yet'
1408+
end
1409+
1410+
let(:content) do
1411+
<<~RUBY
1412+
a = 1
1413+
foo do |a|
1414+
a
1415+
end
1416+
a
1417+
RUBY
1418+
end
1419+
1420+
let(:position) do
1421+
Rucoa::Position.new(
1422+
column: 0,
1423+
line: 1
1424+
)
1425+
end
1426+
1427+
it 'returns highlights' do
1428+
subject
1429+
expect(server.responses).to match(
1430+
[
1431+
hash_including(
1432+
'id' => 1,
1433+
'result' => Array.new(2) do
1434+
a_kind_of(Hash)
1435+
end
1436+
)
1437+
]
1438+
)
1439+
end
1440+
end
13381441
end
13391442
end

0 commit comments

Comments
 (0)