Skip to content

Commit 5519653

Browse files
committed
Remove withKind from TokenSyntax
`with(\.tokenKind, newValue)` should be used instead.
1 parent 9b233dc commit 5519653

File tree

9 files changed

+38
-24
lines changed

9 files changed

+38
-24
lines changed

Examples/Sources/AddOneToIntegerLiterals/AddOneToIntegerLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private class AddOneToIntegerLiterals: SyntaxRewriter {
3131
let int = Int(integerText)!
3232

3333
// Create a new integer literal token with `int + 1` as its text.
34-
let newIntegerLiteralToken = token.withKind(.integerLiteral("\(int + 1)"))
34+
let newIntegerLiteralToken = token.with(\.tokenKind, .integerLiteral("\(int + 1)"))
3535

3636
// Return the new integer literal.
3737
return newIntegerLiteralToken

Sources/SwiftParserDiagnostics/LexerDiagnosticMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public extension SwiftSyntax.TokenDiagnostic {
204204
.replacingFirstOccurence(of: "", with: #"""#)
205205
.replacingLastOccurence(of: "", with: #"""#)
206206

207-
let fixedToken = token.withKind(TokenKind.fromRaw(kind: rawKind, text: replacedText))
207+
let fixedToken = token.with(\.tokenKind, TokenKind.fromRaw(kind: rawKind, text: replacedText))
208208
return [
209209
FixIt(message: .replaceCurlyQuoteByNormalQuote, changes: [.replace(oldNode: Syntax(token), newNode: Syntax(fixedToken))])
210210
]

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ fileprivate extension TokenSyntax {
2020
var negatedAvailabilityKeyword: TokenSyntax {
2121
switch self.tokenKind {
2222
case .poundAvailableKeyword:
23-
return self.withKind(.poundUnavailableKeyword)
23+
return self.with(\.tokenKind, .poundUnavailableKeyword)
2424
case .poundUnavailableKeyword:
25-
return self.withKind(.poundAvailableKeyword)
25+
return self.with(\.tokenKind, .poundAvailableKeyword)
2626
default:
2727
preconditionFailure("The availability token of an AvailabilityConditionSyntax should always be #available or #unavailable")
2828
}

Sources/SwiftRefactor/AddSeparatorsToIntegerLiteral.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public struct AddSeparatorsToIntegerLiteral: RefactoringProvider {
5151
formattedText += value.byAddingGroupSeparators(at: lit.idealGroupSize)
5252
return
5353
lit
54-
.with(\.digits, lit.digits.withKind(.integerLiteral(formattedText)))
54+
.with(\.digits, lit.digits.with(\.tokenKind, .integerLiteral(formattedText)))
5555
}
5656
}
5757

Sources/SwiftRefactor/FormatRawStringLiteral.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public struct FormatRawStringLiteral: RefactoringProvider {
4949
guard maximumHashes > 0 else {
5050
return
5151
lit
52-
.with(\.openDelimiter, lit.openDelimiter?.withKind(.rawStringDelimiter("")))
53-
.with(\.closeDelimiter, lit.closeDelimiter?.withKind(.rawStringDelimiter("")))
52+
.with(\.openDelimiter, lit.openDelimiter?.with(\.tokenKind, .rawStringDelimiter("")))
53+
.with(\.closeDelimiter, lit.closeDelimiter?.with(\.tokenKind, .rawStringDelimiter("")))
5454
}
5555

5656
let delimiters = String(repeating: "#", count: maximumHashes + 1)
5757
return
5858
lit
59-
.with(\.openDelimiter, lit.openDelimiter?.withKind(.rawStringDelimiter(delimiters)))
60-
.with(\.closeDelimiter, lit.closeDelimiter?.withKind(.rawStringDelimiter(delimiters)))
59+
.with(\.openDelimiter, lit.openDelimiter?.with(\.tokenKind, .rawStringDelimiter(delimiters)))
60+
.with(\.closeDelimiter, lit.closeDelimiter?.with(\.tokenKind, .rawStringDelimiter(delimiters)))
6161
}
6262
}
6363

Sources/SwiftRefactor/RemoveSeparatorsFromIntegerLiteral.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public struct RemoveSeparatorsFromIntegerLiteral: RefactoringProvider {
3232
return lit
3333
}
3434
let formattedText = lit.digits.text.filter({ $0 != "_" })
35-
return lit.with(\.digits, lit.digits.withKind(.integerLiteral(formattedText)))
35+
return lit.with(\.digits, lit.digits.with(\.tokenKind, .integerLiteral(formattedText)))
3636
}
3737
}

Sources/SwiftSyntax/Syntax.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ public extension SyntaxProtocol {
306306
return parent.previousToken(viewMode: viewMode)
307307
}
308308

309+
@available(*, deprecated, message: "Use previousToken(viewMode:) instead")
310+
var previousToken: TokenSyntax? {
311+
return previousToken(viewMode: .sourceAccurate)
312+
}
313+
309314
/// Recursively walks through the tree to find the next token semantically
310315
/// after this node.
311316
func nextToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
@@ -323,6 +328,11 @@ public extension SyntaxProtocol {
323328
return parent.nextToken(viewMode: viewMode)
324329
}
325330

331+
@available(*, deprecated, message: "Use nextToken(viewMode:) instead")
332+
var nextToken: TokenSyntax? {
333+
return previousToken(viewMode: .sourceAccurate)
334+
}
335+
326336
/// Returns the first token node that is part of this syntax node.
327337
func firstToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
328338
guard viewMode.shouldTraverse(node: raw) else { return nil }
@@ -338,6 +348,11 @@ public extension SyntaxProtocol {
338348
return nil
339349
}
340350

351+
@available(*, deprecated, message: "Use firstToken(viewMode: .sourceAccurate) instead")
352+
var firstToken: TokenSyntax? {
353+
return firstToken(viewMode: .sourceAccurate)
354+
}
355+
341356
/// Returns the last token node that is part of this syntax node.
342357
func lastToken(viewMode: SyntaxTreeViewMode) -> TokenSyntax? {
343358
guard viewMode.shouldTraverse(node: raw) else { return nil }
@@ -353,6 +368,11 @@ public extension SyntaxProtocol {
353368
return nil
354369
}
355370

371+
@available(*, deprecated, message: "Use lastToken(viewMode: .sourceAccurate) instead")
372+
var lastToken: TokenSyntax? {
373+
return lastToken(viewMode: .sourceAccurate)
374+
}
375+
356376
/// Find the syntax token at the given absolute position within this
357377
/// syntax node or any of its children.
358378
func token(at position: AbsolutePosition) -> TokenSyntax? {

Sources/SwiftSyntax/TokenSyntax.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
6868
return tokenKind.text
6969
}
7070

71-
/// Returns a new TokenSyntax with its kind replaced
72-
/// by the provided token kind.
73-
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
74-
guard raw.kind == .token else {
75-
fatalError("TokenSyntax must have token as its raw")
76-
}
77-
let arena = SyntaxArena()
78-
let newRaw = tokenView.withKind(tokenKind, arena: arena)
79-
let newData = data.replacingSelf(newRaw, arena: arena)
80-
return TokenSyntax(newData)
81-
}
82-
8371
/// The leading trivia (spaces, newlines, etc.) associated with this token.
8472
public var leadingTrivia: Trivia {
8573
get {
@@ -106,7 +94,13 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
10694
return tokenView.formKind()
10795
}
10896
set {
109-
self = withKind(newValue)
97+
guard raw.kind == .token else {
98+
fatalError("TokenSyntax must have token as its raw")
99+
}
100+
let arena = SyntaxArena()
101+
let newRaw = tokenView.withKind(newValue, arena: arena)
102+
let newData = data.replacingSelf(newRaw, arena: arena)
103+
self = TokenSyntax(newData)
110104
}
111105
}
112106

Tests/SwiftSyntaxTest/RawSyntaxTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class RawSyntaxTests: XCTestCase {
142142
XCTAssertEqual(ident.description, "\nfoo ")
143143

144144
let identSyntax = Syntax(raw: ident.raw).as(TokenSyntax.self)!
145-
let barIdentSyntax = identSyntax.withKind(.keyword(.open))
145+
let barIdentSyntax = identSyntax.with(\.tokenKind, .keyword(.open))
146146
let barIdent = barIdentSyntax.raw.as(RawTokenSyntax.self)!
147147

148148
XCTAssertEqual(barIdent.tokenKind, .keyword)

0 commit comments

Comments
 (0)