Skip to content

Commit

Permalink
Make organizeDeclarations options non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 19, 2020
1 parent ee042e2 commit ebde1bb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ final class RulesViewController: NSViewController {
)
return UserSelectionType.list(list)

case .text, .set, .array:
case .text, .int, .set, .array:
let freeText = UserSelectionFreeText(
identifier: descriptor.argumentName,
title: descriptor.displayName,
Expand Down
8 changes: 4 additions & 4 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,12 +697,12 @@ Organizes declarations within class, struct, and enum bodies.

Option | Description
--- | ---
`--categorymark` | Template for category mark comments (defaults to `MARK: %c`)
`--categorymark` | Template for category mark comments. Defaults to "MARK: %c"
`--beforemarks` | Declarations placed before first mark (e.g. `typealias,struct`)
`--lifecycle` | Names of additional Lifecycle methods (e.g. `viewDidLoad`)
`--structthreshold` | Minimum line count to organize struct body (defaults to `nil`)
`--classthreshold` | Minimum line count to organize class body (defaults to `nil`)
`--enumthreshold` | Minimum line count to organize enum body (defaults to `nil`)
`--structthreshold` | Minimum line count to organize struct body. Defaults to 0
`--classthreshold` | Minimum line count to organize class body. Defaults to 0
`--enumthreshold` | Minimum line count to organize enum body. Defaults to 0

<details>
<summary>Examples</summary>
Expand Down
12 changes: 6 additions & 6 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,9 @@ public struct FormatOptions: CustomStringConvertible {
public var categoryMarkComment: String
public var beforeMarks: Set<String>
public var lifecycleMethods: Set<String>
public var organizeClassThreshold: Int?
public var organizeStructThreshold: Int?
public var organizeEnumThreshold: Int?
public var organizeClassThreshold: Int
public var organizeStructThreshold: Int
public var organizeEnumThreshold: Int
public var yodaSwap: YodaMode

// Deprecated
Expand Down Expand Up @@ -378,9 +378,9 @@ public struct FormatOptions: CustomStringConvertible {
categoryMarkComment: String = "MARK: %c",
beforeMarks: Set<String> = [],
lifecycleMethods: Set<String> = [],
organizeClassThreshold: Int? = nil,
organizeStructThreshold: Int? = nil,
organizeEnumThreshold: Int? = nil,
organizeClassThreshold: Int = 0,
organizeStructThreshold: Int = 0,
organizeEnumThreshold: Int = 0,
yodaSwap: YodaMode = .always,
// Doesn't really belong here, but hard to put elsewhere
fragment: Bool = false,
Expand Down
57 changes: 26 additions & 31 deletions Sources/OptionsDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extension FormatOptions {
case binary(true: [String], false: [String])
case `enum`([String])
case text
case int
case array
case set
}
Expand Down Expand Up @@ -161,6 +162,24 @@ extension FormatOptions {
type = .enum(keys)
}

init(argumentName: String,
propertyName: String,
displayName: String,
help: String,
keyPath: WritableKeyPath<FormatOptions, Int>)
{
self.init(
argumentName: argumentName,
propertyName: propertyName,
displayName: displayName,
help: help,
keyPath: keyPath,
fromArgument: { Int($0).map { max(0, $0) } },
toArgument: { String($0) }
)
type = .int
}

init<T: RawRepresentable>(argumentName: String,
propertyName: String,
displayName: String,
Expand Down Expand Up @@ -694,7 +713,7 @@ extension FormatOptions.Descriptor {
argumentName: "categorymark",
propertyName: "categoryMarkComment",
displayName: "Category Mark Comment",
help: "Template for category mark comments (defaults to `MARK: %c`)",
help: "Template for category mark comments. Defaults to \"MARK: %c\"",
keyPath: \.categoryMarkComment,
fromArgument: { $0 },
toArgument: { $0 }
Expand All @@ -717,46 +736,22 @@ extension FormatOptions.Descriptor {
argumentName: "structthreshold",
propertyName: "organizeStructThreshold",
displayName: "Organize Struct Threshold",
help: "Minimum line count to organize struct body (defaults to `nil`)",
keyPath: \.organizeStructThreshold,
fromArgument: { .some(Int($0)) },
toArgument: {
if let lineCount = $0 {
return "\(lineCount)"
} else {
return ""
}
}
help: "Minimum line count to organize struct body. Defaults to 0",
keyPath: \.organizeStructThreshold
)
static let organizeClassThreshold = FormatOptions.Descriptor(
argumentName: "classthreshold",
propertyName: "organizeClassThreshold",
displayName: "Organize Class Threshold",
help: "Minimum line count to organize class body (defaults to `nil`)",
keyPath: \.organizeClassThreshold,
fromArgument: { .some(Int($0)) },
toArgument: {
if let lineCount = $0 {
return "\(lineCount)"
} else {
return ""
}
}
help: "Minimum line count to organize class body. Defaults to 0",
keyPath: \.organizeClassThreshold
)
static let organizeEnumThreshold = FormatOptions.Descriptor(
argumentName: "enumthreshold",
propertyName: "organizeEnumThreshold",
displayName: "Organize Enum Threshold",
help: "Minimum line count to organize enum body (defaults to `nil`)",
keyPath: \.organizeEnumThreshold,
fromArgument: { .some(Int($0)) },
toArgument: {
if let lineCount = $0 {
return "\(lineCount)"
} else {
return ""
}
}
help: "Minimum line count to organize enum body. Defaults to 0",
keyPath: \.organizeEnumThreshold
)

// MARK: - Internal
Expand Down
22 changes: 10 additions & 12 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5063,7 +5063,7 @@ public struct _FormatRules {
}

// Make sure this type's body is longer than the organization threshold
let organizationThreshold: Int?
let organizationThreshold: Int
switch typeDeclaration.kind {
case "class":
organizationThreshold = formatter.options.organizeClassThreshold
Expand All @@ -5072,20 +5072,18 @@ public struct _FormatRules {
case "enum":
organizationThreshold = formatter.options.organizeEnumThreshold
default:
organizationThreshold = nil
organizationThreshold = 0
}

if let organizationThreshold = organizationThreshold {
// Count the number of lines in this declaration
let lineCount = typeDeclaration.body
.flatMap { $0.tokens }
.filter { $0.isLinebreak }
.count
// Count the number of lines in this declaration
let lineCount = typeDeclaration.body
.flatMap { $0.tokens }
.filter { $0.isLinebreak }
.count

// Don't organize this type's body if it is shorter than the minimum organization threshold
if lineCount < organizationThreshold {
return typeDeclaration
}
// Don't organize this type's body if it is shorter than the minimum organization threshold
if lineCount < organizationThreshold {
return typeDeclaration
}

var typeOpeningTokens = typeDeclaration.open
Expand Down

0 comments on commit ebde1bb

Please sign in to comment.