From ebde1bbbc7837629c9854585fabfef067ad13580 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 19 Aug 2020 21:39:40 +0100 Subject: [PATCH] Make organizeDeclarations options non-optional --- .../Source/RulesViewController.swift | 2 +- Rules.md | 8 +-- Sources/Options.swift | 12 ++-- Sources/OptionsDescriptor.swift | 57 +++++++++---------- Sources/Rules.swift | 22 ++++--- 5 files changed, 47 insertions(+), 54 deletions(-) diff --git a/EditorExtension/Application/Source/RulesViewController.swift b/EditorExtension/Application/Source/RulesViewController.swift index 9c905f83b..7cbb8e7ab 100644 --- a/EditorExtension/Application/Source/RulesViewController.swift +++ b/EditorExtension/Application/Source/RulesViewController.swift @@ -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, diff --git a/Rules.md b/Rules.md index c93c13d2c..647d73db9 100644 --- a/Rules.md +++ b/Rules.md @@ -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
Examples diff --git a/Sources/Options.swift b/Sources/Options.swift index 69bcbaafc..6247c5b63 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -311,9 +311,9 @@ public struct FormatOptions: CustomStringConvertible { public var categoryMarkComment: String public var beforeMarks: Set public var lifecycleMethods: Set - 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 @@ -378,9 +378,9 @@ public struct FormatOptions: CustomStringConvertible { categoryMarkComment: String = "MARK: %c", beforeMarks: Set = [], lifecycleMethods: Set = [], - 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, diff --git a/Sources/OptionsDescriptor.swift b/Sources/OptionsDescriptor.swift index fd827e21d..4e57d3a0b 100644 --- a/Sources/OptionsDescriptor.swift +++ b/Sources/OptionsDescriptor.swift @@ -38,6 +38,7 @@ extension FormatOptions { case binary(true: [String], false: [String]) case `enum`([String]) case text + case int case array case set } @@ -161,6 +162,24 @@ extension FormatOptions { type = .enum(keys) } + init(argumentName: String, + propertyName: String, + displayName: String, + help: String, + keyPath: WritableKeyPath) + { + 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(argumentName: String, propertyName: String, displayName: String, @@ -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 } @@ -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 diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 021f0f31e..ecc10a795 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -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 @@ -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