Class: RBI::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rbi/model.rb,
lib/rbi/printer.rb,
lib/rbi/rbs_printer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strictness: nil, comments: nil, &block) ⇒ File

: (?strictness: String?, ?comments: Array?) ?{ (File file) -> void } -> void



164
165
166
167
168
169
# File 'lib/rbi/model.rb', line 164

def initialize(strictness: nil, comments: nil, &block)
  @root = Tree.new #: Tree
  @strictness = strictness
  @comments = comments
  block&.call(self)
end

Instance Attribute Details

#commentsObject

: -> Array



154
155
156
# File 'lib/rbi/model.rb', line 154

def comments
  @comments ||= []
end

#rootObject

: Tree



145
146
147
# File 'lib/rbi/model.rb', line 145

def root
  @root
end

#strictnessObject

: String?



148
149
150
# File 'lib/rbi/model.rb', line 148

def strictness
  @strictness
end

Instance Method Details

#<<(node) ⇒ Object

: (Node node) -> void



172
173
174
# File 'lib/rbi/model.rb', line 172

def <<(node)
  @root << node
end

#comments?Boolean

: -> bool

Returns:

  • (Boolean)


159
160
161
# File 'lib/rbi/model.rb', line 159

def comments?
  !@comments.nil? && !@comments.empty?
end

#empty?Boolean

: -> bool

Returns:

  • (Boolean)


177
178
179
# File 'lib/rbi/model.rb', line 177

def empty?
  @root.empty?
end

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



877
878
879
880
# File 'lib/rbi/printer.rb', line 877

def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
  p = Printer.new(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
  p.visit_file(self)
end

#rbs_print(out: $stdout, indent: 0, print_locs: false) ⇒ Object

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



1236
1237
1238
1239
# File 'lib/rbi/rbs_printer.rb', line 1236

def rbs_print(out: $stdout, indent: 0, print_locs: false)
  p = RBSPrinter.new(out: out, indent: indent, print_locs: print_locs)
  p.visit_file(self)
end

#rbs_string(indent: 0, print_locs: false) ⇒ Object

: (?indent: Integer, ?print_locs: bool) -> String



1242
1243
1244
1245
1246
# File 'lib/rbi/rbs_printer.rb', line 1242

def rbs_string(indent: 0, print_locs: false)
  out = StringIO.new
  rbs_print(out: out, indent: indent, print_locs: print_locs)
  out.string
end

#string(indent: 0, print_locs: false, max_line_length: nil) ⇒ Object

: (?indent: Integer, ?print_locs: bool, ?max_line_length: Integer?) -> String



883
884
885
886
887
888
889
890
# File 'lib/rbi/printer.rb', line 883

def string(indent: 0, print_locs: false, max_line_length: nil)
  # Use a mutable String buffer instead of StringIO for faster concatenation.
  # String#<< is ~2x faster than StringIO#print for many small writes.
  out = +""
  p = Printer.new(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
  p.visit_file(self)
  out
end