Setup Swift LSP and Vim Jan 22 2019

With the release of SourceKit-LSP we can run an LSP server for the Swift programming language. That means that we can take advantage of autocompletion and jump to the definition in our text editors that support connection to an LSP server. I'll show how to set this up using Vim 8.

LAST UPDATED: September/25/2019

First install SourceKit-LSP

Clone the source from the SourceKit-LSP repository

$ cd PATH_TO_WHERE_WE_WISH_TO_INSTALL
$ git clone https://github.com/apple/sourcekit-lsp.git

Prerequisites to build soruceKit-LSP

To build SourceKit, you'll need the swift toolchain if you have Xcode updated you should be fine if not you can download the toolchain from https://swift.org/download/#snapshots.

Once that is ready you can build using the swift-build command:

$ cd sourcekit-lsp/
$ swift build

That will build the LSP server on the .build/debug/ directory. If you have an error using the master branch you might want to find a branch that has your same xcode toolchain. For example, I'm using swift-5.1-branch because thats the tool chain I'm currently on. To change branch you can do the following:

1
2
3
4
5
6
# fetch all the branches in git
$ git fetch
$ git checkout swift-5.1-branch
# and now we can build
$ swift build
# If the build is successful continue with the next steps in the article :)

Now we want our editor to find the sourcekit-lsp executable, so we need to fix our path, you can add it to your ~/.bash_profile:

# Add sourcekit-LSP to the path
export PATH="$PATH:PATH_TO_LSP/sourcekit-lsp/.build/debug/"

Once we have verified that sourcekit-lsp is in our path, we need to install two plugins in Vim to use as clients for the LSP server.

Vim configuration

Install the following plugin vim-LSP which depends on Async. I use Vim-Plug as my plugin manager if you use something different check the instructions on how to install plugins using your plugin manager. Using Vim-Plug:

1
2
3
"" Vim LSP related
Plug 'prabirshrestha/async.vim'
Plug 'prabirshrestha/vim-lsp'

Run the :PlugInstall command on Vim. Once it is installed we need to tell Vim to register the server to use Swift:

1
2
3
4
5
6
7
8
" SourceKit-LSP configuration
if executable('sourcekit-lsp')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'sourcekit-lsp',
        \ 'cmd': {server_info->['sourcekit-lsp']},
        \ 'whitelist': ['swift'],
        \ })
endif

Now the server is registered. You can set up any auto-complete tool you prefer to use the LSP server. I use the default Omni Completion, so I have this line in my vimrc configuration:

1
autocmd FileType swift setlocal omnifunc=lsp#complete

Autocomplete is now enabled for files marked with a FileType of swift. I can use to open the pop-up while in insert mode and take advantage of sourcekit-LSP.

Another important feature is jump-to-definition. We can replace the current which uses ctags to call LspDefinition.

1
autocmd FileType swift nnoremap <C-]> :LspDefinition<CR>

Add that line to your vimrc, save and reload your vim configuration. Now when you are in a swift file, you'll be able to use and jump to the definition of the symbol you have the cursor on. Remember that you can use to jump back.

Extra

SourceKit-LSP doesn't support syntax highlight at the moment. In the meantime, I use Vim Swift for syntax highlight and indentation. You can install it using VimPlug, as always add it to your vimrc.

1
2
" Swift
Plug 'keith/swift.vim'

Now your Vim is ready for Swift. Let me know if it helped or if you have any questions.


** If you want to check what else I'm currently doing be sure to follow me on twitter @rderik or subscribe to the newsletter. If you want to send me a direct message you can send it to derik@rderik.com.