@@ -1413,17 +1413,21 @@ class Labels
1413
1413
1414
1414
def format_key ( q , key )
1415
1415
case key
1416
- in Label
1416
+ when Label
1417
1417
q . format ( key )
1418
- in SymbolLiteral
1418
+ when SymbolLiteral
1419
1419
q . format ( key . value )
1420
1420
q . text ( ":" )
1421
- in DynaSymbol [ parts : [ TStringContent [ value : LABEL ] => part ] ]
1422
- q . format ( part )
1423
- q . text ( ":" )
1424
- in DynaSymbol
1425
- q . format ( key )
1426
- q . text ( ":" )
1421
+ when DynaSymbol
1422
+ parts = key . parts
1423
+
1424
+ if parts . length == 1 && ( part = parts . first ) && part . is_a? ( TStringContent ) && part . value . match? ( LABEL )
1425
+ q . format ( part )
1426
+ q . text ( ":" )
1427
+ else
1428
+ q . format ( key )
1429
+ q . text ( ":" )
1430
+ end
1427
1431
end
1428
1432
end
1429
1433
end
@@ -1433,8 +1437,7 @@ class Rockets
1433
1437
def format_key ( q , key )
1434
1438
case key
1435
1439
when Label
1436
- q . text ( ":" )
1437
- q . text ( key . value . chomp ( ":" ) )
1440
+ q . text ( ":#{ key . value . chomp ( ":" ) } " )
1438
1441
when DynaSymbol
1439
1442
q . text ( ":" )
1440
1443
q . format ( key )
@@ -2145,105 +2148,128 @@ def format(q)
2145
2148
q . group do
2146
2149
q . text ( keyword )
2147
2150
2148
- case node . arguments . parts
2149
- in [ ]
2151
+ parts = node . arguments . parts
2152
+ length = parts . length
2153
+
2154
+ if length == 0
2150
2155
# Here there are no arguments at all, so we're not going to print
2151
2156
# anything. This would be like if we had:
2152
2157
#
2153
2158
# break
2154
2159
#
2155
- in [
2156
- Paren [
2157
- contents : {
2158
- body : [ ArrayLiteral [ contents : { parts : [ _ , _ , *] } ] => array ] }
2159
- ]
2160
- ]
2161
- # Here we have a single argument that is a set of parentheses wrapping
2162
- # an array literal that has at least 2 elements. We're going to print
2163
- # the contents of the array directly. This would be like if we had:
2164
- #
2165
- # break([1, 2, 3])
2166
- #
2167
- # which we will print as:
2168
- #
2169
- # break 1, 2, 3
2170
- #
2171
- q . text ( " " )
2172
- format_array_contents ( q , array )
2173
- in [ Paren [ contents : { body : [ ArrayLiteral => statement ] } ] ]
2174
- # Here we have a single argument that is a set of parentheses wrapping
2175
- # an array literal that has 0 or 1 elements. We're going to skip the
2176
- # parentheses but print the array itself. This would be like if we
2177
- # had:
2178
- #
2179
- # break([1])
2180
- #
2181
- # which we will print as:
2182
- #
2183
- # break [1]
2184
- #
2185
- q . text ( " " )
2186
- q . format ( statement )
2187
- in [ Paren [ contents : { body : [ statement ] } ] ] if skip_parens? ( statement )
2188
- # Here we have a single argument that is a set of parentheses that
2189
- # themselves contain a single statement. That statement is a simple
2190
- # value that we can skip the parentheses for. This would be like if we
2191
- # had:
2192
- #
2193
- # break(1)
2194
- #
2195
- # which we will print as:
2196
- #
2197
- # break 1
2198
- #
2199
- q . text ( " " )
2200
- q . format ( statement )
2201
- in [ Paren => part ]
2202
- # Here we have a single argument that is a set of parentheses. We're
2203
- # going to print the parentheses themselves as if they were the set of
2204
- # arguments. This would be like if we had:
2205
- #
2206
- # break(foo.bar)
2207
- #
2208
- q . format ( part )
2209
- in [ ArrayLiteral [ contents : { parts : [ _ , _ , *] } ] => array ]
2210
- # Here there is a single argument that is an array literal with at
2211
- # least two elements. We skip directly into the array literal's
2212
- # elements in order to print the contents. This would be like if we
2213
- # had:
2214
- #
2215
- # break [1, 2, 3]
2216
- #
2217
- # which we will print as:
2218
- #
2219
- # break 1, 2, 3
2220
- #
2221
- q . text ( " " )
2222
- format_array_contents ( q , array )
2223
- in [ ArrayLiteral => part ]
2224
- # Here there is a single argument that is an array literal with 0 or 1
2225
- # elements. In this case we're going to print the array as it is
2226
- # because skipping the brackets would change the remaining. This would
2227
- # be like if we had:
2228
- #
2229
- # break []
2230
- # break [1]
2231
- #
2232
- q . text ( " " )
2233
- q . format ( part )
2234
- in [ _ ]
2235
- # Here there is a single argument that hasn't matched one of our
2236
- # previous cases. We're going to print the argument as it is. This
2237
- # would be like if we had:
2238
- #
2239
- # break foo
2240
- #
2241
- format_arguments ( q , "(" , ")" )
2242
- else
2160
+ elsif length >= 2
2243
2161
# If there are multiple arguments, format them all. If the line is
2244
2162
# going to break into multiple, then use brackets to start and end the
2245
2163
# expression.
2246
2164
format_arguments ( q , " [" , "]" )
2165
+ else
2166
+ # If we get here, then we're formatting a single argument to the flow
2167
+ # control keyword.
2168
+ part = parts . first
2169
+
2170
+ case part
2171
+ when Paren
2172
+ statements = part . contents . body
2173
+
2174
+ if statements . length == 1
2175
+ statement = statements . first
2176
+
2177
+ if statement . is_a? ( ArrayLiteral )
2178
+ contents = statement . contents
2179
+
2180
+ if contents && contents . parts . length >= 2
2181
+ # Here we have a single argument that is a set of parentheses
2182
+ # wrapping an array literal that has at least 2 elements.
2183
+ # We're going to print the contents of the array directly.
2184
+ # This would be like if we had:
2185
+ #
2186
+ # break([1, 2, 3])
2187
+ #
2188
+ # which we will print as:
2189
+ #
2190
+ # break 1, 2, 3
2191
+ #
2192
+ q . text ( " " )
2193
+ format_array_contents ( q , statement )
2194
+ else
2195
+ # Here we have a single argument that is a set of parentheses
2196
+ # wrapping an array literal that has 0 or 1 elements. We're
2197
+ # going to skip the parentheses but print the array itself.
2198
+ # This would be like if we had:
2199
+ #
2200
+ # break([1])
2201
+ #
2202
+ # which we will print as:
2203
+ #
2204
+ # break [1]
2205
+ #
2206
+ q . text ( " " )
2207
+ q . format ( statement )
2208
+ end
2209
+ elsif skip_parens? ( statement )
2210
+ # Here we have a single argument that is a set of parentheses
2211
+ # that themselves contain a single statement. That statement is
2212
+ # a simple value that we can skip the parentheses for. This
2213
+ # would be like if we had:
2214
+ #
2215
+ # break(1)
2216
+ #
2217
+ # which we will print as:
2218
+ #
2219
+ # break 1
2220
+ #
2221
+ q . text ( " " )
2222
+ q . format ( statement )
2223
+ else
2224
+ # Here we have a single argument that is a set of parentheses.
2225
+ # We're going to print the parentheses themselves as if they
2226
+ # were the set of arguments. This would be like if we had:
2227
+ #
2228
+ # break(foo.bar)
2229
+ #
2230
+ q . format ( part )
2231
+ end
2232
+ else
2233
+ q . format ( part )
2234
+ end
2235
+ when ArrayLiteral
2236
+ contents = part . contents
2237
+
2238
+ if contents && contents . parts . length >= 2
2239
+ # Here there is a single argument that is an array literal with at
2240
+ # least two elements. We skip directly into the array literal's
2241
+ # elements in order to print the contents. This would be like if
2242
+ # we had:
2243
+ #
2244
+ # break [1, 2, 3]
2245
+ #
2246
+ # which we will print as:
2247
+ #
2248
+ # break 1, 2, 3
2249
+ #
2250
+ q . text ( " " )
2251
+ format_array_contents ( q , part )
2252
+ else
2253
+ # Here there is a single argument that is an array literal with 0
2254
+ # or 1 elements. In this case we're going to print the array as it
2255
+ # is because skipping the brackets would change the remaining.
2256
+ # This would be like if we had:
2257
+ #
2258
+ # break []
2259
+ # break [1]
2260
+ #
2261
+ q . text ( " " )
2262
+ q . format ( part )
2263
+ end
2264
+ else
2265
+ # Here there is a single argument that hasn't matched one of our
2266
+ # previous cases. We're going to print the argument as it is. This
2267
+ # would be like if we had:
2268
+ #
2269
+ # break foo
2270
+ #
2271
+ format_arguments ( q , "(" , ")" )
2272
+ end
2247
2273
end
2248
2274
end
2249
2275
end
@@ -3791,15 +3817,18 @@ def initialize(operator, node)
3791
3817
end
3792
3818
3793
3819
def format ( q )
3794
- space = [ If , IfMod , Unless , UnlessMod ] . include? ( q . parent . class )
3795
-
3796
3820
left = node . left
3797
3821
right = node . right
3798
3822
3799
3823
q . format ( left ) if left
3800
- q . text ( " " ) if space
3801
- q . text ( operator )
3802
- q . text ( " " ) if space
3824
+
3825
+ case q . parent
3826
+ when If , IfMod , Unless , UnlessMod
3827
+ q . text ( " #{ operator } " )
3828
+ else
3829
+ q . text ( operator )
3830
+ end
3831
+
3803
3832
q . format ( right ) if right
3804
3833
end
3805
3834
end
0 commit comments