Class: Astel::Location
- Inherits:
-
Data
- Object
- Data
- Astel::Location
- Defined in:
- lib/astel/location.rb
Instance Attribute Summary collapse
-
#end_column ⇒ Object
readonly
Returns the value of attribute end_column.
-
#end_line ⇒ Object
readonly
Returns the value of attribute end_line.
-
#end_offset ⇒ Object
readonly
Returns the value of attribute end_offset.
-
#start_column ⇒ Object
readonly
Returns the value of attribute start_column.
-
#start_line ⇒ Object
readonly
Returns the value of attribute start_line.
-
#start_offset ⇒ Object
readonly
Returns the value of attribute start_offset.
Class Method Summary collapse
Instance Method Summary collapse
- #adjust(start: 0, **changes) ⇒ Object
- #begin_point ⇒ Object
- #contains?(other) ⇒ Boolean
- #empty? ⇒ Boolean
- #end_point ⇒ Object
- #join(other) ⇒ Object
- #length ⇒ Object
- #overlaps?(other) ⇒ Boolean
- #range ⇒ Object
Instance Attribute Details
#end_column ⇒ Object (readonly)
Returns the value of attribute end_column
4 5 6 |
# File 'lib/astel/location.rb', line 4 def end_column @end_column end |
#end_line ⇒ Object (readonly)
Returns the value of attribute end_line
4 5 6 |
# File 'lib/astel/location.rb', line 4 def end_line @end_line end |
#end_offset ⇒ Object (readonly)
Returns the value of attribute end_offset
4 5 6 |
# File 'lib/astel/location.rb', line 4 def end_offset @end_offset end |
#start_column ⇒ Object (readonly)
Returns the value of attribute start_column
4 5 6 |
# File 'lib/astel/location.rb', line 4 def start_column @start_column end |
#start_line ⇒ Object (readonly)
Returns the value of attribute start_line
4 5 6 |
# File 'lib/astel/location.rb', line 4 def start_line @start_line end |
#start_offset ⇒ Object (readonly)
Returns the value of attribute start_offset
4 5 6 |
# File 'lib/astel/location.rb', line 4 def start_offset @start_offset end |
Class Method Details
.from(location) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/astel/location.rb', line 5 def self.from(location) return location if location.is_a?(self) new( start_offset: location.start_offset, end_offset: location.end_offset, start_line: location.start_line, start_column: location.start_column, end_line: location.end_line, end_column: location.end_column ) end |
.point(offset:, line:, column:) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/astel/location.rb', line 18 def self.point(offset:, line:, column:) new( start_offset: offset, end_offset: offset, start_line: line, start_column: column, end_line: line, end_column: column ) end |
Instance Method Details
#adjust(start: 0, **changes) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/astel/location.rb', line 65 def adjust(start: 0, **changes) ending = changes.fetch(:end, 0) raise ArgumentError, "unknown keyword: #{changes.keys.first}" unless (changes.keys - [:end]).empty? adjusted_start = start_offset + start adjusted_end = end_offset + ending adjusted_start_column = start_column + start adjusted_end_column = end_column + ending unless valid_adjustment?( start_offset: adjusted_start, end_offset: adjusted_end, start_column: adjusted_start_column, end_column: adjusted_end_column ) raise RangeError, 'adjusted location is invalid' end self.class.new( start_offset: adjusted_start, end_offset: adjusted_end, start_line: start_line, start_column: adjusted_start_column, end_line: end_line, end_column: adjusted_end_column ) end |
#begin_point ⇒ Object
92 93 94 |
# File 'lib/astel/location.rb', line 92 def begin_point self.class.point(offset: start_offset, line: start_line, column: start_column) end |
#contains?(other) ⇒ Boolean
41 42 43 44 |
# File 'lib/astel/location.rb', line 41 def contains?(other) other = self.class.from(other) start_offset <= other.start_offset && end_offset >= other.end_offset end |
#empty? ⇒ Boolean
37 38 39 |
# File 'lib/astel/location.rb', line 37 def empty? start_offset == end_offset end |
#end_point ⇒ Object
96 97 98 |
# File 'lib/astel/location.rb', line 96 def end_point self.class.point(offset: end_offset, line: end_line, column: end_column) end |
#join(other) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/astel/location.rb', line 51 def join(other) other = self.class.from(other) first = start_offset <= other.start_offset ? self : other last = end_offset >= other.end_offset ? self : other self.class.new( start_offset: first.start_offset, end_offset: last.end_offset, start_line: first.start_line, start_column: first.start_column, end_line: last.end_line, end_column: last.end_column ) end |
#length ⇒ Object
29 30 31 |
# File 'lib/astel/location.rb', line 29 def length end_offset - start_offset end |
#overlaps?(other) ⇒ Boolean
46 47 48 49 |
# File 'lib/astel/location.rb', line 46 def overlaps?(other) other = self.class.from(other) start_offset < other.end_offset && other.start_offset < end_offset end |
#range ⇒ Object
33 34 35 |
# File 'lib/astel/location.rb', line 33 def range start_offset...end_offset end |