@@ -23,35 +23,34 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
2323 let confrom = config. confrom
2424 if confrom. isEmpty {
2525 return " "
26- } else {
26+ } else {
2727 return confrom. joined ( separator: " , " )
2828 }
2929 } ( )
30+
3031 /// 遵循
3132 private lazy var importModule : [ String ] = {
32- return ConfigUserDefault . shared. getConfig ( ) . module
33+ ConfigUserDefault . shared. getConfig ( ) . module
3334 } ( )
3435
35- func perform( with invocation: XCSourceEditorCommandInvocation , completionHandler: @escaping ( Error ? ) -> Void ) -> Void {
36+ func perform( with invocation: XCSourceEditorCommandInvocation , completionHandler: @escaping ( Error ? ) -> Void ) {
3637 print ( " 启动插件 " )
3738
3839 if invocation. commandIdentifier == configCommand {
39- NSWorkspace . shared. open ( URL . init ( fileURLWithPath: " /Applications/SwiftJSONModeler For Xcode.app " ) )
40-
41- } else {
42- handleInvocation ( invocation, handler: completionHandler)
40+ NSWorkspace . shared. open ( URL ( fileURLWithPath: " /Applications/SwiftJSONModeler For Xcode.app " ) )
41+ } else {
42+ handleInvocation ( invocation, handler: completionHandler)
4343 }
4444 completionHandler ( nil )
4545 }
4646
47- private func handleInvocation( _ invacation: XCSourceEditorCommandInvocation , handler: ( Error ? ) -> Void ) -> Void {
48-
47+ private func handleInvocation( _ invacation: XCSourceEditorCommandInvocation , handler: ( Error ? ) -> Void ) {
4948 let buffer = invacation. buffer
50- var line = buffer. lines
51- //importModel(lines: &line)
49+ var line = buffer. lines
50+ // importModel(lines: &line)
5251 // 获取复制内容
5352 guard let paste = NSPasteboard . general. string ( forType: . string) else {
54- handler ( error ( msg: " 复制内容异常 " ) )
53+ handler ( error ( msg: " 复制内容异常 " ) )
5554 return
5655 }
5756 guard !paste. isEmpty else {
@@ -61,29 +60,26 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
6160 let lines = linesFrom ( paste: paste)
6261 switch lines {
6362 case . failure( let error) :
64- handler ( error)
63+ handler ( error)
6564 case . success( let l) :
6665 let objectLines = objectLine ( commandIdentifier: invacation. commandIdentifier, line: l)
6766 addLines ( origin: & line, new: objectLines, invocation: invacation)
6867 importModel ( lines: & line)
6968 handler ( nil )
7069 }
71-
7270 }
73-
74-
75-
7671}
72+
7773// MARK: - Helper
74+
7875private extension SourceEditorCommand {
7976 func error( msg: String ) -> Error {
80- return NSError ( domain: domain, code: 300 , userInfo: [ NSLocalizedDescriptionKey: msg,
81- ] )
77+ return NSError ( domain: domain, code: 300 , userInfo: [ NSLocalizedDescriptionKey: msg] )
8278 }
79+
8380 /// 添加引入模块
84- func importModel( lines: inout NSMutableArray ) {
85-
86- var firstImportIndex : Int = 8 ;
81+ func importModel( lines: inout NSMutableArray ) {
82+ var firstImportIndex : Int = 8
8783 if lines. count < 9 {
8884 firstImportIndex = 0
8985 }
@@ -92,11 +88,11 @@ private extension SourceEditorCommand {
9288 guard let value = value as? String else {
9389 return
9490 }
95- if value. contains ( keyImport) {
91+ if value. contains ( keyImport) {
9692 if value. contains ( " Foundation " ) || value. contains ( " UIKit " ) {
9793 aimIndex = index + 1
98- } else {
99- aimIndex = index
94+ } else {
95+ aimIndex = index
10096 }
10197 break
10298 }
@@ -106,10 +102,11 @@ private extension SourceEditorCommand {
106102 lines. insert ( " \( keyImport) \( modle) \n " , at: aimIndex)
107103 }
108104 }
105+
109106 /// 过滤已添加的Modle
110- func filterModle( lines: inout NSMutableArray ) -> [ String ] {
107+ func filterModle( lines: inout NSMutableArray ) -> [ String ] {
111108 let waitImport = importModule
112- let imported : [ String ] = lines. filter { ( $0 as! String ) . contains ( keyImport) } as! [ String ]
109+ let imported : [ String ] = lines. filter { ( $0 as! String ) . contains ( keyImport) } as! [ String ]
113110 let needImport = waitImport. filter { ( modle) -> Bool in
114111 for line in imported {
115112 if line. contains ( modle) {
@@ -120,53 +117,54 @@ private extension SourceEditorCommand {
120117 }
121118 return needImport
122119 }
120+
123121 /// 进行新增行整合
124122 func addLines( origin: inout NSMutableArray , new line: [ String ] , invocation: XCSourceEditorCommandInvocation ) {
125123 let buffer = invocation. buffer
126124 let sections = buffer. selections as! [ XCSourceTextRange ]
127125 var insertIndex = - 1
128126 if let first = sections. first {
129- insertIndex = first. start. line
127+ insertIndex = first. start. line
130128 }
131129
132130 if insertIndex > 0 {
133- let insertLastLineValue = origin [ insertIndex- 1 ]
134-
131+ let insertLastLineValue = origin [ insertIndex - 1 ]
132+
135133 if origin. count > insertIndex {
136134 let currentLineValue = origin [ insertIndex]
137135 if ( currentLineValue as! String ) != " \n " {
138- let currentLineValue = origin [ insertIndex]
136+ _ = origin [ insertIndex]
139137 insertIndex += 1
140138 }
141139 }
142140 if let value = insertLastLineValue as? String , value == " \n " {
143-
144- } else {
141+ } else {
145142 origin. insert ( " \n " , at: insertIndex)
146143 insertIndex += 1
147144 }
148145 let insertSet = NSIndexSet ( indexesIn: NSRange ( location: insertIndex, length: line. count) )
149146 origin. insert ( line, at: insertSet as IndexSet )
150- } else {
147+ } else {
151148 origin. addObjects ( from: line)
152149 }
153150 }
151+
154152 /// 生成对象
155153 func objectLine( commandIdentifier: CommandId , line: [ String ] ) -> [ String ] {
156154 var keyword = " struct "
157155 if commandIdentifier == classCommand {
158156 keyword = classCommand
159- } else if commandIdentifier == structCommand {
157+ } else if commandIdentifier == structCommand {
160158 keyword = structCommand
161159 }
162160
163161 var objctLines : [ String ] = [ ]
164162 if parent. isEmpty {
165- objctLines. append ( " \( keyword) <#Model#> { " )
166- } else {
163+ objctLines. append ( " \( keyword) <#Model#> { " )
164+ } else {
167165 objctLines. append ( " \( keyword) <#Model#>: \( parent) { " )
168166 }
169-
167+
170168 objctLines. append ( contentsOf: line)
171169 if commandIdentifier == classCommand, importModule. contains ( " HandyJSON " ) {
172170 objctLines. append ( " " )
@@ -178,59 +176,59 @@ private extension SourceEditorCommand {
178176}
179177
180178// MARK: - Transform
179+
181180extension SourceEditorCommand {
182-
183181 /// 类型判断
184182 /// - Parameter value: 值
185183 func typeOf( value: Any ) -> String {
186- if value is NSNull {
187- print ( " 存在null " )
188- return " <#NSNull#> "
189- } else if value is String {
190- print ( " 存在字符串: \( value) " )
191- return " String "
192- } else if value is Int {
193- // 需要先判断int
194- print ( " 存在Int: \( value) " )
195- return " Int "
196- } else if value is Double {
197- print ( " 存在Doble: \( value) " )
198- return " Double "
199- } else if value is [ Any ] {
200- print ( " 存在数组: \( value) " )
201- if let arr = value as? [ Any ] , let first = arr. first {
202- return " [ \( typeOf ( value: first) ) ] "
203- } else {
204- return " unknow "
205- }
206-
207- } else if value is [ String : Any ] {
208- print ( " 存在子类型: \( value) " )
209- return " <#SubModel#> "
210- } else {
211- print ( " 存在未定义项 " )
212- return " unknow "
184+ if value is NSNull {
185+ print ( " 存在null " )
186+ return " <#NSNull#> "
187+ } else if value is String {
188+ print ( " 存在字符串: \( value) " )
189+ return " String "
190+ } else if value is Int {
191+ // 需要先判断int
192+ print ( " 存在Int: \( value) " )
193+ return " Int "
194+ } else if value is Double {
195+ print ( " 存在Doble: \( value) " )
196+ return " Double "
197+ } else if value is [ Any ] {
198+ print ( " 存在数组: \( value) " )
199+ if let arr = value as? [ Any ] , let first = arr. first {
200+ return " [ \( typeOf ( value: first) ) ] "
201+ } else {
202+ return " unknow "
213203 }
214-
204+
205+ } else if value is [ String : Any ] {
206+ print ( " 存在子类型: \( value) " )
207+ return " <#SubModel#> "
208+ } else {
209+ print ( " 存在未定义项 " )
210+ return " unknow "
211+ }
215212 }
216-
217- func linesFrom( paste: String ) -> Result < [ String ] , Error > {
213+
214+ func linesFrom( paste: String ) -> Result < [ String ] , Error > {
218215 do {
219216 let object = try JSONSerialization . jsonObject ( with: paste. data ( using: . utf8) !, options: . mutableLeaves)
220217 guard let dic = object as? [ String : Any ] else {
221- return Result . failure ( error ( msg: " json 对象转换为字典失败! " ) )
218+ return Result . failure ( error ( msg: " json 对象转换为字典失败! " ) )
222219 }
223220 var lines : [ String ] = [ ]
224221 for (label, value) in dic {
225222 lines. append ( " \( label) : \( typeOf ( value: value) ) " )
226223 }
227- lines = lines. map { " \t var " + $0 + " ? " }
224+ lines = lines. map { " \t var " + $0 + " ? " }
225+ let aimLines = Array . init ( lines. reversed ( ) )
228226 print ( " 目标模板 " )
229- print ( lines )
230- return Result . success ( lines )
227+ print ( aimLines )
228+ return Result . success ( aimLines )
231229 } catch let e {
232230 print ( e)
233- return Result . failure ( error ( msg: " json序列化出错! " ) )
231+ return Result . failure ( error ( msg: " json序列化出错! " ) )
234232 }
235233 }
236234}
0 commit comments