123456789_123456789_123456789_123456789_123456789_

Class: RBS::Location

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/location_aux.rb,
ext/rbs_extension/location.c

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

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

[ GitHub ]

  
# 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

Class Method Details

.to_string(location, default: "*:*:*...*:*")

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 94

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

Instance Method Details

#==(other)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 211

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

  ID id = SYM2ID(name);

  if (loc->children != NULL) {
    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)

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 173

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)

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 185

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)

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 161

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_keys

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 234

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)) {
      rb_ary_push(keys, ID2SYM(children->entries[i].name));

    }
  }

  return keys;
}

#_required_keys

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 253

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)) {
      rb_ary_push(keys, ID2SYM(children->entries[i].name));
    }
  }

  return keys;
}

#add_optional_child(name, range)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 98

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

#aref

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 27

alias aref []

#buffer

[ GitHub ]

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

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

#each_optional_key(&block)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 47

def end_column
  end_loc[1]
end

#end_line

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 43

def end_line
  end_loc[0]
end

#end_loc

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 55

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

#end_pos

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 156

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

#initialize(buffer, start_pos, end_pos) (private)

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 119

static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
  rbs_loc *loc = rbs_check_location(self);

  int start = FIX2INT(start_pos);
  int end = FIX2INT(end_pos);

  loc->buffer = buffer;
  loc->rg.start = start;
  loc->rg.end = end;

  return Qnil;
}

#initialize_copy(other) (private)

[ GitHub ]

  
# File 'ext/rbs_extension/location.c', line 132

static VALUE location_initialize_copy(VALUE self, VALUE other) {
  rbs_loc *self_loc = rbs_check_location(self);
  rbs_loc *other_loc = rbs_check_location(other);

  self_loc->buffer = other_loc->buffer;
  self_loc->rg = other_loc->rg;
  if (other_loc->children != NULL) {
    rbs_loc_alloc_children(self_loc, other_loc->children->cap);
    memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
  }

  return Qnil;
}

#inspect

[ GitHub ]

  
# 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

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 126

def key?(name)
  optional_key?(name) || required_key?(name)
end

#name

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 31

def name
  buffer.name
end

#optional_key?(name) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 130

def optional_key?(name)
  _optional_keys.include?(name)
end

#range

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 59

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

#required_key?(name) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 134

def required_key?(name)
  _required_keys.include?(name)
end

#source

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 63

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

#start_column

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 39

def start_column
  start_loc[1]
end

#start_line

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 35

def start_line
  start_loc[0]
end

#start_loc

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 51

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

#start_pos

[ GitHub ]

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

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

#to_json(state = _ = nil)

[ GitHub ]

  
# 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

[ GitHub ]

  
# File 'lib/rbs/location_aux.rb', line 67

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