[ruby/prism] srange_find should only look on current line

https://github.com/ruby/prism/commit/3604aa15e7
This commit is contained in:
Kevin Newton 2024-04-23 15:19:48 -04:00 committed by git
parent 0defbc11a5
commit 81433fd0f5

View File

@ -839,7 +839,7 @@ module Prism
token(node.in_loc), token(node.in_loc),
pattern, pattern,
guard, guard,
srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset || node.location.end_offset, [";", "then"]), srange_find(node.pattern.location.end_offset, node.statements&.location&.start_offset, [";", "then"]),
visit(node.statements) visit(node.statements)
) )
end end
@ -1679,7 +1679,7 @@ module Prism
end end
# until foo; bar end # until foo; bar end
# ^^^^^^^^^^^^^^^^^ # ^^^^^^^^^^^^^^^^^^
# #
# bar until foo # bar until foo
# ^^^^^^^^^^^^^ # ^^^^^^^^^^^^^
@ -1712,7 +1712,7 @@ module Prism
if node.then_keyword_loc if node.then_keyword_loc
token(node.then_keyword_loc) token(node.then_keyword_loc)
else else
srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset || (node.conditions.last.location.end_offset + 1), [";"]) srange_find(node.conditions.last.location.end_offset, node.statements&.location&.start_offset, [";"])
end, end,
visit(node.statements) visit(node.statements)
) )
@ -1871,12 +1871,16 @@ module Prism
# Constructs a new source range by finding the given tokens between the # Constructs a new source range by finding the given tokens between the
# given start offset and end offset. If the needle is not found, it # given start offset and end offset. If the needle is not found, it
# returns nil. # returns nil. Importantly it does not search past newlines or comments.
#
# Note that end_offset is allowed to be nil, in which case this will
# search until the end of the string.
def srange_find(start_offset, end_offset, tokens) def srange_find(start_offset, end_offset, tokens)
tokens.find do |token| if (match = source_buffer.source.byteslice(start_offset...end_offset).match(/(\s*)(#{tokens.join("|")})/))
next unless (index = source_buffer.source.byteslice(start_offset...end_offset).index(token)) _, whitespace, token = *match
offset = start_offset + index token_offset = start_offset + whitespace.bytesize
return [token, Range.new(source_buffer, offset_cache[offset], offset_cache[offset + token.length])]
[token, Range.new(source_buffer, offset_cache[token_offset], offset_cache[token_offset + token.bytesize])]
end end
end end