123456789_123456789_123456789_123456789_123456789_

Class: SyntaxSuggest::LexAll

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
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

Instance Method Summary

Constructor Details

.new(source:, source_lines: nil) ⇒ LexAll

[ GitHub ]

  
# 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)

See additional method definition at line 44.

[ GitHub ]

  
# File 'lib/syntax_suggest/lex_all.rb', line 48

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

Instance Method Details

#[](index)

[ GitHub ]

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

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

#each

[ GitHub ]

  
# 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

#last

[ GitHub ]

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

def last
  @lex.last
end

#to_a

[ GitHub ]

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

def to_a
  @lex
end