@@ -45,9 +45,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
45
45
/// breaks and start/end contextual breaking tokens have been inserted.
46
46
private var preVisitedExprs = [ ExprSyntax] ( )
47
47
48
- /// Lists the tokens that are the closing or right parens of a parenthesized expression (i.e. a
49
- /// tuple expression with 1 element).
50
- private var parenthesizedExprParens = Set < TokenSyntax > ( )
48
+ /// Lists the tokens that are the closing or final delimiter of a node that shouldn't be split
49
+ /// from the preceding token. When breaks are inserted around compound expressions, the breaks are
50
+ /// moved past these tokens.
51
+ private var closingDelimiterTokens = Set < TokenSyntax > ( )
51
52
52
53
init ( configuration: Configuration , operatorContext: OperatorContext ) {
53
54
self . config = configuration
@@ -624,6 +625,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
624
625
}
625
626
626
627
after ( node. colon, tokens: . close)
628
+ closingDelimiterTokens. insert ( node. colon)
627
629
return . visitChildren
628
630
}
629
631
@@ -649,7 +651,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
649
651
// `stackedIndentationBehavior`).
650
652
after ( node. leftParen, tokens: . open)
651
653
before ( node. rightParen, tokens: . close)
652
- parenthesizedExprParens . insert ( node. rightParen)
654
+ closingDelimiterTokens . insert ( node. rightParen)
653
655
654
656
// When there's a comment inside of a parenthesized expression, we want to allow the comment
655
657
// to exist at the EOL with the left paren or on its own line. The contents are always
@@ -1321,6 +1323,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1321
1323
before ( node. firstToken, tokens: . open)
1322
1324
if let comma = node. trailingComma {
1323
1325
after ( comma, tokens: . close, . break( . same) )
1326
+ closingDelimiterTokens. insert ( comma)
1324
1327
} else {
1325
1328
after ( node. lastToken, tokens: . close)
1326
1329
}
@@ -1357,7 +1360,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1357
1360
// When the ternary is wrapped in parens, absorb the closing paren into the ternary's group so
1358
1361
// that it is glued to the last token of the ternary.
1359
1362
let closeScopeToken : TokenSyntax ?
1360
- if let parenExpr = outerMostEnclosingParenthesizedExpr ( from: Syntax ( node. secondChoice) ) {
1363
+ if let parenExpr = outermostEnclosingNode ( from: Syntax ( node. secondChoice) ) {
1361
1364
closeScopeToken = parenExpr. lastToken
1362
1365
} else {
1363
1366
closeScopeToken = node. secondChoice. lastToken
@@ -2878,17 +2881,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2878
2881
}
2879
2882
}
2880
2883
2881
- /// Returns the node for the outermost parenthesized expr (i.e. a single element tuple) that is
2882
- /// ended at the given node. When the given node is not the last component of a parenthesized
2883
- /// expression, this method returns nil .
2884
- private func outerMostEnclosingParenthesizedExpr ( from node: Syntax ) -> Syntax ? {
2885
- guard let afterToken = node. lastToken? . nextToken, parenthesizedExprParens . contains ( afterToken)
2884
+ /// Returns the outermost node enclosing the given node whose closing delimiter(s) must be kept
2885
+ /// alongside the last token of the given node. Any tokens between `node.lastToken` and the
2886
+ /// returned node's `lastToken` are delimiter tokens that shouldn't be preceded by a break .
2887
+ private func outermostEnclosingNode ( from node: Syntax ) -> Syntax ? {
2888
+ guard let afterToken = node. lastToken? . nextToken, closingDelimiterTokens . contains ( afterToken)
2886
2889
else {
2887
2890
return nil
2888
2891
}
2889
2892
var parenthesizedExpr = afterToken. parent
2890
2893
while let nextToken = parenthesizedExpr? . lastToken? . nextToken,
2891
- parenthesizedExprParens . contains ( nextToken) ,
2894
+ closingDelimiterTokens . contains ( nextToken) ,
2892
2895
let nextExpr = nextToken. parent
2893
2896
{
2894
2897
parenthesizedExpr = nextExpr
@@ -2925,7 +2928,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2925
2928
// When `rhs` side is the last sequence in an enclosing parenthesized expression, absorb the
2926
2929
// paren into the right hand side by unindenting after the final closing paren. This glues
2927
2930
// the paren to the last token of `rhs`.
2928
- if let unindentingParenExpr = outerMostEnclosingParenthesizedExpr ( from: Syntax ( rhs) ) {
2931
+ if let unindentingParenExpr = outermostEnclosingNode ( from: Syntax ( rhs) ) {
2929
2932
return ( unindentingNode: unindentingParenExpr, shouldReset: true )
2930
2933
}
2931
2934
return ( unindentingNode: Syntax ( rhs) , shouldReset: true )
@@ -2947,7 +2950,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2947
2950
// When `rhs` side is the last sequence in an enclosing parenthesized expression, absorb the
2948
2951
// paren into the right hand side by unindenting after the final closing paren. This glues the
2949
2952
// paren to the last token of `rhs`.
2950
- if let unindentingParenExpr = outerMostEnclosingParenthesizedExpr ( from: Syntax ( rhs) ) {
2953
+ if let unindentingParenExpr = outermostEnclosingNode ( from: Syntax ( rhs) ) {
2951
2954
return ( unindentingNode: unindentingParenExpr, shouldReset: true )
2952
2955
}
2953
2956
return ( unindentingNode: Syntax ( parenthesizedExpr) , shouldReset: false )
0 commit comments