Swift development in Emacs


Install the Swift toolchain

You need to have the Swift toolchain installed. This is typcially bundled inside of xcode. To find the location of the Swift LSP server, run the following command:

$ xcrun --find sourcekit-lsp
/Library/Developer/CommandLineTools/usr/bin/sourcekit-lsp

Install Swift mode

Install swift-mode to make Emacs Swift aware.

Tell Emacs to use it

To try this out manually, open .swift file and invoke:

M-x eglot

When asked which LSP server to run, type in the one you found above:

/Library/Developer/CommandLineTools/usr/bin/sourcekit-lsp

Emacs, or sourcekit-lsp needs a minute (on my machine, this was literally a minute) to wire up things, eventually, you'll see that Emacs springs to life again, with inline code hints, auto completion and other coding goodness.

To make this permant, add the following to your ~/.emacs:

(use-package swift-mode
  :ensure t
  :init
  (add-to-list
   'eglot-server-programs
   '(swift-mode . ("/Library/Developer/CommandLineTools/usr/bin/sourcekit-lsp")))
  )

Happy coding!


Licensed under CC BY Creative Commons License ~ ✉ torstein.k.johansen @ gmail ~ 🐘 @skybert@hachyderm.io ~ 🎥 youtube.com/@skybert