class IRB::SourceFinder

def find_end(file, first_line)

def find_end(file, first_line)
  lex = RubyLex.new
  lines = File.read(file).lines[(first_line - 1)..-1]
  tokens = RubyLex.ripper_lex_without_warning(lines.join)
  prev_tokens = []
  # chunk with line number
  tokens.chunk { |tok| tok.pos[0] }.each do |lnum, chunk|
    code = lines[0..lnum].join
    prev_tokens.concat chunk
    continue = lex.should_continue?(prev_tokens)
    syntax = lex.check_code_syntax(code, local_variables: [])
    if !continue && syntax == :valid
      return first_line + lnum
    end
  end
  first_line
end