Skip to content

Add whitespaceOnly option to configuration file #195

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

Open
wants to merge 1 commit into
base: swift-5.2-branch
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
5 changes: 5 additions & 0 deletions Documentation/Configuration.md
Original file line number Diff line number Diff line change
@@ -23,6 +23,10 @@ top-level keys and values:
* `tabs` _(number)_: One level of indentation is the given number of
tabs.

* `whitespaceOnly` _(boolean)_: If true, only apply whitespace changes and
omit changes that insert or remove non-whitespace characters (like
trailing commas). Defaults to `false`.

* `tabWidth` _(number)_: The number of spaces that should be considered
equivalent to one tab character. This is used during line length
calculations when tabs are used for indentation.
@@ -93,6 +97,7 @@ An example `.swift-format` configuration file is shown below.
"indentation": {
"spaces": 2
},
"whitespaceOnly": true,
"maximumBlankLines": 1,
"respectsExistingLineBreaks": true,
"lineBreakBeforeControlFlowKeywords": true,
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/SwiftFormatter.swift
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ public final class SwiftFormatter {
operatorContext: operatorContext,
node: transformedSyntax,
printTokenStream: debugOptions.contains(.dumpTokenStream),
whitespaceOnly: false)
whitespaceOnly: configuration.whitespaceOnly)
outputStream.write(printer.prettyPrint())
}
}
8 changes: 8 additions & 0 deletions Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ public struct Configuration: Codable, Equatable {
case lineLength
case tabWidth
case indentation
case whitespaceOnly
case respectsExistingLineBreaks
case lineBreakBeforeControlFlowKeywords
case lineBreakBeforeEachArgument
@@ -61,6 +62,10 @@ public struct Configuration: Codable, Equatable {
/// All indentation will be conducted in multiples of this configuration.
public var indentation: Indent = .spaces(2)

/// When true, only whitespace (e.g. spaces, newlines) are modified. Text changes (e.g. add/remove
/// trailing commas) are not performed.
public var whitespaceOnly = false

/// Indicates that the formatter should try to respect users' discretionary line breaks when
/// possible.
///
@@ -157,6 +162,8 @@ public struct Configuration: Codable, Equatable {
self.tabWidth = try container.decodeIfPresent(Int.self, forKey: .tabWidth) ?? 8
self.indentation
= try container.decodeIfPresent(Indent.self, forKey: .indentation) ?? .spaces(2)
self.whitespaceOnly
= try container.decodeIfPresent(Bool.self, forKey: .whitespaceOnly) ?? false
self.respectsExistingLineBreaks
= try container.decodeIfPresent(Bool.self, forKey: .respectsExistingLineBreaks) ?? true
self.lineBreakBeforeControlFlowKeywords
@@ -193,6 +200,7 @@ public struct Configuration: Codable, Equatable {
try container.encode(lineLength, forKey: .lineLength)
try container.encode(tabWidth, forKey: .tabWidth)
try container.encode(indentation, forKey: .indentation)
try container.encode(whitespaceOnly, forKey: .whitespaceOnly)
try container.encode(respectsExistingLineBreaks, forKey: .respectsExistingLineBreaks)
try container.encode(lineBreakBeforeControlFlowKeywords, forKey: .lineBreakBeforeControlFlowKeywords)
try container.encode(lineBreakBeforeEachArgument, forKey: .lineBreakBeforeEachArgument)
9 changes: 5 additions & 4 deletions Sources/SwiftFormatPrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
@@ -546,14 +546,15 @@ public class PrettyPrinter {
fatalError("Found trailing comma end with no corresponding start.")
}

let shouldHaveTrailingComma = startLineNumber != openCloseBreakCompensatingLineNumber
if shouldHaveTrailingComma && !hasTrailingComma {
let shouldWriteComma = whitespaceOnly ? hasTrailingComma :
startLineNumber != openCloseBreakCompensatingLineNumber

if shouldWriteComma && !hasTrailingComma {
diagnose(.addTrailingComma)
} else if !shouldHaveTrailingComma && hasTrailingComma {
} else if !shouldWriteComma && hasTrailingComma {
diagnose(.removeTrailingComma)
}

let shouldWriteComma = whitespaceOnly ? hasTrailingComma : shouldHaveTrailingComma
if shouldWriteComma {
write(",")
spaceRemaining -= 1