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`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ VarRefsFormatter

Returns a new instance of VarRefsFormatter.



847
848
849
# File 'lib/syntax_tree/node.rb', line 847

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Args

the contents of the array



845
846
847
# File 'lib/syntax_tree/node.rb', line 845

def contents
  @contents
end

Instance Method Details

#format(q) ⇒ Object



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
# File 'lib/syntax_tree/node.rb', line 851

def format(q)
  q.group(0, "[", "]") do
    q.indent do
      q.breakable("")

      separator = -> do
        q.text(",")
        q.fill_breakable
      end

      q.seplist(contents.parts, separator) { |part| q.format(part) }
    end
    q.breakable("")
  end
end