Class: SyntaxTree::FlowControlFormatter

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

Overview

Formats either a Break, Next, or Return node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, node) ⇒ FlowControlFormatter

Returns a new instance of FlowControlFormatter.



2116
2117
2118
2119
# File 'lib/syntax_tree/node.rb', line 2116

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

Instance Attribute Details

#keywordObject (readonly)

String

the keyword to print



2111
2112
2113
# File 'lib/syntax_tree/node.rb', line 2111

def keyword
  @keyword
end

#nodeObject (readonly)

Break | Next | Return

the node being formatted



2114
2115
2116
# File 'lib/syntax_tree/node.rb', line 2114

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
# File 'lib/syntax_tree/node.rb', line 2121

def format(q)
  q.group do
    q.text(keyword)

    case node.arguments.parts
    in []
      # Here there are no arguments at all, so we're not going to print
      # anything. This would be like if we had:
      #
      #     break
      #
    in [
         Paren[
           contents: {
             body: [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
           }
         ]
       ]
      # Here we have a single argument that is a set of parentheses wrapping
      # an array literal that has at least 2 elements. We're going to print
      # the contents of the array directly. This would be like if we had:
      #
      #     break([1, 2, 3])
      #
      # which we will print as:
      #
      #     break 1, 2, 3
      #
      q.text(" ")
      format_array_contents(q, array)
    in [Paren[contents: { body: [ArrayLiteral => statement] }]]
      # Here we have a single argument that is a set of parentheses wrapping
      # an array literal that has 0 or 1 elements. We're going to skip the
      # parentheses but print the array itself. This would be like if we
      # had:
      #
      #     break([1])
      #
      # which we will print as:
      #
      #     break [1]
      #
      q.text(" ")
      q.format(statement)
    in [Paren[contents: { body: [statement] }]] if skip_parens?(statement)
      # Here we have a single argument that is a set of parentheses that
      # themselves contain a single statement. That statement is a simple
      # value that we can skip the parentheses for. This would be like if we
      # had:
      #
      #     break(1)
      #
      # which we will print as:
      #
      #     break 1
      #
      q.text(" ")
      q.format(statement)
    in [Paren => part]
      # Here we have a single argument that is a set of parentheses. We're
      # going to print the parentheses themselves as if they were the set of
      # arguments. This would be like if we had:
      #
      #     break(foo.bar)
      #
      q.format(part)
    in [ArrayLiteral[contents: { parts: [_, _, *] }] => array]
      # Here there is a single argument that is an array literal with at
      # least two elements. We skip directly into the array literal's
      # elements in order to print the contents. This would be like if we
      # had:
      #
      #     break [1, 2, 3]
      #
      # which we will print as:
      #
      #     break 1, 2, 3
      #
      q.text(" ")
      format_array_contents(q, array)
    in [ArrayLiteral => part]
      # Here there is a single argument that is an array literal with 0 or 1
      # elements. In this case we're going to print the array as it is
      # because skipping the brackets would change the remaining. This would
      # be like if we had:
      #
      #     break []
      #     break [1]
      #
      q.text(" ")
      q.format(part)
    in [_]
      # Here there is a single argument that hasn't matched one of our
      # previous cases. We're going to print the argument as it is. This
      # would be like if we had:
      #
      #     break foo
      #
      format_arguments(q, "(", ")")
    else
      # If there are multiple arguments, format them all. If the line is
      # going to break into multiple, then use brackets to start and end the
      # expression.
      format_arguments(q, " [", "]")
    end
  end
end