Class: RBS::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/location_aux.rb,
ext/rbs_extension/location.c

Constant Summary collapse

WithChildren =
self

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(buffer_ = nil, start_pos_ = nil, end_pos_ = nil, buffer: nil, start_pos: nil, end_pos: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/rbs/location_aux.rb', line 9

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



91
92
93
# File 'lib/rbs/location_aux.rb', line 91

def self.to_string(location, default: "*:*:*...*:*")
  location&.to_s || default
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
73
# File 'lib/rbs/location_aux.rb', line 68

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'ext/rbs_extension/location.c', line 221

static VALUE location_aref(VALUE self, VALUE name) {
  rbs_loc *loc = rbs_check_location(self);

  range result;
  ID id = SYM2ID(name);

  if (rbs_loc_list_find(loc->requireds, id, &result)) {
    return rbs_new_location(loc->buffer, result);
  }

  if (rbs_loc_list_find(loc->optionals, id, &result)) {
    if (null_range_p(result)) {
      return Qnil;
    } else {
      return rbs_new_location(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, SYM2ID(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, SYM2ID(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, SYM2ID(name), rg);

  return Qnil;
}

#_optional_keysObject



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'ext/rbs_extension/location.c', line 243

static VALUE location_optional_keys(VALUE self) {
  VALUE keys = rb_ary_new();

  rbs_loc *loc = rbs_check_location(self);
  rbs_loc_list *list = loc->optionals;

  while (list) {
    rb_ary_push(keys, ID2SYM(list->name));
    list = list->next;
  }

  return keys;
}

#_required_keysObject



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'ext/rbs_extension/location.c', line 257

static VALUE location_required_keys(VALUE self) {
  VALUE keys = rb_ary_new();

  rbs_loc *loc = rbs_check_location(self);
  rbs_loc_list *list = loc->requireds;

  while (list) {
    rb_ary_push(keys, ID2SYM(list->name));
    list = list->next;
  }

  return keys;
}

#add_optional_child(name, range) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/rbs/location_aux.rb', line 99

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



95
96
97
# File 'lib/rbs/location_aux.rb', line 95

def add_required_child(name, range)
  _add_required_child(name, range.begin, range.end)
end

#bufferObject



139
140
141
142
# File 'ext/rbs_extension/location.c', line 139

static VALUE location_buffer(VALUE self) {
  rbs_loc *loc = rbs_check_location(self);
  return loc->buffer;
}

#each_optional_key(&block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/rbs/location_aux.rb', line 107

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



115
116
117
118
119
120
121
# File 'lib/rbs/location_aux.rb', line 115

def each_required_key(&block)
  if block
    _required_keys.uniq.each(&block)
  else
    enum_for(:each_required_key)
  end
end

#end_columnObject



40
41
42
# File 'lib/rbs/location_aux.rb', line 40

def end_column
  end_loc[1]
end

#end_lineObject



36
37
38
# File 'lib/rbs/location_aux.rb', line 36

def end_line
  end_loc[0]
end

#end_locObject



50
51
52
53
54
# File 'lib/rbs/location_aux.rb', line 50

def end_loc
  @end_loc ||= begin
    _end_loc || buffer.pos_to_loc(end_pos)
  end
end

#end_posObject



149
150
151
152
# File 'ext/rbs_extension/location.c', line 149

static VALUE location_end_pos(VALUE self) {
  rbs_loc *loc = rbs_check_location(self);
  return INT2FIX(loc->rg.end.char_pos);
}

#inspectObject



3
4
5
6
7
# File 'lib/rbs/location_aux.rb', line 3

def inspect
  rks = each_required_key.to_a
  ops = each_optional_key.to_a.map {|x| "?#{x}" }
  "#<#{self.class}:#{self.__id__} buffer=#{buffer.name}, start=#{start_line}:#{start_column}, pos=#{start_pos}...#{end_pos}, children=#{(rks + ops).join(",")} source='#{source.lines.first&.chomp}'>"
end

#nameObject



24
25
26
# File 'lib/rbs/location_aux.rb', line 24

def name
  buffer.name
end

#rangeObject



56
57
58
# File 'lib/rbs/location_aux.rb', line 56

def range
  @range ||= start_pos...end_pos
end

#sourceObject



60
61
62
# File 'lib/rbs/location_aux.rb', line 60

def source
  @source ||= buffer.content[range] or raise
end

#start_columnObject



32
33
34
# File 'lib/rbs/location_aux.rb', line 32

def start_column
  start_loc[1]
end

#start_lineObject



28
29
30
# File 'lib/rbs/location_aux.rb', line 28

def start_line
  start_loc[0]
end

#start_locObject



44
45
46
47
48
# File 'lib/rbs/location_aux.rb', line 44

def start_loc
  @start_loc ||= begin
    _start_loc || buffer.pos_to_loc(start_pos)
  end
end

#start_posObject



144
145
146
147
# File 'ext/rbs_extension/location.c', line 144

static VALUE location_start_pos(VALUE self) {
  rbs_loc *loc = rbs_check_location(self);
  return INT2FIX(loc->rg.start.char_pos);
}

#to_json(state = _ = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rbs/location_aux.rb', line 75

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_sObject



64
65
66
# File 'lib/rbs/location_aux.rb', line 64

def to_s
  "#{name || "-"}:#{start_line}:#{start_column}...#{end_line}:#{end_column}"
end