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.



9220
9221
9222
9223
9224
# File 'lib/syntax_tree/node.rb', line 9220

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

Instance Attribute Details

#keyword ⇒ Object (readonly)

[String] the name of the keyword used for this loop



9212
9213
9214
# File 'lib/syntax_tree/node.rb', line 9212

def keyword
  @keyword
end

#node ⇒ Object (readonly)

[Until | UntilMod | While | WhileMod] the node that is being formatted



9215
9216
9217
# File 'lib/syntax_tree/node.rb', line 9215

def node
  @node
end

#statements ⇒ Object (readonly)

[untyped] the statements associated with the node



9218
9219
9220
# File 'lib/syntax_tree/node.rb', line 9218

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
# File 'lib/syntax_tree/node.rb', line 9226

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