Skip to content

Commit

Permalink
update indent check
Browse files Browse the repository at this point in the history
  • Loading branch information
Jintin committed Feb 18, 2017
1 parent 61b3259 commit 6cb5d59
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
7 changes: 6 additions & 1 deletion Parser/Indent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ class Indent {
if (block != .parentheses || !Indent.paraAlign) && !indent.indentAdd {
self.count += 1
self.indentAdd = true
} else if indent.indentAdd {
self.indentAdd = true
if indent.count > 0 {
indent.count -= 1
}
} else {
self.indentAdd = indent.indentAdd
self.indentAdd = false
}
if !indent.extraAdd {
if block != .curly {
Expand Down
8 changes: 5 additions & 3 deletions Parser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ extension SwiftParser {
} else if isNext(word: "switch", length: 6) {
isNextSwitch = true
}

retString += String(repeating: Indent.char, count: indent.count + (addExtra ? indent.extra : 0))
if indent.isLeading {
let count = indent.count + (addExtra ? indent.extra : 0)
if count > 0 {
retString += String(repeating: Indent.char, count: count)
}
if indent.isLeading && indent.leading > 0 {
retString += String(repeating: " ", count: indent.leading)
}
}
Expand Down
27 changes: 17 additions & 10 deletions Parser/SwiftParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ let operatorList: [Character: [(String, Int)]] =
"%": [("%=", 2), ("%", 1)],
"^": [("^=", 2)],
"&": [("&&=", 3), ("&&&", 3), ("&&", 2), ("&=", 2), ("&+", 2),
("&-", 2), ("&*", 2), ("&/", 2), ("&%", 2)],
("&-", 2), ("&*", 2), ("&/", 2), ("&%", 2)],
"<": [("<<<", 3), ("<<=", 3), ("<<", 2), ("<=", 2), ("<~~", 3),
("<~", 2), ("<--", 3), ("<-<", 3), ("<-", 2), ("<^>", 3),
("<|>", 3), ("<*>", 3), ("<||?", 4), ("<||", 3), ("<|?", 3),
("<|", 2), ("<", 1)],
("<~", 2), ("<--", 3), ("<-<", 3), ("<-", 2), ("<^>", 3),
("<|>", 3), ("<*>", 3), ("<||?", 4), ("<||", 3), ("<|?", 3),
("<|", 2), ("<", 1)],
">": [(">>>", 3), (">>=", 3), (">>-", 3), (">>", 2), (">=", 2),
(">->", 3), (">", 1)],
(">->", 3), (">", 1)],
"|": [("|||", 3), ("||=", 3), ("||", 2), ("|=", 2), ("|", 1)],
"!": [("!==", 3), ("!=", 2)],
"=": [("===", 3), ("==", 2), ("=", 1)]
Expand Down Expand Up @@ -138,10 +138,10 @@ class SwiftParser {
retString += ", "
return string.nextNonSpaceIndex(string.index(after: strIndex))
case "{", "[", "(":
indentStack.append(indent)
let leading = retString.distance(from: newlineIndex, to: retString.endIndex)
indent = Indent(with: indent, offset: leading, type: IndentType(rawValue: char))

let newIndent = Indent(with: indent, offset: leading, type: IndentType(rawValue: char))
indentStack.append(indent)
indent = newIndent
if indent.block == .curly {
if isNextSwitch {
indent.inSwitch = true
Expand All @@ -159,10 +159,12 @@ class SwiftParser {
case "}", "]", ")":
if let last = indentStack.popLast() {
indent = last
if indent.indentAdd {
indent.indentAdd = false
}
} else {
indent = Indent()
}

if char == "}" {
trimWithIndent(addExtra: false) // MARK: change to newline check
retString.keepSpace()
Expand Down Expand Up @@ -261,7 +263,12 @@ class SwiftParser {
return 1
}
case ",":
self.indent.isLeading = true
if Indent.paraAlign {
self.indent.isLeading = true
}
if self.indent.block == .curly {
return 1
}
default:
break
}
Expand Down
14 changes: 6 additions & 8 deletions Swimat/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<box boxType="custom" borderType="none" borderWidth="0.0" title="Version" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="DOF-Mc-OAB" userLabel="Left">
<rect key="frame" x="20" y="82" width="128" height="193"/>
<rect key="frame" x="20" y="83" width="128" height="191"/>
<view key="contentView" id="JDv-8J-o2W">
<rect key="frame" x="0.0" y="0.0" width="128" height="193"/>
<rect key="frame" x="0.0" y="0.0" width="128" height="191"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="X7k-E7-aFm" userLabel="Icon">
<rect key="frame" x="0.0" y="65" width="128" height="128"/>
<rect key="frame" x="0.0" y="63" width="128" height="128"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="icon" id="y0c-8x-z8A"/>
</imageView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ApI-HP-mgp" userLabel="Name">
<rect key="frame" x="17" y="25" width="95" height="32"/>
<rect key="frame" x="17" y="23" width="95" height="32"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Swimat" id="5Dy-7j-zLg">
<font key="font" size="25" name="Monaco"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="woL-jt-bah">
<rect key="frame" x="35" y="8" width="59" height="17"/>
<rect key="frame" x="37" y="8" width="55" height="15"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Version" id="Dfx-QQ-lvt">
<font key="font" size="13" name="Monaco"/>
<font key="font" size="12" name="Monaco"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
Expand All @@ -83,7 +83,6 @@
<constraints>
<constraint firstAttribute="width" constant="128" id="Hn3-Rs-XPC"/>
</constraints>
<font key="titleFont" metaFont="message" size="11"/>
</box>
<box boxType="custom" borderType="none" borderWidth="0.0" title="Installation" transparent="YES" translatesAutoresizingMaskIntoConstraints="NO" id="n5V-AU-ftz" userLabel="Right">
<rect key="frame" x="168" y="20" width="360" height="317"/>
Expand Down Expand Up @@ -141,7 +140,6 @@ Swimat also comes with a handy command line tool:</string>
<constraints>
<constraint firstAttribute="width" constant="360" id="VRK-hV-ybW"/>
</constraints>
<font key="titleFont" metaFont="message" size="11"/>
</box>
</subviews>
<constraints>
Expand Down
19 changes: 19 additions & 0 deletions SwimatTests/FormatTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,29 @@ class FormatTest: XCTestCase {
}
}

func formatAlign(res: String, expect: String) {
Indent.char = " "
Indent.size = 4
Indent.paraAlign = false
let parser = SwiftParser(string: res)
do {
let result = try parser.format()
print("expect=")
print(expect)
print("result=")
print(result)
assert(result == expect)
} catch {
assertionFailure()
}
}

func testCase1() { //#151
let res = "UIView.animatesss(withDuration: 0.3, animations: {\n"
+ " someCode()\n"
+ "})"
format(res: res, expect: res)
formatAlign(res: res, expect: res)
}

func testCase2() { //#151
Expand All @@ -33,6 +51,7 @@ class FormatTest: XCTestCase {
+ " afterAnimation()\n"
+ "}"
format(res: res, expect: res)
formatAlign(res: res, expect: res)
}

func testCase3() { //#147
Expand Down

0 comments on commit 6cb5d59

Please sign in to comment.