From d1bb858d476446ab82e6caa29c061a0da734c8eb Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 24 Oct 2023 12:28:53 -0400 Subject: [ruby/prism] Match existing Ruby prettyprint https://github.com/ruby/prism/commit/6d8358c083 --- lib/prism/parse_result.rb | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb index fd2737b20c..00ae3aaa1a 100644 --- a/lib/prism/parse_result.rb +++ b/lib/prism/parse_result.rb @@ -16,16 +16,32 @@ module Prism source.byteslice(offset, length) end + # Binary search through the offsets to find the line number for the given + # offset. def line(value) - offsets.bsearch_index { |offset| offset > value } || offsets.length + left = 0 + right = offsets.length - 1 + + while left <= right + mid = left + (right - left) / 2 + return mid if offsets[mid] == value + + if offsets[mid] < value + left = mid + 1 + else + right = mid - 1 + end + end + + left - 1 end def line_offset(value) - offsets[line(value) - 1] + offsets[line(value)] end def column(value) - value - offsets[line(value) - 1] + value - offsets[line(value)] end private @@ -86,7 +102,7 @@ module Prism # The line number where this location starts. def start_line - source.line(start_offset) + source.line(start_offset) + 1 end # The content of the line where this location starts before this location. @@ -97,7 +113,7 @@ module Prism # The line number where this location ends. def end_line - source.line(end_offset - 1) + source.line(end_offset) + 1 end # The column number in bytes where this location starts from the start of -- cgit v1.2.3