Sha256: d1fb182f337d153c664f3c2bcbcd3abed81940fe5e72b37c317574dc860d09a5

Contents?: true

Size: 1.12 KB

Versions: 11

Compression:

Stored size: 1.12 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
      safe_row = bounded(0, row, content.count("\n"))
      scanner = StringScanner.new(content)
      scanner.scan_until(/\n/) while i < safe_row && (i += 1)
      result = scanner.charpos || 0
      scanner.scan_until(/\n|\z/)
      bounded(result, result + col, scanner.pre_match.size)
    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)
      content_up_to_index = content[0...safe_index]
      row = content_up_to_index.count("\n")
      col = 0
      col += 1 while (safe_index -= 1) && safe_index >= 0 && content[safe_index] != "\n"
      [row, col]
    end

    def bounded(a, x, b)
      return a if x < a
      return b if x > b
      x
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
theme-check-1.9.2 lib/theme_check/position_helper.rb
theme-check-1.9.1 lib/theme_check/position_helper.rb
theme-check-1.9.0 lib/theme_check/position_helper.rb
theme-check-1.8.0 lib/theme_check/position_helper.rb
theme-check-1.7.2 lib/theme_check/position_helper.rb
theme-check-1.7.1 lib/theme_check/position_helper.rb
theme-check-1.7.0 lib/theme_check/position_helper.rb
theme-check-1.6.2 lib/theme_check/position_helper.rb
theme-check-1.6.1 lib/theme_check/position_helper.rb
theme-check-1.6.0 lib/theme_check/position_helper.rb
theme-check-1.5.2 lib/theme_check/position_helper.rb