Class: RBI::Printer

Inherits:
Visitor show all
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

: (?out: (IO | StringIO), ?indent: Integer, ?print_locs: bool, ?max_line_length: Integer?) -> void



21
22
23
24
25
26
27
28
29
# File 'lib/rbi/printer.rb', line 21

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)

: Integer



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

def current_indent
  @current_indent
end

#in_visibility_groupObject

: bool



9
10
11
# File 'lib/rbi/printer.rb', line 9

def in_visibility_group
  @in_visibility_group
end

#max_line_lengthObject (readonly)

: Integer?



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

def max_line_length
  @max_line_length
end

#previous_nodeObject (readonly)

: Node?



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

def previous_node
  @previous_node
end

: bool



9
10
11
# File 'lib/rbi/printer.rb', line 9

def print_locs
  @print_locs
end

Instance Method Details

#dedentObject

: -> void



39
40
41
# File 'lib/rbi/printer.rb', line 39

def dedent
  @current_indent -= 2
end

#indentObject

: -> void



34
35
36
# File 'lib/rbi/printer.rb', line 34

def indent
  @current_indent += 2
end

Print a string without indentation nor ‘n` at the end. : (String string) -> void



45
46
47
# File 'lib/rbi/printer.rb', line 45

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

#printl(string) ⇒ Object

Print a string with indentation and ‘n` at the end. : (String string) -> void



65
66
67
68
# File 'lib/rbi/printer.rb', line 65

def printl(string)
  printt
  printn(string)
end

#printn(string = nil) ⇒ Object

Print a string without indentation but with a ‘n` at the end. : (?String? string) -> void



51
52
53
54
# File 'lib/rbi/printer.rb', line 51

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

#printt(string = nil) ⇒ Object

Print a string with indentation but without a ‘n` at the end. : (?String? string) -> void



58
59
60
61
# File 'lib/rbi/printer.rb', line 58

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

#visit_all(nodes) ⇒ Object

: (Array nodes) -> void



72
73
74
75
76
77
78
79
80
# File 'lib/rbi/printer.rb', line 72

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

: (File file) -> void



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rbi/printer.rb', line 84

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