Class: Spellr::Token
Instance Attribute Summary collapse
-
#line ⇒ Object
Returns the value of attribute line.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#replacement ⇒ Object
readonly
Returns the value of attribute replacement.
Class Method Summary collapse
Instance Method Summary collapse
- #byte_range ⇒ Object
- #char_range ⇒ Object
- #coordinates ⇒ Object
- #file_char_range ⇒ Object
- #file_name ⇒ Object
- #highlight(range = char_range) ⇒ Object
-
#initialize(string, line: string, location: ColumnLocation.new) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
- #lstripped_column_location(lstripped) ⇒ Object
- #replace(replacement) ⇒ Object
- #strip ⇒ Object
Methods inherited from String
Constructor Details
#initialize(string, line: string, location: ColumnLocation.new) ⇒ Token
Returns a new instance of Token.
26 27 28 29 30 |
# File 'lib/spellr/token.rb', line 26 def initialize(string, line: string, location: ColumnLocation.new) @location = location @line = line super(string) end |
Instance Attribute Details
#line ⇒ Object
Returns the value of attribute line.
18 19 20 |
# File 'lib/spellr/token.rb', line 18 def line @line end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
18 19 20 |
# File 'lib/spellr/token.rb', line 18 def location @location end |
#replacement ⇒ Object (readonly)
Returns the value of attribute replacement.
18 19 20 |
# File 'lib/spellr/token.rb', line 18 def replacement @replacement end |
Class Method Details
.wrap(value) ⇒ Object
20 21 22 23 24 |
# File 'lib/spellr/token.rb', line 20 def self.wrap(value) return value if value.is_a?(Spellr::Token) Spellr::Token.new(value || '') end |
Instance Method Details
#byte_range ⇒ Object
61 62 63 |
# File 'lib/spellr/token.rb', line 61 def byte_range @byte_range ||= location.byte_offset...(location.byte_offset + bytesize) end |
#char_range ⇒ Object
57 58 59 |
# File 'lib/spellr/token.rb', line 57 def char_range @char_range ||= location.char_offset...(location.char_offset + length) end |
#coordinates ⇒ Object
69 70 71 |
# File 'lib/spellr/token.rb', line 69 def coordinates location.coordinates end |
#file_char_range ⇒ Object
65 66 67 |
# File 'lib/spellr/token.rb', line 65 def file_char_range @file_char_range ||= location.absolute_char_offset...(location.absolute_char_offset + length) end |
#file_name ⇒ Object
82 83 84 |
# File 'lib/spellr/token.rb', line 82 def file_name location.file_name end |
#highlight(range = char_range) ⇒ Object
73 74 75 |
# File 'lib/spellr/token.rb', line 73 def highlight(range = char_range) "#{slice(0...(range.first))}#{Spellr::StringFormat.red slice(range)}#{slice(range.last..-1)}" end |
#inspect ⇒ Object
53 54 55 |
# File 'lib/spellr/token.rb', line 53 def inspect "#<#{self.class.name} #{to_s.inspect} @#{location}>" end |
#lstripped_column_location(lstripped) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/spellr/token.rb', line 40 def lstripped_column_location(lstripped) ColumnLocation.new( byte_offset: bytesize - lstripped.bytesize, char_offset: length - lstripped.length, line_location: location.line_location ) end |
#replace(replacement) ⇒ Object
77 78 79 80 |
# File 'lib/spellr/token.rb', line 77 def replace(replacement) @replacement = replacement location.file.insert(replacement, file_char_range) end |
#strip ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/spellr/token.rb', line 32 def strip @strip ||= begin lstripped = lstrip new_column_location = lstripped_column_location(lstripped) Token.new(lstripped.rstrip, line: line, location: new_column_location) end end |