class IRB::ExtendCommand::ShowSource

def find_end(file, first_line, irb_context)

def find_end(file, first_line, irb_context)
  return first_line unless File.exist?(file)
  lex = RubyLex.new(irb_context)
  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)
    if !continue && syntax == :valid
      return first_line + lnum
    end
  end
  first_line
end