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.



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

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



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

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



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

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



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

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



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

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