@@ -1143,6 +1143,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1143
1143
}
1144
1144
1145
1145
override func visit( _ node: SourceFileSyntax ) -> SyntaxVisitorContinueKind {
1146
+ if shouldFormatterIgnore ( file: node) {
1147
+ appendFormatterIgnored ( node: Syntax ( node) )
1148
+ return . skipChildren
1149
+ }
1146
1150
after ( node. eofToken, tokens: . break( . same, newlines: . soft) )
1147
1151
return . visitChildren
1148
1152
}
@@ -3191,6 +3195,13 @@ class CommentMovingRewriter: SyntaxRewriter {
3191
3195
/// Map of tokens to alternate trivia to use as the token's leading trivia.
3192
3196
var rewriteTokenTriviaMap : [ TokenSyntax : Trivia ] = [ : ]
3193
3197
3198
+ override func visit( _ node: SourceFileSyntax ) -> Syntax {
3199
+ if shouldFormatterIgnore ( file: node) {
3200
+ return Syntax ( node)
3201
+ }
3202
+ return super. visit ( node)
3203
+ }
3204
+
3194
3205
override func visit( _ node: CodeBlockItemSyntax ) -> Syntax {
3195
3206
if shouldFormatterIgnore ( node: Syntax ( node) ) {
3196
3207
return Syntax ( node)
@@ -3280,14 +3291,18 @@ extension TriviaPiece {
3280
3291
3281
3292
/// Returns whether the given trivia includes a directive to ignore formatting for the next node.
3282
3293
///
3283
- /// - Parameter trivia: Leading trivia for a node that the formatter supports ignoring.
3284
- fileprivate func isFormatterIgnorePresent( inTrivia trivia: Trivia ) -> Bool {
3294
+ /// - Parameters:
3295
+ /// - trivia: Leading trivia for a node that the formatter supports ignoring.
3296
+ /// - isWholeFile: Whether to search for a whole-file ignore directive or per node ignore.
3297
+ /// - Returns: Whether the trivia contains the specified type of ignore directive.
3298
+ fileprivate func isFormatterIgnorePresent( inTrivia trivia: Trivia , isWholeFile: Bool ) -> Bool {
3285
3299
func isFormatterIgnore( in commentText: String , prefix: String , suffix: String ) -> Bool {
3286
3300
let trimmed =
3287
3301
commentText. dropFirst ( prefix. count)
3288
3302
. dropLast ( suffix. count)
3289
3303
. trimmingCharacters ( in: . whitespaces)
3290
- return trimmed == " swift-format-ignore "
3304
+ let pattern = isWholeFile ? " swift-format-ignore-file " : " swift-format-ignore "
3305
+ return trimmed == pattern
3291
3306
}
3292
3307
3293
3308
for piece in trivia {
@@ -3317,7 +3332,19 @@ fileprivate func shouldFormatterIgnore(node: Syntax) -> Bool {
3317
3332
// Regardless of the level of nesting, if the ignore directive is present on the first token
3318
3333
// contained within the node then the entire node is eligible for ignoring.
3319
3334
if let firstTrivia = node. firstToken? . leadingTrivia {
3320
- return isFormatterIgnorePresent ( inTrivia: firstTrivia)
3335
+ return isFormatterIgnorePresent ( inTrivia: firstTrivia, isWholeFile: false )
3336
+ }
3337
+ return false
3338
+ }
3339
+
3340
+ /// Returns whether the formatter should ignore the given file by printing it without changing the
3341
+ /// any if its nodes' internal text representation (i.e. print all text inside of the file as it was
3342
+ /// in the original source).
3343
+ ///
3344
+ /// - Parameter file: The root syntax node for a source file.
3345
+ fileprivate func shouldFormatterIgnore( file: SourceFileSyntax ) -> Bool {
3346
+ if let firstTrivia = file. firstToken? . leadingTrivia {
3347
+ return isFormatterIgnorePresent ( inTrivia: firstTrivia, isWholeFile: true )
3321
3348
}
3322
3349
return false
3323
3350
}
0 commit comments