Skip to content

Commit bd90879

Browse files
authored
Merge pull request #88 from r7kamura/feature/highlight-complex
Fix highlight bug on complex assignment pattern
2 parents d045bcd + d922f42 commit bd90879

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

lib/rucoa/handlers/text_document_document_highlight_handler.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,16 @@ def assignment_node
406406
break target if target
407407
else
408408
target = node.previous_siblings.reverse.find do |sibling|
409-
case sibling
410-
when Nodes::LvasgnNode
411-
sibling.name == @node.name
409+
lvasgn_node = [
410+
sibling,
411+
*sibling.descendants # sendの場合はblockの内部まで踏み込まない等の制限が必要
412+
].reverse.find do |sibling_or_sibling_descendant|
413+
case sibling_or_sibling_descendant
414+
when Nodes::LvasgnNode
415+
break sibling_or_sibling_descendant if sibling_or_sibling_descendant.name == @node.name
416+
end
412417
end
418+
break lvasgn_node if lvasgn_node
413419
end
414420
break target if target
415421
end
@@ -438,10 +444,11 @@ def reference_nodes
438444
when Nodes::ArgNode
439445
assignment_node.each_ancestor(:block, :def, :defs).first.body_children
440446
when Nodes::LvasgnNode
441-
[
442-
assignment_node,
443-
*assignment_node.next_siblings
444-
]
447+
(
448+
[assignment_node] + assignment_node.ancestors.take_while do |node|
449+
!SCOPE_BOUNDARY_NODE_TYPES.include?(node.type)
450+
end
451+
).flat_map(&:next_siblings)
445452
end.flat_map do |node|
446453
[
447454
node,

lib/rucoa/nodes/base.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ def ancestors
1919
each_ancestor.to_a
2020
end
2121

22+
# @return [Array<Rucoa::Nodes::Base>]
23+
def child_nodes
24+
each_child_node.to_a
25+
end
26+
2227
# @return [Array<Rucoa::Nodes::Base>]
2328
def descendants
2429
each_descendant.to_a
@@ -149,7 +154,7 @@ def namespace
149154
def next_siblings
150155
return [] unless parent
151156

152-
parent.children[(sibling_index + 1)..]
157+
parent.child_nodes[(sibling_index + 1)..]
153158
end
154159

155160
# @return [Rucoa::Nodes::Base, nil]
@@ -185,7 +190,7 @@ def parent=(node)
185190
def previous_siblings
186191
return [] unless parent
187192

188-
parent.children[0...sibling_index]
193+
parent.child_nodes[0...sibling_index]
189194
end
190195

191196
# @note Override.
@@ -224,7 +229,7 @@ def visit_descendants(
224229

225230
# @return [Integer, nil]
226231
def sibling_index
227-
parent&.children&.index do |child|
232+
parent&.child_nodes&.index do |child|
228233
child.eql?(self)
229234
end
230235
end

spec/rucoa/handlers/text_document_document_highlight_handler_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,5 +1271,37 @@ def a
12711271
)
12721272
end
12731273
end
1274+
1275+
context 'when local variable is referenced with complex assignment' do
1276+
let(:content) do
1277+
<<~RUBY
1278+
if foo
1279+
a = 1
1280+
end
1281+
a
1282+
RUBY
1283+
end
1284+
1285+
let(:position) do
1286+
Rucoa::Position.new(
1287+
column: 0,
1288+
line: 4
1289+
)
1290+
end
1291+
1292+
it 'returns highlights' do
1293+
subject
1294+
expect(server.responses).to match(
1295+
[
1296+
hash_including(
1297+
'id' => 1,
1298+
'result' => Array.new(2) do
1299+
a_kind_of(Hash)
1300+
end
1301+
)
1302+
]
1303+
)
1304+
end
1305+
end
12741306
end
12751307
end

0 commit comments

Comments
 (0)