Class: ThemeCheck::Position

Inherits:
Object
  • Object
show all
Includes:
PositionHelper
Defined in:
lib/theme_check/position.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PositionHelper

#bounded, #from_index_to_row_column, #from_row_column_to_index

Constructor Details

#initialize(needle_arg, contents_arg, line_number_1_indexed: nil, node_markup: nil, node_markup_offset: 0) ⇒ Position

Returns a new instance of Position.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/theme_check/position.rb', line 9

def initialize(
  needle_arg,
  contents_arg,
  line_number_1_indexed: nil,
  node_markup: nil,
  node_markup_offset: 0 # the index of markup inside the node_markup
)
  @needle = needle_arg

  @contents = if contents_arg&.is_a?(String) && !contents_arg.empty?
    contents_arg
  else
    ''
  end

  @line_number_1_indexed = line_number_1_indexed
  @node_markup_offset = node_markup_offset
  @node_markup = node_markup
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



7
8
9
# File 'lib/theme_check/position.rb', line 7

def contents
  @contents
end

Instance Method Details

#content_line_countObject



75
76
77
# File 'lib/theme_check/position.rb', line 75

def content_line_count
  @content_line_count ||= contents.count("\n")
end

#end_columnObject



71
72
73
# File 'lib/theme_check/position.rb', line 71

def end_column
  strict_position.end_column
end

#end_indexObject

0-indexed, exclusive



51
52
53
# File 'lib/theme_check/position.rb', line 51

def end_index
  strict_position.end_index
end

#end_rowObject

0-indexed, exclusive (both taken together are) therefore you might end up on a newline character or the next line



67
68
69
# File 'lib/theme_check/position.rb', line 67

def end_row
  strict_position.end_row
end

#start_columnObject

0-indexed, inclusive



61
62
63
# File 'lib/theme_check/position.rb', line 61

def start_column
  strict_position.start_column
end

#start_indexObject

0-indexed, inclusive



46
47
48
# File 'lib/theme_check/position.rb', line 46

def start_index
  contents.index(needle, start_offset)
end

#start_line_offsetObject



29
30
31
# File 'lib/theme_check/position.rb', line 29

def start_line_offset
  @start_line_offset ||= from_row_column_to_index(contents, line_number, 0)
end

#start_offsetObject



33
34
35
# File 'lib/theme_check/position.rb', line 33

def start_offset
  @start_offset ||= compute_start_offset
end

#start_rowObject

0-indexed, inclusive



56
57
58
# File 'lib/theme_check/position.rb', line 56

def start_row
  strict_position.start_row
end

#strict_positionObject



37
38
39
40
41
42
43
# File 'lib/theme_check/position.rb', line 37

def strict_position
  @strict_position ||= StrictPosition.new(
    needle,
    contents,
    start_index,
  )
end