apple/swift
SourceKit
Active contributors: akyrtzi, ahoppen, bnbarham
Purpose
tools/SourceKit/ is the IDE service that backs Xcode's editor experience and Apple's sourcekit-lsp server. It runs as an out-of-process daemon (or as an in-process framework on Apple platforms) and answers requests like "complete at this offset," "what's the type of the symbol under the cursor," "rename this symbol," and "format this region."
Internally it is a thin shell around lib/IDE/, lib/Index/, lib/Refactoring/, and lib/Markup/. The C++-IDE pages cover the inner workings; this page covers the service binary.
Directory layout
tools/SourceKit/
├── README.txt
├── lib/ # the C++ implementation
│ ├── SwiftLang/ # request → handler mapping (Swift-specific)
│ ├── SourceKit/ # framework-style facade
│ └── ...
├── tools/ # the actual binaries
│ ├── sourcekit/ # the daemon
│ ├── sourcekitd-test/ # CLI test driver
│ ├── sourcekitd-repl/ # interactive request console
│ └── complete-test/ # completion-specific tester
├── bindings/ # Python and Swift client bindings
├── cmake/
└── docs/ # request referenceKey abstractions
| Type | File | Description |
|---|---|---|
sourcekitd_request_t |
tools/SourceKit/include/sourcekitd/sourcekitd.h |
Opaque dictionary representing one client request. |
sourcekitd_response_t |
tools/SourceKit/include/sourcekitd/sourcekitd.h |
Opaque dictionary returned to the client. |
SourceKit::SwiftLangSupport |
tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h |
The Swift-language request dispatcher. |
SourceKit::EditorEditingDoc |
tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp |
An open file's parsing/typecheck state. |
How it works
sequenceDiagram
participant Editor as Xcode / sourcekit-lsp
participant SK as sourcekitd
participant Lang as SwiftLangSupport
participant Compiler as in-process Swift compiler
Editor->>SK: request (XPC dict / framed JSON-like)
SK->>Lang: dispatch by request kind
Lang->>Compiler: setup CompilerInstance, run partial compile
Compiler-->>Lang: results (completions, types, edits, ...)
Lang-->>SK: response dict
SK-->>Editor: responseRequest format
Requests and responses are dictionary-based. Encoded as XPC dictionaries on Apple platforms or framed binary blobs elsewhere. sourcekitd-test accepts a textual format (tools/SourceKit/tools/sourcekitd-test/) for invoking requests from CI.
A typical completion request looks like:
{
key.request: source.request.codecomplete,
key.sourcefile: "/path/to/Foo.swift",
key.offset: 128,
key.compilerargs: ["-sdk", "/path/to/SDK", ...]
}The response is a list of key.results, each carrying a key.name, key.typename, key.kind, etc.
Long-running compiler instances
SourceKit keeps CompilerInstance objects alive between requests for the same file. This is critical for performance: re-parsing and re-type-checking a 5,000-line file on every keystroke would be untenable. The instance maintains a cache, and incremental requests reuse it via IDEInspectionCallbacks (see IDE and SourceKit).
Macros
When a request opens a file containing macros, SourceKit launches swift-plugin-server (or reuses an existing one) and forwards macro expansions to it. Failures in the plugin server become sourcekitd errors rather than crashes.
Integration points
sourcekit-lsp(inapple/sourcekit-lsp) is the most prominent consumer. It is a separate process and translates LSP requests intosourcekitdrequests.- Xcode embeds
sourcekitd-inproc.frameworkfor in-process requests. - Tests --
test/SourceKit/and the unit tests intools/SourceKit/tools/sourcekitd-test/exercise individual requests.
Entry points for modification
- Adding a new request: extend
sourcekitd-request-setintools/SourceKit/lib/SwiftLang/, route to the appropriatelib/IDE/orlib/Refactoring/API, document intools/SourceKit/docs/. - Improving completion: live in
lib/IDE/(see IDE and SourceKit). The SourceKit layer is mostly request marshalling.
Related pages
- IDE and SourceKit -- the C++ libraries beneath this service.
- Sema -- the type checker the IDE drives.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.