Skip to content

Commit

Permalink
Implement wrapParameters option
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMillerSF authored and nicklockwood committed Jan 15, 2020
1 parent 6f24da3 commit c5b5642
Show file tree
Hide file tree
Showing 9 changed files with 559 additions and 308 deletions.
31 changes: 29 additions & 2 deletions Rules.md
Expand Up @@ -1514,13 +1514,40 @@ Align wrapped function arguments or collection elements.

Option | Description
--- | ---
`--wraparguments` | Wrap function args: "before-first", "after-first", "preserve"
`--wraparguments` | Wrap all arguments: "before-first", "after-first", "preserve"
`--wrapparameters` | Wrap func params: "before-first", "after-first", "preserve"
`--wrapcollections` | Wrap array/dict: "before-first", "after-first", "preserve"
`--closingparen` | Closing paren position: "balanced" (default) or "same-line"

<details>
<summary>Examples</summary>

**NOTE:** For backwards compatibility with previous versions, if no value is
provided for `--wrapparameters`, the value for `--wraparguments` will be used.
`--wraparguments before-first`

```diff
- foo(bar: Int,
- baz: String)

+ foo(
+ bar: Int,
+ baz: String
+ )
```

```diff
- class Foo<Bar,
- Baz>

+ class Foo<
+ Bar,
+ Baz
+ >
```

`--wrapparameters after-first`

```diff
- func foo(bar: Int,
- baz: String) {
Expand All @@ -1535,7 +1562,7 @@ Option | Description
}
```

Or for `--wrapcollections before-first`:
`--wrapcollections before-first`:

