Class: RBS::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/location.rb

Direct Known Subclasses

WithChildren

Defined Under Namespace

Classes: WithChildren

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer:, start_pos:, end_pos:) ⇒ Location

Returns a new instance of Location.



7
8
9
10
11
# File 'lib/rbs/location.rb', line 7

def initialize(buffer:, start_pos:, end_pos:)
  @buffer = buffer
  @start_pos = start_pos
  @end_pos = end_pos
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



3
4
5
# File 'lib/rbs/location.rb', line 3

def buffer
  @buffer
end

#end_posObject (readonly)

Returns the value of attribute end_pos.



5
6
7
# File 'lib/rbs/location.rb', line 5

def end_pos
  @end_pos
end

#start_posObject (readonly)

Returns the value of attribute start_pos.



4
5
6
# File 'lib/rbs/location.rb', line 4

def start_pos
  @start_pos
end

Class Method Details

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



57
58
59
# File 'lib/rbs/location.rb', line 57

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

Instance Method Details

#+(other) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rbs/location.rb', line 68

def +(other)
  if other
    raise "Invalid concat: buffer=#{buffer.name}, other.buffer=#{other.buffer.name}" unless other.buffer == buffer

    self.class.new(buffer: buffer,
                   start_pos: start_pos,
                   end_pos: other.end_pos)
  else
    self
  end
end

#<<(other) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/rbs/location.rb', line 85

def <<(other)
  if other
    raise "Invalid concat: buffer=#{buffer.name}, other.buffer=#{other.buffer.name}" unless other.buffer == buffer
    @end_pos = other.end_pos
    @source = nil
    @end_loc = nil
  end
  self
end

#==(other) ⇒ Object



61
62
63
64
65
66
# File 'lib/rbs/location.rb', line 61

def ==(other)
  other.is_a?(Location) &&
    other.buffer == buffer &&
    other.start_pos == start_pos &&
    other.end_pos == end_pos
end

#concat(*others) ⇒ Object



80
81
82
83
# File 'lib/rbs/location.rb', line 80

def concat(*others)
  others.each { |other| self << other }
  self
end

#end_columnObject



33
34
35
# File 'lib/rbs/location.rb', line 33

def end_column
  end_loc[1]
end

#end_lineObject



29
30
31
# File 'lib/rbs/location.rb', line 29

def end_line
  end_loc[0]
end

#end_locObject



41
42
43
# File 'lib/rbs/location.rb', line 41

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

#inspectObject



13
14
15
# File 'lib/rbs/location.rb', line 13

def inspect
  "#<#{self.class}:#{self.__id__} @buffer=#{buffer.name}, @pos=#{start_pos}...#{end_pos}, source='#{source.lines.first}', start_line=#{start_line}, start_column=#{start_column}>"
end

#nameObject



17
18
19
# File 'lib/rbs/location.rb', line 17

def name
  buffer.name
end

#pred?(loc) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/rbs/location.rb', line 95

def pred?(loc)
  loc.is_a?(Location) &&
    loc.name == name &&
    loc.start_pos == end_pos
end

#rangeObject



45
46
47
# File 'lib/rbs/location.rb', line 45

def range
  start_pos...end_pos
end

#sourceObject



49
50
51
# File 'lib/rbs/location.rb', line 49

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

#start_columnObject



25
26
27
# File 'lib/rbs/location.rb', line 25

def start_column
  start_loc[1]
end

#start_lineObject



21
22
23
# File 'lib/rbs/location.rb', line 21

def start_line
  start_loc[0]
end

#start_locObject



37
38
39
# File 'lib/rbs/location.rb', line 37

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

#to_json(state = _ = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbs/location.rb', line 101

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



53
54
55
# File 'lib/rbs/location.rb', line 53

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

#with_children(required: {}, optional: {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rbs/location.rb', line 117

def with_children(required: {}, optional: {})
  # @type var required: Hash[Symbol, Range[Integer] | Location]
  # @type var optional: Hash[Symbol, Range[Integer] | Location | nil]

  this = WithChildren.new(buffer: buffer, start_pos: start_pos, end_pos: end_pos)

  req = required.transform_values do |value|
    case value
    when Location
      value.range
    else
      value
    end
  end

  opt = optional.transform_values do |value|
    case value
    when Location
      value.range
    else
      value
    end
  end

  this.required_children.merge!(req)
  this.optional_children.merge!(opt)

  this
end