Class: Prism::Location
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/prism/parse_result.rb, ext/prism/extension.c |
Overview
This represents a location in the source.
Class Method Summary
-
.new(source, start_offset, length) ⇒ Location
constructor
Create a new location object with the given source, start byte offset, and byte length.
Instance Attribute Summary
-
#length
readonly
The length of this location in bytes.
-
#start_offset
readonly
The byte offset from the beginning of the source where this location starts.
-
#source
readonly
protected
A Source object that is used to determine more information from the given offset and length.
Instance Method Summary
-
#==(other)
Returns true if the given other location is equal to this location.
-
#adjoin(string)
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.
-
#cached_end_code_units_column(cache)
The end column in code units using the given cache to fetch or calculate the value.
-
#cached_end_code_units_offset(cache)
The end offset from the start of the file in code units using the given cache to fetch or calculate the value.
-
#cached_start_code_units_column(cache)
The start column in code units using the given cache to fetch or calculate the value.
-
#cached_start_code_units_offset(cache)
The start offset from the start of the file in code units using the given cache to fetch or calculate the value.
-
#chop
Returns a new location that is the result of chopping off the last byte.
-
#copy(source: self.source, start_offset: self.start_offset, length: self.length)
Create a new location object with the given options.
-
#end_character_column
The column in characters where this location ends from the start of the line.
-
#end_character_offset
The character offset from the beginning of the source where this location ends.
-
#end_code_units_column(encoding = Encoding::UTF_16LE)
The column in code units of the given encoding where this location ends from the start of the line.
-
#end_code_units_offset(encoding = Encoding::UTF_16LE)
The offset from the start of the file in code units of the given encoding.
-
#end_column
The column in bytes where this location ends from the start of the line.
-
#end_line
The line number where this location ends.
-
#end_offset
The byte offset from the beginning of the source where this location ends.
-
#join(other)
Returns a new location that stretches from this location to the given other location.
-
#slice
The source code that this location represents.
-
#slice_lines
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_lines
Returns all of the lines of the source code associated with this location.
-
#start_character_column
The column in characters where this location ends from the start of the line.
-
#start_character_offset
The character offset from the beginning of the source where this location starts.
-
#start_code_units_column(encoding = Encoding::UTF_16LE)
The column in code units of the given encoding where this location starts from the start of the line.
-
#start_code_units_offset(encoding = Encoding::UTF_16LE)
The offset from the start of the file in code units of the given encoding.
-
#start_column
The column in bytes where this location starts from the start of the line.
-
#start_line
The line number where this location starts.
-
#start_line_slice
The content of the line where this location starts before this location.
-
#deconstruct_keys(keys)
Internal use only
Implement the hash pattern matching interface for
Location. -
#inspect
Internal use only
Returns a string representation of this location.
-
#pretty_print(q)
Internal use only
Implement the pretty print interface for
Location.
Constructor Details
.new(source, start_offset, length) ⇒ Location
Create a new location object with the given source, start byte offset, and byte length.
: (Source source, Integer start_offset, Integer length) -> void
# File 'lib/prism/parse_result.rb', line 468
def initialize(source, start_offset, length) @source = source @start_offset = start_offset @length = length end
Instance Attribute Details
#length (readonly)
The length of this location in bytes.
# File 'lib/prism/parse_result.rb', line 462
attr_reader :length #: Integer
#source (readonly, protected)
A Source object that is used to determine more information from the given offset and length.
# File 'lib/prism/parse_result.rb', line 454
attr_reader :source #: Source
#start_offset (readonly)
The byte offset from the beginning of the source where this location starts.
# File 'lib/prism/parse_result.rb', line 459
attr_reader :start_offset #: Integer
Instance Method Details
#==(other)
Returns true if the given other location is equal to this location.
: (untyped other) -> bool
# File 'lib/prism/parse_result.rb', line 676
def ==(other) Location === other && other.start_offset == start_offset && other.end_offset == end_offset end
#adjoin(string)
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.
: (String string) -> Location
# File 'lib/prism/parse_result.rb', line 699
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
#cached_end_code_units_column(cache)
The end column in code units using the given cache to fetch or calculate the value.
: (_CodeUnitsCache cache) -> Integer
# File 'lib/prism/parse_result.rb', line 655
def cached_end_code_units_column(cache) cache[end_offset] - cache[source.line_start(end_offset)] end
#cached_end_code_units_offset(cache)
The end offset from the start of the file in code units using the given cache to fetch or calculate the value.
: (_CodeUnitsCache cache) -> Integer
# File 'lib/prism/parse_result.rb', line 569
def cached_end_code_units_offset(cache) cache[end_offset] end
#cached_start_code_units_column(cache)
The start column in code units using the given cache to fetch or calculate the value.
: (_CodeUnitsCache cache) -> Integer
# File 'lib/prism/parse_result.rb', line 623
def cached_start_code_units_column(cache) cache[start_offset] - cache[source.line_start(start_offset)] end
#cached_start_code_units_offset(cache)
The start offset from the start of the file in code units using the given cache to fetch or calculate the value.
: (_CodeUnitsCache cache) -> Integer
# File 'lib/prism/parse_result.rb', line 539
def cached_start_code_units_offset(cache) cache[start_offset] end
#chop
Returns a new location that is the result of chopping off the last byte.
: () -> Location
#copy(source: self.source, start_offset: self.start_offset, length: self.length)
Create a new location object with the given options.
: (?source: Source, ?start_offset: Integer, ?length: Integer) -> Location
# File 'lib/prism/parse_result.rb', line 477
def copy(source: self.source, start_offset: self.start_offset, length: self.length) Location.new(source, start_offset, length) end
#deconstruct_keys(keys)
Implement the hash pattern matching interface for Location.
: (Array? keys) -> Hash[Symbol, untyped]
# File 'lib/prism/parse_result.rb', line 662
def deconstruct_keys(keys) # :nodoc: { start_offset: start_offset, end_offset: end_offset } end
#end_character_column
The column in characters where this location ends from the start of the line.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 639
def end_character_column source.character_column(end_offset) end
#end_character_offset
The character offset from the beginning of the source where this location ends.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 554
def end_character_offset source.character_offset(end_offset) end
#end_code_units_column(encoding = Encoding::UTF_16LE)
The column in code units of the given encoding where this location ends from the start of the line.
: (?Encoding encoding) -> Integer
# File 'lib/prism/parse_result.rb', line 647
def end_code_units_column(encoding = Encoding::UTF_16LE) source.code_units_column(end_offset, encoding) end
#end_code_units_offset(encoding = Encoding::UTF_16LE)
The offset from the start of the file in code units of the given encoding.
: (Encoding encoding) -> Integer
# File 'lib/prism/parse_result.rb', line 561
def end_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(end_offset, encoding) end
#end_column
The column in bytes where this location ends from the start of the line.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 631
def end_column source.column(end_offset) end
#end_line
The line number where this location ends.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 591
def end_line source.line(end_offset) end
#end_offset
The byte offset from the beginning of the source where this location ends.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 546
def end_offset start_offset + length end
#inspect
Returns a string representation of this location.
: () -> String
# File 'lib/prism/parse_result.rb', line 491
def inspect # :nodoc: "#<Prism::Location @start_offset=#{@start_offset} @length=#{@length} start_line=#{start_line}>" end
#join(other)
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.
: (Location other) -> Location
# File 'lib/prism/parse_result.rb', line 687
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
#pretty_print(q)
Implement the pretty print interface for Location.
: (PP q) -> void
# File 'lib/prism/parse_result.rb', line 669
def pretty_print(q) # :nodoc: q.text("(#{start_line},#{start_column})-(#{end_line},#{end_column})") end
#slice
The source code that this location represents.
: () -> String
# File 'lib/prism/parse_result.rb', line 505
def slice source.slice(start_offset, length) end
#slice_lines
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.
: () -> String
# File 'lib/prism/parse_result.rb', line 514
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
#source_lines
Returns all of the lines of the source code associated with this location.
: () -> Array
# File 'lib/prism/parse_result.rb', line 498
def source_lines source.lines end
#start_character_column
The column in characters where this location ends from the start of the line.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 607
def start_character_column source.character_column(start_offset) end
#start_character_offset
The character offset from the beginning of the source where this location starts.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 524
def start_character_offset source.character_offset(start_offset) end
#start_code_units_column(encoding = Encoding::UTF_16LE)
The column in code units of the given encoding where this location starts from the start of the line.
: (?Encoding encoding) -> Integer
# File 'lib/prism/parse_result.rb', line 615
def start_code_units_column(encoding = Encoding::UTF_16LE) source.code_units_column(start_offset, encoding) end
#start_code_units_offset(encoding = Encoding::UTF_16LE)
The offset from the start of the file in code units of the given encoding.
: (Encoding encoding) -> Integer
# File 'lib/prism/parse_result.rb', line 531
def start_code_units_offset(encoding = Encoding::UTF_16LE) source.code_units_offset(start_offset, encoding) end
#start_column
The column in bytes where this location starts from the start of the line.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 599
def start_column source.column(start_offset) end
#start_line
The line number where this location starts.
: () -> Integer
# File 'lib/prism/parse_result.rb', line 576
def start_line source.line(start_offset) end
#start_line_slice
The content of the line where this location starts before this location.
: () -> String
# File 'lib/prism/parse_result.rb', line 583
def start_line_slice offset = source.line_start(start_offset) source.slice(offset, start_offset - offset) end