Class: SyntaxTree::LoopFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



12414
12415
12416
12417
12418
# File 'lib/syntax_tree.rb', line 12414

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



12406
12407
12408
# File 'lib/syntax_tree.rb', line 12406

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



12409
12410
12411
# File 'lib/syntax_tree.rb', line 12409

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



12412
12413
12414
# File 'lib/syntax_tree.rb', line 12412

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
# File 'lib/syntax_tree.rb', line 12420

def format(q)
  # If the predicate of the loop contains an assignment (in which case we
  # can't know for certain that that assignment doesn't impact the
  # statements inside the loop) then we can't use the modifier form and we
  # must use the block form.
  if [Assign, MAssign, OpAssign].include?(node.predicate.class)
    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