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.



9273
9274
9275
9276
9277
# File 'lib/syntax_tree/node.rb', line 9273

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



9265
9266
9267
# File 'lib/syntax_tree/node.rb', line 9265

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



9268
9269
9270
# File 'lib/syntax_tree/node.rb', line 9268

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



9271
9272
9273
# File 'lib/syntax_tree/node.rb', line 9271

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
# File 'lib/syntax_tree/node.rb', line 9279

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