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.



8998
8999
9000
9001
9002
# File 'lib/syntax_tree/node.rb', line 8998

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



8990
8991
8992
# File 'lib/syntax_tree/node.rb', line 8990

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



8993
8994
8995
# File 'lib/syntax_tree/node.rb', line 8993

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



8996
8997
8998
# File 'lib/syntax_tree/node.rb', line 8996

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
# File 'lib/syntax_tree/node.rb', line 9004

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