Class: RBI::Printer

Inherits:
Visitor show all
Defined in:
lib/rbi/printer.rb

Constant Summary collapse

MAX_CACHED_INDENT =

Pre-computed indentation strings to avoid allocating “ ” * indent on every line.

50
INDENT_CACHE =

: Integer

Array.new(MAX_CACHED_INDENT + 1) { |i| (" " * i).freeze }.freeze

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 | String), ?indent: Integer, ?print_locs: bool, ?max_line_length: Integer?) -> void



25
26
27
28
29
30
31
32
33
# File 'lib/rbi/printer.rb', line 25

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 = false #: bool
  @previous_node = nil #: Node?
  @max_line_length = max_line_length
end

Instance Attribute Details

#current_indentObject (readonly)

: Integer



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

def current_indent
  @current_indent
end

#in_visibility_groupObject

: bool



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

def in_visibility_group
  @in_visibility_group
end

#max_line_lengthObject (readonly)

: Integer?



22
23
24
# File 'lib/rbi/printer.rb', line 22

def max_line_length
  @max_line_length
end

#previous_nodeObject (readonly)

: Node?



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

def previous_node
  @previous_node
end

: bool



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

def print_locs
  @print_locs
end

Instance Method Details

#current_indent_stringObject

: -> String



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

def current_indent_string
  INDENT_CACHE[@current_indent] || " " * @current_indent
end

#dedentObject

: -> void



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

def dedent
  @current_indent -= 2
end

#indentObject

: -> void



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

def indent
  @current_indent += 2
end

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



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

def print(string)
  @out << string
end

#printl(string) ⇒ Object

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



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

def printl(string)
  @out << current_indent_string
  @out << string
  @out << "\n"
end

#printn(string = nil) ⇒ Object

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



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

def printn(string = nil)
  if string
    @out << string
  end
  @out << "\n"
end

#printt(string = nil) ⇒ Object

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



69
70
71
72
# File 'lib/rbi/printer.rb', line 69

def printt(string = nil)
  @out << current_indent_string
  @out << string if string
end

#visit_all(nodes) ⇒ Object

: (Array nodes) -> void



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

def visit_all(nodes)
  return if nodes.empty?

  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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rbi/printer.rb', line 98

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

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