Class: SyntaxTree::FlowControlFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



2842
2843
2844
2845
# File 'lib/syntax_tree.rb', line 2842

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

Instance Attribute Details

#keywordObject (readonly)

String

the keyword to print



2837
2838
2839
# File 'lib/syntax_tree.rb', line 2837

def keyword
  @keyword
end

#nodeObject (readonly)

Break | Next

the node being formatted



2840
2841
2842
# File 'lib/syntax_tree.rb', line 2840

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
# File 'lib/syntax_tree.rb', line 2847

def format(q)
  arguments = node.arguments

  q.group do
    q.text(keyword)

    if arguments.parts.any?
      if arguments.parts.length == 1 && arguments.parts.first.is_a?(Paren)
        q.format(arguments)
      else
        q.text(" ")
        q.nest(keyword.length + 1) { q.format(arguments) }
      end
    end
  end
end