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.



3050
3051
3052
3053
# File 'lib/syntax_tree.rb', line 3050

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

Instance Attribute Details

#keywordObject (readonly)

String

the keyword to print



3045
3046
3047
# File 'lib/syntax_tree.rb', line 3045

def keyword
  @keyword
end

#nodeObject (readonly)

Break | Next

the node being formatted



3048
3049
3050
# File 'lib/syntax_tree.rb', line 3048

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
# File 'lib/syntax_tree.rb', line 3055

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