Swift on VS Code

The Swift programming language can be setup with VS Code with most IDE features Xcode has like code completion, static analysis, indexing and debugging on Linux.

Requirements:

  1. Install Swift on your system and make sure it is part of your path. For ARM64 use swift-arm64 repository and for x86_64 download from Swift.org.
  2. Install VS Code from the official repository or code-server to remotely run VS Code in the browser.
  3. Install CodeLLDB and SourceKit-LSP extensions. If running a VS Code fork like code-server then just download the extension and install the VSIX file.
  4. Create configurations in .vscode folder to build and debug the target. This can vary based on the package’s executables and libraries. The following is an example for Foo package.

.vscode/tasks.json

{
    "version": "2.0.0",
    "tasks": [
        // compile your SPM project
        {
            "label": "swift-build",
            "type": "shell",
            "command": "swift build"
        },
        // compile your SPM tests
        {
            "label": "swift-build-tests",
            "type": "process",
            "command": "swift",
            "group": "build",
            "args": [
                "build",
                "--build-tests"
            ]
        }
    ]
}

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "program": "${workspaceFolder}/.build/debug/FooTool",
            "args": [],
            "cwd": "${workspaceFolder}",
            "preLaunchTask": "swift-build"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Test",
            "program": "./.build/debug/FooPackageTests.xctest",
            "preLaunchTask": "swift-build-tests"
        }
    ]
}