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.



9109
9110
9111
9112
9113
# File 'lib/syntax_tree/node.rb', line 9109

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



9101
9102
9103
# File 'lib/syntax_tree/node.rb', line 9101

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



9104
9105
9106
# File 'lib/syntax_tree/node.rb', line 9104

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



9107
9108
9109
# File 'lib/syntax_tree/node.rb', line 9107

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
# File 'lib/syntax_tree/node.rb', line 9115

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