Class: SyntaxTree::LoopFormatter

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

Overview

Formats an Until, UntilMod, While, or WhileMod node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, node, statements) ⇒ LoopFormatter

Returns a new instance of LoopFormatter.



9186
9187
9188
9189
9190
# File 'lib/syntax_tree/node.rb', line 9186

def initialize(keyword, node, statements)
  @keyword = keyword
  @node = node
  @statements = statements
end

Instance Attribute Details

#keywordObject (readonly)

String

the name of the keyword used for this loop



9178
9179
9180
# File 'lib/syntax_tree/node.rb', line 9178

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



9181
9182
9183
# File 'lib/syntax_tree/node.rb', line 9181

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



9184
9185
9186
# File 'lib/syntax_tree/node.rb', line 9184

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
# File 'lib/syntax_tree/node.rb', line 9192

def format(q)
  if ContainsAssignment.call(node.predicate)
    format_break(q)
    q.break_parent
    return
  end

  q.group do
    q
      .if_break { format_break(q) }
      .if_flat do
        Parentheses.flat(q) do
          q.format(statements)
          q.text(" #{keyword} ")
          q.format(node.predicate)
        end
      end
  end
end