Skip to content

Use methods on Sequence instead of SyntaxCollection #569

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
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
20 changes: 10 additions & 10 deletions Sources/SwiftFormatRules/ModifierListSyntax+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@ extension ModifierListSyntax {

/// Returns modifier list without the given modifier.
func remove(name: String) -> ModifierListSyntax {
guard has(modifier: name) else { return self }
for (index, mod) in self.enumerated() {
if mod.name.text == name {
return removing(childAt: index)
}
}
return self
let newModifiers = filter { $0.name.text != name }
return ModifierListSyntax(newModifiers)
}

/// Returns a formatted declaration modifier token with the given name.
Expand Down Expand Up @@ -71,9 +66,13 @@ extension ModifierListSyntax {
trailingTrivia: .spaces(1)) : modifier

if index == 0 {
guard formatTrivia else { return inserting(modifier, at: index) }
guard formatTrivia else {
newModifiers.insert(modifier, at: index)
return ModifierListSyntax(newModifiers)
}
guard let firstMod = first, let firstTok = firstMod.firstToken(viewMode: .sourceAccurate) else {
return inserting(modifier, at: index)
newModifiers.insert(modifier, at: index)
return ModifierListSyntax(newModifiers)
}
let formattedMod = replaceTrivia(
on: modifier,
Expand All @@ -87,7 +86,8 @@ extension ModifierListSyntax {
newModifiers.insert(formattedMod, at: 0)
return ModifierListSyntax(newModifiers)
} else {
return inserting(modifier, at: index)
newModifiers.insert(modifier, at: index)
return ModifierListSyntax(newModifiers)
}
}

Expand Down