Skip to content

Commit

Permalink
Add options for spacing around delimiter (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
facumenzella authored and nicklockwood committed Jun 2, 2023
1 parent e875d61 commit c613f87
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
1 change: 1 addition & 0 deletions Rules.md
Expand Up @@ -1910,6 +1910,7 @@ Option | Description
`--operatorfunc` | Spacing for operator funcs: "spaced" (default) or "no-space"
`--nospaceoperators` | Comma-delimited list of operators without surrounding space
`--ranges` | Spacing for ranges: "spaced" (default) or "no-space"
`--typedelimiter` | "trailing" (default) or "leading-trailing"

<details>
<summary>Examples</summary>
Expand Down
6 changes: 6 additions & 0 deletions Sources/OptionDescriptor.swift
Expand Up @@ -696,6 +696,12 @@ struct _Descriptors {
}
}
)
let spaceAroundDelimiter = OptionDescriptor(
argumentName: "typedelimiter",
displayName: "Spacing around delimiter",
help: "\"trailing\" (default) or \"leading-trailing\"",
keyPath: \.spaceAroundDelimiter
)
let spaceAroundRangeOperators = OptionDescriptor(
argumentName: "ranges",
displayName: "Ranges",
Expand Down
11 changes: 10 additions & 1 deletion Sources/Options.swift
Expand Up @@ -354,6 +354,12 @@ public enum EnumNamespacesMode: String, CaseIterable {
case structsOnly = "structs-only"
}

/// Whether or not to add spacing around data type delimiter
public enum SpaceAroundDelimiter: String, CaseIterable {
case trailing
case leadingTrailing = "leading-trailing"
}

/// Configuration options for formatting. These aren't actually used by the
/// Formatter class itself, but it makes them available to the format rules.
public struct FormatOptions: CustomStringConvertible {
Expand Down Expand Up @@ -440,6 +446,7 @@ public struct FormatOptions: CustomStringConvertible {
public var genericTypes: String
public var useSomeAny: Bool
public var wrapEffects: WrapEffects
public var spaceAroundDelimiter: SpaceAroundDelimiter

// Deprecated
public var indentComments: Bool
Expand Down Expand Up @@ -545,7 +552,8 @@ public struct FormatOptions: CustomStringConvertible {
ignoreConflictMarkers: Bool = false,
swiftVersion: Version = .undefined,
fileInfo: FileInfo = FileInfo(),
timeout: TimeInterval = 1)
timeout: TimeInterval = 1,
spaceAroundDelimiter: SpaceAroundDelimiter = .trailing)
{
self.lineAfterMarks = lineAfterMarks
self.indent = indent
Expand Down Expand Up @@ -631,6 +639,7 @@ public struct FormatOptions: CustomStringConvertible {
self.genericTypes = genericTypes
self.useSomeAny = useSomeAny
self.wrapEffects = wrapEffects
self.spaceAroundDelimiter = spaceAroundDelimiter
// Doesn't really belong here, but hard to put elsewhere
self.fragment = fragment
self.ignoreConflictMarkers = ignoreConflictMarkers
Expand Down
12 changes: 8 additions & 4 deletions Sources/Rules.swift
Expand Up @@ -452,7 +452,7 @@ public struct _FormatRules {
/// preceded by a space, unless it appears at the beginning of a line.
public let spaceAroundOperators = FormatRule(
help: "Add or remove space around operators or delimiters.",
options: ["operatorfunc", "nospaceoperators", "ranges"]
options: ["operatorfunc", "nospaceoperators", "ranges", "typedelimiter"]
) { formatter in
formatter.forEachToken { i, token in
switch token {
Expand Down Expand Up @@ -559,11 +559,15 @@ public struct _FormatRules {
// Ensure there is a space after the token
formatter.insert(.space(" "), at: i + 1)
}
if formatter.token(at: i - 1)?.isSpace == true,
formatter.token(at: i - 2)?.isLinebreak == false
{

let spaceBeforeToken = formatter.token(at: i - 1)?.isSpace == true
&& formatter.token(at: i - 2)?.isLinebreak == false

if spaceBeforeToken, formatter.options.spaceAroundDelimiter == .trailing {
// Remove space before the token
formatter.removeToken(at: i - 1)
} else if !spaceBeforeToken, formatter.options.spaceAroundDelimiter == .leadingTrailing {
formatter.insertSpace(" ", at: i)
}
default:
break
Expand Down
57 changes: 57 additions & 0 deletions Tests/RulesTests+Spacing.swift
Expand Up @@ -1167,6 +1167,63 @@ class SpacingTests: RulesTests {
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options)
}

func testSpaceAroundDataTypeDelimiterLeadingAdded() {
let input = "class Implementation: ImplementationProtocol {}"
let output = "class Implementation : ImplementationProtocol {}"
let options = FormatOptions(spaceAroundDelimiter: .leadingTrailing)
testFormatting(
for: input,
output,
rule: FormatRules.spaceAroundOperators,
options: options
)
}

func testSpaceAroundDataTypeDelimiterLeadingTrailingAdded() {
let input = "class Implementation:ImplementationProtocol {}"
let output = "class Implementation : ImplementationProtocol {}"
let options = FormatOptions(spaceAroundDelimiter: .leadingTrailing)
testFormatting(
for: input,
output,
rule: FormatRules.spaceAroundOperators,
options: options
)
}

func testSpaceAroundDataTypeDelimiterLeadingTrailingNotModified() {
let input = "class Implementation : ImplementationProtocol {}"
let options = FormatOptions(spaceAroundDelimiter: .leadingTrailing)
testFormatting(
for: input,
rule: FormatRules.spaceAroundOperators,
options: options
)
}

func testSpaceAroundDataTypeDelimiterTrailingAdded() {
let input = "class Implementation:ImplementationProtocol {}"
let output = "class Implementation: ImplementationProtocol {}"

let options = FormatOptions(spaceAroundDelimiter: .trailing)
testFormatting(
for: input,
output,
rule: FormatRules.spaceAroundOperators,
options: options
)
}

func testSpaceAroundDataTypeDelimiterLeadingNotAdded() {
let input = "class Implementation: ImplementationProtocol {}"
let options = FormatOptions(spaceAroundDelimiter: .trailing)
testFormatting(
for: input,
rule: FormatRules.spaceAroundOperators,
options: options
)
}

// MARK: - spaceAroundComments

func testSpaceAroundCommentInParens() {
Expand Down

0 comments on commit c613f87

Please sign in to comment.