Class: Liquid2::StaticAnalysis::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid2/static_analysis.rb

Overview

The location of a variable, tag or filter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_name, index) ⇒ Span

Returns a new instance of Span.



10
11
12
13
# File 'lib/liquid2/static_analysis.rb', line 10

def initialize(template_name, index)
  @template_name = template_name
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



8
9
10
# File 'lib/liquid2/static_analysis.rb', line 8

def index
  @index
end

#template_nameObject (readonly)

Returns the value of attribute template_name.



8
9
10
# File 'lib/liquid2/static_analysis.rb', line 8

def template_name
  @template_name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



35
36
37
38
39
# File 'lib/liquid2/static_analysis.rb', line 35

def ==(other)
  self.class == other.class &&
    @template_name == other.template_name &&
    @index == other.index
end

#hashObject



43
44
45
# File 'lib/liquid2/static_analysis.rb', line 43

def hash
  [@template_name, @index].hash
end

#line_col(source) ⇒ [Integer, Integer]

Returns The line and column number of this span in source.

Parameters:

  • Template source text.

Returns:

  • The line and column number of this span in source.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/liquid2/static_analysis.rb', line 17

def line_col(source)
  lines = source.lines
  cumulative_length = 0
  target_line_index = -1

  lines.each_with_index do |line, i|
    cumulative_length += line.length
    next unless @index < cumulative_length

    target_line_index = i
    line_number = target_line_index + 1
    column_number = @index - (cumulative_length - lines[target_line_index].length)
    return [line_number, column_number]
  end

  raise "index is out of bounds for span"
end