Visual Studio Code (VSCode) is a cross-platform text and source code editor from Microsoft. It’s one of the most exciting open source projects today, with regular updates from hundreds of contributors. VSCode was among the first tools to support Language Server Protocol (LSP), which has played a large part in providing a great developer experience, in a variety of languages and technologies.
With the previously announced support for LSP for Swift now available in early development, it’s a great time to see how this integration works for yourself.
So this week, we’ll walk through the process of how to get started with Swift’s new Language Server Protocol support in Visual Studio Code on macOS. If you haven’t tried writing Swift outside Xcode, or are already a VSCode user and new to the language entirely, this article will tell you everything you need to know.
Step 0: Install Xcode
If you don’t already have Xcode installed on your machine, open the Terminal app and run the following command:
$ xcode-select --install
Running this command presents a system prompt.
Click the “Get Xcode” button and continue installation on the App Store.
Step 1: Install Visual Studio Code
Download Visual Studio Code
and install it to your system Applications folder.
Open the app and
follow the instructions for launching from the command line.
You’ll need to have the code
command accessible from $PATH
in order to install the SourceKit-LSP extension later on.
Step 2: Install the Latest Swift Toolchain
Go to Swift.org and download the latest trunk development snapshot (at the time of writing, this was from November 16th, 2018). Once it’s finished downloading, run the package to install the Xcode toolchain. To enable it, open Xcode, select the “Xcode > Preferences…” menu item (⌘,), navigate to Components and choose Swift Development Snapshot.
Step 3: Install Node and NPM
VSCode extensions are written in JavaScript / TypeScript.
If you’re not already set up for JS development,
you can download Node (a JavaScript run-time for outside the browser)
and npm (a package manager for Node)
with Homebrew using the following commands
or manually by following these instructions:
$ brew install node
To verify that you have a working installation, run the following command:
$ npm --version6.4.1
Step 4: Build and Install SourceKit-LSP
With all of the dependencies taken care of, we’re now ready for the main attraction. From the command line, clone the sourcekit-lsp repository, navigate to the resulting directory, and build the Swift project.
$ git clone https://github.com/apple/sourcekit-lsp.git
$cd sourcekit-lsp
$ swift build
If successful,
the completed binary will be available from
of the hidden .build/debug
directory.
Move that binary to a standard directory in your $PATH
,
like /usr/local/bin
or /usr/bin
.
$mv .build/debug/sourcekit-lsp /usr/local/bin
You can verify that everything is working as expected
by running the sourcekit-lsp
command:
$ sourcekit-lsp
This command launches a new language server process,
but don’t worry if it doesn’t provide any feedback to STDOUT
—
that means it’s working as intended.
Exit the process with an ETX signal (^C).
Step 5: Build and Install SourceKit-LSP Extension for Visual Studio Code
Now that you have the Swift language server available, the final step is to build and install the extension that allows Visual Studio Code to communicate with it.
From the sourcekit-lsp
directory in the previous step,
navigate to the Editors/vscode
directory,
use npm
to build the extension
and then use the code
command to install it:
$cd Editors/vscode/
$ npm run createDevPackage
$ code --install-extension out/sourcekit-lsp-vscode-dev.vsix
Now launch (or relaunch) VSCode and open a Swift project, such as this one, and enjoy an early preview of the functionality provided by Language Server Protocol support for Swift.
So there you have it — the makings of a first-class Swift development experience outside of Xcode. For now, Swift support for Language Server Protocol is limited to code completion, quick help, diagnostics, jumping to symbol definitions, and finding references. But we couldn’t be more excited for the future of this project and what it means for the prospects of the Swift language outside the Apple ecosystem.