class RBS::Location
Pure-Ruby implementation of the primitives that back RBS::Location.
On CRuby these come from the C extension (ext/rbs_extension/legacy_location.c). JRuby loads this instead, before rbs/location_aux.rb layers the public API on top, so RBS::Location behaves identically without the native extension.
Location is the range on buffer, start_pos..end_pos. The index is based on characters.
A location can have child locations.
Constants
- WithChildren
Attributes
Buffer
The buffer this location points on.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 16 def self.new(buffer_ = nil, start_pos_ = nil, end_pos_ = nil, buffer: nil, start_pos: nil, end_pos: nil) __skip__ = begin if buffer && start_pos && end_pos super(buffer, start_pos, end_pos) else super(buffer_, start_pos_, end_pos_) end end end
Object::new
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 12 def initialize(buffer, start_pos, end_pos) @buffer = buffer @start_pos = start_pos @end_pos = end_pos @required_children = {} #: Hash[Symbol, [ Integer, Integer ]] @optional_children = {} #: Hash[Symbol, [ Integer, Integer ]?] end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 102 def self.to_string(location, default: "*:*:*...*:*") location&.to_s || default end
Returns a string representation suitable for terminal output.
Location.to_string(loc) # => a.rb:1:0...3:4 Location.to_string(nil) # => *:*:*..*:*
Public Instance Methods
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 79 def ==(other) other.is_a?(Location) && other.buffer == buffer && other.start_pos == start_pos && other.end_pos == end_pos end
(RequiredChildKeys) → Location[bot, bot]
(OptionalChildKeys) → Location[bot, bot]?
(Symbol) → Location[bot, bot]?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 48 def [](name) if (range = @required_children[name]) return Location.new(@buffer, range[0], range[1]) end if @optional_children.key?(name) range = @optional_children[name] return range && Location.new(@buffer, range[0], range[1]) end raise "Unknown child name given: #{name}" end
Returns Location instance for given child name.
# @type var loc: Location::WithChildren[:name, :args] loc[:name] # => Location loc[:args] # => may be nil
Note that passing unknown symbol raises an error even if the child is optional. You need explicitly set nil for absent optional children.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 32 def _add_optional_child(name, start_pos, end_pos) @optional_children[name] = [start_pos, end_pos] end
(OptionalChildKeys name) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 36 def _add_optional_no_child(name) @optional_children[name] = nil end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 28 def _add_required_child(name, start_pos, end_pos) @required_children[name] = [start_pos, end_pos] end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 24 def _end_pos @end_pos end
The raw index of character the range ends at.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 44 def _optional_keys @optional_children.keys end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 40 def _required_keys @required_children.keys end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/wasm/location.rb, line 20 def _start_pos @start_pos end
The raw index of character the range starts from.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 110 def add_optional_child(name, range) if range _add_optional_child(name, range.begin, range.end) else _add_optional_no_child(name); end end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 106 def add_required_child(name, range) _add_required_child(name, range.begin, range.end) end
() { (Symbol) → void } → void
() → Enumerator[Symbol, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 118 def each_optional_key(&block) if block _optional_keys.uniq.each(&block) else enum_for(:each_optional_key) end end
() { (Symbol) → void } → void
() → Enumerator[Symbol, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 126 def each_required_key(&block) if block _required_keys.uniq.each(&block) else enum_for(:each_required_key) end end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 55 def end_column end_loc[1] end
Column of the end_pos (0 origin, absolute)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 51 def end_line end_loc[0] end
Line of the end_pos (1 origin, absolute)
Buffer::loc
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 63 def end_loc @end_loc ||= buffer.top_buffer.pos_to_loc(end_pos) end
The absolute line-column pair of the end position
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 35 def end_pos buffer.absolute_position(_end_pos) || raise end
The absolute end index of character the range ends at
It returns the index in the buffer.top_buffer.
() → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 5 def inspect rks = each_required_key.to_a ops = each_optional_key.to_a.map {|x| "?#{x}" } src = if source.length <= 1 source.inspect else source.each_line.first&.chomp&.inspect end "#<#{self.class}:#{self.__id__} buffer=#{buffer.name}, start=#{start_line}:#{start_column}, pos=#{start_pos}...#{end_pos}, children=#{(rks + ops).join(",")} source=#{src}>" end
(Symbol) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 134 def key?(name) optional_key?(name) || required_key?(name) end
() → self
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 146 def local_location loc = Location.new(buffer.detach, _start_pos, _end_pos) each_optional_key do |key| value = self[key] if value loc.add_optional_child(key, value._start_pos...value._end_pos) else loc.add_optional_child(key, nil) end end each_required_key do |key| value = self[key] or raise loc.add_required_child(key, value._start_pos...value._end_pos) end loc #: self end
Returns the location of the buffer, but the buffer is detached from the parent buffer
() → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 166 def local_source local_location.source end
Returns the source of local_location
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 39 def name buffer.name end
Returns the name of the buffer.
(Symbol) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 138 def optional_key?(name) _optional_keys.include?(name) end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 67 def range @range ||= start_pos...end_pos end
The absolute range of the start and end position
(Symbol) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 142 def required_key?(name) _required_keys.include?(name) end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 71 def source @source ||= (buffer.top_buffer.content[range] || raise) end
A substring of buffer associated to the location.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 47 def start_column start_loc[1] end
Column of the start_pos (0 origin, absolute)
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 43 def start_line start_loc[0] end
Line of the start_pos (1 origin, absolute)
Buffer::loc
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 59 def start_loc @start_loc ||= buffer.top_buffer.pos_to_loc(start_pos) end
The absolute line-column pair of the start position
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 31 def start_pos buffer.absolute_position(_start_pos) || raise end
The absolute start index of character the range starts from
It returns the index in the buffer.top_buffer.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/location_aux.rb, line 86 def to_json(state = nil) { start: { line: start_line, column: start_column }, end: { line: end_line, column: end_column }, buffer: { name: name&.to_s } }.to_json(state) end