From 4eaa370037ffbce76dd6842e10cf1bdcd0551013 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Fri, 21 Oct 2016 08:35:37 +0100 Subject: [PATCH] Cleanup --- CommandLineTool/main.swift | 192 +++++++++--------- EditorExtension/Application/AppDelegate.swift | 2 +- .../Application/ViewController.swift | 2 +- EditorExtension/Extension/CommandErrors.swift | 2 +- .../Extension/FormatEntireFileCommand.swift | 2 +- .../FormatSelectedSourceCommand.swift | 2 +- .../Extension/SourceEditorExtension.swift | 2 +- SwiftFormat/Formatter.swift | 2 +- SwiftFormat/Rules.swift | 2 +- SwiftFormat/SwiftFormat.h | 2 +- SwiftFormat/SwiftFormat.swift | 2 +- SwiftFormat/Tokenizer.swift | 2 +- SwiftFormatTests/FormatterTests.swift | 2 +- SwiftFormatTests/RulesTests.swift | 2 +- SwiftFormatTests/SwiftFormatTests.swift | 2 +- SwiftFormatTests/TokenizerTests.swift | 2 +- 16 files changed, 113 insertions(+), 109 deletions(-) diff --git a/CommandLineTool/main.swift b/CommandLineTool/main.swift index 48d7fc84..cd0f2f69 100644 --- a/CommandLineTool/main.swift +++ b/CommandLineTool/main.swift @@ -5,7 +5,7 @@ // Version 0.13 // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: @@ -61,6 +61,102 @@ func expandPath(_ path: String) -> URL { return URL(fileURLWithPath: path, relativeTo: directoryURL) } +func optionsForArguments(_ args: [String: String]) throws -> FormatOptions { + + func processOption(_ key: String, handler: (String) throws -> Void) throws { + guard let value = args[key] else { + return + } + guard !value.isEmpty else { + print("error: --\(key) option expects a value.") + throw NSError() + } + do { + try handler(value.lowercased()) + } catch { + print("error: unsupported --\(key) value: \(value).") + throw error + } + } + + var options = FormatOptions() + try processOption("indent") { + switch $0 { + case "tab", "tabs": + options.indent = "\t" + default: + if let spaces = Int($0) { + options.indent = String(repeating: " ", count: spaces) + break + } + throw NSError() + } + } + try processOption("semicolons") { + switch $0 { + case "inline": + options.allowInlineSemicolons = true + case "never": + options.allowInlineSemicolons = false + default: + throw NSError() + } + } + try processOption("commas") { + switch $0 { + case "always": + options.trailingCommas = true + case "inline": + options.trailingCommas = false + default: + throw NSError() + } + } + try processOption("linebreaks") { + switch $0 { + case "cr": + options.linebreak = "\r" + case "lf": + options.linebreak = "\n" + case "crlf": + options.linebreak = "\r\n" + default: + throw NSError() + } + } + try processOption("ranges") { + switch $0 { + case "space", "spaced", "spaces": + options.spaceAroundRangeOperators = true + case "nospace": + options.spaceAroundRangeOperators = false + default: + throw NSError() + } + } + try processOption("empty") { + switch $0 { + case "void": + options.useVoid = true + case "tuple": + options.useVoid = false + default: + throw NSError() + } + } + try processOption("fragment") { + switch $0 { + case "true": + options.fragment = true + case "false": + options.fragment = false + default: + throw NSError() + } + } + return options +} + func processArguments(_ args: [String]) { guard let args = preprocessArguments(args, [ "output", @@ -92,100 +188,8 @@ func processArguments(_ args: [String]) { let inputURL = args["1"].map { expandPath($0) } let outputURL = (args["output"] ?? args["1"]).map { expandPath($0) } - func processOption(_ key: String, handler: (String) throws -> Void) throws { - guard let value = args[key] else { - return - } - guard !value.isEmpty else { - print("error: --\(key) option expects a value.") - throw NSError() - } - do { - try handler(value.lowercased()) - } catch { - print("error: unsupported --\(key) value: \(value).") - throw error - } - } - // Get options - var options = FormatOptions() - do { - try processOption("indent") { - switch $0 { - case "tab", "tabs": - options.indent = "\t" - default: - if let spaces = Int($0) { - options.indent = String(repeating: " ", count: spaces) - break - } - throw NSError() - } - } - try processOption("semicolons") { - switch $0 { - case "inline": - options.allowInlineSemicolons = true - case "never": - options.allowInlineSemicolons = false - default: - throw NSError() - } - } - try processOption("commas") { - switch $0 { - case "always": - options.trailingCommas = true - case "inline": - options.trailingCommas = false - default: - throw NSError() - } - } - try processOption("linebreaks") { - switch $0 { - case "cr": - options.linebreak = "\r" - case "lf": - options.linebreak = "\n" - case "crlf": - options.linebreak = "\r\n" - default: - throw NSError() - } - } - try processOption("ranges") { - switch $0 { - case "space", "spaced", "spaces": - options.spaceAroundRangeOperators = true - case "nospace": - options.spaceAroundRangeOperators = false - default: - throw NSError() - } - } - try processOption("empty") { - switch $0 { - case "void": - options.useVoid = true - case "tuple": - options.useVoid = false - default: - throw NSError() - } - } - try processOption("fragment") { - switch $0 { - case "true": - options.fragment = true - case "false": - options.fragment = false - default: - throw NSError() - } - } - } catch { + guard let options = try? optionsForArguments(args) else { return } diff --git a/EditorExtension/Application/AppDelegate.swift b/EditorExtension/Application/AppDelegate.swift index dc4d6248..f39875d6 100644 --- a/EditorExtension/Application/AppDelegate.swift +++ b/EditorExtension/Application/AppDelegate.swift @@ -3,7 +3,7 @@ // SwiftFormat for Xcode // // Created by Tony Arnold on 5/10/16. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/EditorExtension/Application/ViewController.swift b/EditorExtension/Application/ViewController.swift index 2704f304..6fc24167 100644 --- a/EditorExtension/Application/ViewController.swift +++ b/EditorExtension/Application/ViewController.swift @@ -3,7 +3,7 @@ // SwiftFormat for Xcode // // Created by Tony Arnold on 5/10/16. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/EditorExtension/Extension/CommandErrors.swift b/EditorExtension/Extension/CommandErrors.swift index ac46b984..0f560651 100644 --- a/EditorExtension/Extension/CommandErrors.swift +++ b/EditorExtension/Extension/CommandErrors.swift @@ -3,7 +3,7 @@ // Swift Formatter // // Created by Tony Arnold on 6/10/16. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/EditorExtension/Extension/FormatEntireFileCommand.swift b/EditorExtension/Extension/FormatEntireFileCommand.swift index 9b0f7c8c..f2cc64f1 100644 --- a/EditorExtension/Extension/FormatEntireFileCommand.swift +++ b/EditorExtension/Extension/FormatEntireFileCommand.swift @@ -3,7 +3,7 @@ // Swift Formatter // // Created by Tony Arnold on 5/10/16. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/EditorExtension/Extension/FormatSelectedSourceCommand.swift b/EditorExtension/Extension/FormatSelectedSourceCommand.swift index 5a5943db..de74483a 100644 --- a/EditorExtension/Extension/FormatSelectedSourceCommand.swift +++ b/EditorExtension/Extension/FormatSelectedSourceCommand.swift @@ -3,7 +3,7 @@ // Swift Formatter // // Created by Tony Arnold on 5/10/16. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/EditorExtension/Extension/SourceEditorExtension.swift b/EditorExtension/Extension/SourceEditorExtension.swift index 6b1f58e8..9ea2b8f4 100644 --- a/EditorExtension/Extension/SourceEditorExtension.swift +++ b/EditorExtension/Extension/SourceEditorExtension.swift @@ -3,7 +3,7 @@ // Swift Formatter // // Created by Tony Arnold on 5/10/16. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormat/Formatter.swift b/SwiftFormat/Formatter.swift index 6feceeaa..69d83af8 100644 --- a/SwiftFormat/Formatter.swift +++ b/SwiftFormat/Formatter.swift @@ -5,7 +5,7 @@ // Version 0.13 // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormat/Rules.swift b/SwiftFormat/Rules.swift index 26f542aa..a0c15138 100644 --- a/SwiftFormat/Rules.swift +++ b/SwiftFormat/Rules.swift @@ -5,7 +5,7 @@ // Version 0.13 // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormat/SwiftFormat.h b/SwiftFormat/SwiftFormat.h index 8725763c..70eaa5cd 100644 --- a/SwiftFormat/SwiftFormat.h +++ b/SwiftFormat/SwiftFormat.h @@ -5,7 +5,7 @@ // Version 0.13 // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormat/SwiftFormat.swift b/SwiftFormat/SwiftFormat.swift index a301d5cc..71713d5c 100644 --- a/SwiftFormat/SwiftFormat.swift +++ b/SwiftFormat/SwiftFormat.swift @@ -5,7 +5,7 @@ // Version 0.13 // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormat/Tokenizer.swift b/SwiftFormat/Tokenizer.swift index d7e35e82..cbddb2bd 100644 --- a/SwiftFormat/Tokenizer.swift +++ b/SwiftFormat/Tokenizer.swift @@ -5,7 +5,7 @@ // Version 0.13 // // Created by Nick Lockwood on 11/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormatTests/FormatterTests.swift b/SwiftFormatTests/FormatterTests.swift index 2df2b0a9..69fb6e92 100644 --- a/SwiftFormatTests/FormatterTests.swift +++ b/SwiftFormatTests/FormatterTests.swift @@ -3,7 +3,7 @@ // SwiftFormat // // Created by Nick Lockwood on 30/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormatTests/RulesTests.swift b/SwiftFormatTests/RulesTests.swift index ca4485d0..69b7a443 100644 --- a/SwiftFormatTests/RulesTests.swift +++ b/SwiftFormatTests/RulesTests.swift @@ -3,7 +3,7 @@ // SwiftFormat // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormatTests/SwiftFormatTests.swift b/SwiftFormatTests/SwiftFormatTests.swift index 0ba19a33..e35c745c 100644 --- a/SwiftFormatTests/SwiftFormatTests.swift +++ b/SwiftFormatTests/SwiftFormatTests.swift @@ -3,7 +3,7 @@ // SwiftFormat // // Created by Nick Lockwood on 28/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: diff --git a/SwiftFormatTests/TokenizerTests.swift b/SwiftFormatTests/TokenizerTests.swift index d91f2c0f..00e79ddf 100644 --- a/SwiftFormatTests/TokenizerTests.swift +++ b/SwiftFormatTests/TokenizerTests.swift @@ -3,7 +3,7 @@ // SwiftFormat // // Created by Nick Lockwood on 12/08/2016. -// Copyright 2016 Charcoal Design +// Copyright 2016 Nick Lockwood // // Distributed under the permissive zlib license // Get the latest version from here: