@@ -22,15 +22,30 @@ func command(identifier: String) -> Command? {
2222 return Command ( rawValue: String ( component) )
2323}
2424
25- struct Options {
26- let justTypes : Bool
27- }
25+ let defaultOptionsForCommand : [ Command : [ String : Any ] ] = [
26+ . pasteJSONAsTypes: [ " just-types " : true ] ,
27+ . pasteJSONAsCode: [ " just-types " : false ] ,
28+ ]
2829
29- let optionsForCommand : [ Command : Options ] = [
30- . pasteJSONAsTypes: Options ( justTypes: true ) ,
31- . pasteJSONAsCode: Options ( justTypes: false )
30+ let languageOptionsForCommand : [ Command : [ Language : [ String : Any ] ] ] = [
31+ . pasteJSONAsTypes: [
32+ . objc: [ " features " : " implementation " , " just-types " : false ] ,
33+ . objcHeader: [ " features " : " interface " ]
34+ ] ,
35+ . pasteJSONAsCode: [
36+ . objc: [
37+ // Objective-C is not ideal yet, so extra comments are useful
38+ " extra-comments " : true ,
39+ // We also intentionally output implementation *and* interfaces until we can emit
40+ // better instructions for importing your header
41+ " features " : " all "
42+ ] ,
43+ . objcHeader: [ " features " : " interface " ] ,
44+ . swift: [ " initializers " : true ]
45+ ]
3246]
3347
48+
3449class PasteJSONCommand : NSObject , XCSourceEditorCommand {
3550 func error( _ message: String , details: String = " No details " ) -> NSError {
3651 return NSError ( domain: " quicktype " , code: 1 , userInfo: [
@@ -136,13 +151,23 @@ class PasteJSONCommand: NSObject, XCSourceEditorCommand {
136151 completionHandler ( error ( displayMessage, details: message) )
137152 }
138153
154+ func getOptions( _ command: Command , _ language: Language ) -> [ String : Any ] {
155+ let defaults = defaultOptionsForCommand [ command] ?? [ : ]
156+ let options = languageOptionsForCommand [ command] ? [ language] ?? [ : ]
157+ return defaults. merging ( options, uniquingKeysWith: { $1 } )
158+ }
159+
160+ func getTargetLanguage( _ command: Command , _ invocation: Invocation ) -> Language ? {
161+ return languageFor ( contentUTI: invocation. buffer. contentUTI as CFString )
162+ }
163+
139164 func perform( with invocation: Invocation , completionHandler: @escaping ( Error ? ) -> Void ) -> Void {
140165 guard let command = command ( identifier: invocation. commandIdentifier) else {
141166 completionHandler ( error ( " Unrecognized command " ) )
142167 return
143168 }
144169
145- guard let language = languageFor ( contentUTI : invocation. buffer . contentUTI as CFString ) else {
170+ guard let language = getTargetLanguage ( command , invocation) else {
146171 completionHandler ( error ( " Cannot generate code for \( invocation. buffer. contentUTI) " ) )
147172 return
148173 }
@@ -152,11 +177,7 @@ class PasteJSONCommand: NSObject, XCSourceEditorCommand {
152177 " language " : language. rawValue
153178 ] )
154179
155- guard let options = optionsForCommand [ command] else {
156- completionHandler ( error ( " Could not determine command options " ) )
157- return
158- }
159-
180+ let options = getOptions ( command, language)
160181 let runtime = Runtime . shared
161182
162183 if !runtime. isInitialized && !runtime. initialize ( ) {
@@ -171,7 +192,7 @@ class PasteJSONCommand: NSObject, XCSourceEditorCommand {
171192
172193 runtime. quicktype ( json,
173194 language: language,
174- justTypes : options. justTypes ,
195+ options : options,
175196 fail: { self . handleError ( message: $0, invocation, completionHandler) } ,
176197 success: { self . handleSuccess ( lines: $0, invocation, completionHandler) } )
177198 }
0 commit comments