Class: RBS::Location
- Inherits:
-
Object
- Object
- RBS::Location
- Defined in:
- lib/rbs/location.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#end_pos ⇒ Object
readonly
Returns the value of attribute end_pos.
-
#start_pos ⇒ Object
readonly
Returns the value of attribute start_pos.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
- #end_column ⇒ Object
- #end_line ⇒ Object
- #end_loc ⇒ Object
-
#initialize(buffer:, start_pos:, end_pos:) ⇒ Location
constructor
A new instance of Location.
- #inspect ⇒ Object
- #name ⇒ Object
- #pred?(loc) ⇒ Boolean
- #source ⇒ Object
- #start_column ⇒ Object
- #start_line ⇒ Object
- #start_loc ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
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
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
3 4 5 |
# File 'lib/rbs/location.rb', line 3 def buffer @buffer end |
#end_pos ⇒ Object (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_pos ⇒ Object (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
.concat(*locations) ⇒ Object
76 77 78 |
# File 'lib/rbs/location.rb', line 76 def self.concat(*locations) locations.inject {|l1, l2| l1 + l2 } end |
.to_string(location, default: "*:*:*...*:*") ⇒ Object
53 54 55 |
# File 'lib/rbs/location.rb', line 53 def self.to_string(location, default: "*:*:*...*:*") location&.to_s || default end |
Instance Method Details
#+(other) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rbs/location.rb', line 64 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
57 58 59 60 61 62 |
# File 'lib/rbs/location.rb', line 57 def ==(other) other.is_a?(Location) && other.buffer == buffer && other.start_pos == start_pos && other.end_pos == end_pos end |
#end_column ⇒ Object
33 34 35 |
# File 'lib/rbs/location.rb', line 33 def end_column end_loc[1] end |
#end_line ⇒ Object
29 30 31 |
# File 'lib/rbs/location.rb', line 29 def end_line end_loc[0] end |
#end_loc ⇒ Object
41 42 43 |
# File 'lib/rbs/location.rb', line 41 def end_loc @end_loc ||= buffer.pos_to_loc(end_pos) end |
#inspect ⇒ Object
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 |
#name ⇒ Object
17 18 19 |
# File 'lib/rbs/location.rb', line 17 def name buffer.name end |
#pred?(loc) ⇒ Boolean
80 81 82 83 84 |
# File 'lib/rbs/location.rb', line 80 def pred?(loc) loc.is_a?(Location) && loc.name == name && loc.start_pos == end_pos end |
#source ⇒ Object
45 46 47 |
# File 'lib/rbs/location.rb', line 45 def source @source ||= buffer.content[start_pos...end_pos] end |
#start_column ⇒ Object
25 26 27 |
# File 'lib/rbs/location.rb', line 25 def start_column start_loc[1] end |
#start_line ⇒ Object
21 22 23 |
# File 'lib/rbs/location.rb', line 21 def start_line start_loc[0] end |
#start_loc ⇒ Object
37 38 39 |
# File 'lib/rbs/location.rb', line 37 def start_loc @start_loc ||= buffer.pos_to_loc(start_pos) end |
#to_json(*args) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rbs/location.rb', line 86 def to_json(*args) { start: { line: start_line, column: start_column }, end: { line: end_line, column: end_column }, buffer: { name: name&.to_s } }.to_json(*args) end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/rbs/location.rb', line 49 def to_s "#{name || "-"}:#{start_line}:#{start_column}...#{end_line}:#{end_column}" end |