Class: RBI::Printer

Inherits:
Visitor show all
Extended by:
T::Sig
Defined in:
lib/rbi/printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit

Constructor Details

#initialize(out: $stdout, indent: 0, print_locs: false, max_line_length: nil) ⇒ Printer

Returns a new instance of Printer.



30
31
32
33
34
35
36
37
38
# File 'lib/rbi/printer.rb', line 30

def initialize(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
  super()
  @out = out
  @current_indent = indent
  @print_locs = print_locs
  @in_visibility_group = T.let(false, T::Boolean)
  @previous_node = T.let(nil, T.nilable(Node))
  @max_line_length = max_line_length
end

Instance Attribute Details

#current_indentObject (readonly)

Returns the value of attribute current_indent.



17
18
19
# File 'lib/rbi/printer.rb', line 17

def current_indent
  @current_indent
end

#in_visibility_groupObject

Returns the value of attribute in_visibility_group.



11
12
13
# File 'lib/rbi/printer.rb', line 11

def in_visibility_group
  @in_visibility_group
end

#max_line_lengthObject (readonly)

Returns the value of attribute max_line_length.



20
21
22
# File 'lib/rbi/printer.rb', line 20

def max_line_length
  @max_line_length
end

#previous_nodeObject (readonly)

Returns the value of attribute previous_node.



14
15
16
# File 'lib/rbi/printer.rb', line 14

def previous_node
  @previous_node
end

Returns the value of attribute print_locs.



11
12
13
# File 'lib/rbi/printer.rb', line 11

def print_locs
  @print_locs
end

Instance Method Details

#dedentObject



48
49
50
# File 'lib/rbi/printer.rb', line 48

def dedent
  @current_indent -= 2
end

#indentObject



43
44
45
# File 'lib/rbi/printer.rb', line 43

def indent
  @current_indent += 2
end


54
55
56
# File 'lib/rbi/printer.rb', line 54

def print(string)
  @out.print(string)
end

#printl(string) ⇒ Object



74
75
76
77
# File 'lib/rbi/printer.rb', line 74

def printl(string)
  printt
  printn(string)
end

#printn(string = nil) ⇒ Object



60
61
62
63
# File 'lib/rbi/printer.rb', line 60

def printn(string = nil)
  print(string) if string
  print("\n")
end

#printt(string = nil) ⇒ Object



67
68
69
70
# File 'lib/rbi/printer.rb', line 67

def printt(string = nil)
  print(" " * @current_indent)
  print(string) if string
end

#visit_all(nodes) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/rbi/printer.rb', line 80

def visit_all(nodes)
  previous_node = @previous_node
  @previous_node = nil
  nodes.each do |node|
    visit(node)
    @previous_node = node
  end
  @previous_node = previous_node
end

#visit_file(file) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbi/printer.rb', line 91

def visit_file(file)
  strictness = file.strictness
  if strictness
    printl("# typed: #{strictness}")
  end
  unless file.comments.empty?
    printn if strictness
    visit_all(file.comments)
  end

  unless file.root.empty? && file.root.comments.empty?
    printn if strictness || !file.comments.empty?
    visit(file.root)
  end
end