Class: SyntaxTree::ArrayLiteral::VarRefsFormatter

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

Overview

Formats an array that contains only a list of variable references. To make things simpler, if there are a bunch, we format them all using the “fill” algorithm as opposed to breaking them into a ton of lines. For example,

[foo, bar, baz]

instead of becoming:

[
  foo,
  bar,
  baz
]

would instead become:

[
  foo, bar,
  baz
]

provided the line length was hit between ‘bar` and `baz`.

Defined Under Namespace

Classes: Separator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ VarRefsFormatter

Returns a new instance of VarRefsFormatter.



1117
1118
1119
# File 'lib/syntax_tree/node.rb', line 1117

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Args

the contents of the array



1115
1116
1117
# File 'lib/syntax_tree/node.rb', line 1115

def contents
  @contents
end

Instance Method Details

#format(q) ⇒ Object



1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
# File 'lib/syntax_tree/node.rb', line 1121

def format(q)
  q.text("[")
  q.group do
    q.indent do
      q.breakable_empty
      q.seplist(contents.parts, Separator.new) { |part| q.format(part) }
      q.if_break { q.text(",") } if q.trailing_comma?
    end
    q.breakable_empty
  end
  q.text("]")
end