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.



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

def initialize
  @tokens = []
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Instance Method Details

#dispatch(location, type) ⇒ Object



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

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_alias_method_node(node) ⇒ Object



255
256
257
258
259
# File 'lib/irb/color.rb', line 255

def visit_alias_method_node(node)
  dispatch_alias_method_name node.new_name
  dispatch_alias_method_name node.old_name
  super
end

#visit_array_node(node) ⇒ Object



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

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_call_node(node) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/irb/color.rb', line 261

def visit_call_node(node)
  if node.call_operator_loc.nil? && OPERATORS.include?(node.name)
    # Operators should not be colored as method call
  elsif (node.call_operator_loc.nil? || node.call_operator_loc.slice == "::") &&
      /\A\p{Upper}/.match?(node.name)
    # Constant-like methods should not be colored as method call
  else
    dispatch node.message_loc, :message_name
  end
  super
end

#visit_call_operator_write_node(node) ⇒ Object Also known as: visit_call_and_write_node, visit_call_or_write_node



273
274
275
276
# File 'lib/irb/color.rb', line 273

def visit_call_operator_write_node(node)
  dispatch node.message_loc, :message_name
  super
end

#visit_def_node(node) ⇒ Object



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

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

#visit_interpolated_symbol_node(node) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/irb/color.rb', line 280

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



297
298
299
300
301
302
303
304
305
306
# File 'lib/irb/color.rb', line 297

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