In this article we learned how you can push Xcode IDE to the next limits by writing your own extension. Go to Xcode > Preferences > Key Bindings > search for “Lines Sorter”. Like any other editor command, you can assign a keys combination to yours. Voila, your lines must be ordered alphabetically now. Go to Editor > Lines Sorter > Sort Selected Lines.Source editor extension will be launched in a separate instance of Xcode that can be distinguished by a darker top bar. Run SourceEditorExtension target and select Xcode app from the list.I will not dive too deeply into details, but Apple got you covered with this tutorial. First of all, both the app target and the source editor extension must be signed with your developer certificate.Testing Xcode Source Editor Extensions is different from what you have used to when developing macOS and iOS apps. line, by : isLessThanIgnoringLeadingWhitespacesAndTabs ) Testing the Commandįinally let’s see our work in action. Let’s define a custom comparator that takes care of it. We usually don’t want to take indentation into account as we sort. Lines of codes tend to have different indentation levels that affects their sorting. We pass < operator to sort alphabetically. Call sort method to perform the actual sorting by mutating the input NSMutableArray in place.Do some sanity checks that at least two lines of code are selected within the editor.Open SourceEditorCommand.swift and paste the method there:įunc perform ( with invocation : XCSourceEditorCommandInvocation, completionHandler : ( Error ?) -> Void ) -> Void We already know that as soon as the command is activated from Xcode Editor menu, an invocation instance is sent to a the command class. Make sure to check it out after reading this article. I got so much carried away by writing this article that I ended up with LinesSorter extension and open sourced it recently. Sounds like an easy task? It sure is after you learning the basics of XcodeKit and the structure of source editor extension target. Here is how extension name and command names are displayed in Editor menu:Īs you might have already guessed, our extension will sort selected lines of code. There is no need to make any other edits as long as we have only one command. Let’s change it into Sort Selected Lines.īundle display name stands for your extension name in Editor menu. XCSourceEditorCommandName - a command name as it will be displayed in the second level of Xcode Editor menu.Make sure you set it to something unique within your extension. XCSourceEditorCommandIdentifier - a command invocation identifier.Xcode has already created a command class with exact same name once you created the source editor target. XCSourceEditorCommandClassName - a name of the command class.Lets unfold it and inspect inner properties. Tap Activate when it prompts you Activate “SourceEditorExtension” scheme.Īt this point the targets must look like this:Īll editor extension targets contain an extra entry in their ist files named NSExtension. Now add Xcode Source Editor Extension Target to your newly created project. Xcode editor extensions cannot exist on their own and must be wired up to a macOS application.įirst off, we create a macOS project in Xcode named LinesSorter. By the end of this article, your command will look something like this: XCSourceTextBuffer - a buffer used to manipulate the text contents and selections in a source editor.Įxtension’s commands are accessible from Xcode Editor dropdown menu.As already noted, multiple invocations can be handled by a single XCSourceEditorCommand. XCSourceEditorCommandInvocation - an instance of the command sent to your extension. ![]() You must implement at least one of these in your extension. ![]() You can think of it as a sink where one or more command invocations are handled. XCSourceEditorCommand - the protocol that stands for the source editor command handler.You can think of it as an AppDelegate from your iOS and macOS apps. XCSourceEditorExtension - the protocol that every Xcode Source Editor Extension must implement.They can read and modify the contents of current source file, select and deselect text within that file. Extensions have quite limited functionality. You create extensions to the source editor by means of XcodeKit framework. Explaining Xcode Source Editor Extensions In this article you will learn how to create Xcode Source Editor Extension that adds some extra functionality to Xcode. Although it is well-integrated with the most development workflows, from time to time you might feel like missing some basic features. Xcode is the core tool for Apple development.
0 Comments
Leave a Reply. |