Class: PlatformosCheck::Tokens

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/platformos_check/language_server/tokens.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ Tokens

Returns a new instance of Tokens.



24
25
26
# File 'lib/platformos_check/language_server/tokens.rb', line 24

def initialize(buffer)
  @buffer = buffer
end

Instance Method Details

#each(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/platformos_check/language_server/tokens.rb', line 28

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