docs(swift): add a Swift language page (#6791)

Adds a Swift language page (en, zh) covering toolchain setup, how the sourcekit-lsp workspace root is found, SwiftPM packages and Xcode projects through a BSP config, choosing an Xcode with DEVELOPER_DIR, formatting, single-file runs, debugging with lldb-dap, and current limitations.

Closes #6789

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-29 14:38:45 +08:00
1 джерело 3811dfb512
коміт f2641b9697
5 files changed
+206 -2

No files matched your search

+2
Переглянути файл
@@ -138,6 +138,7 @@
"en/languages/cpp",
"en/languages/csharp",
"en/languages/rust",
"en/languages/swift",
"en/languages/sql"
]
},
@@ -367,6 +368,7 @@
"zh/languages/cpp",
"zh/languages/csharp",
"zh/languages/rust",
"zh/languages/swift",
"zh/languages/sql"
]
},
+1 -1
Переглянути файл
@@ -184,7 +184,7 @@ The table lists every built-in language definition. Server and debugger names in
Notes:
- **C, C++, Objective-C, Swift**: `lldb-dap` comes with LLVM or Xcode. C and C++ can also be debugged with **CodeLLDB** or GDB 14+; see [C and C++](/en/languages/cpp).
- **C, C++, Objective-C, Swift**: `lldb-dap` comes with LLVM or Xcode. C and C++ can also be debugged with **CodeLLDB** or GDB 14+; see [C and C++](/en/languages/cpp). For Swift and Xcode projects, see [Swift](/en/languages/swift).
- **Ruby**: `rdbg` comes from the `debug` gem.
- **GDScript**: the language server and debugger are the Godot editor itself; see [Godot](/en/languages/godot).
- **Arduino**: needs Arduino IDE 2 (Arduino Language Server, Arduino CLI and clangd).
+101
Переглянути файл
@@ -0,0 +1,101 @@
---
title: "Swift"
description: "Swift code intelligence with sourcekit-lsp for SwiftPM and Xcode projects, formatting, single-file runs and debugging with lldb-dap."
---
OpenIDEA supports Swift through [sourcekit-lsp](https://github.com/swiftlang/sourcekit-lsp), the language server that ships with the Swift toolchain. Highlighting, folding and the outline work without any setup. Completion, navigation, diagnostics, refactorings and formatting come from sourcekit-lsp. For the other languages, see the [support matrix](/en/languages/index).
| Language | Extensions | Language server | Debugger |
| -------- | --------------------------- | --------------- | -------- |
| Swift | `.swift`, `.swiftinterface` | sourcekit-lsp | lldb-dap |
## Setup
The IDE does not install Swift tools. Install a toolchain, which brings `swift`, `sourcekit-lsp` and `lldb-dap`:
- **macOS**: install Xcode, or only the Command Line Tools (`xcode-select --install`). Xcode is needed for iOS, macOS app and other Apple platform projects.
- **Linux and Windows**: install a toolchain from [swift.org](https://www.swift.org/install).
When `sourcekit-lsp` is not found, opening a Swift file shows an install hint instead of an **Install** button. On macOS, `/usr/bin/sourcekit-lsp` runs the copy from the Xcode chosen with `xcode-select` (or `DEVELOPER_DIR`, see [Several Xcode versions](#several-xcode-versions)). To use another binary, set **Executable** for sourcekit-lsp in **Settings › Languages & Frameworks › Language Servers**.
## Language server
sourcekit-lsp finds the workspace root by looking up from the file for one of these files: `Package.swift`, `buildServer.json`, `compile_commands.json` or `.sourcekit-lsp`. The outermost folder within the project that has one becomes the root; without any, the project folder is the root.
sourcekit-lsp then reads the build settings for that root:
- **SwiftPM**: a root with `Package.swift` works without setup. sourcekit-lsp resolves the package and its dependencies itself.
- **Build server**: a `.bsp/*.json` file or a `buildServer.json` in the root points sourcekit-lsp at a Build Server Protocol server. Xcode projects use this (see [Xcode projects](#xcode-projects)).
- **`compile_commands.json`**: a compilation database, for example from CMake.
`.sourcekit-lsp/config.json` holds sourcekit-lsp's own options; see the [sourcekit-lsp configuration reference](https://github.com/swiftlang/sourcekit-lsp/blob/main/Documentation/Configuration%20File.md).
### Xcode projects
sourcekit-lsp cannot read `.xcodeproj` or `.xcworkspace` files. Without a build server, files in an Xcode project get no build settings, and you see errors such as "No such module". Generate a `buildServer.json` in the project root with one of these tools:
- **[xcode-build-server](https://github.com/SolaWing/xcode-build-server)**: `brew install xcode-build-server`, then in the project root:
```bash
xcode-build-server config -workspace App.xcworkspace -scheme App
```
Use `-project App.xcodeproj` instead of `-workspace` for a project without a workspace. xcode-build-server takes the compile flags from Xcode's build logs, so build the scheme once in Xcode or with `xcodebuild` first, and build again after adding files or changing build settings.
- **[sourcekit-xcode-bsp](https://github.com/slime-studio/sourcekit-xcode-bsp)**: needs Xcode 26 or newer. Install it with `brew install slime-studio/tap/sourcekit-xcode-bsp` and run `sourcekit-xcode-bsp init` in the project root; it finds the `.xcworkspace` or `.xcodeproj` itself and targets the iOS Simulator by default (the `platform` field in `buildServer.json`). It is an early release (0.1.x).
Open the folder that contains `buildServer.json` (or `.bsp/`) as the project, or a parent of it with no other root marker between, so that it is the workspace root. After generating the file, reopen the project so that sourcekit-lsp starts with it.
<Tip>`buildServer.json` holds absolute paths for your machine. Add it to `.gitignore` instead of committing it.</Tip>
### Several Xcode versions
To run sourcekit-lsp from a specific Xcode, set `DEVELOPER_DIR` in the server's environment: in **Settings › Languages & Frameworks › Language Servers**, select sourcekit-lsp and add it under **Startup › Environment variables**, or in `settings.toml`:
```toml
[languageServers.sourcekit-lsp]
env = { DEVELOPER_DIR = "/Applications/Xcode-beta.app/Contents/Developer" }
```
Set it per project to use a different Xcode in each. It applies to sourcekit-lsp only; to build or run with the same Xcode, set `DEVELOPER_DIR` in the run configuration's environment variables too.
## Formatting
**Reformat Code** (`⌥⌘L` / `Ctrl+Alt+L`) uses sourcekit-lsp, which formats with [swift-format](https://github.com/swiftlang/swift-format) from the same toolchain. swift-format reads its options from a `.swift-format` file in the file's folder or a parent folder.
## Run and debug
### Single files
A `.swift` file can be run with **Run Context Configuration** (`⌃⇧R` / `Ctrl+Shift+F10`). The IDE runs `swift <file>` with the `swift` on your `PATH`.
### SwiftPM packages
There are no SwiftPM run or test configurations yet. Run `swift build`, `swift run` and `swift test` in the terminal, or create a **Shell Script** configuration for them (see [Run configurations](/en/guide/run)).
### Debugging
Debugging uses lldb-dap. On macOS the IDE finds Xcode's copy through `xcrun -f lldb-dap`; elsewhere it comes with the Swift toolchain on `PATH`. To debug a SwiftPM executable:
<Steps>
<Step title="Build it">
Run `swift build`. The executable is written to `.build/debug/<product>`.
</Step>
<Step title="Create a Program configuration">
In **Run › Edit Configurations…**, add a **Program** configuration with **Executable** set to
`.build/debug/<product>` and **Debug adapter** set to **lldb-dap (C, C++, Swift)**.
</Step>
<Step title="Debug">
Start it with **Debug** (`⌃D` / `Shift+F9`).
</Step>
</Steps>
Rebuild with `swift build` after editing, as the configuration does not build first. **Attach to Process…** also attaches to a running Swift program with lldb-dap when CodeLLDB is not installed. See [Debugging](/en/guide/debugging) for breakpoints, stepping and the variables view.
## Limitations
- There are no run, test or debug configurations for SwiftPM targets or Xcode schemes, and no test tree for XCTest or Swift Testing.
- Xcode projects need a `buildServer.json` that you generate yourself (see [Xcode projects](#xcode-projects)).
- **Expand Macro** does nothing: sourcekit-lsp shows the expansion in a new document, which the IDE does not open yet.
- There is no SwiftUI Preview, Interface Builder, asset catalog editor or simulator integration. Use Xcode for those.
- sourcekit-lsp and lldb-dap are not installed by the IDE.
+1 -1
Переглянути файл
@@ -179,7 +179,7 @@ IDE 安装工具从不需要 `sudo`。工具装在本地数据目录下 IDE 自
说明:
- **C、C++、Objective-C、Swift**:`lldb-dap` 随 LLVM 或 Xcode 提供。C 和 C++ 也可以用 **CodeLLDB** 或 GDB 14 及以上版本调试,见 [C 与 C++](/zh/languages/cpp)。
- **C、C++、Objective-C、Swift**:`lldb-dap` 随 LLVM 或 Xcode 提供。C 和 C++ 也可以用 **CodeLLDB** 或 GDB 14 及以上版本调试,见 [C 与 C++](/zh/languages/cpp)。Swift 和 Xcode 项目见 [Swift](/zh/languages/swift)。
- **Ruby**:`rdbg` 来自 `debug` gem。
- **GDScript**:语言服务器和调试器就是 Godot 编辑器本身,见 [Godot](/zh/languages/godot)。
- **Arduino**:需要 Arduino IDE 2(包含 Arduino Language Server、Arduino CLI 和 clangd)。
+101
Переглянути файл
@@ -0,0 +1,101 @@
---
title: "Swift"
description: "基于 sourcekit-lsp 的 Swift 代码智能(SwiftPM 与 Xcode 项目),代码格式化,单文件运行,以及用 lldb-dap 调试。"
---
OpenIDEA 通过 Swift 工具链自带的语言服务器 [sourcekit-lsp](https://github.com/swiftlang/sourcekit-lsp) 支持 Swift。语法高亮、折叠和结构视图无需任何配置即可使用;补全、导航、诊断、重构和格式化由 sourcekit-lsp 提供。其他语言见[语言支持总览](/zh/languages/index)。
| 语言 | 扩展名 | 语言服务器 | 调试器 |
| ----- | --------------------------- | ------------- | -------- |
| Swift | `.swift`、`.swiftinterface` | sourcekit-lsp | lldb-dap |
## 安装
IDE 不安装 Swift 相关工具。安装一个工具链即可,其中包含 `swift`、`sourcekit-lsp` 和 `lldb-dap`:
- **macOS**:安装 Xcode,或只安装命令行工具(`xcode-select --install`)。iOS、macOS 应用等 Apple 平台项目需要完整的 Xcode。
- **Linux 和 Windows**:从 [swift.org](https://www.swift.org/install) 安装工具链。
找不到 `sourcekit-lsp` 时,打开 Swift 文件会显示安装提示,没有 **安装** 按钮。在 macOS 上,`/usr/bin/sourcekit-lsp` 运行的是 `xcode-select`(或 `DEVELOPER_DIR`,见[多个 Xcode 版本](#多个-xcode-版本))所选 Xcode 中的副本。如需使用其他可执行文件,在 **设置 › 语言和框架 › 语言服务器** 中为 sourcekit-lsp 设置 **可执行文件**。
## 语言服务器
sourcekit-lsp 从当前文件向上查找以下文件来确定工作区根目录:`Package.swift`、`buildServer.json`、`compile_commands.json`、`.sourcekit-lsp`。项目内含有其中之一的最外层文件夹就是根目录;都没有时,项目文件夹就是根目录。
之后 sourcekit-lsp 按根目录读取构建设置:
- **SwiftPM**:根目录有 `Package.swift` 时无需配置,sourcekit-lsp 会自己解析包及其依赖。
- **构建服务器**:根目录中的 `.bsp/*.json` 或 `buildServer.json` 让 sourcekit-lsp 连接一个 Build Server Protocol 服务器。Xcode 项目就用这种方式(见 [Xcode 项目](#xcode-项目))。
- **`compile_commands.json`**:编译数据库,例如由 CMake 生成。
`.sourcekit-lsp/config.json` 存放 sourcekit-lsp 自己的选项,见 [sourcekit-lsp 配置文件说明](https://github.com/swiftlang/sourcekit-lsp/blob/main/Documentation/Configuration%20File.md)。
### Xcode 项目
sourcekit-lsp 读不了 `.xcodeproj` 和 `.xcworkspace`。没有构建服务器时,Xcode 项目里的文件拿不到构建设置,会出现“No such module”之类的错误。用下面任一工具在项目根目录生成 `buildServer.json`:
- **[xcode-build-server](https://github.com/SolaWing/xcode-build-server)**:`brew install xcode-build-server`,然后在项目根目录执行:
```bash
xcode-build-server config -workspace App.xcworkspace -scheme App
```
没有 workspace 的项目把 `-workspace` 换成 `-project App.xcodeproj`。xcode-build-server 从 Xcode 的构建日志中读取编译参数,所以要先在 Xcode 里或用 `xcodebuild` 构建一次该 scheme;新增文件或修改构建设置后也要重新构建。
- **[sourcekit-xcode-bsp](https://github.com/slime-studio/sourcekit-xcode-bsp)**:需要 Xcode 26 及以上版本。用 `brew install slime-studio/tap/sourcekit-xcode-bsp` 安装,然后在项目根目录执行 `sourcekit-xcode-bsp init`;它会自己找到 `.xcworkspace` 或 `.xcodeproj`,默认面向 iOS 模拟器(即 `buildServer.json` 中的 `platform` 字段)。它还处于早期版本(0.1.x)。
把包含 `buildServer.json`(或 `.bsp/`)的文件夹作为项目打开,或打开它的上级文件夹且中间没有其他根目录标记文件,这样它才会成为工作区根目录。生成文件后重新打开项目,让 sourcekit-lsp 随之启动。
<Tip>`buildServer.json` 里是本机的绝对路径。把它加入 `.gitignore`,不要提交。</Tip>
### 多个 Xcode 版本
要让 sourcekit-lsp 使用某个特定的 Xcode,在服务器的环境中设置 `DEVELOPER_DIR`:在 **设置 › 语言和框架 › 语言服务器** 中选中 sourcekit-lsp,在 **启动 › 环境变量** 中添加;或者写在 `settings.toml` 中:
```toml
[languageServers.sourcekit-lsp]
env = { DEVELOPER_DIR = "/Applications/Xcode-beta.app/Contents/Developer" }
```
按项目设置即可让每个项目使用不同的 Xcode。它只对 sourcekit-lsp 生效;要用同一个 Xcode 构建或运行,在运行配置的环境变量里也设置 `DEVELOPER_DIR`。
## 格式化
**重新格式化代码**(`⌥⌘L` / `Ctrl+Alt+L`)交给 sourcekit-lsp,它使用同一工具链中的 [swift-format](https://github.com/swiftlang/swift-format) 格式化。swift-format 从文件所在文件夹或上级文件夹中的 `.swift-format` 文件读取选项。
## 运行和调试
### 单个文件
`.swift` 文件可以用 **运行上下文配置**(`⌃⇧R` / `Ctrl+Shift+F10`)运行。IDE 用 `PATH` 上的 `swift` 执行 `swift <文件>`。
### SwiftPM 包
目前还没有 SwiftPM 的运行或测试配置。在终端里执行 `swift build`、`swift run` 和 `swift test`,或者为它们新建 **Shell 脚本** 配置(见[运行配置](/zh/guide/run))。
### 调试
调试使用 lldb-dap。在 macOS 上,IDE 通过 `xcrun -f lldb-dap` 找到 Xcode 中的副本;其他平台使用 `PATH` 上 Swift 工具链自带的副本。调试一个 SwiftPM 可执行程序:
<Steps>
<Step title="构建">
执行 `swift build`,可执行文件会写到 `.build/debug/<product>`。
</Step>
<Step title="新建程序配置">
在 **运行 › 编辑配置…** 中添加一个 **程序** 配置,把 **可执行文件** 设为 `.build/debug/<product>`,**调试适配器** 设为
**lldb-dap (C, C++, Swift)**。
</Step>
<Step title="调试">
用 **调试**(`⌃D` / `Shift+F9`)启动。
</Step>
</Steps>
这个配置不会先构建,修改代码后要重新执行 `swift build`。未安装 CodeLLDB 时,**附加到进程…** 也会用 lldb-dap 附加到正在运行的 Swift 程序。断点、单步和变量视图见[调试](/zh/guide/debugging)。
## 限制
- 还没有针对 SwiftPM 目标或 Xcode scheme 的运行、测试和调试配置,也没有 XCTest 或 Swift Testing 的测试树。
- Xcode 项目需要你自己生成 `buildServer.json`(见 [Xcode 项目](#xcode-项目))。
- **Expand Macro** 不起作用:sourcekit-lsp 会在新文档中显示展开结果,而 IDE 目前还不会打开它。
- 没有 SwiftUI 预览、Interface Builder、资源目录编辑器和模拟器集成,这些请使用 Xcode。
- sourcekit-lsp 和 lldb-dap 不由 IDE 安装。