9
9
import AppKit
10
10
import XcodeKit
11
11
12
-
13
-
14
-
15
-
16
12
class SourceEditorCommand : NSObject , XCSourceEditorCommand {
17
13
let config = Config ( )
18
14
private var completionHandler : ( Error ? ) -> Void = { _ in }
@@ -75,28 +71,7 @@ class SourceEditorCommand: NSObject, XCSourceEditorCommand {
75
71
completionHandler ( nil )
76
72
}
77
73
78
-
79
- private func handleInvocation( _ invacation: XCSourceEditorCommandInvocation , handler: ( Error ? ) -> Void ) {
80
-
81
-
82
- let buffer = invacation. buffer
83
- var line = buffer. lines
84
- // importModel(lines: &line)
85
- if pasteboardTest. isEmpty {
86
- handler ( error ( msg: " 复制文本不能为空 " ) )
87
- return
88
- }
89
- let lines = linesFrom ( paste: pasteboardTest)
90
- switch lines {
91
- case . failure( let error) :
92
- handler ( error)
93
- case . success( let l) :
94
- let objectLines = objectLine ( commandIdentifier: invacation. commandIdentifier, line: l)
95
- addLines ( origin: & line, new: objectLines, invocation: invacation)
96
- importModel ( lines: & line)
97
- handler ( nil )
98
- }
99
- }
74
+
100
75
}
101
76
102
77
// MARK: - 添加通知
@@ -115,8 +90,6 @@ private extension SourceEditorCommand {
115
90
}
116
91
}
117
92
118
- // MARK: - json Helper
119
-
120
93
private extension SourceEditorCommand {
121
94
func error( msg: String ) -> Error {
122
95
return NSError ( domain: domain, code: 300 , userInfo: [ NSLocalizedDescriptionKey: msg] )
@@ -162,129 +135,5 @@ private extension SourceEditorCommand {
162
135
}
163
136
return needImport
164
137
}
165
-
166
- /// 进行新增行整合
167
- func addLines( origin: inout NSMutableArray , new line: [ String ] , invocation: XCSourceEditorCommandInvocation ) {
168
- let buffer = invocation. buffer
169
- let sections = buffer. selections as! [ XCSourceTextRange ]
170
- var insertIndex = - 1
171
- if let first = sections. first {
172
- insertIndex = first. start. line
173
- }
174
-
175
- if insertIndex > 0 {
176
- let insertLastLineValue = origin [ insertIndex - 1 ]
177
-
178
- if origin. count > insertIndex {
179
- let currentLineValue = origin [ insertIndex]
180
- if ( currentLineValue as! String ) != " \n " {
181
- _ = origin [ insertIndex]
182
- insertIndex += 1
183
- }
184
- }
185
- if let value = insertLastLineValue as? String , value == " \n " {
186
- } else {
187
- origin. insert ( " \n " , at: insertIndex)
188
- insertIndex += 1
189
- }
190
- let insertSet = NSIndexSet ( indexesIn: NSRange ( location: insertIndex, length: line. count) )
191
- origin. insert ( line, at: insertSet as IndexSet )
192
- } else {
193
- origin. addObjects ( from: line)
194
- }
195
- }
196
-
197
- /// 生成对象
198
- func objectLine( commandIdentifier: CommandId , line: [ String ] ) -> [ String ] {
199
- var keyword = keyStruct
200
- if commandIdentifier == classFromJSONCommand {
201
- keyword = keyClass
202
- } else if commandIdentifier == structFromJSONCommand {
203
- keyword = keyStruct
204
- }
205
-
206
- var objctLines : [ String ] = [ ]
207
- let parent = config. parent
208
- var modelName = " <#Model#> "
209
- modelName = config. prefix + modelName + config. subffix
210
- if parent. isEmpty {
211
- objctLines. append ( " \( keyword) \( modelName) { " )
212
- } else {
213
- objctLines. append ( " \( keyword) \( modelName) : \( parent) { " )
214
- }
215
-
216
- objctLines. append ( contentsOf: line)
217
- if commandIdentifier == classFromJSONCommand, parent. contains ( " HandyJSON " ) {
218
- objctLines. append ( " " )
219
- objctLines. append ( " \t required init() { } " )
220
- }
221
- objctLines. append ( " } " )
222
- return objctLines
223
- }
224
- }
225
138
226
- // MARK: - Transform
227
-
228
- extension SourceEditorCommand {
229
- /// 类型判断
230
- /// - Parameter value: 值
231
- func typeOf( value: Any ) -> String {
232
- if value is NSNull {
233
- print ( " 存在null " )
234
- return " <#NSNull#> "
235
- } else if value is String {
236
- print ( " 存在字符串: \( value) " )
237
- return " String "
238
- } else if value is Int {
239
- // 需要先判断int
240
- print ( " 存在Int: \( value) " )
241
- return " Int "
242
- } else if value is Double {
243
- print ( " 存在Doble: \( value) " )
244
- return " Double "
245
- } else if value is [ Any ] {
246
- print ( " 存在数组: \( value) " )
247
- if let arr = value as? [ Any ] , let first = arr. first {
248
- return " [ \( typeOf ( value: first) ) ] "
249
- } else {
250
- return " unknow "
251
- }
252
-
253
- } else if value is [ String : Any ] {
254
- print ( " 存在子类型: \( value) " )
255
- return " <#SubModel#> "
256
- } else {
257
- print ( " 存在未定义项 " )
258
- return " unknow "
259
- }
260
- }
261
-
262
- func linesFrom( paste: String ) -> Result < [ String ] , Error > {
263
- do {
264
- let object = try JSONSerialization . jsonObject ( with: paste. data ( using: . utf8) !, options: . mutableLeaves)
265
- guard let dic = object as? [ String : Any ] else {
266
- return Result . failure ( error ( msg: " json 对象转换为字典失败! " ) )
267
- }
268
- var lines : [ String ] = [ ]
269
- for (label, value) in dic {
270
- lines. append ( " \( label) : \( typeOf ( value: value) ) " )
271
- }
272
- if !config. isNotOptional {
273
- if config. isImplicitlyOptional {
274
- lines = lines. map { " \t var " + $0 + " ! " }
275
- } else {
276
- lines = lines. map { " \t var " + $0 + " ? " }
277
- }
278
- } else {
279
- lines = lines. map { " \t var " + $0 }
280
- }
281
- let aimLines = Array . init ( lines. reversed ( ) )
282
- print ( " 目标模板 " )
283
- print ( aimLines)
284
- return Result . success ( aimLines)
285
- } catch let e {
286
- print ( e)
287
- return Result . failure ( error ( msg: " json序列化出错! " ) )
288
- }
289
- }
290
139
}
0 commit comments