Class: RBS::Location

Inherits:
Object
  • Object
show all
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 =

Returns:

self

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject (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

Overloads:

  • .new(arg0, start_pos, end_pos) ⇒ instance

    Parameters:

    • arg0 (Buffer)
    • start_pos (Integer)
    • end_pos (Integer)

    Returns:

    • (instance)
  • .new(buffer:, start_pos:, end_pos:) ⇒ instance

    Parameters:

    • buffer: (Buffer)
    • start_pos: (Integer)
    • end_pos: (Integer)

    Returns:

    • (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)  # => *:*:*..*:*

Parameters:

  • location (Location[untyped, untyped], nil)
  • default: (::String default) (defaults to: "*:*:*...*:*")

Returns:

  • (String)


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

Parameters:

  • other (Object)

Returns:

  • (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.

Overloads:

  • #[](arg0) ⇒ Location[bot, bot]

    Parameters:

    • arg0 (RequiredChildKeys)

    Returns:

  • #[](arg0) ⇒ Location[bot, bot]?

    Parameters:

    • arg0 (OptionalChildKeys)

    Returns:

  • #[](arg0) ⇒ Location[bot, bot]?

    Parameters:

    • arg0 (Symbol)

    Returns:



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.

Parameters:

  • name (OptionalChildKeys)
  • start_pos (Integer)
  • end_pos (Integer)


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.

Parameters:

  • name (OptionalChildKeys)


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.

Parameters:

  • name (RequiredChildKeys)
  • start_pos (Integer)
  • end_pos (Integer)


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_posObject



155
156
157
# File 'ext/rbs_extension/legacy_location.c', line 155

def _end_pos
  @end_pos
end

#_optional_keysArray[Symbol]

Returns:

  • (Array[Symbol])


247
248
249
# File 'ext/rbs_extension/legacy_location.c', line 247

def _optional_keys
  @optional_children.keys
end

#_required_keysArray[Symbol]

Returns:

  • (Array[Symbol])


266
267
268
# File 'ext/rbs_extension/legacy_location.c', line 266

def _required_keys
  @required_children.keys
end

#_start_posObject



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.

Parameters:

  • name (OptionalChildKeys)
  • range (Range[Integer], nil)


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.

Parameters:

  • name (RequiredChildKeys)
  • range (Range[Integer])


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

#arefObject



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

alias aref []

#each_optional_keyvoid #each_optional_keyEnumerator[Symbol, void]

Overloads:

  • #each_optional_keyvoid

    This method returns an undefined value.

  • #each_optional_keyEnumerator[Symbol, void]

    Returns:

    • (Enumerator[Symbol, void])

Yields:

Yield Parameters:

  • arg0 (Symbol)

Yield Returns:

  • (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_keyvoid #each_required_keyEnumerator[Symbol, void]

Overloads:

  • #each_required_keyvoid

    This method returns an undefined value.

  • #each_required_keyEnumerator[Symbol, void]

    Returns:

    • (Enumerator[Symbol, void])

Yields:

Yield Parameters:

  • arg0 (Symbol)

Yield Returns:

  • (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_columnObject



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

def end_column
  end_loc[1]
end

#end_lineObject



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

def end_line
  end_loc[0]
end

#end_locObject



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_posObject



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

def end_pos
  buffer.absolute_position(_end_pos) || raise
end

#inspectString

Returns:

  • (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

Parameters:

  • (Symbol)

Returns:

  • (Boolean)


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

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

#local_locationself

Returns the location of the buffer, but the buffer is detached from the parent buffer

Returns:

  • (self)


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_sourceString

Returns the source of #local_location

Returns:

  • (String)


123
124
125
# File 'sig/location.rbs', line 123

def local_source
  local_location.source
end

#nameObject

Returns the name of the buffer.

Returns:

  • (Object)


31
32
33
# File 'sig/location.rbs', line 31

def name
  buffer.name
end

#optional_key?(name) ⇒ Boolean

Parameters:

  • (Symbol)

Returns:

  • (Boolean)


138
139
140
# File 'lib/rbs/location_aux.rb', line 138

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

#rangeObject



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

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

#required_key?(name) ⇒ Boolean

Parameters:

  • (Symbol)

Returns:

  • (Boolean)


142
143
144
# File 'lib/rbs/location_aux.rb', line 142

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

#sourceString

A substring of buffer associated to the location.

Returns:

  • (String)


69
70
71
# File 'sig/location.rbs', line 69

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

#start_columnObject



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

def start_column
  start_loc[1]
end

#start_lineObject



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

def start_line
  start_loc[0]
end

#start_locObject



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_posObject



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_sString

Returns:

  • (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