Sha256: 8484803e1e479ed3da2a9f5aeaa2d5d0391e6b0b7bf52f8c7bc67277f318151d

Contents?: true

Size: 1.21 KB

Versions: 25

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module PlatformosCheck
  Token = Struct.new(
    :content,
    :start, # inclusive
    :end # exclusive
  )

  TAG_START = Liquid::TagStart
  TAG_END = Liquid::TagEnd
  VARIABLE_START = Liquid::VariableStart
  VARIABLE_END = Liquid::VariableEnd
  SPLITTER = %r{
    (?=(?:#{TAG_START}|#{VARIABLE_START}))| # positive lookahead on tag/variable start
    (?<=(?:#{TAG_END}|#{VARIABLE_END}))     # positive lookbehind on tag/variable end
  }xom

  # Implemented as an Enumerable so we stop iterating on the find once
  # we have what we want. Kind of a perf thing.
  class Tokens
    include Enumerable

    def initialize(buffer)
      @buffer = buffer
    end

    def each(&block)
      return to_enum(:each) unless block

      chunks = @buffer.split(SPLITTER)
      chunks.shift if chunks[0] && chunks[0].empty?

      prev = Token.new('', 0, 0)
      curr = Token.new('', 0, 0)

      while (content = chunks.shift)

        curr.start = prev.end
        curr.end = curr.start + content.size

        yield(Token.new(
          content,
          curr.start,
          curr.end
        ))

        # recycling structs
        tmp = prev
        prev = curr
        curr = tmp
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.2.0 lib/platformos_check/language_server/tokens.rb
platformos-check-0.1.0 lib/platformos_check/language_server/tokens.rb
platformos-check-0.0.3 lib/platformos_check/language_server/tokens.rb
platformos-check-0.0.2 lib/platformos_check/language_server/tokens.rb
platformos-check-0.0.1 lib/platformos_check/language_server/tokens.rb