lsp-mode: 30+ languages in one minor mode

George Pittarelli

Follow along

These slides are generated from an org file with some speaker notes and code blocks

Available at https://gjp.cc/lsp

wget gjp.cc/lsp-mode.org
# or
git clone github.com/gpittarelli/lsp-presentation

What do we want

IDE features:

  • auto-complete
  • documentation on hover
  • jump to definition
  • refactoring menu
  • error detection
  • project-wide diagnostics

Implement it per-language

  • R: ESS
  • Clojure: CIDER
  • Typescript: tsserver and tide-mode
  • C#: omnisharp and omnisharp-emacs

Attempts to generalize

  • textmate files: syntax highlighting
  • tags files (etags, ctags); GNU global
  • ycmd YouCompleteMe

LSP: Language Server Protocol

Implement language diagnostics once, as a server

All editors can share the smarts

lsp-grid.png

Overview: https://langserver.org/

Spec: https://microsoft.github.io/language-server-protocol/specification

LSP: Overview

LSP: Overview (2)

  • Synchronize file/project state
  • Query for specific information on hover/completion
  • Send commands for refactors; get back text changes to apply

lsp-mode

Minor mode:

  • implements an LSP client
  • hooks into standard Emacs APIs
  • provides API for defining clients

Available on MELPA:

(require 'package)
(package-initialize)
(add-to-list 'package-archives
  '("melpa" . "https://melpa.org/packages/") t)
(package-install 'lsp-mode)

lsp-mode provides

How to define a client

(lsp-define-stdio-client
 lsp-css
 "css"
 lsp-css--get-root
 '("css-languageserver" "--stdio"))

language packages

(package-install 'lsp-css)

Example setup

(defun my-rust-mode-setup ()
  (company-mode)
  (lsp-rust-enable)
  (eldoc-mode t)
  (flycheck-mode)
  (lsp-ui-mode))
(add-hook 'rust-mode-hook #'my-rust-mode-setup)
(add-to-list 'auto-mode-alist '("\\.rs" . rust-mode))

Testing

Protocol parsing in emacs

  • frankly, hacky (manual dechunking)
  • process filters and lots of manual parsing
  • alternative: just use buffers

Future goal: autosetup

Based on major mode and/or file extension, we should be able to automatically:

  • download the server
  • install the necessary lsp-plugins
  • provide good defaults

Alternatives

Note there's a couple defunct "emacs-lsp", etc. packages on GitHub from previous attempts by other people.

eglot is the only reasonably working alternative