Class: RBS::Location
- Inherits:
-
Object
- Object
- RBS::Location
- Includes:
- _ToJson
- Defined in:
- lib/rbs/wasm/location.rb,
lib/rbs/location_aux.rb,
sig/location.rbs,
ext/rbs_extension/legacy_location.c
Overview
Location is the range on buffer, start_pos..end_pos.
The index is based on characters.
A location can have child locations.
Constant Summary collapse
- WithChildren =
self
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Class Method Summary collapse
- .new(buffer_ = nil, start_pos_ = nil, end_pos_ = nil, buffer: nil, start_pos: nil, end_pos: nil) ⇒ Object
-
.to_string(location, default: "*:*:*...*:*") ⇒ String
Returns a string representation suitable for terminal output.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#[](name) ⇒ Object
Returns
Locationinstance for given child name. - #_add_optional_child(name, start, end) ⇒ void
- #_add_optional_no_child(name) ⇒ void
- #_add_required_child(name, start, end) ⇒ void
- #_end_pos ⇒ Object
- #_optional_keys ⇒ Array[Symbol]
- #_required_keys ⇒ Array[Symbol]
- #_start_pos ⇒ Object
- #add_optional_child(name, range) ⇒ void
- #add_required_child(name, range) ⇒ void
- #aref ⇒ Object
- #each_optional_key {|arg0| ... } ⇒ Object
- #each_required_key {|arg0| ... } ⇒ Object
- #end_column ⇒ Object
- #end_line ⇒ Object
- #end_loc ⇒ Object
- #end_pos ⇒ Object
- #inspect ⇒ String
- #key?(name) ⇒ Boolean
-
#local_location ⇒ self
Returns the location of the buffer, but the buffer is detached from the parent buffer.
-
#local_source ⇒ String
Returns the source of
#local_location. -
#name ⇒ Object
Returns the name of the buffer.
- #optional_key?(name) ⇒ Boolean
- #range ⇒ Object
- #required_key?(name) ⇒ Boolean
-
#source ⇒ String
A substring of buffer associated to the location.
- #start_column ⇒ Object
- #start_line ⇒ Object
- #start_loc ⇒ Object
- #start_pos ⇒ Object
- #to_json(state = nil) ⇒ Object
- #to_s ⇒ String
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
145 146 147 |
# File 'ext/rbs_extension/legacy_location.c', line 145 def buffer @buffer end |
Class Method Details
.new(arg0, start_pos, end_pos) ⇒ instance .new(buffer:, start_pos:, end_pos:) ⇒ instance
16 17 18 19 20 21 22 23 24 25 |
# File '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 |
.to_string(location, default: "*:*:*...*:*") ⇒ String
Returns a string representation suitable for terminal output.
Location.to_string(loc) # => a.rb:1:0...3:4
Location.to_string(nil) # => *:*:*..*:*
80 81 82 |
# File 'sig/location.rbs', line 80 def self.to_string(location, default: "*:*:*...*:*") location&.to_s || default end |
Instance Method Details
#==(other) ⇒ Boolean
79 80 81 82 83 84 |
# File '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 |
#[](arg0) ⇒ Location[bot, bot] #[](arg0) ⇒ Location[bot, bot]? #[](arg0) ⇒ Location[bot, bot]?
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.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'sig/location.rbs', line 99 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 |
#_add_optional_child(name, start, end) ⇒ void
This method returns an undefined value.
173 174 175 |
# File 'ext/rbs_extension/legacy_location.c', line 173 def _add_optional_child(name, start_pos, end_pos) @optional_children[name] = [start_pos, end_pos] end |
#_add_optional_no_child(name) ⇒ void
This method returns an undefined value.
186 187 188 |
# File 'ext/rbs_extension/legacy_location.c', line 186 def _add_optional_no_child(name) @optional_children[name] = nil end |
#_add_required_child(name, start, end) ⇒ void
This method returns an undefined value.
160 161 162 |
# File 'ext/rbs_extension/legacy_location.c', line 160 def _add_required_child(name, start_pos, end_pos) @required_children[name] = [start_pos, end_pos] end |
#_end_pos ⇒ Object
155 156 157 |
# File 'ext/rbs_extension/legacy_location.c', line 155 def _end_pos @end_pos end |
#_optional_keys ⇒ Array[Symbol]
247 248 249 |
# File 'ext/rbs_extension/legacy_location.c', line 247 def _optional_keys @optional_children.keys end |
#_required_keys ⇒ Array[Symbol]
266 267 268 |
# File 'ext/rbs_extension/legacy_location.c', line 266 def _required_keys @required_children.keys end |
#_start_pos ⇒ Object
150 151 152 |
# File 'ext/rbs_extension/legacy_location.c', line 150 def _start_pos @start_pos end |
#add_optional_child(name, range) ⇒ void
This method returns an undefined value.
110 111 112 113 114 115 116 |
# File '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 |
#add_required_child(name, range) ⇒ void
This method returns an undefined value.
106 107 108 |
# File 'lib/rbs/location_aux.rb', line 106 def add_required_child(name, range) _add_required_child(name, range.begin, range.end) end |
#aref ⇒ Object
27 |
# File 'lib/rbs/location_aux.rb', line 27 alias aref [] |
#each_optional_key ⇒ void #each_optional_key ⇒ Enumerator[Symbol, void]
118 119 120 121 122 123 124 |
# File '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 |
#each_required_key ⇒ void #each_required_key ⇒ Enumerator[Symbol, void]
126 127 128 129 130 131 132 |
# File '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 |
#end_column ⇒ Object
55 56 57 |
# File 'lib/rbs/location_aux.rb', line 55 def end_column end_loc[1] end |
#end_line ⇒ Object
51 52 53 |
# File 'lib/rbs/location_aux.rb', line 51 def end_line end_loc[0] end |
#end_loc ⇒ Object
63 64 65 |
# File 'lib/rbs/location_aux.rb', line 63 def end_loc @end_loc ||= buffer.top_buffer.pos_to_loc(end_pos) end |
#end_pos ⇒ Object
35 36 37 |
# File 'lib/rbs/location_aux.rb', line 35 def end_pos buffer.absolute_position(_end_pos) || raise end |
#inspect ⇒ String
5 6 7 8 9 10 11 12 13 14 |
# File '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 |
#key?(name) ⇒ Boolean
134 135 136 |
# File 'lib/rbs/location_aux.rb', line 134 def key?(name) optional_key?(name) || required_key?(name) end |
#local_location ⇒ self
Returns the location of the buffer, but the buffer is detached from the parent buffer
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'sig/location.rbs', line 119 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 |
#local_source ⇒ String
Returns the source of #local_location
123 124 125 |
# File 'sig/location.rbs', line 123 def local_source local_location.source end |
#name ⇒ Object
Returns the name of the buffer.
31 32 33 |
# File 'sig/location.rbs', line 31 def name buffer.name end |
#optional_key?(name) ⇒ Boolean
138 139 140 |
# File 'lib/rbs/location_aux.rb', line 138 def optional_key?(name) _optional_keys.include?(name) end |
#range ⇒ Object
67 68 69 |
# File 'lib/rbs/location_aux.rb', line 67 def range @range ||= start_pos...end_pos end |
#required_key?(name) ⇒ Boolean
142 143 144 |
# File 'lib/rbs/location_aux.rb', line 142 def required_key?(name) _required_keys.include?(name) end |
#source ⇒ String
A substring of buffer associated to the location.
69 70 71 |
# File 'sig/location.rbs', line 69 def source @source ||= (buffer.top_buffer.content[range] || raise) end |
#start_column ⇒ Object
47 48 49 |
# File 'lib/rbs/location_aux.rb', line 47 def start_column start_loc[1] end |
#start_line ⇒ Object
43 44 45 |
# File 'lib/rbs/location_aux.rb', line 43 def start_line start_loc[0] end |
#start_loc ⇒ Object
59 60 61 |
# File 'lib/rbs/location_aux.rb', line 59 def start_loc @start_loc ||= buffer.top_buffer.pos_to_loc(start_pos) end |
#start_pos ⇒ Object
31 32 33 |
# File 'lib/rbs/location_aux.rb', line 31 def start_pos buffer.absolute_position(_start_pos) || raise end |
#to_json(state = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File '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 |
#to_s ⇒ String
75 76 77 |
# File 'lib/rbs/location_aux.rb', line 75 def to_s "#{name || "-"}:#{start_line}:#{start_column}...#{end_line}:#{end_column}" end |