-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement removing excessive trivia using swift-syntax
- Loading branch information
Showing
4 changed files
with
117 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import SwiftSyntax | ||
|
||
final class ExcessiveTriviaRemover: SyntaxRewriter { | ||
/// Remove empty lines if there are more than 1 lines stacked together. | ||
override func visitAny(_ node: Syntax) -> Syntax? { | ||
var node = node | ||
|
||
var modifiedLeadingTrivia = false | ||
let newLeadingTrivia = node.leadingTrivia.pieces.map { piece in | ||
if case let .newlines(count) = piece, | ||
count > 1 { | ||
modifiedLeadingTrivia = true | ||
return TriviaPiece.newlines(1) | ||
} else { | ||
return piece | ||
} | ||
} | ||
if modifiedLeadingTrivia { | ||
node = node.with( | ||
\.leadingTrivia, | ||
Trivia(pieces: newLeadingTrivia) | ||
) | ||
} | ||
|
||
var modifiedTrailingTrivia = false | ||
let newTrailingTrivia = node.trailingTrivia.pieces.map { piece in | ||
if case let .newlines(count) = piece, | ||
count > 1 { | ||
modifiedTrailingTrivia = true | ||
return TriviaPiece.newlines(1) | ||
} else { | ||
return piece | ||
} | ||
} | ||
if modifiedTrailingTrivia { | ||
node = node.with( | ||
\.trailingTrivia, | ||
Trivia(pieces: newTrailingTrivia) | ||
) | ||
} | ||
|
||
let modified = modifiedLeadingTrivia || modifiedTrailingTrivia | ||
return modified ? self.rewrite(node) : nil | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...s/EnumeratorMacroImpl/PostProcessor.swift → .../EnumeratorMacroImpl/SwitchRewriter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters