@@ -52,10 +52,10 @@ let basicFormatFile = SourceFile {
5252 open override func visit(_ node: TokenSyntax) -> TokenSyntax {
5353 var leadingTrivia = node.leadingTrivia
5454 var trailingTrivia = node.trailingTrivia
55- if requiresLeadingSpace(node.tokenKind ) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
55+ if requiresLeadingSpace(node) && leadingTrivia.isEmpty && lastRewrittenToken?.trailingTrivia.isEmpty != false {
5656 leadingTrivia += .space
5757 }
58- if requiresTrailingSpace(node.tokenKind ) && trailingTrivia.isEmpty {
58+ if requiresTrailingSpace(node) && trailingTrivia.isEmpty {
5959 trailingTrivia += .space
6060 }
6161 if let keyPath = getKeyPath(Syntax(node)), requiresLeadingNewline(keyPath), !(leadingTrivia.first?.isNewline ?? false) {
@@ -121,8 +121,8 @@ let basicFormatFile = SourceFile {
121121 }
122122 }
123123
124- FunctionDecl ( " open func requiresLeadingSpace(_ tokenKind: TokenKind ) -> Bool " ) {
125- SwitchStmt ( expression: Expr ( " tokenKind " ) ) {
124+ FunctionDecl ( " open func requiresLeadingSpace(_ token: TokenSyntax ) -> Bool " ) {
125+ SwitchStmt ( expression: Expr ( " token. tokenKind" ) ) {
126126 for token in SYNTAX_TOKENS {
127127 if token. requiresLeadingSpace {
128128 SwitchCase ( " case . \( raw: token. swiftKind) : " ) {
@@ -136,8 +136,21 @@ let basicFormatFile = SourceFile {
136136 }
137137 }
138138
139- FunctionDecl ( " open func requiresTrailingSpace(_ tokenKind: TokenKind) -> Bool " ) {
140- SwitchStmt ( expression: Expr ( " tokenKind " ) ) {
139+ FunctionDecl ( " open func requiresTrailingSpace(_ token: TokenSyntax) -> Bool " ) {
140+ SwitchStmt ( """
141+ switch (token.tokenKind, token.nextToken(viewMode: .sourceAccurate)?.tokenKind) {
142+ case (.asKeyword, .exclamationMark),
143+ (.asKeyword, .postfixQuestionMark),
144+ (.initKeyword, .postfixQuestionMark),
145+ (.tryKeyword, .exclamationMark),
146+ (.tryKeyword, .postfixQuestionMark):
147+ return false
148+ default:
149+ break
150+ }
151+ """ )
152+
153+ SwitchStmt ( expression: Expr ( " token.tokenKind " ) ) {
141154 for token in SYNTAX_TOKENS {
142155 if token. requiresTrailingSpace {
143156 SwitchCase ( " case . \( raw: token. swiftKind) : " ) {
0 commit comments