@@ -62,7 +62,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
62
62
self . config = configuration
63
63
self . operatorContext = operatorContext
64
64
self . maxlinelength = config. lineLength
65
- super. init ( viewMode: . sourceAccurate )
65
+ super. init ( viewMode: . all )
66
66
}
67
67
68
68
func makeStream( from node: Syntax ) -> [ Token ] {
@@ -328,7 +328,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
328
328
// Due to visitation order, the matching .open break is added in ParameterClauseSyntax.
329
329
after ( node. signature. lastToken, tokens: . close)
330
330
}
331
-
331
+
332
332
arrangeParameterClause ( node. signature. input, forcesBreakBeforeRightParen: node. body != nil )
333
333
334
334
// Prioritize keeping "<modifiers> init<punctuation>" together.
@@ -525,7 +525,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
525
525
526
526
// Breaks are only allowed after `else` when there's a comment; otherwise there shouldn't be
527
527
// any newlines between `else` and the open brace or a following `if`.
528
- if let tokenAfterElse = elseKeyword. nextToken, tokenAfterElse. leadingTrivia. hasLineComment {
528
+ if let tokenAfterElse = elseKeyword. nextToken ( viewMode : . all ) , tokenAfterElse. leadingTrivia. hasLineComment {
529
529
after ( node. elseKeyword, tokens: . break( . same, size: 1 ) )
530
530
} else if let elseBody = node. elseBody, elseBody. is ( IfStmtSyntax . self) {
531
531
after ( node. elseKeyword, tokens: . space)
@@ -793,7 +793,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
793
793
// to exist at the EOL with the left paren or on its own line. The contents are always
794
794
// indented on the following lines, since parens always create a scope. An open/close break
795
795
// pair isn't used here to avoid forcing the closing paren down onto a new line.
796
- if node. leftParen. nextToken? . leadingTrivia. hasLineComment ?? false {
796
+ if node. leftParen. nextToken ( viewMode : . all ) ? . leadingTrivia. hasLineComment ?? false {
797
797
after ( node. leftParen, tokens: . break( . continue, size: 0 ) )
798
798
}
799
799
} else if elementCount > 1 {
@@ -951,8 +951,8 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
951
951
// When this function call is wrapped by a try-expr or await-expr, the group applied when
952
952
// visiting that wrapping expression is sufficient. Adding another group here in that case
953
953
// can result in unnecessarily breaking after the try/await keyword.
954
- if !( base. firstToken? . previousToken? . parent? . is ( TryExprSyntax . self) ?? false
955
- || base. firstToken? . previousToken? . parent? . is ( AwaitExprSyntax . self) ?? false ) {
954
+ if !( base. firstToken? . previousToken ( viewMode : . all ) ? . parent? . is ( TryExprSyntax . self) ?? false
955
+ || base. firstToken? . previousToken ( viewMode : . all ) ? . parent? . is ( AwaitExprSyntax . self) ?? false ) {
956
956
before ( base. firstToken, tokens: . open)
957
957
after ( calledMemberAccessExpr. name. lastToken, tokens: . close)
958
958
}
@@ -1304,7 +1304,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1304
1304
if let lastElemTok = node. elements. lastToken {
1305
1305
after ( lastElemTok, tokens: . break( breakKindClose, newlines: . soft) , . close)
1306
1306
} else {
1307
- before ( tokenToOpenWith. nextToken, tokens: . break( breakKindClose, newlines: . soft) , . close)
1307
+ before ( tokenToOpenWith. nextToken ( viewMode : . all ) , tokens: . break( breakKindClose, newlines: . soft) , . close)
1308
1308
}
1309
1309
1310
1310
if isNestedInPostfixIfConfig ( node: Syntax ( node) ) {
@@ -1915,7 +1915,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1915
1915
var closesNeeded : Int = 0
1916
1916
var closeAfterToken : TokenSyntax ? = nil
1917
1917
1918
- if let typeAnnotation = node. typeAnnotation {
1918
+ if let typeAnnotation = node. typeAnnotation, !typeAnnotation . type . is ( MissingTypeSyntax . self ) {
1919
1919
after (
1920
1920
typeAnnotation. colon,
1921
1921
tokens: . break( . open( kind: . continuation) , newlines: . elective( ignoresDiscretionary: true ) ) )
@@ -2190,6 +2190,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2190
2190
}
2191
2191
2192
2192
override func visit( _ node: GenericWhereClauseSyntax ) -> SyntaxVisitorContinueKind {
2193
+ guard node. whereKeyword != node. lastToken else {
2194
+ verbatimToken ( Syntax ( node) )
2195
+ return . skipChildren
2196
+ }
2197
+
2193
2198
after ( node. whereKeyword, tokens: . break( . open) )
2194
2199
after ( node. lastToken, tokens: . break( . close, size: 0 ) )
2195
2200
@@ -2378,6 +2383,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2378
2383
2379
2384
// MARK: - Nodes representing unknown or malformed syntax
2380
2385
2386
+ override func visit( _ node: UnexpectedNodesSyntax ) -> SyntaxVisitorContinueKind {
2387
+ verbatimToken ( Syntax ( node) )
2388
+ return . skipChildren
2389
+ }
2390
+
2381
2391
override func visit( _ node: UnknownDeclSyntax ) -> SyntaxVisitorContinueKind {
2382
2392
verbatimToken ( Syntax ( node) )
2383
2393
return . skipChildren
@@ -2429,7 +2439,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2429
2439
appendMultilineStringSegments ( at: pendingSegmentIndex)
2430
2440
} else if !ignoredTokens. contains ( token) {
2431
2441
// Otherwise, it's just a regular token, so add the text as-is.
2432
- appendToken ( . syntax( token. text) )
2442
+ appendToken ( . syntax( token. presence == . present ? token . text : " " ) )
2433
2443
}
2434
2444
2435
2445
appendTrailingTrivia ( token)
@@ -2883,7 +2893,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
2883
2893
}
2884
2894
2885
2895
private func extractLeadingTrivia( _ token: TokenSyntax ) {
2886
- var isStartOfFile = token. previousToken == nil
2896
+ var isStartOfFile = token. previousToken ( viewMode : . all ) == nil
2887
2897
let trivia = token. leadingTrivia
2888
2898
2889
2899
// If we're at the end of the file, determine at which index to stop checking trivia pieces to
@@ -3260,12 +3270,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3260
3270
/// alongside the last token of the given node. Any tokens between `node.lastToken` and the
3261
3271
/// returned node's `lastToken` are delimiter tokens that shouldn't be preceded by a break.
3262
3272
private func outermostEnclosingNode( from node: Syntax ) -> Syntax ? {
3263
- guard let afterToken = node. lastToken? . nextToken, closingDelimiterTokens. contains ( afterToken)
3273
+ guard let afterToken = node. lastToken? . nextToken ( viewMode : . all ) , closingDelimiterTokens. contains ( afterToken)
3264
3274
else {
3265
3275
return nil
3266
3276
}
3267
3277
var parenthesizedExpr = afterToken. parent
3268
- while let nextToken = parenthesizedExpr? . lastToken? . nextToken,
3278
+ while let nextToken = parenthesizedExpr? . lastToken? . nextToken ( viewMode : . all ) ,
3269
3279
closingDelimiterTokens. contains ( nextToken) ,
3270
3280
let nextExpr = nextToken. parent
3271
3281
{
@@ -3355,9 +3365,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3355
3365
// followed by a dot (for example, in an implicit member reference)---removing the spaces in
3356
3366
// those situations would cause the parser to greedily treat the combined sequence of
3357
3367
// operator characters as a single operator.
3358
- if case . postfixOperator? = token. previousToken? . tokenKind { return true }
3368
+ if case . postfixOperator? = token. previousToken ( viewMode : . all ) ? . tokenKind { return true }
3359
3369
3360
- switch token. nextToken? . tokenKind {
3370
+ switch token. nextToken ( viewMode : . all ) ? . tokenKind {
3361
3371
case . prefixOperator? , . prefixPeriod? : return true
3362
3372
default : return false
3363
3373
}
@@ -3389,7 +3399,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3389
3399
// The leading trivia of the next token, after the ignored node, may contain content that
3390
3400
// belongs with the ignored node. The trivia extraction that is performed for `lastToken` later
3391
3401
// excludes that content so it needs to be extracted and added to the token stream here.
3392
- if let next = node. lastToken? . nextToken, let trivia = next. leadingTrivia. first {
3402
+ if let next = node. lastToken? . nextToken ( viewMode : . all ) , let trivia = next. leadingTrivia. first {
3393
3403
switch trivia {
3394
3404
case . lineComment, . blockComment:
3395
3405
trivia. write ( to: & nodeText)
@@ -3581,7 +3591,7 @@ class CommentMovingRewriter: SyntaxRewriter {
3581
3591
override func visit( _ node: SequenceExprSyntax ) -> ExprSyntax {
3582
3592
for element in node. elements {
3583
3593
if let binaryOperatorExpr = element. as ( BinaryOperatorExprSyntax . self) ,
3584
- let followingToken = binaryOperatorExpr. operatorToken. nextToken,
3594
+ let followingToken = binaryOperatorExpr. operatorToken. nextToken ( viewMode : . all ) ,
3585
3595
followingToken. leadingTrivia. hasLineComment
3586
3596
{
3587
3597
// Rewrite the trivia so that the comment is in the operator token's leading trivia.
0 commit comments