• Stars
    star
    2,308
  • Rank 19,952 (Top 0.4 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 10 years ago
  • Updated 18 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An adorable little framework and command line tool for interacting with SourceKit.

SourceKitten

An adorable little framework and command line tool for interacting with SourceKit.

SourceKitten links and communicates with sourcekitd.framework to parse the Swift AST, extract comment docs for Swift or Objective-C projects, get syntax data for a Swift file and lots more!

SwiftPM

Installation

Building SourceKitten requires Xcode 13.3 or later or a Swift 5.6 toolchain or later with the Swift Package Manager.

SourceKitten typically supports previous versions of SourceKit.

Homebrew

Run brew install sourcekitten.

Swift Package Manager

Run swift build in the root directory of this project.

Bazel

Add the following to your WORKSPACE file:

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 this project.

Package

Download and open SourceKitten.pkg from the releases tab.

Command Line Usage

Once SourceKitten is installed, you may use it from the command line.

$ sourcekitten help
OVERVIEW: An adorable little command line tool for interacting with SourceKit

USAGE: sourcekitten <subcommand>

OPTIONS:
  --version               Show the version.
  -h, --help              Show help information.

SUBCOMMANDS:
  complete                Generate code completion options
  doc                     Print Swift or Objective-C docs as JSON
  format                  Format Swift file
  index                   Index Swift file 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 of SourceKitten

  See 'sourcekitten help <subcommand>' for detailed help.

How is SourceKit resolved?

SourceKitten searches for SourceKit in the following order:

  • $XCODE_DEFAULT_TOOLCHAIN_OVERRIDE
  • $TOOLCHAIN_DIR
  • xcrun -find swift
  • /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
  • /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
  • ~/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
  • ~/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain

On Linux, SourceKit is expected to be located in /usr/lib/libsourcekitdInProc.so or specified by the LINUX_SOURCEKIT_LIB_PATH environment variable.

Projects Built With SourceKitten

  • SwiftLint: A tool to enforce Swift style and conventions.
  • Jazzy: Soulful docs for Swift & Objective-C.
  • Sourcery: Meta-programming for Swift, stop writing boilerplate code.
  • SwiftyMocky: Framework for mock generation.
  • SourceKittenDaemon: Swift Auto Completions for any Text Editor.
  • SourceDocs: Command Line Tool that generates Markdown documentation from inline source code comments.
  • Cuckoo: First boilerplate-free mocking framework for Swift.
  • IBAnalyzer: Find common xib and storyboard-related problems without running your app or writing unit tests.
  • Taylor: Measure Swift code metrics and get reports in Xcode, Jenkins and other CI platforms.
See More

Complete

Running sourcekitten complete --file file.swift --offset 123 or sourcekitten complete --text "0." --offset 2 will print out code completion options for the offset in the file/text provided:

[{
  "descriptionKey" : "advancedBy(n: Distance)",
  "associatedUSRs" : "s:FSi10advancedByFSiFSiSi s:FPSs21RandomAccessIndexType10advancedByuRq_S__Fq_Fqq_Ss16ForwardIndexType8Distanceq_ s:FPSs16ForwardIndexType10advancedByuRq_S__Fq_Fqq_S_8Distanceq_ s:FPSs10Strideable10advancedByuRq_S__Fq_Fqq_S_6Strideq_ s:FPSs11_Strideable10advancedByuRq_S__Fq_Fqq_S_6Strideq_",
  "kind" : "source.lang.swift.decl.function.method.instance",
  "sourcetext" : "advancedBy(<#T##n: Distance##Distance#>)",
  "context" : "source.codecompletion.context.thisclass",
  "typeName" : "Int",
  "moduleName" : "Swift",
  "name" : "advancedBy(n: Distance)"
},
{
  "descriptionKey" : "advancedBy(n: Self.Distance, limit: Self)",
  "associatedUSRs" : "s:FeRq_Ss21RandomAccessIndexType_SsS_10advancedByuRq_S__Fq_FTqq_Ss16ForwardIndexType8Distance5limitq__q_",
  "kind" : "source.lang.swift.decl.function.method.instance",
  "sourcetext" : "advancedBy(<#T##n: Self.Distance##Self.Distance#>, limit: <#T##Self#>)",
  "context" : "source.codecompletion.context.superclass",
  "typeName" : "Self",
  "moduleName" : "Swift",
  "name" : "advancedBy(n: Self.Distance, limit: Self)"
},
...
]

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

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

Doc

Running sourcekitten doc will pass all arguments after what is parsed to xcodebuild (or directly to the compiler, SourceKit/clang, in the --single-file mode).

Example usage

  1. sourcekitten doc -- -workspace SourceKitten.xcworkspace -scheme SourceKittenFramework
  2. sourcekitten doc --single-file file.swift -- -j4 file.swift
  3. sourcekitten doc --module-name Alamofire -- -project Alamofire.xcodeproj
  4. sourcekitten doc -- -workspace Haneke.xcworkspace -scheme Haneke
  5. sourcekitten doc --objc Realm/Realm.h -- -x objective-c -isysroot $(xcrun --show-sdk-path) -I $(pwd)

Structure

Running sourcekitten structure --file file.swift or sourcekitten structure --text "struct A { func b() {} }" will return a JSON array of structure information:

{
  "key.substructure" : [
    {
      "key.kind" : "source.lang.swift.decl.struct",
      "key.offset" : 0,
      "key.nameoffset" : 7,
      "key.namelength" : 1,
      "key.bodyoffset" : 10,
      "key.bodylength" : 13,
      "key.length" : 24,
      "key.substructure" : [
        {
          "key.kind" : "source.lang.swift.decl.function.method.instance",
          "key.offset" : 11,
          "key.nameoffset" : 16,
          "key.namelength" : 3,
          "key.bodyoffset" : 21,
          "key.bodylength" : 0,
          "key.length" : 11,
          "key.substructure" : [

          ],
          "key.name" : "b()"
        }
      ],
      "key.name" : "A"
    }
  ],
  "key.offset" : 0,
  "key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
  "key.length" : 24
}

Syntax

Running sourcekitten syntax --file file.swift or sourcekitten syntax --text "import Foundation // Hello World" will return a JSON array of syntax highlighting information:

[
  {
    "offset" : 0,
    "length" : 6,
    "type" : "source.lang.swift.syntaxtype.keyword"
  },
  {
    "offset" : 7,
    "length" : 10,
    "type" : "source.lang.swift.syntaxtype.identifier"
  },
  {
    "offset" : 18,
    "length" : 14,
    "type" : "source.lang.swift.syntaxtype.comment"
  }
]

Request

Running sourcekitten request --yaml [FILE|TEXT] will execute a sourcekit request with the given yaml. For example:

key.request: source.request.cursorinfo
key.sourcefile: "/tmp/foo.swift"
key.offset: 8
key.compilerargs:
  - "/tmp/foo.swift"

SourceKittenFramework

Most of the functionality of the sourcekitten command line tool is actually encapsulated in a framework named SourceKittenFramework.

If you’re interested in using SourceKitten as part of another tool, or perhaps extending its functionality, take a look at the SourceKittenFramework source code to see if the API fits your needs.

Note: SourceKitten is written entirely in Swift, and the SourceKittenFramework API is not designed to interface with Objective-C.

License

MIT licensed.

More Repositories

1

Yams

A Sweet and Swifty YAML parser.
C
1,108
star
2

PeerKit

An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps
Swift
872
star
3

CardsAgainst

An iOS game for horrible people
Swift
574
star
4

ZenTuner

A minimal chromatic tuner for iOS & macOS.
Swift
530
star
5

JPSThumbnailAnnotation

A simple mapkit annotation for displaying images and details.
Objective-C
475
star
6

DeckRocket

DeckRocket turns your iPhone into a remote for Deckset presentations
Objective-C
397
star
7

JPSVolumeButtonHandler

JPSVolumeButtonHandler provides an easy block interface to hardware volume buttons on iOS devices. Perfect for camera apps!
Objective-C
333
star
8

SwiftEdit

A proof-of-concept editor, written in Swift, that supports Swift syntax highlighting using SourceKit.
Swift
239
star
9

AWSPics

An AWS CloudFormation stack to run a serverless password-protected photo gallery
JavaScript
219
star
10

Milestones

An app to count down the days until upcoming milestones.
Swift
201
star
11

JPSImagePickerController

A pixel-perfect clone of iOS 7's UIImagePickerController, with a few improvements.
Objective-C
156
star
12

JPSKeyboardLayoutGuide

Easily make your Auto Layout view controllers keyboard aware
Objective-C
69
star
13

SwiftXPC

XPC simplified for Swift. Deal with Swift and NS* objects instead of xpc_object_t.
Swift
63
star
14

Mix2Files

iOS Project to mix several audio files (mp3's, m4a's, caf's) together at different intervals
Objective-C
40
star
15

LetterpressPlayer

Solves Letterpress games via OCR and colour categorization. Work in progress.
Objective-C
37
star
16

talks

Slides for a few of the talks I've given
JSONiq
35
star
17

UICollectionView-Animation-Bug

There's a confirmed bug in UICollectionView. This project highlights a workaround.
Objective-C
33
star
18

jekyll_app_site

Small Jekyll template to present an iPhone app with a static site
CSS
24
star
19

JPSDisplayLink

JPSDisplayLink provides an easy block interface to CADisplayLink-based animations.
Objective-C
23
star
20

AudioEmailer

Very small iOS app to record, playback and email audio. Includes a realtime dB level meter using F3
Objective-C
23
star
21

pod-diffs

Guide and example for a neat way to modify pods using patchfiles
Objective-C
22
star
22

MPCMultipeerClient

Wrapper around MultipeerConnectivity to simplify common use cases
Objective-C
15
star
23

swiftlint-bazel-example

Example SwiftLint integration with custom native rules using Bazel
Starlark
13
star
24

SwiftIvarTypeDetector

Print out the type encoding for properties in a Swift class
Objective-C
12
star
25

Selfish

A tool to automatically insert explicit `self` references in the Swift files in the current directory.
Swift
8
star
26

retest

Re-run failed GitHub Workflow runs on PRs by commenting "/retest".
TypeScript
5
star
27

rust-ios-bazel-demo

A demo iOS app building with Bazel, Rust and Swift
C++
4
star
28

PCCanvas

Simple drawing canvas for core graphics. Easily accepts PaintCode code.
Objective-C
4
star
29

SwiftClassTodo

Todo example from Swift class
Swift
4
star
30

advent-of-code-2021

Swift
3
star
31

Vertigrow-iPad

iPad app I built for Vertigrow a long time ago to help them mockup living walls as a sales tool. Basic image dragging app.
Objective-C
3
star
32

node-docker

Demo Node.JS App using Docker
Shell
3
star
33

laptop

Laptop is a shell script that turns your Mac OS X laptop into an awesome development machine.
Shell
2
star
34

AutoLayoutHelpers

Super space saving auto layout helpers.
Objective-C
2
star
35

rules_xcodeproj_ios_app

Sample rules_xcodeproj iOS app
Starlark
2
star
36

jpsim.github.com

My personal website
Ruby
2
star
37

presto-gem

Gem for interacting with Presto cards.
Ruby
2
star
38

cocoapods-static-xcframework-issue

Issue with CocoaPods and static xcframeworks
Ruby
2
star
39

GoogleMapsSDKBugs

Swift
2
star
40

SwiftPMUtilityExample

Swift
2
star
41

InputClick

The most barebones project ever with the simplest implementation of the UIInputViewAudioFeedback protocol.
Objective-C
2
star
42

fastmac

Shell
1
star
43

retest-action

A GitHub Action to re-run failed GitHub workflow runs on pull requests
TypeScript
1
star
44

applebugs

Collection of sample code for radars I've filed
Objective-C
1
star
45

presto_api

REST API for interacting with Presto cards using the presto ruby gem
Ruby
1
star
46

SourceKit

Swift Package Manager package for SourceKit
Swift
1
star