Sha256: 00168de0b2f690245f27a0e66f9b98d2eb8e1e0e953f48a2ca4ba78f125eb4cc

Contents?: true

Size: 1.01 KB

Versions: 15

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true
# Note: Everything is 0-indexed here.

module ThemeCheck
  module PositionHelper
    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

    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

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
theme-check-1.5.1 lib/theme_check/position_helper.rb
theme-check-1.5.0 lib/theme_check/position_helper.rb
theme-check-1.4.0 lib/theme_check/position_helper.rb
theme-check-1.3.0 lib/theme_check/position_helper.rb
theme-check-1.2.0 lib/theme_check/position_helper.rb
theme-check-1.1.0 lib/theme_check/position_helper.rb
theme-check-1.0.0 lib/theme_check/position_helper.rb
theme-check-0.10.2 lib/theme_check/position_helper.rb
theme-check-0.10.1 lib/theme_check/position_helper.rb
theme-check-0.10.0 lib/theme_check/position_helper.rb
theme-check-0.9.1 lib/theme_check/position_helper.rb
theme-check-0.9.0 lib/theme_check/position_helper.rb
theme-check-0.8.3 lib/theme_check/position_helper.rb
theme-check-0.8.2 lib/theme_check/position_helper.rb
theme-check-0.8.1 lib/theme_check/position_helper.rb