Class: RBS::Location
- Inherits:
-
Object
- Object
- RBS::Location
- Defined in:
- lib/rbs/location_aux.rb,
ext/rbs_extension/location.c
Constant Summary collapse
- WithChildren =
self
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: "*:*:*...*:*") ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](name) ⇒ Object (also: #aref)
- #_add_optional_child(name, start, end) ⇒ Object
- #_add_optional_no_child(name) ⇒ Object
- #_add_required_child(name, start, end) ⇒ Object
- #_optional_keys ⇒ Object
- #_required_keys ⇒ Object
- #add_optional_child(name, range) ⇒ Object
- #add_required_child(name, range) ⇒ Object
- #buffer ⇒ Object
- #each_optional_key(&block) ⇒ Object
- #each_required_key(&block) ⇒ Object
- #end_column ⇒ Object
- #end_line ⇒ Object
- #end_loc ⇒ Object
- #end_pos ⇒ Object
- #inspect ⇒ Object
- #key?(name) ⇒ Boolean
- #name ⇒ Object
- #optional_key?(name) ⇒ Boolean
- #range ⇒ Object
- #required_key?(name) ⇒ Boolean
- #source ⇒ Object
- #start_column ⇒ Object
- #start_line ⇒ Object
- #start_loc ⇒ Object
- #start_pos ⇒ Object
- #to_json(state = _ = nil) ⇒ Object
- #to_s ⇒ Object
Class Method Details
.new(buffer_ = nil, start_pos_ = nil, end_pos_ = nil, buffer: nil, start_pos: nil, end_pos: nil) ⇒ Object
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: "*:*:*...*:*") ⇒ Object
94 95 96 |
# File 'lib/rbs/location_aux.rb', line 94 def self.to_string(location, default: "*:*:*...*:*") location&.to_s || default end |
Instance Method Details
#==(other) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/rbs/location_aux.rb', line 71 def ==(other) other.is_a?(Location) && other.buffer == buffer && other.start_pos == start_pos && other.end_pos == end_pos end |
#[](name) ⇒ Object Also known as: aref
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'ext/rbs_extension/location.c', line 236 static VALUE location_aref(VALUE self, VALUE name) { rbs_loc *loc = rbs_check_location(self); rbs_constant_id_t id = rbs_constant_pool_find_ruby_symbol(name); if (loc->children != NULL && id != RBS_CONSTANT_ID_UNSET) { for (unsigned short i = 0; i < loc->children->len; i++) { if (loc->children->entries[i].name == id) { rbs_loc_range result = loc->children->entries[i].rg; if (RBS_LOC_OPTIONAL_P(loc, i) && NULL_LOC_RANGE_P(result)) { return Qnil; } else { return rbs_new_location_from_loc_range(loc->buffer, result); } } } } VALUE string = rb_funcall(name, rb_intern("to_s"), 0); rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string)); } |
#_add_optional_child(name, start, end) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'ext/rbs_extension/location.c', line 192 static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) { rbs_loc *loc = rbs_check_location(self); range rg; rg.start = rbs_loc_position(FIX2INT(start)); rg.end = rbs_loc_position(FIX2INT(end)); rbs_loc_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg); return Qnil; } |
#_add_optional_no_child(name) ⇒ Object
204 205 206 207 208 209 210 |
# File 'ext/rbs_extension/location.c', line 204 static VALUE location_add_optional_no_child(VALUE self, VALUE name) { rbs_loc *loc = rbs_check_location(self); rbs_loc_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), NULL_RANGE); return Qnil; } |
#_add_required_child(name, start, end) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'ext/rbs_extension/location.c', line 180 static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) { rbs_loc *loc = rbs_check_location(self); range rg; rg.start = rbs_loc_position(FIX2INT(start)); rg.end = rbs_loc_position(FIX2INT(end)); rbs_loc_add_required_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg); return Qnil; } |
#_optional_keys ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'ext/rbs_extension/location.c', line 263 static VALUE location_optional_keys(VALUE self) { VALUE keys = rb_ary_new(); rbs_loc *loc = rbs_check_location(self); rbs_loc_children *children = loc->children; if (children == NULL) { return keys; } for (unsigned short i = 0; i < children->len; i++) { if (RBS_LOC_OPTIONAL_P(loc, i)) { rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name); VALUE key_sym = rbs_constant_to_ruby_symbol(key_id); rb_ary_push(keys, key_sym); } } return keys; } |
#_required_keys ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'ext/rbs_extension/location.c', line 283 static VALUE location_required_keys(VALUE self) { VALUE keys = rb_ary_new(); rbs_loc *loc = rbs_check_location(self); rbs_loc_children *children = loc->children; if (children == NULL) { return keys; } for (unsigned short i = 0; i < children->len; i++) { if (RBS_LOC_REQUIRED_P(loc, i)) { rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name); VALUE key_sym = rbs_constant_to_ruby_symbol(key_id); rb_ary_push(keys, key_sym); } } return keys; } |
#add_optional_child(name, range) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/rbs/location_aux.rb', line 102 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) ⇒ Object
98 99 100 |
# File 'lib/rbs/location_aux.rb', line 98 def add_required_child(name, range) _add_required_child(name, range.begin, range.end) end |
#buffer ⇒ Object
156 157 158 159 |
# File 'ext/rbs_extension/location.c', line 156 static VALUE location_buffer(VALUE self) { rbs_loc *loc = rbs_check_location(self); return loc->buffer; } |
#each_optional_key(&block) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/rbs/location_aux.rb', line 110 def each_optional_key(&block) if block _optional_keys.uniq.each(&block) else enum_for(:each_optional_key) end end |
#each_required_key(&block) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/rbs/location_aux.rb', line 118 def each_required_key(&block) if block _required_keys.uniq.each(&block) else enum_for(:each_required_key) end end |
#end_column ⇒ Object
47 48 49 |
# File 'lib/rbs/location_aux.rb', line 47 def end_column end_loc[1] end |
#end_line ⇒ Object
43 44 45 |
# File 'lib/rbs/location_aux.rb', line 43 def end_line end_loc[0] end |
#end_loc ⇒ Object
55 56 57 |
# File 'lib/rbs/location_aux.rb', line 55 def end_loc @end_loc ||= buffer.pos_to_loc(end_pos) end |
#end_pos ⇒ Object
166 167 168 169 |
# File 'ext/rbs_extension/location.c', line 166 static VALUE location_end_pos(VALUE self) { rbs_loc *loc = rbs_check_location(self); return INT2FIX(loc->rg.end); } |
#inspect ⇒ Object
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
126 127 128 |
# File 'lib/rbs/location_aux.rb', line 126 def key?(name) optional_key?(name) || required_key?(name) end |
#name ⇒ Object
31 32 33 |
# File 'lib/rbs/location_aux.rb', line 31 def name buffer.name end |
#optional_key?(name) ⇒ Boolean
130 131 132 |
# File 'lib/rbs/location_aux.rb', line 130 def optional_key?(name) _optional_keys.include?(name) end |
#range ⇒ Object
59 60 61 |
# File 'lib/rbs/location_aux.rb', line 59 def range @range ||= start_pos...end_pos end |
#required_key?(name) ⇒ Boolean
134 135 136 |
# File 'lib/rbs/location_aux.rb', line 134 def required_key?(name) _required_keys.include?(name) end |
#source ⇒ Object
63 64 65 |
# File 'lib/rbs/location_aux.rb', line 63 def source @source ||= (buffer.content[range] || raise) end |
#start_column ⇒ Object
39 40 41 |
# File 'lib/rbs/location_aux.rb', line 39 def start_column start_loc[1] end |
#start_line ⇒ Object
35 36 37 |
# File 'lib/rbs/location_aux.rb', line 35 def start_line start_loc[0] end |
#start_loc ⇒ Object
51 52 53 |
# File 'lib/rbs/location_aux.rb', line 51 def start_loc @start_loc ||= buffer.pos_to_loc(start_pos) end |
#start_pos ⇒ Object
161 162 163 164 |
# File 'ext/rbs_extension/location.c', line 161 static VALUE location_start_pos(VALUE self) { rbs_loc *loc = rbs_check_location(self); return INT2FIX(loc->rg.start); } |
#to_json(state = _ = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rbs/location_aux.rb', line 78 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 ⇒ Object
67 68 69 |
# File 'lib/rbs/location_aux.rb', line 67 def to_s "#{name || "-"}:#{start_line}:#{start_column}...#{end_line}:#{end_column}" end |