Class: IRB::SourceFinder::Source
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/irb/source_finder.rb |
Class Method Summary
- .new(file, line, ast_source = nil) ⇒ Source constructor
Instance Attribute Summary
- #binary_file? ⇒ Boolean readonly
- #file readonly
- #file_exist? ⇒ Boolean readonly
- #line readonly
Instance Method Summary
- #colorized_content
- #file_content
- #find_end private
Constructor Details
.new(file, line, ast_source = nil) ⇒ Source
Instance Attribute Details
#binary_file? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/irb/source_finder.rb', line 21
def binary_file? # If the line is zero, it means that the target's source is probably in a binary file. @line.zero? end
#file (readonly)
[ GitHub ]# File 'lib/irb/source_finder.rb', line 10
attr_reader :file, :line
#file_exist? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/irb/source_finder.rb', line 17
def file_exist? File.exist?(@file) end
#line (readonly)
[ GitHub ]# File 'lib/irb/source_finder.rb', line 10
attr_reader :file, :line
Instance Method Details
#colorized_content
[ GitHub ]# File 'lib/irb/source_finder.rb', line 30
def colorized_content if !binary_file? && file_exist? end_line = find_end # To correctly colorize, we need to colorize full content and extract the relevant lines. colored = IRB::Color.colorize_code(file_content) colored.lines[@line - 1...end_line].join elsif @ast_source IRB::Color.colorize_code(@ast_source) end end
#file_content
[ GitHub ]# File 'lib/irb/source_finder.rb', line 26
def file_content @file_content ||= File.read(@file) end
#find_end (private)
[ GitHub ]# File 'lib/irb/source_finder.rb', line 43
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