Skip to content
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

Adds option for trimmingTrailingWhitespace to ignore whitespace only lines #800

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions Source/SourceKittenFramework/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public final class File { // swiftlint:disable:this type_body_length
/// - Returns: formatted String
/// - Throws: Request.Error
public func format(trimmingTrailingWhitespace: Bool,
ignoringEmptyLines: Bool = true,
useTabs: Bool,
indentWidth: Int) throws -> String {
guard let path = path else {
Expand Down Expand Up @@ -131,10 +132,12 @@ public final class File { // swiftlint:disable:this type_body_length

if trimmingTrailingWhitespace {
newContents = newContents.map {
$0.bridge().trimmingTrailingCharacters(in: .whitespaces)
if !$0.containsNonWhitespaceCharacters, ignoringEmptyLines {
return $0.bridge()
}
return $0.bridge().trimmingTrailingCharacters(in: .whitespaces)
}
}

return newContents.joined(separator: "\n") + "\n"
}

Expand Down Expand Up @@ -508,3 +511,10 @@ private extension XMLIndexer {
return elements.map(dictionary(from:)) as [SourceKitRepresentable]
}
}

private extension String {
var containsNonWhitespaceCharacters: Bool {
return self.contains(where: {!$0.isWhitespace})
}
}