Module: RDoc::Parser::RubyColorizer
- Defined in:
- lib/rdoc/parser/ruby_colorizer.rb
Overview
Ruby code syntax highlighter. Colorize result is an array of RDoc::Parser::RubyColorizer::ColoredToken Actual color for each token kind is determined elsewhere (e.g., HTML generator)
Defined Under Namespace
Classes: ColoredToken
Constant Summary collapse
- OP_TOKENS =
Prism operator token types except assignment ‘=’
%i[ AMPERSAND AMPERSAND_AMPERSAND BANG BANG_EQUAL BANG_TILDE CARET COLON COLON_COLON EQUAL_EQUAL EQUAL_GREATER EQUAL_TILDE GREATER GREATER_GREATER LESS LESS_EQUAL LESS_EQUAL_GREATER LESS_LESS MINUS MINUS_GREATER PERCENT PIPE PIPE_PIPE PLUS QUESTION_MARK SLASH STAR STAR_STAR TILDE UAMPERSAND UMINUS UPLUS USTAR USTAR_STAR ].to_set
- TOKEN_TYPE_MAP =
Prism token type to ColoredToken kind map
{ IDENTIFIER: :identifier, METHOD_NAME: :identifier, INSTANCE_VARIABLE: :ivar, CLASS_VARIABLE: :identifier, GLOBAL_VARIABLE: :identifier, BACK_REFERENCE: :identifier, NUMBERED_REFERENCE: :identifier, CONSTANT: :constant, LABEL: :value, INTEGER: :value, INTEGER_IMAGINARY: :value, INTEGER_RATIONAL: :value, INTEGER_RATIONAL_IMAGINARY: :value, FLOAT: :value, FLOAT_IMAGINARY: :value, FLOAT_RATIONAL: :value, FLOAT_RATIONAL_IMAGINARY: :value, COMMENT: :comment, EMBDOC_BEGIN: :comment, EMBDOC_LINE: :comment, EMBDOC_END: :comment }
Class Method Summary collapse
-
.colorize(code) ⇒ Object
Colorize the entire
codeand returns colored token stream. -
.partial_colorize(whole_code, node, prism_tokens, start_offset = nil, end_offset = nil) ⇒ Object
Colorize partial
nodeinwhole_codeand returns colored token stream.
Class Method Details
.colorize(code) ⇒ Object
Colorize the entire code and returns colored token stream.
53 54 55 56 57 58 |
# File 'lib/rdoc/parser/ruby_colorizer.rb', line 53 def colorize(code) result = Prism.parse_lex(code) program_node, unordered_tokens = result.value prism_tokens = unordered_tokens.map(&:first).sort_by! { |token| token.location.start_offset } partial_colorize(code, program_node, prism_tokens, 0, code.bytesize) end |
.partial_colorize(whole_code, node, prism_tokens, start_offset = nil, end_offset = nil) ⇒ Object
Colorize partial node in whole_code and returns colored token stream.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rdoc/parser/ruby_colorizer.rb', line 61 def partial_colorize(whole_code, node, prism_tokens, start_offset = nil, end_offset = nil) start_offset ||= node.location.start_offset end_offset ||= node.location.end_offset visitor = NodeColorizeVisitor.new node.accept(visitor) prior_tokens = visitor.tokens.sort_by {|_, start_offset, _| start_offset } normal_tokens = normal_tokens(slice_by_location(prism_tokens, start_offset, end_offset)) colored_tokens = unify_tokens(whole_code, prior_tokens, normal_tokens, start_offset, end_offset) colored_tokens.unshift(ColoredToken.new(:plain, ' ' * node.location.start_column)) if node.location.start_column > 0 colored_tokens end |