Class: IRB::Color::ColorizeVisitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/irb/color.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeColorizeVisitor

Returns a new instance of ColorizeVisitor.



231
232
233
# File 'lib/irb/color.rb', line 231

def initialize
  @tokens = []
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



230
231
232
# File 'lib/irb/color.rb', line 230

def tokens
  @tokens
end

Instance Method Details

#dispatch(location, type) ⇒ Object



235
236
237
238
239
# File 'lib/irb/color.rb', line 235

def dispatch(location, type)
  if location
    @tokens << [location.start_line, location.start_column, 1, location.end_line, location.end_column, type, location.slice]
  end
end

#visit_array_node(node) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/irb/color.rb', line 241

def visit_array_node(node)
  if node.opening&.match?(/\A%[iI]/)
    dispatch node.opening_loc, :symbol
    dispatch node.closing_loc, :symbol
  end
  super
end

#visit_def_node(node) ⇒ Object



249
250
251
252
# File 'lib/irb/color.rb', line 249

def visit_def_node(node)
  dispatch node.name_loc, :method_name
  super
end

#visit_interpolated_symbol_node(node) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/irb/color.rb', line 254

def visit_interpolated_symbol_node(node)
  dispatch node.opening_loc, :symbol
  node.parts.each do |part|
    case part
    when Prism::StringNode
      dispatch part.content_loc, :symbol
    when Prism::EmbeddedStatementsNode
      dispatch part.opening_loc, :symbol
      dispatch part.closing_loc, :symbol
    when Prism::EmbeddedVariableNode
      dispatch part.operator_loc, :symbol
    end
  end
  dispatch node.closing_loc, :symbol
  super
end

#visit_symbol_node(node) ⇒ Object



271
272
273
274
275
276
277
278
279
280
# File 'lib/irb/color.rb', line 271

def visit_symbol_node(node)
  if (node.opening_loc.nil? && node.closing == ':') || node.closing&.match?(/\A['"]:\z/)
    # Colorize { symbol: 1 } and { 'symbol': 1 } as label
    dispatch node.location, :LABEL
  else
    dispatch node.opening_loc, :symbol
    dispatch node.value_loc, :symbol
    dispatch node.closing_loc, :symbol
  end
end