Visual Studio Code (VSCode) is a cross-platform open source and text editor from Microsoft. It’s one of the coolest open source projects out there today, with regular updates from hundreds of contributors. VSCode is one of the first tools to support Language Server Protocol (LSP), having played a key role in providing a great developer experience in multiple languages and technologies.
This week, I’ll show you 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 of Xcode, or are already a VSCode user and are completely new to the language, then this article will tell you everything you need to know.
Step 0: Install Xcode
If you don’t have Xcode installed on your machine, open the Terminal app and run the following command:
$ xcode-select --install
Running this command will bring up a system prompt.
Click the “Get Xcode” button and continue to install on the App Store.
You can verify that everything is working as expected by running the command sourcekit-lsp:
$ xcrun sourcekit-lsp
This command starts a new language server process, but don’t worry if it doesn’t give any response to STDOUT – it means it’s working as expected. Exit the process using ETX signals (Control (^) C).
Step 1: Install Visual Studio Code
Download Visual Studio Code and install it to your system’s Applications folder. Open the app and follow the instructions to launch it from the command line. You need a code command accessible from $ PATH to install the SourceKit-LSP extension later.
Step 2: Install Node and NPM
brew install node
To verify that you have the installation working, run the following command:
$ npm --version
Step 3: Build and install the SourceKit-LSP extension for Visual Studio Code
From the command line, copy the repository “sourcekit-lsp”: https://github.com/apple/sourcekit-lsp and navigate to Editors / vscode in the output directory. Use npm to create the extension and then use code to install it:
$ 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
Now reopen VSCode and open a Swift project, such as “this project”: https://github.com/flight-school/money and test Language Server Protocol support for Swift.
So there you have it – creating the best Swift development experience outside of Xcode.