Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/syntax_suggest/clean_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module SyntaxSuggest
class CleanDocument
def initialize(source:)
lines = clean_sweep(source: source)
@document = CodeLine.from_source(lines.join, lines: lines)
@document = CodeLine.from_source(lines.join)
end

# Call all of the document "cleaners"
Expand Down
7 changes: 3 additions & 4 deletions lib/syntax_suggest/code_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ class CodeLine

# Returns an array of CodeLine objects
# from the source string
def self.from_source(source, lines: nil)
lines ||= source.lines
lex_array_for_line = LexAll.new(source: source, source_lines: lines).each_with_object(Hash.new { |h, k| h[k] = [] }) { |lex, hash| hash[lex.line] << lex }
lines.map.with_index do |line, index|
def self.from_source(source)
lex_array_for_line = LexAll.new(source: source).each_with_object(Hash.new { |h, k| h[k] = [] }) { |lex, hash| hash[lex.line] << lex }
source.lines.map.with_index do |line, index|
CodeLine.new(
line: line,
index: index,
Expand Down
23 changes: 2 additions & 21 deletions lib/syntax_suggest/lex_all.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# frozen_string_literal: true

module SyntaxSuggest
# 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.
# Lexes the whole source and wraps the tokens in `LexValue`
#
# Example usage:
#
Expand All @@ -18,21 +12,8 @@ module SyntaxSuggest
class LexAll
include Enumerable

def initialize(source:, source_lines: nil)
def initialize(source:)
@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|
Expand Down