VSCode is a cross-platform text and source code editor from Microsoft, and among the first tools to support Language Server Protocol. 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.

swift

4.2

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.

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.

{% warning %}

Swift support for Language Server Protocol is still in early development
and doesn't currently offer pre-built packages for
the language server (sourcekit-lsp)
or the Visual Studio Code extension.
For now,
downloading, building, and installing these components and their dependencies
is a manual process that requires some familiarity with the command line
(it should take about 15 minutes on a reasonably fast internet connection).

When LSP support is made generally available,
it will be much easier to get everything set up.

{% endwarning %}

{% asset vscode-banner.png %}

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.

{% asset xcode-select-window.png %}

Click the "Get Xcode" button
and continue installation on the App Store.

Electron apps
have a reputation for being big and slow,
but don't let that stop you from giving VSCode a try ---
its performance and memory footprint are comparable to a native app.

{% endinfo %}

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 --version
6.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.

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.

{% asset vscode-swift-lsp-screenshot.png %}

{% info %}

To get the full experience of working with Swift from VSCode,
we recommend that you also check out
the Swift Language extension
(for syntax highlighting until LSP supports this functionality)
and the Swift Lint extension
(for real-time style and convention diagnostics).

{% endinfo %}

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.