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



905
906
907
# File 'lib/syntax_tree/node.rb', line 905

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Args

the contents of the array



903
904
905
# File 'lib/syntax_tree/node.rb', line 903

def contents
  @contents
end

Instance Method Details

#format(q) ⇒ Object



909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/syntax_tree/node.rb', line 909

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