Class: Luoma::StaticAnalysis::Location
- Inherits:
-
Object
- Object
- Luoma::StaticAnalysis::Location
- Defined in:
- lib/luoma/static_analysis.rb,
sig/luoma/static_analysis.rbs
Overview
The location of a variable, tag or filter.
Instance Attribute Summary collapse
-
#template ⇒ Luoma::Template
readonly
Returns the value of attribute template.
-
#token ⇒ Luoma::t_token
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
- #hash ⇒ Object
-
#initialize(template, token) ⇒ Location
constructor
(Template, t_token) -> void.
-
#line_col(index) ⇒ [Numeric, Numeric]
Return the line and column number of the given index.
-
#span ⇒ [[Numeric, Numeric],[Numeric, Numeric]]
Return the line and column number for the start and end index spanning this location.
-
#value ⇒ String
Return the substring spanning this location.
Constructor Details
#initialize(template, token) ⇒ Location
(Template, t_token) -> void
11 12 13 14 |
# File 'lib/luoma/static_analysis.rb', line 11 def initialize(template, token) @template = template @token = token end |
Instance Attribute Details
#template ⇒ Luoma::Template (readonly)
Returns the value of attribute template.
8 9 10 |
# File 'lib/luoma/static_analysis.rb', line 8 def template @template end |
#token ⇒ Luoma::t_token (readonly)
Returns the value of attribute token.
8 9 10 |
# File 'lib/luoma/static_analysis.rb', line 8 def token @token end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
41 42 43 44 45 |
# File 'lib/luoma/static_analysis.rb', line 41 def ==(other) self.class == other.class && @template.name == other.template.name && @token == other.token end |
#hash ⇒ Object
49 50 51 |
# File 'lib/luoma/static_analysis.rb', line 49 def hash [@template.name, @token].hash end |
#line_col(index) ⇒ [Numeric, Numeric]
Return the line and column number of the given index.
(Integer) -> [Integer, Integer]
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/luoma/static_analysis.rb', line 19 def line_col(index) lines = @template.lines cumulative_length = 0 target_line_index = -1 lines.each_with_index do |line, i| cumulative_length += line.length if index < cumulative_length target_line_index = i break end end raise LuomaError.new("index out of bounds") if target_line_index == -1 line_number = target_line_index + 1 line = lines[target_line_index] column_number = index - (cumulative_length - line.length) [line_number, column_number] end |
#span ⇒ [[Numeric, Numeric],[Numeric, Numeric]]
Return the line and column number for the start and end index spanning this location.
() -> [[Integer, Integer],[Integer, Integer]]
57 58 59 |
# File 'lib/luoma/static_analysis.rb', line 57 def span [line_col(@token[1]), line_col(@token[2])] end |
#value ⇒ String
Return the substring spanning this location.
() -> String
64 65 66 |
# File 'lib/luoma/static_analysis.rb', line 64 def value Luoma.get_token_value(@token, @template.source) end |