Sha256: 3aa3e4e7a7f8591103b853643a76025c7300aa992b2f4da500388134f082cf94

Contents?: true

Size: 712 Bytes

Versions: 3

Compression:

Stored size: 712 Bytes

Contents

module Hotcell
  class Source
    PACK_MODE = 'c*'

    attr_reader :source, :file

    def self.wrap source, *args
      source.is_a?(Hotcell::Source) ? source : Source.new(source, *args)
    end

    def initialize source, file = nil
      @source, @file = source, file
    end

    def encoding
      'UTF-8'
    end

    def data
      @data ||= source.unpack(PACK_MODE)
    end

    def info position
      parsed = data[0..position]
      line = parsed.count(10) + 1
      lastnl = (parsed.rindex(10) || -1) + 1
      column = parsed[lastnl..position].pack(PACK_MODE).force_encoding(encoding).size
      { line: line, column: column }
    end

    def inspect
      "Source: `#{source}`"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hotcell-0.3.0 lib/hotcell/source.rb
hotcell-0.2.0 lib/hotcell/source.rb
hotcell-0.1.0 lib/hotcell/source.rb