Skip to content

Latest commit

 

History

History

Editors

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Editor Integration

This document contains information about how to configure an editor to use SourceKit-LSP. If your editor is not listed below, but it supports the Language Server Protocol (LSP), see Other Editors.

In general, you will need to know where to find the sourcekit-lsp server executable. Some examples:

  • With Xcode 11.4+
    • xcrun sourcekit-lsp - run the server
    • xcrun --find sourcekit-lsp - get the full path to the server
  • Toolchain from Swift.org
    • Linux
      • You will find sourcekit-lsp in the bin directory of the toolchain.
    • macOS
      • xcrun --toolchain swift sourcekit-lsp - run the server
      • xcrun --toolchain swift --find sourcekit-lsp - get the full path to the server
  • Built from source
    • .build/<platform>/<configuration>/sourcekit-lsp

Visual Studio Code

The Swift for Visual Studio Code extension uses SourceKit-LSP for code completion, jump to definition and error annotations. Install the extension from the marketplace to add it to your VSCode environment.

Sublime Text

Before using SourceKit-LSP with Sublime Text, you will need to install the LSP, LSP-SourceKit and Swift-Next packages from Package Control. Then toggle the server on by typing in command palette LSP: Enable Language Server Globally or LSP: Enable Language Server in Project.

Emacs

There is an Emacs client for SourceKit-LSP in the main Emacs LSP repository.

Vim 8 or Neovim

All methods below assume sourcekit-lsp is in your PATH. If it's not then replace sourcekit-lsp with the absolute path to the sourcekit-lsp executable.

vim-lsp

Install vim-lsp. In your .vimrc, configure vim-lsp to use sourcekit-lsp for Swift source files like so:

if executable('sourcekit-lsp')
    au User lsp_setup call lsp#register_server({
        \ 'name': 'sourcekit-lsp',
        \ 'cmd': {server_info->['sourcekit-lsp']},
        \ 'whitelist': ['swift'],
        \ })
endif

In order for vim to recognize Swift files, you need to configure the filetype. Otherwise, :LspStatus will show that sourcekit-lsp is not running even if a Swift file is open.

If you are already using a Swift plugin for vim, like swift.vim, this may be setup already. Otherwise, you can set the filetype manually:

augroup filetype
  au! BufRead,BufNewFile *.swift set ft=swift
augroup END

That's it! As a test, open a swift file, put cursor on top of a symbol in normal mode and run :LspDefinition. More commands are documented here.

There are many Vim solutions for code completion. For instance, you may want to use LSP for omnifunc:

autocmd FileType swift setlocal omnifunc=lsp#complete

With this added in .vimrc, you can use <c-x><c-o> in insert mode to trigger sourcekit-lsp completion.

coc.nvim

With coc.nvim installed, the easiest is to use the coc-sourcekit plugin:

:CocInstall coc-sourcekit

Alternatively open your coc config (:CocConfig in vim) and add:

  "languageserver": {
    "sourcekit-lsp": {
      "filetypes": ["swift"],
      "command": "sourcekit-lsp",
    }
  }

As a test, open a Swift file, put the cursor on top of a symbol in normal mode and run:

:call CocAction('jumpDefinition')

Neovim 0.8 and above

since version 0.8, neovim has native LSP support, which can be used to connect to sourcekit-lsp directly. To do so, add the following to a .lua config file (such as lua/swift.lua):

require'lspconfig'.sourcekit.setup{
  cmd = {'$TOOLCHAIN_PATH/usr/bin/sourcekit-lsp'}
}

where $TOOLCHAIN_PATH is the path to your active toolchain (for example, /Library/Developer/Toolchains/swift-latest.xctoolchain). This should enable the lsp server directly, and you can test it by opening a swift file and running :LspInfo--you should get a window popping up saying "1 client attached to this buffer" and be able to do navigation and such.

The default LSP commands are not bound to many keys, so it is also useful to create some keybindings to help with various LSP activities. Here are some of the known lsp commands that work with sourcekit-lsp:

vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    --enable omnifunc completion
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    -- buffer local mappings
    local opts = { buffer = ev.buf }
    -- go to definition
    vim.keymap.set('n','gd',vim.lsp.buf.definition,opts)
    --puts doc header info into a float page
    vim.keymap.set('n','K',vim.lsp.buf.hover,opts)

    -- workspace management. Necessary for multi-module projects
    vim.keymap.set('n','<space>wa',vim.lsp.buf.add_workspace_folder, opts)
    vim.keymap.set('n','<space>wr',vim.lsp.buf.remove_workspace_folder, opts)
    vim.keymap.set('n','<space>wl',function()
            print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end,opts)

    -- add LSP code actions
    vim.keymap.set({'n','v'},'<space>ca',vim.lsp.buf.code_action,opts)                

    -- find references of a type
    vim.keymap.set('n','gr',vim.lsp.buf.references,opts)
  end,
})

Further information on neovim's LSP integration(including detailed information on configuration) can be found in neovim's documentation.

Theia Cloud IDE

You can use SourceKit-LSP with Theia by using the theiaide/theia-swift image. To use the image you need to have Docker installed first.

The following command pulls the image and runs Theia IDE on http://localhost:3000 with the current directory as a workspace.

docker run -it -p 3000:3000 -v "$(pwd):/home/project:cached" theiaide/theia-swift:next

You can pass additional arguments to Theia after the image name, for example to enable debugging:

docker run -it -p 3000:3000 --expose 9229 -p 9229:9229 -v "$(pwd):/home/project:cached" theiaide/theia-swift:next --inspect=0.0.0.0:9229

Image Variants

theiaide/theia-swift:latest This image is based on the latest stable released version.

theiaide/theia-swift:next This image is based on the nightly published version.

theia-swift-docker source theia-apps

BBEdit

Support for LSP is built in to BBEdit 14.0 and later.

If sourcekit-lsp is in your $PATH or is discoverable by using xcrun --find sourcekit-lsp, BBEdit will use it automatically. Otherwise you can manually configure BBEdit to use a suitable sourcekit-lsp as needed.

You can read more about BBEdit's LSP support and configuration hints here.

Other Editors

SourceKit-LSP should work with any editor that supports the Language Server Protocol (LSP). Each editor has its own mechanism for configuring an LSP server, so consult your editor's documentation for the specifics. In general, you can configure your editor to use SourceKit-LSP for Swift, C, C++, Objective-C and Objective-C++ files; the editor will need to be configured to find the sourcekit-lsp executable (see the top-level README for build instructions), which expects to communicate with the editor over stdin and stdout.