diff --git a/CommandLineTool/main.swift b/CommandLineTool/main.swift index 1189e528..a0140ae5 100644 --- a/CommandLineTool/main.swift +++ b/CommandLineTool/main.swift @@ -2,7 +2,7 @@ // SwiftFormat // main.swift // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Charcoal Design @@ -33,7 +33,7 @@ import Foundation -let version = "0.1" +let version = "0.2" func processInput(inputURL: NSURL, andWriteToOutput outputURL: NSURL, withOptions options: FormattingOptions) -> Int { let manager = NSFileManager.defaultManager() @@ -116,7 +116,7 @@ func preprocessArguments(args: [String], _ names: [String]) -> [String: String]? // Handle quotes and spaces var arg = arg var unterminated = false - if quoted { + if quoted { if arg.hasSuffix("\"") { arg = arg.substringToIndex(arg.endIndex.advancedBy(-1)) unterminated = false diff --git a/CommandLineTool/swiftformat b/CommandLineTool/swiftformat index 25b91fce..e48a4711 100755 Binary files a/CommandLineTool/swiftformat and b/CommandLineTool/swiftformat differ diff --git a/LICENCE.md b/LICENCE.md index 53203273..5a4f2794 100755 --- a/LICENCE.md +++ b/LICENCE.md @@ -1,6 +1,6 @@ SwiftFormat -Version 0.1, August 22nd, 2016 +Version 0.2, August 22nd, 2016 Copyright (c) 2016 Nick Lockwood diff --git a/README.md b/README.md index 036fc6e6..8e98bb45 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,13 @@ At some point I should probably add an intermediate parsing stage that identifie Release notes ---------------- +Version 0.2 + +- Fixed formatting of generic function types +- Fixed indenting of `if case` statements +- Changed `private(set)` indenting to match Apple standard +- Added swiftformat as a build phase to SwiftFormat, so I'm eating my own dogfood + Version 0.1 - First release diff --git a/SwiftFormat.xcodeproj/project.pbxproj b/SwiftFormat.xcodeproj/project.pbxproj index 5b7ed6a0..0d3c3e26 100644 --- a/SwiftFormat.xcodeproj/project.pbxproj +++ b/SwiftFormat.xcodeproj/project.pbxproj @@ -151,6 +151,7 @@ isa = PBXNativeTarget; buildConfigurationList = 01A0EAB81D5DB4D000A0A8E3 /* Build configuration list for PBXNativeTarget "SwiftFormat" */; buildPhases = ( + 0120A1351D6BB010006EFF5D /* ShellScript */, 01A0EA9F1D5DB4CF00A0A8E3 /* Sources */, 01A0EAA01D5DB4CF00A0A8E3 /* Frameworks */, 01A0EAA11D5DB4CF00A0A8E3 /* Headers */, @@ -258,6 +259,22 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + 0120A1351D6BB010006EFF5D /* ShellScript */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputPaths = ( + ); + outputPaths = ( + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "CommandLineTool/swiftformat ."; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ 01A0EA9F1D5DB4CF00A0A8E3 /* Sources */ = { isa = PBXSourcesBuildPhase; diff --git a/SwiftFormat/Formatter.swift b/SwiftFormat/Formatter.swift index 4d7c715e..c7250438 100644 --- a/SwiftFormat/Formatter.swift +++ b/SwiftFormat/Formatter.swift @@ -2,7 +2,7 @@ // SwiftFormat // Formatter.swift // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Charcoal Design @@ -46,7 +46,7 @@ public struct FormattingOptions { /// directly is that it allows mutation during enumeration, and /// transparently handles changes that affect the current token index. public class Formatter { - private (set) var tokens: [Token] + private(set) var tokens: [Token] let options: FormattingOptions private var indexStack: [Int] = [] @@ -129,7 +129,7 @@ public class Formatter { } indexStack.popLast() } - + /// As above, but only loops through tokens with the specified type public func forEachToken(ofType type: TokenType, _ body: (Int, Token) -> Void) { forEachToken(matching: { $0.type == type }, body) @@ -480,7 +480,7 @@ public func spaceAroundOperators(formatter: Formatter) { (previousNonWhitespaceToken.string == "?" && scopeStack.last?.string != "?") || (previousNonWhitespaceToken.string == "!" && scopeStack.last?.string != "!")) && (previousNonWhitespaceToken.type != .Identifier || - !spaceAfter(previousNonWhitespaceToken.string)) { + !spaceAfter(previousNonWhitespaceToken.string)) { if previousTokenWasWhitespace { formatter.removeTokenAtIndex(i - 1) } @@ -616,6 +616,7 @@ public func indent(formatter: Formatter) { var scopeIndexStack: [Int] = [] var scopeStartLineIndexes: [Int] = [] + var lastNonWhitespaceOrLinebreakIndex = -1 var lastNonWhitespaceIndex = -1 var indentStack = [""] var lineIndex = 0 @@ -704,8 +705,7 @@ public func indent(formatter: Formatter) { case .Identifier: // TODO: handle in switch token.string { - case "else", - "as", + case "as", "dynamicType", "false", "is", @@ -714,6 +714,11 @@ public func indent(formatter: Formatter) { "throws", "true": return false + case "else": + if let token = formatter.tokenAtIndex(lastNonWhitespaceOrLinebreakIndex) { + return token.string == "}" + } + return false default: return true } @@ -838,6 +843,9 @@ public func indent(formatter: Formatter) { setIndent(indent, atIndex: startOfLine(atIndex: i)) } lastNonWhitespaceIndex = i + if token.type != .Linebreak { + lastNonWhitespaceOrLinebreakIndex = i + } } } } diff --git a/SwiftFormat/SwiftFormat.h b/SwiftFormat/SwiftFormat.h index 14acba23..8defc667 100644 --- a/SwiftFormat/SwiftFormat.h +++ b/SwiftFormat/SwiftFormat.h @@ -2,7 +2,7 @@ // SwiftFormat // SwiftFormat.h // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Charcoal Design diff --git a/SwiftFormat/SwiftFormat.swift b/SwiftFormat/SwiftFormat.swift index 0b6de497..e5d07295 100644 --- a/SwiftFormat/SwiftFormat.swift +++ b/SwiftFormat/SwiftFormat.swift @@ -2,7 +2,7 @@ // SwiftFormat // SwiftFormat.swift // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Charcoal Design diff --git a/SwiftFormat/Tokenizer.swift b/SwiftFormat/Tokenizer.swift index 85eae2a3..d58e0cb3 100644 --- a/SwiftFormat/Tokenizer.swift +++ b/SwiftFormat/Tokenizer.swift @@ -2,7 +2,7 @@ // SwiftFormat // Tokenizer.swift // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 11/08/2016. // Copyright 2016 Charcoal Design diff --git a/SwiftFormatTests/FormatterTests.swift b/SwiftFormatTests/FormatterTests.swift index 3db64fb7..6955e10e 100644 --- a/SwiftFormatTests/FormatterTests.swift +++ b/SwiftFormatTests/FormatterTests.swift @@ -2,7 +2,7 @@ // SwiftFormat // FormatterTests.swift // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Charcoal Design @@ -676,6 +676,12 @@ class FormatterTests: XCTestCase { XCTAssertEqual(format(input, rules: [indent]), output) } + func testIndentElseAfterComment() { + let input = "if x {}\n//comment\nelse {}" + let output = "if x {}\n//comment\nelse {}" + XCTAssertEqual(format(input, rules: [indent]), output) + } + // MARK: indent comments func testCommentIndenting() { diff --git a/SwiftFormatTests/TokenizerTests.swift b/SwiftFormatTests/TokenizerTests.swift index c717af01..8b5d1723 100644 --- a/SwiftFormatTests/TokenizerTests.swift +++ b/SwiftFormatTests/TokenizerTests.swift @@ -2,7 +2,7 @@ // SwiftFormat // TokenizerTests.swift // -// Version 0.1 +// Version 0.2 // // Created by Nick Lockwood on 12/08/2016. // Copyright 2016 Charcoal Design