Skip to content

Commit 3604aa1

Browse files
committed
srange_find should only look on current line
1 parent 2ae6b04 commit 3604aa1

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ def visit_in_node(node)
839839
token(node.in_loc),
840840
pattern,
841841
guard,
842-
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset || node.location.end_offset, [";", "then"]),
842+
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, [";", "then"]),
843843
visit(node.statements)
844844
)
845845
end
@@ -1679,7 +1679,7 @@ def visit_unless_node(node)
16791679
end
16801680

16811681
# until foo; bar end
1682-
# ^^^^^^^^^^^^^^^^^
1682+
# ^^^^^^^^^^^^^^^^^^
16831683
#
16841684
# bar until foo
16851685
# ^^^^^^^^^^^^^
@@ -1712,7 +1712,7 @@ def visit_when_node(node)
17121712
if node.then_keyword_loc
17131713
token(node.then_keyword_loc)
17141714
else
1715-
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset || (node.conditions.last.location.end_offset + 1), [";"])
1715+
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset, [";"])
17161716
end,
17171717
visit(node.statements)
17181718
)
@@ -1871,12 +1871,16 @@ def srange_offsets(start_offset, end_offset)
18711871

18721872
# Constructs a new source range by finding the given tokens between the
18731873
# given start offset and end offset. If the needle is not found, it
1874-
# returns nil.
1874+
# returns nil. Importantly it does not search past newlines or comments.
1875+
#
1876+
# Note that end_offset is allowed to be nil, in which case this will
1877+
# search until the end of the string.
18751878
def srange_find(start_offset, end_offset, tokens)
1876-
tokens.find do |token|
1877-
next unless (index = source_buffer.source.byteslice(start_offset...end_offset).index(token))
1878-
offset = start_offset + index
1879-
return [token, Range.new(source_buffer, offset_cache[offset], offset_cache[offset + token.length])]
1879+
if (match = source_buffer.source.byteslice(start_offset...end_offset).match(/(\s*)(#{tokens.join("|")})/))
1880+
_, whitespace, token = *match
1881+
token_offset = start_offset + whitespace.bytesize
1882+
1883+
[token, Range.new(source_buffer, offset_cache[token_offset], offset_cache[token_offset + token.bytesize])]
18801884
end
18811885
end
18821886

0 commit comments

Comments
 (0)