mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-06-16 10:34:34 +00:00
Improved option value naming consistency
This commit is contained in:
@@ -138,21 +138,21 @@ func printHelp(as type: CLI.OutputType) {
|
||||
--header header comments. "strip", "ignore", or the text you wish use
|
||||
--hexgrouping hex grouping,threshold (default: 4,8) or "none", "ignore"
|
||||
--hexliteralcase casing for hex literals. "uppercase" (default) or "lowercase"
|
||||
--ifdef #if indenting. "indent" (default), "noindent" or "outdent"
|
||||
--ifdef #if indenting. "indent" (default), "no-indent" or "outdent"
|
||||
--indent number of spaces to indent, or "tab" to use tabs
|
||||
--indentcase indent cases inside a switch. "true" or "false" (default)
|
||||
--linebreaks linebreak character to use. "cr", "crlf" or "lf" (default)
|
||||
--octalgrouping octal grouping,threshold or "none", "ignore". default: 4,8
|
||||
--operatorfunc spacing for operator funcs. "spaced" (default) or "nospace"
|
||||
--operatorfunc spacing for operator funcs. "spaced" (default) or "no-space"
|
||||
--patternlet let/var placement in patterns. "hoist" (default) or "inline"
|
||||
--ranges spacing for ranges. "spaced" (default) or "nospace"
|
||||
--ranges spacing for ranges. "spaced" (default) or "no-space"
|
||||
--semicolons allow semicolons. "never" or "inline" (default)
|
||||
--self explicit self. "insert", "remove" (default) or "init-only"
|
||||
--importgrouping "testable-top", "testable-bottom" or "alphabetized" (default)
|
||||
--stripunusedargs "closure-only", "unnamed-only" or "always" (default)
|
||||
--trimwhitespace trim trailing space. "always" (default) or "nonblank-lines"
|
||||
--wraparguments wrap function args. "beforefirst", "afterfirst", "preserve"
|
||||
--wrapcollections wrap array/dict. "beforefirst", "afterfirst", "preserve"
|
||||
--wraparguments wrap function args. "before-first", "after-first", "preserve"
|
||||
--wrapcollections wrap array/dict. "before-first", "after-first", "preserve"
|
||||
""", as: type)
|
||||
print("")
|
||||
}
|
||||
|
||||
+31
-3
@@ -34,16 +34,44 @@ import Foundation
|
||||
/// The indenting mode to use for #if/#endif statements
|
||||
public enum IndentMode: String {
|
||||
case indent
|
||||
case noIndent = "noindent"
|
||||
case noIndent = "no-indent"
|
||||
case outdent
|
||||
|
||||
public init?(rawValue: String) {
|
||||
switch rawValue {
|
||||
case "indent":
|
||||
self = .indent
|
||||
case "no-indent", "noindent":
|
||||
self = .noIndent
|
||||
case "outdent":
|
||||
self = .outdent
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap mode for arguments
|
||||
public enum WrapMode: String {
|
||||
case beforeFirst = "beforefirst"
|
||||
case afterFirst = "afterfirst"
|
||||
case beforeFirst = "before-first"
|
||||
case afterFirst = "after-first"
|
||||
case preserve
|
||||
case disabled
|
||||
|
||||
public init?(rawValue: String) {
|
||||
switch rawValue {
|
||||
case "before-first", "beforefirst":
|
||||
self = .beforeFirst
|
||||
case "after-first", "afterfirst":
|
||||
self = .afterFirst
|
||||
case "preserve":
|
||||
self = .preserve
|
||||
case "disabled":
|
||||
self = .disabled
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Argument type for stripping
|
||||
|
||||
@@ -34,7 +34,8 @@ import Foundation
|
||||
extension FormatOptions {
|
||||
struct Descriptor {
|
||||
enum ArgumentType: EnumAssociable {
|
||||
case binary(true: [String], false: [String]) // index 0 should be the official value, while others are tolerable values
|
||||
// index 0 is official value, others are acceptable
|
||||
case binary(true: [String], false: [String])
|
||||
case list([String])
|
||||
case text
|
||||
}
|
||||
@@ -273,7 +274,7 @@ extension FormatOptions.Descriptor {
|
||||
displayName: "Ranges",
|
||||
keyPath: \.spaceAroundRangeOperators,
|
||||
trueValues: ["spaced", "space", "spaces"],
|
||||
falseValues: ["nospace"]
|
||||
falseValues: ["no-space", "nospace"]
|
||||
)
|
||||
static let spaceAroundOperatorDeclarations = FormatOptions.Descriptor(
|
||||
argumentName: "operatorfunc",
|
||||
@@ -281,7 +282,7 @@ extension FormatOptions.Descriptor {
|
||||
displayName: "Operator Functions",
|
||||
keyPath: \.spaceAroundOperatorDeclarations,
|
||||
trueValues: ["spaced", "space", "spaces"],
|
||||
falseValues: ["nospace"]
|
||||
falseValues: ["no-space", "nospace"]
|
||||
)
|
||||
static let useVoid = FormatOptions.Descriptor(
|
||||
argumentName: "empty",
|
||||
@@ -343,21 +344,21 @@ extension FormatOptions.Descriptor {
|
||||
propertyName: "ifdefIndent",
|
||||
displayName: "Ifdef Indent",
|
||||
keyPath: \.ifdefIndent,
|
||||
options: ["indent", "noindent", "outdent"]
|
||||
options: ["indent", "no-indent", "outdent"]
|
||||
)
|
||||
static let wrapArguments = FormatOptions.Descriptor(
|
||||
argumentName: "wraparguments",
|
||||
propertyName: "wrapArguments",
|
||||
displayName: "Wrap Arguments",
|
||||
keyPath: \.wrapArguments,
|
||||
options: ["beforefirst", "afterfirst", "preserve", "disabled"]
|
||||
options: ["before-first", "after-first", "preserve", "disabled"]
|
||||
)
|
||||
static let wrapCollections = FormatOptions.Descriptor(
|
||||
argumentName: "wrapcollections",
|
||||
propertyName: "wrapCollections",
|
||||
displayName: "Wrap Collections",
|
||||
keyPath: \.wrapCollections,
|
||||
options: ["beforefirst", "afterfirst", "preserve", "disabled"]
|
||||
options: ["before-first", "after-first", "preserve", "disabled"]
|
||||
)
|
||||
static let closingParen = FormatOptions.Descriptor(
|
||||
argumentName: "closingparen",
|
||||
@@ -532,6 +533,6 @@ extension FormatOptions.Descriptor {
|
||||
propertyName: "wrapCollections",
|
||||
displayName: "Wrap Elements",
|
||||
keyPath: \.wrapCollections,
|
||||
options: ["beforefirst", "afterfirst", "preserve", "disabled"]
|
||||
options: ["before-first", "after-first", "preserve", "disabled"]
|
||||
)
|
||||
}
|
||||
|
||||
@@ -235,6 +235,22 @@ class ArgumentsTests: XCTestCase {
|
||||
XCTAssertEqual(args["header"], "hello world\\ngoodbye world")
|
||||
}
|
||||
|
||||
func testParseIgnoreFileHeader() throws {
|
||||
let config = "--header ignore"
|
||||
let data = Data(config.utf8)
|
||||
let args = try parseConfigFile(data)
|
||||
let options = try Options(args, in: "/")
|
||||
XCTAssertEqual(options.formatOptions?.fileHeader, .ignore)
|
||||
}
|
||||
|
||||
func testParseUppercaseIgnoreFileHeader() throws {
|
||||
let config = "--header IGNORE"
|
||||
let data = Data(config.utf8)
|
||||
let args = try parseConfigFile(data)
|
||||
let options = try Options(args, in: "/")
|
||||
XCTAssertEqual(options.formatOptions?.fileHeader, .ignore)
|
||||
}
|
||||
|
||||
// MARK: config file serialization
|
||||
|
||||
// file header comment encoding
|
||||
|
||||
@@ -53,7 +53,7 @@ class InferenceTests: XCTestCase {
|
||||
let tokens = files.flatMap { tokenize($0) }
|
||||
let options = Options(formatOptions: inferFormatOptions(from: tokens))
|
||||
let arguments = serialize(options: options, excludingDefaults: true, separator: " ")
|
||||
XCTAssertEqual(arguments, "--binarygrouping none --decimalgrouping none --hexgrouping none --octalgrouping none --wrapcollections beforefirst")
|
||||
XCTAssertEqual(arguments, "--binarygrouping none --decimalgrouping none --hexgrouping none --octalgrouping none --wrapcollections before-first")
|
||||
}
|
||||
|
||||
// MARK: indent
|
||||
|
||||
@@ -237,10 +237,10 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
let descriptor = FormatOptions.Descriptor.spaceAroundRangeOperators
|
||||
let fromOptionsExpectation: [OptionArgumentMapping<Bool>] = [
|
||||
(optionValue: true, argumentValue: "spaced"),
|
||||
(optionValue: false, argumentValue: "nospace"),
|
||||
(optionValue: false, argumentValue: "no-space"),
|
||||
]
|
||||
validateDescriptor(descriptor, displayName: "Ranges", argumentName: "ranges", propertyName: "spaceAroundRangeOperators")
|
||||
validateArgumentsBinaryType(descriptor, controlTrue: ["spaced", "space", "spaces"], controlFalse: ["nospace"])
|
||||
validateArgumentsBinaryType(descriptor, controlTrue: ["spaced", "space", "spaces"], controlFalse: ["no-space", "nospace"])
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.spaceAroundRangeOperators, expectations: fromOptionsExpectation)
|
||||
validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.spaceAroundRangeOperators)
|
||||
validateDescriptorThrowsOptionsError(descriptor)
|
||||
@@ -250,10 +250,10 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
let descriptor = FormatOptions.Descriptor.spaceAroundOperatorDeclarations
|
||||
let fromOptionsExpectation: [OptionArgumentMapping<Bool>] = [
|
||||
(optionValue: true, argumentValue: "spaced"),
|
||||
(optionValue: false, argumentValue: "nospace"),
|
||||
(optionValue: false, argumentValue: "no-space"),
|
||||
]
|
||||
validateDescriptor(descriptor, displayName: "Operator Functions", argumentName: "operatorfunc", propertyName: "spaceAroundOperatorDeclarations")
|
||||
validateArgumentsBinaryType(descriptor, controlTrue: ["spaced", "space", "spaces"], controlFalse: ["nospace"])
|
||||
validateArgumentsBinaryType(descriptor, controlTrue: ["spaced", "space", "spaces"], controlFalse: ["no-space", "nospace"])
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.spaceAroundOperatorDeclarations, expectations: fromOptionsExpectation)
|
||||
validateFromArgumentsBinaryType(descriptor, keyPath: \FormatOptions.spaceAroundOperatorDeclarations)
|
||||
validateDescriptorThrowsOptionsError(descriptor)
|
||||
@@ -433,14 +433,16 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
let descriptor = FormatOptions.Descriptor.ifdefIndent
|
||||
let expectedMapping: [OptionArgumentMapping<IndentMode>] = [
|
||||
(optionValue: IndentMode.indent, argumentValue: "indent"),
|
||||
(optionValue: IndentMode.noIndent, argumentValue: "noindent"),
|
||||
(optionValue: IndentMode.noIndent, argumentValue: "no-indent"),
|
||||
(optionValue: IndentMode.outdent, argumentValue: "outdent"),
|
||||
]
|
||||
|
||||
let alternateMapping: [OptionArgumentMapping<IndentMode>] = [
|
||||
(optionValue: IndentMode.noIndent, argumentValue: "noindent"),
|
||||
]
|
||||
validateDescriptor(descriptor, displayName: "Ifdef Indent", argumentName: "ifdef", propertyName: "ifdefIndent")
|
||||
validateArgumentsListType(descriptor, validArguments: ["indent", "noindent", "outdent"])
|
||||
validateArgumentsListType(descriptor, validArguments: ["indent", "no-indent", "outdent"])
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.ifdefIndent, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.ifdefIndent, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.ifdefIndent, expectations: expectedMapping + alternateMapping)
|
||||
validateDescriptorThrowsOptionsError(descriptor)
|
||||
}
|
||||
|
||||
@@ -461,30 +463,42 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
func testWrapArguments() {
|
||||
let descriptor = FormatOptions.Descriptor.wrapArguments
|
||||
let expectedMapping: [OptionArgumentMapping<WrapMode>] = [
|
||||
(optionValue: .beforeFirst, argumentValue: "beforefirst"),
|
||||
(optionValue: .afterFirst, argumentValue: "afterfirst"),
|
||||
(optionValue: .beforeFirst, argumentValue: "before-first"),
|
||||
(optionValue: .afterFirst, argumentValue: "after-first"),
|
||||
(optionValue: .preserve, argumentValue: "preserve"),
|
||||
(optionValue: .disabled, argumentValue: "disabled"),
|
||||
]
|
||||
let alternateMapping: [OptionArgumentMapping<WrapMode>] = [
|
||||
(optionValue: .beforeFirst, argumentValue: "beforefirst"),
|
||||
(optionValue: .afterFirst, argumentValue: "afterfirst"),
|
||||
]
|
||||
validateDescriptor(descriptor, displayName: "Wrap Arguments", argumentName: "wraparguments", propertyName: "wrapArguments")
|
||||
validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "preserve", "disabled"])
|
||||
validateArgumentsListType(descriptor, validArguments: [
|
||||
"before-first", "after-first", "preserve", "disabled",
|
||||
])
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.wrapArguments, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.wrapArguments, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.wrapArguments, expectations: expectedMapping + alternateMapping)
|
||||
validateDescriptorThrowsOptionsError(descriptor)
|
||||
}
|
||||
|
||||
func testWrapCollections() {
|
||||
let descriptor = FormatOptions.Descriptor.wrapCollections
|
||||
let expectedMapping: [OptionArgumentMapping<WrapMode>] = [
|
||||
(optionValue: .beforeFirst, argumentValue: "beforefirst"),
|
||||
(optionValue: .afterFirst, argumentValue: "afterfirst"),
|
||||
(optionValue: .beforeFirst, argumentValue: "before-first"),
|
||||
(optionValue: .afterFirst, argumentValue: "after-first"),
|
||||
(optionValue: .preserve, argumentValue: "preserve"),
|
||||
(optionValue: .disabled, argumentValue: "disabled"),
|
||||
]
|
||||
let alternateMapping: [OptionArgumentMapping<WrapMode>] = [
|
||||
(optionValue: .beforeFirst, argumentValue: "beforefirst"),
|
||||
(optionValue: .afterFirst, argumentValue: "afterfirst"),
|
||||
]
|
||||
validateDescriptor(descriptor, displayName: "Wrap Collections", argumentName: "wrapcollections", propertyName: "wrapCollections")
|
||||
validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "preserve", "disabled"])
|
||||
validateArgumentsListType(descriptor, validArguments: [
|
||||
"before-first", "after-first", "preserve", "disabled",
|
||||
])
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping + alternateMapping)
|
||||
validateDescriptorThrowsOptionsError(descriptor)
|
||||
}
|
||||
|
||||
@@ -663,15 +677,21 @@ class OptionsDescriptorTests: XCTestCase {
|
||||
func testWrapElements() {
|
||||
let descriptor = FormatOptions.Descriptor.wrapElements
|
||||
let expectedMapping: [OptionArgumentMapping<WrapMode>] = [
|
||||
(optionValue: .beforeFirst, argumentValue: "beforefirst"),
|
||||
(optionValue: .afterFirst, argumentValue: "afterfirst"),
|
||||
(optionValue: .beforeFirst, argumentValue: "before-first"),
|
||||
(optionValue: .afterFirst, argumentValue: "after-first"),
|
||||
(optionValue: .preserve, argumentValue: "preserve"),
|
||||
(optionValue: .disabled, argumentValue: "disabled"),
|
||||
]
|
||||
let alternateMapping: [OptionArgumentMapping<WrapMode>] = [
|
||||
(optionValue: .beforeFirst, argumentValue: "beforefirst"),
|
||||
(optionValue: .afterFirst, argumentValue: "afterfirst"),
|
||||
]
|
||||
validateDescriptor(descriptor, displayName: "Wrap Elements", argumentName: "wrapelements", propertyName: "wrapCollections")
|
||||
validateArgumentsListType(descriptor, validArguments: ["beforefirst", "afterfirst", "preserve", "disabled"])
|
||||
validateArgumentsListType(descriptor, validArguments: [
|
||||
"before-first", "after-first", "preserve", "disabled",
|
||||
])
|
||||
validateFromOptions(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping)
|
||||
validateFromArguments(descriptor, keyPath: \FormatOptions.wrapCollections, expectations: expectedMapping + alternateMapping)
|
||||
validateDescriptorThrowsOptionsError(descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user