Class: BetterHtml::Tokenizer::Location

Inherits:
Parser::Source::Range
  • Object
show all
Defined in:
lib/better_html/tokenizer/location.rb

Instance Method Summary collapse

Constructor Details

#initialize(buffer, begin_pos, end_pos) ⇒ Location

Returns a new instance of Location.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/better_html/tokenizer/location.rb', line 9

def initialize(buffer, begin_pos, end_pos)
  raise ArgumentError,
    "first argument must be Parser::Source::Buffer" unless buffer.is_a?(::Parser::Source::Buffer)

  if begin_pos > buffer.source.size
    raise ArgumentError,
      "begin_pos location #{begin_pos} is out of range for document of size #{buffer.source.size}"
  end

  if (end_pos - 1) > buffer.source.size
    raise ArgumentError,
      "end_pos location #{end_pos} is out of range for document of size #{buffer.source.size}"
  end

  super(buffer, begin_pos, end_pos)
end

Instance Method Details

#adjust(begin_pos: 0, end_pos: 0) ⇒ Object



50
51
52
# File 'lib/better_html/tokenizer/location.rb', line 50

def adjust(begin_pos: 0, end_pos: 0)
  self.class.new(@source_buffer, @begin_pos + begin_pos, @end_pos + end_pos)
end

#beginObject



62
63
64
# File 'lib/better_html/tokenizer/location.rb', line 62

def begin
  with(end_pos: @begin_pos)
end

#endObject



66
67
68
# File 'lib/better_html/tokenizer/location.rb', line 66

def end
  with(begin_pos: @end_pos)
end

#line_rangeObject



30
31
32
# File 'lib/better_html/tokenizer/location.rb', line 30

def line_range
  Range.new(start_line, stop_line)
end

#line_source_with_underlineObject



39
40
41
42
43
44
# File 'lib/better_html/tokenizer/location.rb', line 39

def line_source_with_underline
  spaces = source_line.scan(/\A\s*/).first
  column_without_spaces = [column - spaces.length, 0].max
  underscore_length = (end_pos - begin_pos).clamp(1, (source_line.length - column_without_spaces))
  "#{source_line.gsub(/\A\s*/, "")}\n#{" " * column_without_spaces}#{"^" * underscore_length}"
end

#offset(offset) ⇒ Object



58
59
60
# File 'lib/better_html/tokenizer/location.rb', line 58

def offset(offset)
  with(begin_pos: offset + @begin_pos, end_pos: offset + @end_pos)
end

#rangeObject



26
27
28
# File 'lib/better_html/tokenizer/location.rb', line 26

def range
  Range.new(begin_pos, end_pos, true)
end

#resize(new_size) ⇒ Object



54
55
56
# File 'lib/better_html/tokenizer/location.rb', line 54

def resize(new_size)
  with(end_pos: @begin_pos + new_size)
end

#with(begin_pos: @begin_pos, end_pos: @end_pos) ⇒ Object



46
47
48
# File 'lib/better_html/tokenizer/location.rb', line 46

def with(begin_pos: @begin_pos, end_pos: @end_pos)
  self.class.new(@source_buffer, begin_pos, end_pos)
end