Class: SyntaxTree::FlowControlFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Formats either a Break or Next node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, node) ⇒ FlowControlFormatter

Returns a new instance of FlowControlFormatter.



2584
2585
2586
2587
# File 'lib/syntax_tree/node.rb', line 2584

def initialize(keyword, node)
  @keyword = keyword
  @node = node
end

Instance Attribute Details

#keywordObject (readonly)

String

the keyword to print



2579
2580
2581
# File 'lib/syntax_tree/node.rb', line 2579

def keyword
  @keyword
end

#nodeObject (readonly)

Break | Next

the node being formatted



2582
2583
2584
# File 'lib/syntax_tree/node.rb', line 2582

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
# File 'lib/syntax_tree/node.rb', line 2589

def format(q)
  arguments = node.arguments

  q.group do
    q.text(keyword)

    if arguments.parts.any?
      if arguments.parts.length == 1
        part = arguments.parts.first

        if part.is_a?(Paren)
          q.format(arguments)
        elsif part.is_a?(ArrayLiteral)
          q.text(" ")
          q.format(arguments)
        else
          format_arguments(q, "(", ")")
        end
      else
        format_arguments(q, " [", "]")
      end
    end
  end
end