Install Elixir LS Lexical for Nvim


Here's how to install the best Elixir language server, lexical, for nvim.

Frankly, the first part of this article is not needed by most, because there's already a great installation guide for lexical.

I like keeping a record of important things that I've done in my dev experience, so this is for me. But it is essentially the same as the official guide.

Prereqs

elixir/erlang

Of course, make sure to install erlang and elixir.

gitlfs

Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.

xdg-open https://git-lfs.com/
# download package
tar xvf git-lfs-linux-amd64-v3.6.1.tar.gz
cd git-lfs-3.6.1
./install.sh
git lfs install
# retry if fails with: git lfs install --force

Ensure a successful install message appears.

Lexical

mkdir -p ~/bin
cd ~/bin
git clone git@github.com:lexical-lsp/lexical.git
cd lexical
mix deps.get
mix package

Connect to Neovim

Somewhere in your custom lua config for nvim, add something like this:

local common = require('jt.lsp.common')
local lspconfig = require('lspconfig')

lspconfig.lexical.setup {
  on_attach = common.on_attach,
  capabilities = common.capabilities,
  cmd = { "/home/jaketrent/bin/lexical/_build/dev/package/lexical/bin/start_lexical.sh" },
  root_dir = function(fname)
    return lspconfig.util.root_pattern("mix.exs", ".git")(fname) or vim.loop.cwd()
  end,
  filetypes = { "elixir", "eelixir", "heex" },
  settings = {}
}

Note that I'm using neovim/nvim-lspconfig. I also attach some common config to the LS (containing key shortcuts, etc).

The most important line is the cmd, where there's an absolute path (~ is not expanded) to start_lexical.sh.

Restart nvim, open some elixir, and you'll be rocking better than ever with docs, suggest and formatting. But you'll continued to be bummed by what you don't have in this language server, because even though lexical is the best, all elixir language servers lack a lot. But we have hope in the future.