Class: RBI::Printer
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
-
#current_indent ⇒ Object
readonly
: Integer.
-
#in_visibility_group ⇒ Object
: bool.
-
#max_line_length ⇒ Object
readonly
: Integer?.
-
#previous_node ⇒ Object
readonly
: Node?.
-
#print_locs ⇒ Object
: bool.
Instance Method Summary collapse
-
#current_indent_string ⇒ Object
: -> String.
-
#dedent ⇒ Object
: -> void.
-
#indent ⇒ Object
: -> void.
-
#initialize(out: $stdout, indent: 0, print_locs: false, max_line_length: nil) ⇒ Printer
constructor
: (?out: (IO | StringIO | String), ?indent: Integer, ?print_locs: bool, ?max_line_length: Integer?) -> void.
-
#print(string) ⇒ Object
Print a string without indentation nor ‘n` at the end.
-
#printl(string) ⇒ Object
Print a string with indentation and ‘n` at the end.
-
#printn(string = nil) ⇒ Object
Print a string without indentation but with a ‘n` at the end.
-
#printt(string = nil) ⇒ Object
Print a string with indentation but without a ‘n` at the end.
-
#visit_all(nodes) ⇒ Object
: (Array nodes) -> void.
-
#visit_file(file) ⇒ Object
: (File file) -> void.
Methods inherited from Visitor
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_indent ⇒ Object (readonly)
: Integer
19 20 21 |
# File 'lib/rbi/printer.rb', line 19 def current_indent @current_indent end |
#in_visibility_group ⇒ Object
: bool
13 14 15 |
# File 'lib/rbi/printer.rb', line 13 def in_visibility_group @in_visibility_group end |
#max_line_length ⇒ Object (readonly)
: Integer?
22 23 24 |
# File 'lib/rbi/printer.rb', line 22 def max_line_length @max_line_length end |
#previous_node ⇒ Object (readonly)
: Node?
16 17 18 |
# File 'lib/rbi/printer.rb', line 16 def previous_node @previous_node end |
#print_locs ⇒ Object
: bool
13 14 15 |
# File 'lib/rbi/printer.rb', line 13 def print_locs @print_locs end |
Instance Method Details
#current_indent_string ⇒ Object
: -> String
48 49 50 |
# File 'lib/rbi/printer.rb', line 48 def current_indent_string INDENT_CACHE[@current_indent] || " " * @current_indent end |
#dedent ⇒ Object
: -> void
43 44 45 |
# File 'lib/rbi/printer.rb', line 43 def dedent @current_indent -= 2 end |
#indent ⇒ Object
: -> void
38 39 40 |
# File 'lib/rbi/printer.rb', line 38 def indent @current_indent += 2 end |
#print(string) ⇒ Object
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 |