Updated to version 0.2

This commit is contained in:
Nick Lockwood
2016-08-22 23:52:17 +01:00
parent 21e47b42d7
commit ecda34dda3
11 changed files with 53 additions and 15 deletions
+3 -3
View File
@@ -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
Binary file not shown.
+1 -1
View File
@@ -1,6 +1,6 @@
SwiftFormat
Version 0.1, August 22nd, 2016
Version 0.2, August 22nd, 2016
Copyright (c) 2016 Nick Lockwood
+7
View File
@@ -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
+17
View File
@@ -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;
+14 -6
View File
@@ -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
}
}
}
}
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+7 -1
View File
@@ -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() {
+1 -1
View File
@@ -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