Skip to content

Commit

Permalink
remove unnecessary char like ; #166
Browse files Browse the repository at this point in the history
  • Loading branch information
Jintin committed Jun 18, 2017
1 parent 1f9442e commit c7994b1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
1 change: 1 addition & 0 deletions Extension/SourceEditorCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
Indent.paraAlign = Pref.isParaAlign()

let parser = SwiftParser(string: invocation.buffer.completeBuffer)
parser.autoRemoveChar = Pref.isAutoRemoveChar()
do {
let newLines = try parser.format().components(separatedBy: "\n")
let lines = invocation.buffer.lines
Expand Down
2 changes: 1 addition & 1 deletion Parser/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ extension String {
for compare in list! {
if isNext(string: compare.0, length: compare.1, strIndex: operIndex) {
let operEnd = index(operIndex, offsetBy: compare.1)
let obj2Index = nextNonSpaceIndex( operEnd)
let obj2Index = nextNonSpaceIndex(operEnd)
if let obj2 = try findObject(from: obj2Index) {
return (string: obj1.string + " " + compare.0 + " " + obj2.string, index: obj2.index)
} else {
Expand Down
10 changes: 10 additions & 0 deletions Parser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ extension SwiftParser {
return false
}

func isPrevious(str: String) -> Bool {

if let start = string.index(strIndex, offsetBy: -str.characters.count, limitedBy: string.startIndex) {
if let _ = string.range(of: str, options: [], range: start ..< strIndex) {
return true
}
}
return false
}

func isBetween(words: (start: String, end: String, endLength: Int)...) -> Bool {
// MARK: check word, not position
if strIndex < string.endIndex {
Expand Down
13 changes: 13 additions & 0 deletions Parser/Pref.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation

enum Pref: String {
case paraAlign
case autoRemoveChar

static func getUserDefaults() -> UserDefaults {
return UserDefaults.init(suiteName: "com.jintin.swimat.config")!
Expand All @@ -19,4 +20,16 @@ enum Pref: String {
getUserDefaults().synchronize()
}

static func isAutoRemoveChar() -> Bool {
if let _ = getUserDefaults().object(forKey: Pref.autoRemoveChar.rawValue) {
return getUserDefaults().bool(forKey: Pref.autoRemoveChar.rawValue)
}
return true
}

static func setAutoRemoveChar(isAlign: Bool) {
getUserDefaults().set(isAlign, forKey: Pref.autoRemoveChar.rawValue)
getUserDefaults().synchronize()
}

}
11 changes: 10 additions & 1 deletion Parser/SwiftParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let operatorList: [Character: [(String, Int)]] =
"|": [("|||", 3), ("||=", 3), ("||", 2), ("|=", 2), ("|", 1)],
"!": [("!==", 3), ("!=", 2)],
"=": [("===", 3), ("==", 2), ("=", 1)],
"?" : []
"?": []
]

fileprivate let negativeCheckSigns: [Character] =
Expand All @@ -42,6 +42,7 @@ class SwiftParser {
var indentStack = [Indent]()
var newlineIndex: String.Index
var isNextSwitch: Bool = false
var autoRemoveChar: Bool = false

init(string: String) {
self.string = string
Expand All @@ -54,6 +55,7 @@ class SwiftParser {
let char = string[strIndex]
strIndex = try check(char: char)
}
removeUnnecessaryChar()
return retString.trim()
}

Expand Down Expand Up @@ -137,6 +139,7 @@ class SwiftParser {
retString += quote.string
return quote.index
case "\n":
removeUnnecessaryChar()
indent.line += 1
return checkLine(char)
case " ", "\t":
Expand Down Expand Up @@ -254,6 +257,12 @@ class SwiftParser {
}
}

func removeUnnecessaryChar() {
if autoRemoveChar && retString.last == ";" {
retString = retString.substring(to: retString.index(before: retString.endIndex))
}
}

func checkInCase() -> Bool {
if indent.inCase {
indent.inCase = false
Expand Down
26 changes: 17 additions & 9 deletions Swimat/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="HFN-HU-Szk">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F73" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="HFN-HU-Szk">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12120"/>
<capability name="box content view" minToolsVersion="7.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -30,11 +30,11 @@
<objects>
<viewController title="Swimat" id="5NS-zJ-YxT" customClass="SwimatViewController" customModule="Swimat" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="neG-K5-vhD">
<rect key="frame" x="0.0" y="0.0" width="554" height="401"/>
<rect key="frame" x="0.0" y="0.0" width="554" height="431"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<tabView translatesAutoresizingMaskIntoConstraints="NO" id="Mlv-5f-ipi">
<rect key="frame" x="-7" y="-10" width="568" height="403"/>
<rect key="frame" x="-7" y="-10" width="568" height="433"/>
<font key="font" metaFont="system"/>
<tabViewItems>
<tabViewItem label="Info" identifier="1" id="Yn6-4a-tYh">
Expand Down Expand Up @@ -153,14 +153,11 @@ Swimat also comes with a handy command line tool:</string>
</tabViewItem>
<tabViewItem label="Configure" identifier="2" id="SPT-08-AQ8">
<view key="view" id="hPC-XX-mhM">
<rect key="frame" x="10" y="33" width="548" height="357"/>
<rect key="frame" x="10" y="33" width="548" height="387"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button translatesAutoresizingMaskIntoConstraints="NO" id="rxX-dK-ndq">
<rect key="frame" x="15" y="338" width="146" height="18"/>
<constraints>
<constraint firstAttribute="height" constant="14" id="aAo-mF-Ad6"/>
</constraints>
<rect key="frame" x="15" y="368" width="146" height="18"/>
<buttonCell key="cell" type="check" title="Use Parameter Align" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="slC-5X-kxA">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
Expand Down Expand Up @@ -196,10 +193,20 @@ class SwimatTest: XCTestCase {
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button translatesAutoresizingMaskIntoConstraints="NO" id="o5T-Qx-O3u">
<rect key="frame" x="15" y="344" width="181" height="18"/>
<buttonCell key="cell" type="check" title="Remove unnecessary char" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="FDi-dU-Exn">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
</subviews>
<constraints>
<constraint firstItem="zyC-iZ-yHg" firstAttribute="top" secondItem="o5T-Qx-O3u" secondAttribute="bottom" constant="14" id="8lp-3l-7ZN"/>
<constraint firstItem="o5T-Qx-O3u" firstAttribute="top" secondItem="rxX-dK-ndq" secondAttribute="bottom" constant="10" id="94r-Rh-det"/>
<constraint firstItem="rxX-dK-ndq" firstAttribute="top" secondItem="hPC-XX-mhM" secondAttribute="top" constant="3" id="AgZ-tI-hcT"/>
<constraint firstAttribute="trailing" secondItem="zyC-iZ-yHg" secondAttribute="trailing" constant="17" id="C25-Mh-kck"/>
<constraint firstItem="o5T-Qx-O3u" firstAttribute="leading" secondItem="hPC-XX-mhM" secondAttribute="leading" constant="17" id="Ko0-qb-MfD"/>
<constraint firstItem="rxX-dK-ndq" firstAttribute="leading" secondItem="hPC-XX-mhM" secondAttribute="leading" constant="17" id="UPO-KS-LjV"/>
<constraint firstItem="zyC-iZ-yHg" firstAttribute="leading" secondItem="hPC-XX-mhM" secondAttribute="leading" constant="17" id="ZM9-ur-zoY"/>
<constraint firstAttribute="bottom" secondItem="zyC-iZ-yHg" secondAttribute="bottom" constant="9" id="mEJ-4f-DUz"/>
Expand All @@ -217,6 +224,7 @@ class SwimatTest: XCTestCase {
</constraints>
</view>
<connections>
<outlet property="autoRemoveChar" destination="o5T-Qx-O3u" id="mq1-Fe-GtL"/>
<outlet property="installButton" destination="9nB-vh-mrK" id="etJ-nb-tps"/>
<outlet property="paraAlign" destination="rxX-dK-ndq" id="fVj-LP-khb"/>
<outlet property="source" destination="zyC-iZ-yHg" id="hhf-LB-Ims"/>
Expand Down
7 changes: 7 additions & 0 deletions Swimat/SwimatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ class SwimatViewController: NSViewController {
}

@IBOutlet weak var paraAlign: NSButton!

@IBOutlet weak var autoRemoveChar: NSButton!

@IBOutlet weak var source: NSTextField!

override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
paraAlign.state = Pref.isParaAlign() ? 1 : 0
autoRemoveChar.state = Pref.isParaAlign() ? 1 : 0
formatSource()
}

Expand All @@ -44,6 +48,9 @@ class SwimatViewController: NSViewController {
formatSource()
}

@IBAction func autoRemoveChar(_ sender: NSButton) {
Pref.setParaAlign(isAlign: sender.state == 1)
}
func formatSource() {
Indent.char = " "
Indent.size = 4
Expand Down
29 changes: 19 additions & 10 deletions SwimatTests/FormatTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ class FormatTest: XCTestCase {
Indent.size = 4
Indent.paraAlign = true
let parser = SwiftParser(string: res)
do {
let result = try parser.format()
print("expect=")
print(expect)
print("result=")
print(result)
assert(result == expect)
} catch {
assertionFailure()
}
format(parser: parser, expect: expect)
}


func formatAlign(res: String, expect: String) {
Indent.char = " "
Indent.size = 4
Indent.paraAlign = false
let parser = SwiftParser(string: res)
format(parser: parser, expect: expect)
}

func format(parser: SwiftParser, expect: String) {
do {
let result = try parser.format()
print("expect=")
Expand Down Expand Up @@ -144,4 +140,17 @@ class FormatTest: XCTestCase {
formatAlign(res: res, expect: result)
}

func testCase11() { //#163
let res = "a ? (b as? Int) : Int((c as? Double) ?? 0)"
format(res: res, expect: res)
formatAlign(res: res, expect: res)
}

func testCase12() { //#166
let res = "c = a + b;"
let ret = "c = a + b"
let parser = SwiftParser(string: res)
parser.autoRemoveChar = true
format(parser: parser, expect: ret)
}
}

0 comments on commit c7994b1

Please sign in to comment.