class Prism::Location
This represents a location in the source.
Attributes
The length of this location in bytes.
A Source object that is used to determine more information from the given offset and length.
The byte offset from the beginning of the source where this location starts.
Public Class Methods
Source
# File lib/prism/parse_result.rb, line 402 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
Create a new location object with the given source, start byte offset, and byte length.
Public Instance Methods
Source
# File lib/prism/parse_result.rb, line 654 def ==(other) Location === other && other.start_offset == start_offset && other.end_offset == end_offset end
Returns true if the given other location is equal to this location.
Source
# File lib/prism/parse_result.rb, line 677 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
Join this location with the first occurrence of the string in the source that occurs after this location on the same line, and return the new location. This will raise an error if the string does not exist.
Source
# File lib/prism/parse_result.rb, line 633 def cached_end_code_units_column(cache) cache[end_offset] - cache[source.line_start(end_offset)] end
The end column in code units using the given cache to fetch or calculate the value.
Source
# File lib/prism/parse_result.rb, line 547 def cached_end_code_units_offset(cache) cache[end_offset] end
The end offset from the start of the file in code units using the given cache to fetch or calculate the value.
Source
# File lib/prism/parse_result.rb, line 601 def cached_start_code_units_column(cache) cache[start_offset] - cache[source.line_start(start_offset)] end
The start column in code units using the given cache to fetch or calculate the value.
Source
# File lib/prism/parse_result.rb, line 517 def cached_start_code_units_offset(cache) cache[start_offset] end
The start offset from the start of the file in code units using the given cache to fetch or calculate the value.
Source
# File lib/prism/parse_result.rb, line 462 def chop copy(length: length == 0 ? length : length - 1) end
Returns a new location that is the result of chopping off the last byte.
Source
# File lib/prism/parse_result.rb, line 448 def comments [*@leading_comments, *@trailing_comments] #: Array[Comment] end
Returns all comments that are associated with this location (both leading and trailing comments).
Source
# File lib/prism/parse_result.rb, line 455 def copy(source: self.source, start_offset: self.start_offset, length: self.length) Location.new(source, start_offset, length) end
Create a new location object with the given options.
Source
# File lib/prism/parse_result.rb, line 617 def end_character_column source.character_column(end_offset) end
The column in characters where this location ends from the start of the line.
Source
# File lib/prism/parse_result.rb, line 532 def end_character_offset source.character_offset(end_offset) end
The character offset from the beginning of the source where this location ends.
Source
# File lib/prism/parse_result.rb, line 625 def end_code_units_column(encoding = Encoding::UTF_16LE) source.code_units_column(end_offset, encoding) end
The column in code units of the given encoding where this location ends from the start of the line.
Source
# File lib/prism/parse_result.rb, line 539 def end_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(end_offset, encoding) end
The offset from the start of the file in code units of the given encoding.
Source
# File lib/prism/parse_result.rb, line 609 def end_column source.column(end_offset) end
The column in bytes where this location ends from the start of the line.
Source
# File lib/prism/parse_result.rb, line 569 def end_line source.line(end_offset) end
The line number where this location ends.
Source
# File lib/prism/parse_result.rb, line 524 def end_offset start_offset + length end
The byte offset from the beginning of the source where this location ends.
Source
# File lib/prism/parse_result.rb, line 665 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
Returns a new location that stretches from this location to the given other location. Raises an error if this location is not before the other location or if they donβt share the same source.
Source
# File lib/prism/parse_result.rb, line 425 def leading_comment(comment) leading_comments << comment end
Attach a comment to the leading comments of this location.
Source
# File lib/prism/parse_result.rb, line 418 def leading_comments @leading_comments ||= [] end
These are the comments that are associated with this location that exist before the start of this location.
Source
# File lib/prism/parse_result.rb, line 483 def slice source.slice(start_offset, length) end
The source code that this location represents.
Source
# File lib/prism/parse_result.rb, line 492 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
The source code that this location represents starting from the beginning of the line that this location starts on to the end of the line that this location ends on.
Source
# File lib/prism/parse_result.rb, line 476 def source_lines source.lines end
Returns all of the lines of the source code associated with this location.
Source
# File lib/prism/parse_result.rb, line 585 def start_character_column source.character_column(start_offset) end
The column in characters where this location ends from the start of the line.
Source
# File lib/prism/parse_result.rb, line 502 def start_character_offset source.character_offset(start_offset) end
The character offset from the beginning of the source where this location starts.
Source
# File lib/prism/parse_result.rb, line 593 def start_code_units_column(encoding = Encoding::UTF_16LE) source.code_units_column(start_offset, encoding) end
The column in code units of the given encoding where this location starts from the start of the line.
Source
# File lib/prism/parse_result.rb, line 509 def start_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(start_offset, encoding) end
The offset from the start of the file in code units of the given encoding.
Source
# File lib/prism/parse_result.rb, line 577 def start_column source.column(start_offset) end
The column in bytes where this location starts from the start of the line.
Source
# File lib/prism/parse_result.rb, line 554 def start_line source.line(start_offset) end
The line number where this location starts.
Source
# File lib/prism/parse_result.rb, line 561 def start_line_slice offset = source.line_start(start_offset) source.slice(offset, start_offset - offset) end
The content of the line where this location starts before this location.
Source
# File lib/prism/parse_result.rb, line 440 def trailing_comment(comment) trailing_comments << comment end
Attach a comment to the trailing comments of this location.
Source
# File lib/prism/parse_result.rb, line 433 def trailing_comments @trailing_comments ||= [] end
These are the comments that are associated with this location that exist after the end of this location.