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)


7
8
9
10
11
12
13
# File 'lib/better_html/tokenizer/location.rb', line 7

def initialize(buffer, begin_pos, end_pos)
  raise ArgumentError, 'first argument must be Parser::Source::Buffer' unless buffer.is_a?(::Parser::Source::Buffer)
  raise ArgumentError, "begin_pos location #{begin_pos} is out of range for document of size #{buffer.source.size}" if begin_pos > buffer.source.size
  raise ArgumentError, "end_pos location #{end_pos} is out of range for document of size #{buffer.source.size}" if (end_pos - 1) > buffer.source.size

  super(buffer, begin_pos, end_pos)
end

Instance Method Details

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



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

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

#beginObject



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

def begin
  with(end_pos: @begin_pos)
end

#endObject



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

def end
  with(begin_pos: @end_pos)
end

#line_rangeObject



19
20
21
# File 'lib/better_html/tokenizer/location.rb', line 19

def line_range
  Range.new(start_line, stop_line)
end

#line_source_with_underlineObject



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

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, source_line.length - column_without_spaces].min, 1].max
  "#{source_line.gsub(/\A\s*/, '')}\n#{' ' * column_without_spaces}#{'^' * underscore_length}"
end

#offset(offset) ⇒ Object



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

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

#rangeObject



15
16
17
# File 'lib/better_html/tokenizer/location.rb', line 15

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

#resize(new_size) ⇒ Object



43
44
45
# File 'lib/better_html/tokenizer/location.rb', line 43

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

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



35
36
37
# File 'lib/better_html/tokenizer/location.rb', line 35

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