Search code examples
sourcekitxcodekit

XcodeKit and SourceKit


I don't have experience in XcodeKit and SourceKit apart from a few articles that I read and the official pages https://developer.apple.com/documentation/xcodekit#topics https://github.com/apple/swift/tree/master/tools/SourceKit

I'm trying to understand the differences between the two and what they are capable of doing exactly.

I think XcodeKit essentially let you add commands to Xcode menu and makes it possible to interact with the source code you have open by creating an app extension which you can then also distribute through the App Store. As far as I could understand XcodeKit only let you get information from the current focused file (not all the files in project), then you can get selected text etc... At least that's what I read until Xcode 9, not sure if things are better now.

SourceKit also interacts with source code but looks like more powerful but more low level.

What I'm wondering is:

  • Does SourceKit make it possible to access to all the Swift and ObjC classes that you have in your open project, and then extract informations out of it? For example let's say that I want to extract informations like structs and classes, methods of each class, and then generate some sort of output out of it. How do I use SourceKit (or XcodeKit?) to do that?
  • Any link to examples would be great!
  • Is SourceKit still actual and supported by Apple?

https://github.com/apple/swift/blob/master/tools/SourceKit/docs/Protocol.md

Thanks


Solution

  • Depending on your end goal, you have a few options:

    • SwiftSyntax is a new (beta) open-source framework from Apple for walking through/modifying a document’s AST (abstract syntax tree). More information & an example are available on the GitHub repository.

    • As mentioned, SourceKitten is a Swift library for communicating with SourceKit. SourceKit is the backbone of Xcode, which is really just a glorified text editor without it. It provides everything from semantically-analyzed substructures and generated interfaces to code-completion and syntax highlighting—everything an IDE needs. It is the most encompassing option and it's not going anywhere anytime soon (as Maxim mentioned, Apple's even developing a LSP interface for it.) Shameless plug: I’ve further developed Sylvester 😼, a typed, XPC-available SourceKitten (SourceKit) interface with some sugar. The SKEditorOpen request should provide you with enough information to extract whatever you want from a source document.

    • XcodeKit exposes an extremely limited API to interacting with the source code. In fact, it only provides a mutable buffer of the open document in the editor that the user invoked the command from.

    As for your inquiry on retrieving the currently open documents in the editor, your only option would be to use the wonderful scripting interface that Xcode exposes. The Xcode SDEF file can be found at /Applications/Xcode.app/Contents/Resources/Xcode.sdef. You can use the ScriptingBridge framework to do so from a Cocoa application. If you're using Swift, then the SwiftScripting repository is crucially helpful.