```diff
- let foo = [bar,
Expand Down
28 changes: 27 additions & 1 deletion Sources/Examples.swift
Expand Up @@ -928,6 +928,32 @@ private struct Examples {
"""

let wrapArguments = """
**NOTE:** For backwards compatibility with previous versions, if no value is
provided for `--wrapparameters`, the value for `--wraparguments` will be used.
`--wraparguments before-first`
```diff
- foo(bar: Int,
- baz: String)
+ foo(
+ bar: Int,
+ baz: String
+ )
```
```diff
- class Foo<Bar,
- Baz>
+ class Foo<
+ Bar,
+ Baz
+ >
```
`--wrapparameters after-first`
```diff
- func foo(bar: Int,
- baz: String) {
Expand All @@ -942,7 +968,7 @@ private struct Examples {
}
```
Or for `--wrapcollections before-first`:
`--wrapcollections before-first`:
```diff
- let foo = [bar,
Expand Down
3 changes: 3 additions & 0 deletions Sources/Options.swift
Expand Up @@ -249,6 +249,7 @@ public struct FormatOptions: CustomStringConvertible {
public var fileHeader: HeaderStrippingMode
public var ifdefIndent: IndentMode
public var wrapArguments: WrapMode
public var wrapParameters: WrapMode
public var wrapCollections: WrapMode
public var closingParenOnSameLine: Bool
public var uppercaseHex: Bool
Expand Down Expand Up @@ -300,6 +301,7 @@ public struct FormatOptions: CustomStringConvertible {
fileHeader: HeaderStrippingMode = .ignore,
ifdefIndent: IndentMode = .indent,
wrapArguments: WrapMode = .preserve,
wrapParameters: WrapMode = .preserve,
wrapCollections: WrapMode = .preserve,
closingParenOnSameLine: Bool = false,
uppercaseHex: Bool = true,
Expand Down Expand Up @@ -343,6 +345,7 @@ public struct FormatOptions: CustomStringConvertible {
self.fileHeader = fileHeader
self.ifdefIndent = ifdefIndent
self.wrapArguments = wrapArguments
self.wrapParameters = wrapParameters
self.wrapCollections = wrapCollections
self.closingParenOnSameLine = closingParenOnSameLine
self.uppercaseHex = uppercaseHex
Expand Down
11 changes: 10 additions & 1 deletion Sources/OptionsDescriptor.swift
Expand Up @@ -231,6 +231,7 @@ extension FormatOptions.Descriptor {
fileHeader,
ifdefIndent,
wrapArguments,
wrapParameters,
wrapCollections,
closingParen,
hexLiteralCase,
Expand Down Expand Up @@ -390,10 +391,18 @@ extension FormatOptions.Descriptor {
argumentName: "wraparguments",
propertyName: "wrapArguments",
displayName: "Wrap Arguments",
help: "Wrap function args: \"before-first\", \"after-first\", \"preserve\"",
help: "Wrap all arguments: \"before-first\", \"after-first\", \"preserve\"",
keyPath: \.wrapArguments,
options: ["before-first", "after-first", "preserve", "disabled"]
)
static let wrapParameters = FormatOptions.Descriptor(
argumentName: "wrapparameters",
propertyName: "wrapParameters",
displayName: "Wrap Parameters",
help: "Wrap func params: \"before-first\", \"after-first\", \"preserve\"",
keyPath: \.wrapParameters,
options: ["before-first", "after-first", "preserve", "disabled"]
)
static let wrapCollections = FormatOptions.Descriptor(
argumentName: "wrapcollections",
propertyName: "wrapCollections",
Expand Down
20 changes: 18 additions & 2 deletions Sources/ParsingHelpers.swift
Expand Up @@ -867,7 +867,7 @@ extension Formatter {
}
}
for scopeType in ["(", "[", "<"] {
forEach(.startOfScope(scopeType)) { i, _ in
forEach(.startOfScope(scopeType)) { i, _ in // TODO: use token closure arg
guard let endOfScope = endOfScope(at: i) else {
return
}
Expand All @@ -888,9 +888,25 @@ extension Formatter {
// Not an argument list, or only one argument
return
}

func isParameterList() -> Bool {
if last(.keyword, before: i) == .keyword("func") {
// Is parameters at start of function
return true
}

if last(.nonSpaceOrCommentOrLinebreak, before: i) == .delimiter(":"),
next(.nonSpaceOrCommentOrLinebreak, after: endOfScope) == .operator("->", .infix) {
// Is parameter list for a closure type
return true
}

return false
}

checkNestedScopes = false
endOfScopeOnSameLine = options.closingParenOnSameLine
fallthrough
mode = isParameterList() ? options.wrapParameters : options.wrapArguments
case "<":
mode = options.wrapArguments
case "[":
Expand Down
4 changes: 2 additions & 2 deletions Sources/Rules.swift
Expand Up @@ -3067,7 +3067,7 @@ public struct _FormatRules {
public let wrap = FormatRule(
help: "Wrap lines that exceed the specified maximum width.",
options: ["maxwidth"],
sharedOptions: ["wraparguments", "wrapcollections", "closingparen", "indent",
sharedOptions: ["wraparguments", "wrapparameters", "wrapcollections", "closingparen", "indent",
"trimwhitespace", "linebreaks", "tabwidth", "maxwidth"]
) { formatter in
let maxWidth = formatter.options.maxWidth
Expand Down Expand Up @@ -3188,7 +3188,7 @@ public struct _FormatRules {
/// Normalize argument wrapping style
public let wrapArguments = FormatRule(
help: "Align wrapped function arguments or collection elements.",
options: ["wraparguments", "wrapcollections", "closingparen"],
options: ["wraparguments", "wrapparameters", "wrapcollections", "closingparen"],
sharedOptions: ["indent", "trimwhitespace", "linebreaks", "tabwidth", "maxwidth"]
) { formatter in
formatter.wrapCollectionsAndArguments(completePartialWrapping: true)
Expand Down
2 changes: 1 addition & 1 deletion Tests/MetadataTests.swift
Expand Up @@ -134,7 +134,7 @@ class MetadataTests: XCTestCase {
referencedOptions.append(.lineBreak)
case .identifier("wrapCollectionsAndArguments"):
referencedOptions += [
.wrapArguments, .wrapCollections, .closingParen,
.wrapArguments, .wrapParameters, .wrapCollections, .closingParen,
.indentation, .truncateBlankLines, .lineBreak, .tabWidth, .maxWidth,
]
case .identifier("options") where formatter.token(at: index + 1) == .operator(".", .infix):
Expand Down

0 comments on commit c5b5642

Please sign in to comment.