class IRB::SourceFinder::Source
def binary_file?
def binary_file? # If the line is zero, it means that the target's source is probably in a binary file. @line.zero? end
def colorized_content
def colorized_content if !binary_file? && file_exist? # To correctly colorize, we need to colorize full content and extract the relevant lines. colored_lines = IRB::Color.colorize_code(file_content).lines # Handle wrong line number case: line_no passed to eval is wrong, file is edited after load, etc return if colored_lines.size < @line colored_lines[@line - 1...find_end].join elsif @ast_source IRB::Color.colorize_code(@ast_source) end end
def file_content
def file_content @file_content ||= File.read(@file) end
def file_exist?
def file_exist? File.exist?(@file) end
def find_end
def find_end lex = RubyLex.new code = file_content lines = code.lines[(@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 @line + lnum end end @line end
def initialize(file, line, ast_source = nil)
def initialize(file, line, ast_source = nil) @file = file @line = line @ast_source = ast_source end