Class: PrettierPrint::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/prettier_print.rb

Overview

A node in the print tree that represents a group of items which the printer should try to fit onto one line. This is the basic command to tell the printer when to break. Groups are usually nested, and the printer will try to fit everything on one line, but if it doesn’t fit it will break the outermost group first and try again. It will continue breaking groups until everything fits (or there are no more groups to break).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depth, contents: []) ⇒ Group

Returns a new instance of Group.



152
153
154
155
156
# File 'lib/prettier_print.rb', line 152

def initialize(depth, contents: [])
  @depth = depth
  @contents = contents
  @break = false
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



150
151
152
# File 'lib/prettier_print.rb', line 150

def contents
  @contents
end

#depthObject (readonly)

Returns the value of attribute depth.



150
151
152
# File 'lib/prettier_print.rb', line 150

def depth
  @depth
end

Instance Method Details

#breakObject



158
159
160
# File 'lib/prettier_print.rb', line 158

def break
  @break = true
end

#break?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/prettier_print.rb', line 162

def break?
  @break
end

#pretty_print(q) ⇒ Object



166
167
168
169
170
# File 'lib/prettier_print.rb', line 166

def pretty_print(q)
  q.group(2, break? ? "breakGroup([" : "group([", "])") do
    q.seplist(contents) { |content| q.pp(content) }
  end
end