Module: ThemeCheck::PositionHelper

Included in:
LanguageServer::CompletionEngine, LanguageServer::DocumentLinkEngine, Position
Defined in:
lib/theme_check/position_helper.rb

Instance Method Summary collapse

Instance Method Details

#bounded(a, x, b) ⇒ Object



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

def bounded(a, x, b)
  [a, [x, b].min].max
end

#from_index_to_row_column(content, index) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/theme_check/position_helper.rb', line 23

def from_index_to_row_column(content, index)
  return [0, 0] unless content.is_a?(String) && !content.empty?
  return [0, 0] unless index.is_a?(Integer)
  safe_index = bounded(0, index, content.size - 1)
  lines = content[0..safe_index].lines
  row = lines.size - 1
  col = lines.last.size - 1
  [row, col]
end

#from_row_column_to_index(content, row, col) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/theme_check/position_helper.rb', line 6

def from_row_column_to_index(content, row, col)
  return 0 unless content.is_a?(String) && !content.empty?
  return 0 unless row.is_a?(Integer) && col.is_a?(Integer)
  i = 0
  result = 0
  safe_row = bounded(0, row, content.lines.size - 1)
  lines = content.lines
  line_size = lines[i].size
  while i < safe_row
    result += line_size
    i += 1
    line_size = lines[i].size
  end
  result += bounded(0, col, line_size - 1)
  result
end