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.



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

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

#locationObject (readonly)

Returns the value of attribute location.



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

def location
  @location
end

#replacementObject (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_rangeObject



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

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

#char_rangeObject



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

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

#coordinatesObject



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

def coordinates
  location.coordinates
end

#file_char_rangeObject



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_nameObject



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

#inspectObject



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

#stripObject



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