Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Xcode Extensions

Xcode Extensions

WWDC16で発表されたXcode Source Editor Extensionsの話

Yoonchul Koh

June 23, 2016
Tweet

More Decks by Yoonchul Koh

Other Decks in Programming

Transcript

  1. 1. File - New - Project… 2. OS X -

    Application - Cocoa Application 3. File - New - Target... 4. OS X - ApplicationExtensions - Xcode Source Editor Extension 5. Activate "APP_NAME" scheme?
  2. import Foundation import XcodeKit class SourceEditorExtension: NSObject, XCSourceEditorExtension { /*

    func extensionDidFinishLaunching() { // If your extension needs to do any work at launch, implement this optional method. } */ /* var commandDefinitions: [[XCSourceEditorCommandDefinitionKey: AnyObject]] { // If your extension needs to return a collection of command definitions that differs from those in its Info.plist, implement this optional property getter. return [] } */ }
  3. import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func

    perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { // Implement your command here, invoking the completion handler when done. Pass it nil on success, and an NSError on failure. completionHandler(nil) } }
  4. import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func

    perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { // ExtensionΛ࣮ߦͨ࣌͠ʹ։͍͍ͯΔϑΝΠϧ಺༰ // NSMutableArray - // __NSCFString, NSTaggedPointerString, ... for line in invocation.buffer.lines { debugPrint(line) } // ExtensionΛ࣮ߦͨ࣌͠ʹબ୒͍ͯ͠ΔҐஔ৘ใ // NSMutableArray - XCSourceTextRange for selection in invocation.buffer.selections { debugPrint(selection) } completionHandler(nil) } }
  5. import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func

    perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { let lineCount = invocation.buffer.lines.count for i in 0...(lineCount-1) { let line = invocation.buffer.lines[i] let newLine = "\(String(line).trimmingCharacters(in: CharacterSet.newlines)) // " invocation.buffer.lines.replaceObject(at: i, with: newLine) } completionHandler(nil) } }
  6. 1. Target > My Mac - Run 2. Choose an

    app to run > Xcode Beta 3. Dark Xcode 4. Open Project 5. Menu > Editor > ProjectName > ExtensionName
  7. import Foundation import XcodeKit class SourceEditorCommand: NSObject, XCSourceEditorCommand { func

    perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { let lineCount = invocation.buffer.lines.count for i in 0...(lineCount-1) { let line = invocation.buffer.lines[i] let newLine = "\(String(line).trimmingCharacters(in: CharacterSet.newlines)) // " invocation.buffer.lines.replaceObject(at: i, with: newLine) } completionHandler(nil) } }
  8. import Foundation // import XcodeKit // // class SourceEditorCommand: NSObject,

    XCSourceEditorCommand { // // func perform(with invocation: XCSourceEditorCommandInvocation, completionHandler: (NSError?) -> Void ) -> Void { // let lineCount = invocation.buffer.lines.count // for i in 0...(lineCount-1) { // let line = invocation.buffer.lines[i] // let newLine = "\(String(line).trimmingCharacters(in: CharacterSet.newlines)) // " // invocation.buffer.lines.replaceObject(at: i, with: newLine) // } // // completionHandler(nil) // } // // } //
  9. Summary • ࠓग़དྷΔ͜ͱ͸։͍͍ͯΔϑΝΠϧΛεΫϦϓτʹᷰΘͤͯԿ ͔Λॲཧ͢ΔɻඞཁͰ͋Ε͹݁ՌΛฦ͚ͩ͢ɻ • ͔͠͠ग़དྷΔ͜ͱ͕গͳ͗͢Δɻɻ • Swift Color LiteralsΈ͍ͨʹύϨοτΛදࣔͨ͠ΓɺΤσΟλͷ

    දࣔΛม͑Δ͜ͱ͸Ͱ͖ͳ͍ • Xcode editor extensionͱ͍͏ΤσΟλͷ֎ଆͷextension΋͋Δ Β͍͠ɻ͚ΕͲ৘ใݟ౰ͨΒͣɻɻ • ͱ͸͍͑ࠓޙ֦ॆ͞Ε͍ͯ͘ͱࢥ͏ͷͰΞοϓσʔτʹظ଴ʂ