class Prism::Location
This represents a location in the source.
def ==(other)
def ==(other) Location === other && other.start_offset == start_offset && other.end_offset == end_offset end
def adjoin(string)
that occurs after this location on the same line, and return the new
Join this location with the first occurrence of the string in the source
def adjoin(string) line_suffix = source.slice(end_offset, source.line_end(end_offset) - end_offset) line_suffix_index = line_suffix.byteindex(string) raise "Could not find #{string}" if line_suffix_index.nil? Location.new(source, start_offset, length + line_suffix_index + string.bytesize) end
def chop
def chop copy(length: length == 0 ? length : length - 1) end
def comments
Returns all comments that are associated with this location (both leading
def comments [*@leading_comments, *@trailing_comments] end
def copy(source: self.source, start_offset: self.start_offset, length: self.length)
def copy(source: self.source, start_offset: self.start_offset, length: self.length) Location.new(source, start_offset, length) end
def deconstruct_keys(keys)
def deconstruct_keys(keys) { start_offset: start_offset, end_offset: end_offset } end
def end_character_column
The column number in characters where this location ends from the start of
def end_character_column source.character_column(end_offset) end
def end_character_offset
The character offset from the beginning of the source where this location
def end_character_offset source.character_offset(end_offset) end
def end_code_units_column(encoding = Encoding::UTF_16LE)
The column number in code units of the given encoding where this location
def end_code_units_column(encoding = Encoding::UTF_16LE) source.code_units_column(end_offset, encoding) end
def end_code_units_offset(encoding = Encoding::UTF_16LE)
def end_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(end_offset, encoding) end
def end_column
The column number in bytes where this location ends from the start of the
def end_column source.column(end_offset) end
def end_line
def end_line source.line(end_offset) end
def end_offset
def end_offset start_offset + length end
def initialize(source, start_offset, length)
Create a new location object with the given source, start byte offset, and
def initialize(source, start_offset, length) @source = source @start_offset = start_offset @length = length # These are used to store comments that are associated with this location. # They are initialized to `nil` to save on memory when there are no # comments to be attached and/or the comment-related APIs are not used. @leading_comments = nil @trailing_comments = nil end
def inspect
def inspect "#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>" end
def join(other)
other location. Raises an error if this location is not before the other
Returns a new location that stretches from this location to the given
def join(other) raise "Incompatible sources" if source != other.source raise "Incompatible locations" if start_offset > other.start_offset Location.new(source, start_offset, other.end_offset - start_offset) end
def leading_comment(comment)
def leading_comment(comment) leading_comments << comment end
def leading_comments
These are the comments that are associated with this location that exist
def leading_comments @leading_comments ||= [] end
def pretty_print(q)
def pretty_print(q) q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})") end
def slice
def slice source.slice(start_offset, length) end
def slice_lines
of the line that this location starts on to the end of the line that this
The source code that this location represents starting from the beginning
def slice_lines line_start = source.line_start(start_offset) line_end = source.line_end(end_offset) source.slice(line_start, line_end - line_start) end
def source_lines
def source_lines source.lines end
def start_character_column
The column number in characters where this location ends from the start of
def start_character_column source.character_column(start_offset) end
def start_character_offset
The character offset from the beginning of the source where this location
def start_character_offset source.character_offset(start_offset) end
def start_code_units_column(encoding = Encoding::UTF_16LE)
The column number in code units of the given encoding where this location
def start_code_units_column(encoding = Encoding::UTF_16LE) source.code_units_column(start_offset, encoding) end
def start_code_units_offset(encoding = Encoding::UTF_16LE)
def start_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(start_offset, encoding) end
def start_column
The column number in bytes where this location starts from the start of
def start_column source.column(start_offset) end
def start_line
def start_line source.line(start_offset) end
def start_line_slice
def start_line_slice offset = source.line_start(start_offset) source.slice(offset, start_offset - offset) end
def trailing_comment(comment)
def trailing_comment(comment) trailing_comments << comment end
def trailing_comments
These are the comments that are associated with this location that exist
def trailing_comments @trailing_comments ||= [] end