Skip to content

Commit

Permalink
Merge pull request swiftlang#609 from allevato/unify-helpers
Browse files Browse the repository at this point in the history
Replace `with` calls with in-place mutation; clean up helpers.
  • Loading branch information
allevato committed Sep 4, 2023
2 parents 2d5461e + 1c960a9 commit 0ebb3c8
Show file tree
Hide file tree
Showing 33 changed files with 501 additions and 676 deletions.
183 changes: 0 additions & 183 deletions Sources/SwiftFormat/Core/AddModifierRewriter.swift

This file was deleted.

5 changes: 2 additions & 3 deletions Sources/SwiftFormat/Core/LegacyTriviaBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ private final class LegacyTriviaBehaviorRewriter: SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> TokenSyntax {
var token = token
if let pendingLeadingTrivia = pendingLeadingTrivia {
token = token.with(\.leadingTrivia, pendingLeadingTrivia + token.leadingTrivia)
token.leadingTrivia = pendingLeadingTrivia + token.leadingTrivia
self.pendingLeadingTrivia = nil
}
if token.nextToken(viewMode: .sourceAccurate) != nil,
let firstIndexToMove = token.trailingTrivia.firstIndex(where: shouldTriviaPieceBeMoved)
{
pendingLeadingTrivia = Trivia(pieces: Array(token.trailingTrivia[firstIndexToMove...]))
token =
token.with(\.trailingTrivia, Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove])))
token.trailingTrivia = Trivia(pieces: Array(token.trailingTrivia[..<firstIndexToMove]))
}
return token
}
Expand Down
63 changes: 23 additions & 40 deletions Sources/SwiftFormat/Core/ModifierListSyntax+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
import SwiftSyntax

extension DeclModifierListSyntax {

func has(modifier: String) -> Bool {
return contains { $0.name.text == modifier }
}

func has(modifier: TokenKind) -> Bool {
return contains { $0.name.tokenKind == modifier }
}

/// Returns the declaration's access level modifier, if present.
var accessLevelModifier: DeclModifierSyntax? {
for modifier in self {
Expand All @@ -35,41 +26,33 @@ extension DeclModifierListSyntax {
return nil
}

/// Returns modifier list without the given modifier.
func remove(name: String) -> DeclModifierListSyntax {
return filter { $0.name.text != name }
/// Returns true if the modifier list contains any of the keywords in the given set.
func contains(anyOf keywords: Set<Keyword>) -> Bool {
return contains {
switch $0.name.tokenKind {
case .keyword(let keyword): return keywords.contains(keyword)
default: return false
}
}
}

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

/// Inserts the given modifier into the list at a specific index.
///
/// If the modifier is being inserted at the front of the list, the current front element's
/// leading trivia will be moved to the new element to preserve any leading comments and newlines.
mutating func triviaPreservingInsert(
_ modifier: DeclModifierSyntax, at index: SyntaxChildrenIndex
) {
var modifier = modifier
modifier.trailingTrivia = [.spaces(1)]

guard index == self.startIndex else {
self.insert(modifier, at: index)
return
}
guard var firstMod = first, let firstTok = firstMod.firstToken(viewMode: .sourceAccurate) else {
self.insert(modifier, at: index)
return
/// Returns a copy of the modifier list with any of the modifiers in the given set removed.
func removing(anyOf keywords: Set<Keyword>) -> DeclModifierListSyntax {
return filter {
switch $0.name.tokenKind {
case .keyword(let keyword): return !keywords.contains(keyword)
default: return true
}
}

modifier.leadingTrivia = firstTok.leadingTrivia
firstMod.leadingTrivia = []
firstMod.trailingTrivia = [.spaces(1)]
self[self.startIndex] = firstMod
self.insert(modifier, at: self.startIndex)
}
}
37 changes: 0 additions & 37 deletions Sources/SwiftFormat/Core/TokenSyntax+Convenience.swift

This file was deleted.

Loading

0 comments on commit 0ebb3c8

Please sign in to comment.