Class: TreeStand::Utils::Printer

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tree_stand/utils/printer.rb

Overview

Used to pretty-print the node.

Examples:

pp node
# (expression
#  (sum
#   left: (number)              | 1
#   ("+")                       | +
#   right: (variable)))         | x

Instance Method Summary collapse

Constructor Details

#initialize(ralign:) ⇒ void

Parameters:

  • ralign (Integer)

    the right alignment for the text column.



21
22
23
# File 'lib/tree_stand/utils/printer.rb', line 21

def initialize(ralign:)
  @ralign = ralign
end

Instance Method Details

Parameters:

  • node (TreeStand::Node)
  • io (IO, StringIO, String) (defaults to: StringIO.new)

Returns:

  • (IO, StringIO, String)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tree_stand/utils/printer.rb', line 27

def print(node, io: StringIO.new)
  lines = pretty_output_lines(node)

  lines.each do |line|
    if line.text.empty?
      io << line.sexpr << "\n"
      next
    end

    io << "#{line.sexpr}#{" " * (@ralign - line.sexpr.size)}| #{line.text}\n"
  end

  io
end