Class: Rich::Span
- Inherits:
-
Object
- Object
- Rich::Span
- Defined in:
- lib/rich/text.rb
Overview
A span of styled text within a Text object
Instance Attribute Summary collapse
-
#end ⇒ Integer
readonly
End position (exclusive).
-
#start ⇒ Integer
readonly
Start position (inclusive).
-
#style ⇒ Style
readonly
Style for this span.
Instance Method Summary collapse
-
#adjust_delete(position, length) ⇒ Object
Adjust span after deletion at position.
-
#adjust_insert(position, length) ⇒ Object
Adjust span after insertion at position.
-
#initialize(start_pos, end_pos, style) ⇒ Span
constructor
A new instance of Span.
- #inspect ⇒ Object
-
#length ⇒ Integer
Length of the span.
-
#overlaps?(start_pos, end_pos) ⇒ Boolean
Check if span overlaps with a range.
Constructor Details
Instance Attribute Details
#end ⇒ Integer (readonly)
Returns End position (exclusive).
14 15 16 |
# File 'lib/rich/text.rb', line 14 def end @end end |
#start ⇒ Integer (readonly)
Returns Start position (inclusive).
11 12 13 |
# File 'lib/rich/text.rb', line 11 def start @start end |
#style ⇒ Style (readonly)
Returns Style for this span.
17 18 19 |
# File 'lib/rich/text.rb', line 17 def style @style end |
Instance Method Details
#adjust_delete(position, length) ⇒ Object
Adjust span after deletion at position
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rich/text.rb', line 48 def adjust_delete(position, length) delete_end = position + length if delete_end <= @start Span.new(@start - length, @end - length, @style) elsif position >= @end self elsif position <= @start && delete_end >= @end nil # Span completely deleted elsif position <= @start Span.new(position, @end - length, @style) elsif delete_end >= @end Span.new(@start, position, @style) else Span.new(@start, @end - length, @style) end end |
#adjust_insert(position, length) ⇒ Object
Adjust span after insertion at position
37 38 39 40 41 42 43 44 45 |
# File 'lib/rich/text.rb', line 37 def adjust_insert(position, length) if position <= @start Span.new(@start + length, @end + length, @style) elsif position < @end Span.new(@start, @end + length, @style) else self end end |
#inspect ⇒ Object
66 67 68 |
# File 'lib/rich/text.rb', line 66 def inspect "#<Rich::Span [#{@start}:#{@end}] #{@style}>" end |
#length ⇒ Integer
Returns Length of the span.
27 28 29 |
# File 'lib/rich/text.rb', line 27 def length @end - @start end |
#overlaps?(start_pos, end_pos) ⇒ Boolean
Check if span overlaps with a range
32 33 34 |
# File 'lib/rich/text.rb', line 32 def overlaps?(start_pos, end_pos) @start < end_pos && @end > start_pos end |