Class: Byebug::SourceFileFormatter
- Inherits:
-
Object
- Object
- Byebug::SourceFileFormatter
- Defined in:
- lib/convenient_service/dependencies/extractions/byebug_syntax_highlighting.rb
Instance Method Summary collapse
-
#file ⇒ Object
This is a replacement of the internal
Byebug::SourceFileFormatter.file
method which adds syntax highlighting capability to it.
Instance Method Details
#file ⇒ Object
This is a replacement of the internal Byebug::SourceFileFormatter.file
method which adds syntax highlighting capability to it.
The original implementation simply returns the file path which is passed to the Byebug::SourceFileFormatter
constructor just as file
. (See github.com/deivid-rodriguez/byebug/blob/master/lib/byebug/source_file_formatter.rb#L13)
The current replacement, instead of returning the original file path, returns a copy of it, where the syntax is highlighted by the Rouge gem(A pure Ruby code highlighter). (See github.com/rouge-ruby/rouge)
In order to create a copy, it utilizes Ruby’s tempfile
stdlib. (See ruby-doc.org/stdlib-2.7.0/libdoc/tempfile/rdoc/Tempfile.html) A tempfile is automatically deleted from the underlying OS when it is garbage-collected.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/convenient_service/dependencies/extractions/byebug_syntax_highlighting.rb', line 30 def file @highlighted_file ||= begin if defined? Rouge source = File.read(@file) theme = Rouge::Themes::Monokai.new formatter = Rouge::Formatters::Terminal256.new(theme) lexer = Rouge::Lexers::Ruby.new dest = formatter.format(lexer.lex(source)) # Tempfile with the highlighted syntax is assigned to the instance variable # in order to prevent its premature garbage collection. @tempfile_with_highlighted_syntax = Tempfile.new.tap { |t| t.write(dest) }.tap(&:close) @tempfile_with_highlighted_syntax.path else warn %q{Rouge(a pure Ruby code highlighter) is not defined. Maybe you forgot to require it? (require 'rouge')} @file end end end |