Open2

Swift with VSCode

saharasahara

Swift Package Manager

Generate a package

swift package init ${module name}

If type is not specified in --type option, then the type of generated package is library.

Example

Poker % swift package init
Creating library package: Poker
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/Poker/Poker.swift
Creating Tests/
Creating Tests/PokerTests/
Creating Tests/PokerTests/PokerTests.swiftPoker % swift package init
Creating library package: Poker
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/Poker/Poker.swift
Creating Tests/
Creating Tests/PokerTests/
Creating Tests/PokerTests/PokerTests.swift

Test

Run swift test in package root.
Use --filter option to specify test file or test case.

saharasahara

VSCode

Prerequisite

Node.js is installed in advance.

Sourcekit-lsp Extension for VSCode

To see a guide, see below (sorry, Japanese only)

https://qiita.com/TeaRainbow7/items/b7bee4095e511ef36547

% cd somefolder
% git clone https://github.com/apple/sourcekit-lsp.git
...
% cd sourcekit-lsp/Editors/vscode
% npm run createDevPackage
% code --install-extension out/sourcekit-lsp-vscode-dev.vsix
...
Extension 'sourcekit-lsp-vscode-dev.vsix' was successfully installed.

And set the path of the sourcekit-lsp executable in vscode setting view.
Find the path using the command below:

% xcrun --find sourcekit-lsp
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp

sourcekit-lsp-extension-setting-executable-path

Official Page

https://github.com/apple/sourcekit-lsp/blob/main/Editors/vscode/README.md

swift-format

Install formatter. As a swift formatter, nicklockwood/SwiftFormat is famous. These days apple/swift-format is developed. In this example, use latter.

https://github.com/apple/swift-format

If you want install locally, edit Package.swift as below:

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "Komondor",
    products: [ ... ],
    dependencies: [
        // My dependencies
        .package(url: "https://github.com/orta/PackageConfig.git", from: "0.0.1"),
        // Dev deps
        .package(url: "https://github.com/orta/Komondor.git", from: "0.0.1"),
+        .package(url: "https://github.com/apple/swift-format.git", .branch("swift-5.3-branch")),
    ],
    targets: [...]
)

Or to install globally, use mint:

% mint install apple/swift-format
🌱 Finding latest version of swift-format
🌱 Cloning swift-format 0.50400.0
🌱 Resolving package
🌱 Building package
🌱 Installed swift-format 0.50400.0
🌱 Linked swift-format 0.50400.0 to /usr/local/bin

VSCode extension

Swift Community has the vscode extension for apple/swift-format. That is vknabel/vscode-swiftformat.

https://github.com/vknabel/vscode-apple-swift-format