SourceKitten Documentation

repository·main·Indexed 25 days ago

https://github.com/jpsim/sourcekitten

A framework and command line tool for interacting with SourceKit. SourceKitten enables developers to parse Swift AST, extract documentation, obtain syntax data, and perform code completion for Swift and Objective-C projects. It provides subcommands such as complete, doc, format, index, module-info, request, structure, and syntax to interact with SourceKit and output data in JSON format.

Tokens
1.4K
Snippets
7
Records
10
Agent score
32%

What's inside SourceKitten

  1. How SourceKitten resolves SourceKit

    main

    SourceKitten searches for the SourceKit framework in the following order:

    1. $XCODE_DEFAULT_TOOLCHAIN_OVERRIDE
    2. $TOOLCHAIN_DIR
    3. xcrun -find swift
    4. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
    5. /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
    6. ~/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
    7. ~/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain

    On Linux: SourceKit is expected to be at /usr/lib/libsourcekitdInProc.so or specified via the LINUX_SOURCEKIT_LIB_PATH environment variable.

  2. Install SourceKitten

    main

    You can install SourceKitten using several methods depending on your environment:

    Homebrew

    Run brew install sourcekitten.

    Swift Package Manager

    Run swift build in the root directory of the project.

    Bazel

    Add the following to your WORKSPACE file (replace SOME_VERSION and SOME_SHA with the appropriate values):

    SOURCEKITTEN_VERSION = "SOME_VERSION"
    SOURCEKITTEN_SHA = "SOME_SHA"
    http_archive(
        name = "com_github_jpsim_sourcekitten",
        url = "https://github.com/jpsim/SourceKitten/archive/refs/tags/%s.tar.gz" % (SOURCEKITTEN_VERSION),
        sha256 = SOURCEKITTEN_SHA,
        strip_prefix = "SourceKitten-%s" % SOURCEKITTEN_VERSION
    )

    Then run: bazel run @com_github_jpsim_sourcekitten//:sourcekitten -- -h

    Xcode (via Make)

    Run make install in the root directory of the project.

    Package

    Download and open the SourceKitten.pkg from the releases tab.

    brew install sourcekitten
  3. Generate code completion with `complete`

    main

    The complete subcommand generates code completion options for a specific offset in a file or a string of text.

    To use the iOS SDK, pass -sdk and -target arguments preceded by --.

    Example: Completing text with iOS SDK

    sourcekitten complete --text "import UIKit ; UIColor." --offset 22 -- -target arm64-apple-ios9.0 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk
    sourcekitten complete --file file.swift --offset 123
  4. Extract documentation with `doc`

    main

    The doc subcommand passes arguments to xcodebuild (or directly to the compiler in --single-file mode) to extract documentation.

    Example usage patterns:

    1. Workspace/Scheme: sourcekitten doc -- -workspace SourceKitten.xcworkspace -scheme SourceKittenFramework
    2. Single File: sourcekitten doc --single-file file.swift -- -j4 file.swift
    3. Module: sourcekitten doc --module-name Alamofire -- -project Alamofire.xcodeproj
    4. Objective-C with sysroot: sourcekitten doc --objc Realm/Realm.h -- -x objective-c -isysroot $(xcrun --show-sdk-path) -I $(pwd)
    sourcekitten doc -- -workspace SourceKitten.xcworkspace -scheme SourceKittenFramework
  5. Run raw SourceKit requests with `request`

    main

    The request subcommand allows you to execute a raw SourceKit request using a YAML configuration.

    Example usage:

    sourcekitten request --yaml [FILE|TEXT]

    Example YAML input:

    key.request: source.request.cursorinfo
    key.sourcefile: "/tmp/foo.swift"
    key.offset: 8
    key.compilerargs:
      - "/tmp/foo.swift"
    sourcekitten request --yaml [FILE|TEXT]
  6. Use SourceKitten command line subcommands

    main

    SourceKitten provides a variety of subcommands to interact with SourceKit. Use sourcekitten help <subcommand> to see detailed information for any specific command.

    Available Subcommands

    • complete: Generate code completion options.
    • doc: Print Swift or Objective-C docs as JSON.
    • format: Format Swift files.
    • index: Index Swift files and print as JSON.
    • module-info: Obtain information about a Swift module and print as JSON.
    • request: Run a raw SourceKit request.
    • structure: Print Swift structure information as JSON.
    • syntax: Print Swift syntax information as JSON.
    • version: Display the current version.

    Global Options

    • --version: Show the version.
    • -h, --help: Show help information.
    $ sourcekitten help
  7. Get Swift structure information with `structure`

    main

    The structure subcommand returns a JSON array representing the structural information of a Swift file or text snippet (e.g., structs, classes, functions).

    Example usage:

    sourcekitten structure --file file.swift
    # OR
    sourcekitten structure --text "struct A { func b() {} }"
    sourcekitten structure --file file.swift
  8. Get syntax highlighting data with `syntax`

    main

    The syntax subcommand returns a JSON array containing syntax highlighting information (offsets, lengths, and types) for a Swift file or text snippet.

    Example usage:

    sourcekitten syntax --file file.swift
    # OR
    sourcekitten syntax --text "import Foundation // Hello World"
    sourcekitten syntax --file file.swift
  9. Use the `index` command to index a Swift file

    main

    The index command allows you to index a specific Swift file and output the resulting index data as a JSON string. This is useful for programmatic access to SourceKit index information for a single file.

    To use this command, you must provide:

    1. A --file option specifying the relative or absolute path to the Swift file.
    2. One or more positional arguments representing the compiler arguments to pass to SourceKit (e.g., -sdk, -I, etc.).