@@ -13,29 +13,44 @@ import XcodeKit
13
13
class SourceEditorCommand : NSObject , XCSourceEditorCommand {
14
14
15
15
func perform( with invocation: XCSourceEditorCommandInvocation , completionHandler: @escaping ( Error ? ) -> Void ) -> Void {
16
+
16
17
let textRange = invocation. buffer. selections. firstObject as! XCSourceTextRange
17
18
var startLine = textRange. start. line
18
19
19
- let endRegex = """
20
- .* \\ {
21
- """
22
- let isPropertyRegex = " .*(let|var) \\ s+. \\ w+ \\ s*:(?!(.*( \\ (| \\ {| \\ =))) "
23
- let propertyRegex = " \\ w+ \\ s*(:) "
20
+ let endRegex = " .+ \\ s*: \\ s*.* \\ { "
21
+ let propertyRegex = " .*(let|var) \\ s+ \\ w+ \\ s*(:|=).+ "
22
+ let propertyNamePartRegex = " \\ w+ \\ s*(:|=) "
23
+ let propertyNameRegex = " \\ w+ "
24
24
let uppercase = " A " ... " Z "
25
25
26
- var enumCase : [ ( key: String , value: String ) ] = [ ]
26
+ var openBracketCount : Int = 0
27
+ let openBracketRegex = " \\ } "
28
+ let closeBracketRegex = " \\ { "
29
+ let isClosureRegex = " -> "
27
30
31
+ var enumCase : [ ( key: String , value: String ) ] = [ ]
32
+
28
33
while startLine >= 0 {
29
34
guard let lineText = invocation. buffer. lines [ startLine] as? String else { startLine -= 1 ; continue }
30
- if lineText. isMatch ( endRegex) {
35
+
36
+ openBracketCount += lineText. match ( regex: openBracketRegex) . count
37
+ if openBracketCount > 0 {
38
+ openBracketCount -= lineText. match ( regex: closeBracketRegex) . count
39
+ startLine -= 1
40
+ continue
41
+ } else if lineText. isMatch ( endRegex) {
31
42
break
32
43
}
33
- var propertyName : String = lineText. match ( regex: isPropertyRegex)
34
- if propertyRegex. isEmpty {
44
+ guard !lineText. isMatch ( isClosureRegex) else {
35
45
startLine -= 1
36
46
continue
37
47
}
38
- propertyName = propertyName. match ( regex: propertyRegex) . replacingOccurrences ( of: " : " , with: " " )
48
+ var propertyName : String = lineText. match ( regex: propertyRegex)
49
+ if propertyName. isEmpty {
50
+ startLine -= 1
51
+ continue
52
+ }
53
+ propertyName = propertyName. match ( regex: propertyNamePartRegex) . match ( regex: propertyNameRegex)
39
54
var rawValue = " "
40
55
var previousWordIsUppercase = false
41
56
for character in propertyName {
@@ -52,9 +67,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
52
67
}
53
68
54
69
}
55
- if !rawValue. isEmpty {
56
- enumCase. append ( ( key: propertyName, value: rawValue) )
57
- }
70
+ enumCase. append ( ( key: propertyName, value: rawValue) )
58
71
startLine -= 1
59
72
}
60
73
@@ -80,17 +93,16 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
80
93
writeLine += 1
81
94
let key = caseTuple. key
82
95
let value = caseTuple. value
83
- var text = " \( indentSpaces) \( caseIndentSpaces) case \( key) "
84
- if !value . isEmpty , key != value {
85
- text += " = \" \( value) \" "
96
+ writeText = " \( indentSpaces) \( caseIndentSpaces) case \( key) "
97
+ if key != value, !value . isEmpty {
98
+ writeText += " = \" \( value) \" "
86
99
}
87
- writeText = text
88
100
invocation. buffer. lines. insert ( writeText, at: writeLine)
89
101
updateSelectionIndexs. append ( writeLine)
90
102
}
91
- let endText = " \( indentSpaces) } "
103
+ writeText = " \( indentSpaces) } "
92
104
writeLine += 1
93
- invocation. buffer. lines. insert ( endText , at: writeLine)
105
+ invocation. buffer. lines. insert ( writeText , at: writeLine)
94
106
95
107
let textRanges = updateSelectionIndexs. map { ( index) -> XCSourceTextRange in
96
108
let sourceTextRange = XCSourceTextRange ( )
@@ -99,7 +111,6 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
99
111
return sourceTextRange
100
112
}
101
113
102
-
103
114
invocation. buffer. selections. setArray ( textRanges)
104
115
105
116
completionHandler ( nil )
0 commit comments