Skip to content

Replace with calls with in-place mutation; clean up helpers. #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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