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.



2151
2152
2153
2154
# File 'lib/syntax_tree/node.rb', line 2151

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

Instance Attribute Details

#keywordObject (readonly)

String

the keyword to print



2146
2147
2148
# File 'lib/syntax_tree/node.rb', line 2146

def keyword
  @keyword
end

#nodeObject (readonly)

Break | Next | Return

the node being formatted



2149
2150
2151
# File 'lib/syntax_tree/node.rb', line 2149

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



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
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
# File 'lib/syntax_tree/node.rb', line 2156

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