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.



9287
9288
9289
9290
9291
# File 'lib/syntax_tree/node.rb', line 9287

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



9279
9280
9281
# File 'lib/syntax_tree/node.rb', line 9279

def keyword
  @keyword
end

#nodeObject (readonly)

Until | UntilMod | While | WhileMod

the node that is being formatted



9282
9283
9284
# File 'lib/syntax_tree/node.rb', line 9282

def node
  @node
end

#statementsObject (readonly)

untyped

the statements associated with the node



9285
9286
9287
# File 'lib/syntax_tree/node.rb', line 9285

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
# File 'lib/syntax_tree/node.rb', line 9293

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