Class: Spellr::Token

Inherits:
String show all
Defined in:
lib/spellr/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from String

#spellr_normalize

Constructor Details

#initialize(string, line: string, location: ColumnLocation.new) ⇒ Token

Returns a new instance of Token.



25
26
27
28
29
# File 'lib/spellr/token.rb', line 25

def initialize(string, line: string, location: ColumnLocation.new)
  @location = location
  @line = line
  super(string)
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



17
18
19
# File 'lib/spellr/token.rb', line 17

def line
  @line
end

#locationObject (readonly)

Returns the value of attribute location.



17
18
19
# File 'lib/spellr/token.rb', line 17

def location
  @location
end

#replacementObject (readonly)

Returns the value of attribute replacement.



17
18
19
# File 'lib/spellr/token.rb', line 17

def replacement
  @replacement
end

Class Method Details

.wrap(value) ⇒ Object



19
20
21
22
23
# File 'lib/spellr/token.rb', line 19

def self.wrap(value)
  return value if value.is_a?(Spellr::Token)

  Spellr::Token.new(value || '')
end

Instance Method Details

#byte_rangeObject



60
61
62
# File 'lib/spellr/token.rb', line 60

def byte_range
  @byte_range ||= location.byte_offset...(location.byte_offset + bytesize)
end

#char_rangeObject



56
57
58
# File 'lib/spellr/token.rb', line 56

def char_range
  @char_range ||= location.char_offset...(location.char_offset + length)
end

#coordinatesObject



68
69
70
# File 'lib/spellr/token.rb', line 68

def coordinates
  location.coordinates
end

#file_char_rangeObject



64
65
66
# File 'lib/spellr/token.rb', line 64

def file_char_range
  @file_char_range ||= location.absolute_char_offset...(location.absolute_char_offset + length)
end

#file_nameObject



81
82
83
# File 'lib/spellr/token.rb', line 81

def file_name
  location.file_name
end

#highlight(range = char_range) ⇒ Object



72
73
74
# File 'lib/spellr/token.rb', line 72

def highlight(range = char_range)
  "#{slice(0...(range.first))}#{Spellr::StringFormat.red slice(range)}#{slice(range.last..-1)}"
end

#inspectObject



52
53
54
# File 'lib/spellr/token.rb', line 52

def inspect
  "#<#{self.class.name} #{to_s.inspect} @#{location}>"
end

#lstripped_column_location(lstripped) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/spellr/token.rb', line 39

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



76
77
78
79
# File 'lib/spellr/token.rb', line 76

def replace(replacement)
  @replacement = replacement
  location.file.insert(replacement, file_char_range)
end

#stripObject



31
32
33
34
35
36
37
# File 'lib/spellr/token.rb', line 31

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