lib/prism/parse_result.rb in prism-0.17.1 vs lib/prism/parse_result.rb in prism-0.18.0
- old
+ new
@@ -23,44 +23,54 @@
@offsets = offsets
end
# Perform a byteslice on the source code using the given byte offset and
# byte length.
- def slice(offset, length)
- source.byteslice(offset, length)
+ def slice(byte_offset, length)
+ source.byteslice(byte_offset, length)
end
# Binary search through the offsets to find the line number for the given
# byte offset.
- def line(value)
- start_line + find_line(value)
+ def line(byte_offset)
+ start_line + find_line(byte_offset)
end
# Return the byte offset of the start of the line corresponding to the given
# byte offset.
- def line_offset(value)
- offsets[find_line(value)]
+ def line_start(byte_offset)
+ offsets[find_line(byte_offset)]
end
# Return the column number for the given byte offset.
- def column(value)
- value - offsets[find_line(value)]
+ def column(byte_offset)
+ byte_offset - line_start(byte_offset)
end
+ # Return the character offset for the given byte offset.
+ def character_offset(byte_offset)
+ source.byteslice(0, byte_offset).length
+ end
+
+ # Return the column number in characters for the given byte offset.
+ def character_column(byte_offset)
+ character_offset(byte_offset) - character_offset(line_start(byte_offset))
+ end
+
private
# Binary search through the offsets to find the line number for the given
# byte offset.
- def find_line(value)
+ def find_line(byte_offset)
left = 0
right = offsets.length - 1
while left <= right
mid = left + (right - left) / 2
- return mid if offsets[mid] == value
+ return mid if offsets[mid] == byte_offset
- if offsets[mid] < value
+ if offsets[mid] < byte_offset
left = mid + 1
else
right = mid - 1
end
end
@@ -119,23 +129,35 @@
# The source code that this location represents.
def slice
source.slice(start_offset, length)
end
+ # The character offset from the beginning of the source where this location
+ # starts.
+ def start_character_offset
+ source.character_offset(start_offset)
+ end
+
# The byte offset from the beginning of the source where this location ends.
def end_offset
start_offset + length
end
+ # The character offset from the beginning of the source where this location
+ # ends.
+ def end_character_offset
+ source.character_offset(end_offset)
+ end
+
# The line number where this location starts.
def start_line
source.line(start_offset)
end
# The content of the line where this location starts before this location.
def start_line_slice
- offset = source.line_offset(start_offset)
+ offset = source.line_start(start_offset)
source.slice(offset, start_offset - offset)
end
# The line number where this location ends.
def end_line
@@ -146,13 +168,25 @@
# the line.
def start_column
source.column(start_offset)
end
+ # The column number in characters where this location ends from the start of
+ # the line.
+ def start_character_column
+ source.character_column(start_offset)
+ end
+
# The column number in bytes where this location ends from the start of the
# line.
def end_column
source.column(end_offset)
+ end
+
+ # The column number in characters where this location ends from the start of
+ # the line.
+ def end_character_column
+ source.character_column(end_offset)
end
# Implement the hash pattern matching interface for Location.
def deconstruct_keys(keys)
{ start_offset: start_offset, end_offset: end_offset }