Skip to content

Commit 0ebb3c8

Browse files
authored
Merge pull request #609 from allevato/unify-helpers
Replace `with` calls with in-place mutation; clean up helpers.
2 parents 2d5461e + 1c960a9 commit 0ebb3c8

33 files changed

+501
-676
lines changed

Sources/SwiftFormat/Core/AddModifierRewriter.swift

-183
This file was deleted.

Sources/SwiftFormat/Core/LegacyTriviaBehavior.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ private final class LegacyTriviaBehaviorRewriter: SyntaxRewriter {
1818
override func visit(_ token: TokenSyntax) -> TokenSyntax {
1919
var token = token
2020
if let pendingLeadingTrivia = pendingLeadingTrivia {
21-
token = token.with(\.leadingTrivia, pendingLeadingTrivia + token.leadingTrivia)
21+
token.leadingTrivia = pendingLeadingTrivia + token.leadingTrivia
2222
self.pendingLeadingTrivia = nil
2323
}
2424
if token.nextToken(viewMode: .sourceAccurate) != nil,
2525
let firstIndexToMove = token.trailingTrivia.firstIndex(where: shouldTriviaPieceBeMoved)
2626
{
2727
pendingLeadingTrivia = Trivia(pieces: Array(token.trailingTrivia[firstIndexToMove...]))
28-
token =
29-
token.with(\.trailingTrivia, Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove])))
28+
token.trailingTrivia = Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove]))
3029
}
3130
return token
3231
}

Sources/SwiftFormat/Core/ModifierListSyntax+Convenience.swift

+23-40
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
import SwiftSyntax
1414

1515
extension DeclModifierListSyntax {
16-
17-
func has(modifier: String) -> Bool {
18-
return contains { $0.name.text == modifier }
19-
}
20-
21-
func has(modifier: TokenKind) -> Bool {
22-
return contains { $0.name.tokenKind == modifier }
23-
}
24-
2516
/// Returns the declaration's access level modifier, if present.
2617
var accessLevelModifier: DeclModifierSyntax? {
2718
for modifier in self {
@@ -35,41 +26,33 @@ extension DeclModifierListSyntax {
3526
return nil
3627
}
3728

38-
/// Returns modifier list without the given modifier.
39-
func remove(name: String) -> DeclModifierListSyntax {
40-
return filter { $0.name.text != name }
29+
/// Returns true if the modifier list contains any of the keywords in the given set.
30+
func contains(anyOf keywords: Set<Keyword>) -> Bool {
31+
return contains {
32+
switch $0.name.tokenKind {
33+
case .keyword(let keyword): return keywords.contains(keyword)
34+
default: return false
35+
}
36+
}
4137
}
4238

43-
/// Returns a formatted declaration modifier token with the given name.
44-
func createModifierToken(name: String) -> DeclModifierSyntax {
45-
let id = TokenSyntax.identifier(name, trailingTrivia: .spaces(1))
46-
let newModifier = DeclModifierSyntax(name: id, detail: nil)
47-
return newModifier
39+
/// Removes any of the modifiers in the given set from the modifier list, mutating it in-place.
40+
mutating func remove(anyOf keywords: Set<Keyword>) {
41+
self = filter {
42+
switch $0.name.tokenKind {
43+
case .keyword(let keyword): return !keywords.contains(keyword)
44+
default: return true
45+
}
46+
}
4847
}
4948

50-
/// Inserts the given modifier into the list at a specific index.
51-
///
52-
/// If the modifier is being inserted at the front of the list, the current front element's
53-
/// leading trivia will be moved to the new element to preserve any leading comments and newlines.
54-
mutating func triviaPreservingInsert(
55-
_ modifier: DeclModifierSyntax, at index: SyntaxChildrenIndex
56-
) {
57-
var modifier = modifier
58-
modifier.trailingTrivia = [.spaces(1)]
59-
60-
guard index == self.startIndex else {
61-
self.insert(modifier, at: index)
62-
return
63-
}
64-
guard var firstMod = first, let firstTok = firstMod.firstToken(viewMode: .sourceAccurate) else {
65-
self.insert(modifier, at: index)
66-
return
49+
/// Returns a copy of the modifier list with any of the modifiers in the given set removed.
50+
func removing(anyOf keywords: Set<Keyword>) -> DeclModifierListSyntax {
51+
return filter {
52+
switch $0.name.tokenKind {
53+
case .keyword(let keyword): return !keywords.contains(keyword)
54+
default: return true
55+
}
6756
}
68-
69-
modifier.leadingTrivia = firstTok.leadingTrivia
70-
firstMod.leadingTrivia = []
71-
firstMod.trailingTrivia = [.spaces(1)]
72-
self[self.startIndex] = firstMod
73-
self.insert(modifier, at: self.startIndex)
7457
}
7558
}

Sources/SwiftFormat/Core/TokenSyntax+Convenience.swift

-37
This file was deleted.

0 commit comments

Comments
 (0)