Class: SyntaxSuggest::LexAll

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/syntax_suggest/lex_all.rb

Overview

Ripper.lex is not guaranteed to lex the entire source document

This class guarantees the whole document is lex-ed by iteratively lexing the document where ripper stopped.

Prism likely doesn’t have the same problem. Once ripper support is removed we can likely reduce the complexity here if not remove the whole concept.

Example usage:

lex = LexAll.new(source: source)
lex.each do |value|
  puts value.line
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, source_lines: nil) ⇒ LexAll

Returns a new instance of LexAll.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/syntax_suggest/lex_all.rb', line 21

def initialize(source:, source_lines: nil)
  @lex = self.class.lex(source, 1)
  lineno = @lex.last[0][0] + 1
  source_lines ||= source.lines
  last_lineno = source_lines.length

  until lineno >= last_lineno
    lines = source_lines[lineno..]

    @lex.concat(
      self.class.lex(lines.join, lineno + 1)
    )

    lineno = @lex.last[0].first + 1
  end

  last_lex = nil
  @lex.map! { |elem|
    last_lex = LexValue.new(elem[0].first, elem[1], elem[2], elem[3], last_lex)
  }
end

Class Method Details

.lex(source, line_number) ⇒ Object



44
45
46
# File 'lib/syntax_suggest/lex_all.rb', line 44

def self.lex(source, line_number)
  Prism.lex_compat(source, line: line_number).value.sort_by { |values| values[0] }
end

Instance Method Details

#[](index) ⇒ Object



64
65
66
# File 'lib/syntax_suggest/lex_all.rb', line 64

def [](index)
  @lex[index]
end

#eachObject



57
58
59
60
61
62
# File 'lib/syntax_suggest/lex_all.rb', line 57

def each
  return @lex.each unless block_given?
  @lex.each do |x|
    yield x
  end
end

#lastObject



68
69
70
# File 'lib/syntax_suggest/lex_all.rb', line 68

def last
  @lex.last
end

#to_aObject



53
54
55
# File 'lib/syntax_suggest/lex_all.rb', line 53

def to_a
  @lex
end