Neovim: nvim-lsp + elixir

Hi all,

Anyone using nvim-lsp + Neovim 0.5.0?
How did you configure it for Elixir?

https://neovim.io/doc/user/lsp.html

The following LSP configs are included:
als
bashls

rust-analyzer …

Elixir??
https://github.com/neovim/nvim-lsp → Configurations

Issue(closed): Elixir Language Server · Issue #58 · neovim/nvim-lspconfig · GitHub

1 Like

OK, this is something I am really curious about too.

1 Like

Using terminal Vim 8, not Neovim, and not using the aforementioned plugins, so my post is slightly off-topic, but regarding Elixir language server support I am very happy with my setup, using ALE and ElixirLS. As far as I know, it should work with Neovim too.

2 Likes

How’s 0.5.0?

Never tried the built-in lsp client, but I use ElixirLS with coc (after spending some time with ALE and LanguageClient-neovim) on nvim 0.4.3, if it’s relevant :slight_smile:

1 Like

I have tried it.

I am not sure if NeoVim has implemented enough of the protocol yet. I wasn’t able to get GoToDefinition or FindReferences to work.

Completions with omnifunc seemed to work, but I didn’t try to get it set up with an actual completions plugin (to have it show completions as I type).

Hover documentation was nice, and is presented with a neovim floating window.

2 Likes

For now I stay on stable release and I use vim-lsp. Works ok.

1 Like

Hey!
I just wanted to mention that nvim-lsp just merged an elixir-ls configuration into master.

So if anybody come here wondering about how to do that, it should be available now. :slight_smile:

https://github.com/neovim/nvim-lsp#elixirls

8 Likes

Could someone setup this correctly?

Yes, I use native LSP with elixir-ls

I have this in my lspconfig for elixir.

-- Elixir
require'lspconfig'.elixirls.setup{
  cmd = { vim.loop.os_homedir().."/path/to/elixir-ls/language_server.sh" };
}

I also have these setup:

" LSP
nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>
nnoremap <silent> gD <cmd>lua vim.lsp.buf.declaration()<CR>
nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>
nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>
nnoremap <silent> K <cmd>lua vim.lsp.buf.hover()<CR>
nnoremap <silent> <C-k> <cmd>lua vim.lsp.buf.signature_help()<CR>
nnoremap <silent> [g <cmd>lua vim.lsp.diagnostic.goto_prev()<CR>
nnoremap <silent> ]g <cmd>lua vim.lsp.diagnostic.goto_next()<CR>

command! -nargs=0 Format :lua vim.lsp.buf.formatting()

I am using completion-nvim for my popup menu but have also been playing around with compe

" Use <Tab> and <S-Tab> to navigate through popup menu
inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"

" Manually trigger completion
imap <silent> <C-Space> <Plug>(completion_trigger)

" Set completeopt to have a better completion experience
set completeopt=menuone,noinsert,noselect

" Avoid showing message extra message when using completion
set shortmess+=c

let g:completion_matching_strategy_list = ['exact', 'substring', 'fuzzy']
7 Likes

:tada: Neovim fam! 0.5.0 is officially here!

7 Likes

Hey!

I wanted to setup NeoVim 0.5 with elixir-ls + autocompletion following this article, and the autocompletion is working, I can see NeoVim communicates with the language server, but syntax highlighting for Elixir is not working for some reason. Everything has the same text color. When I tried some random Ruby project, code was syntax highlighted.

Any help would be really appreciated.

Here’s my init.vim file:

call plug#begin('~/.config/nvim/autoload/plugged')

Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/nvim-compe'
Plug 'joshdick/onedark.vim'

call plug#end()

lua <<EOF

local lspconfig = require("lspconfig")

-- Neovim doesn't support snippets out of the box, so we need to mutate the
-- capabilities we send to the language server to let them know we want snippets.
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true

-- Setup our autocompletion. These configuration options are the default ones
-- copied out of the documentation.
require "compe".setup {
  enabled = true,
  autocomplete = true,
  debug = false,
  min_length = 1,
  preselect = "disabled",
  throttle_time = 80,
  source_timeout = 200,
  incomplete_delay = 400,
  max_abbr_width = 100,
  max_kind_width = 100,
  max_menu_width = 100,
  documentation = true,
  source = {
    path = true,
    buffer = true,
    calc = true,
    vsnip = true,
    nvim_lsp = true,
    nvim_lua = true,
    spell = true,
    tags = true,
    treesitter = true
  }
}

-------------------------------------

-- A callback that will get called when a buffer connects to the language server.
-- Here we create any key maps that we want to have on that buffer.
local on_attach = function(_, bufnr)
  local function map(...)
    vim.api.nvim_buf_set_keymap(bufnr, ...)
  end
  local map_opts = {noremap = true, silent = true}

  map("n", "df", "<cmd>lua vim.lsp.buf.formatting()<cr>", map_opts)
  map("n", "gd", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<cr>", map_opts)
  map("n", "dt", "<cmd>lua vim.lsp.buf.definition()<cr>", map_opts)
  map("n", "K", "<cmd>lua vim.lsp.buf.hover()<cr>", map_opts)
  map("n", "gD", "<cmd>lua vim.lsp.buf.implementation()<cr>", map_opts)
  map("n", "<c-k>", "<cmd>lua vim.lsp.buf.signature_help()<cr>", map_opts)
  map("n", "1gD", "<cmd>lua vim.lsp.buf.type_definition()<cr>", map_opts)

  -- These have a different style than above because I was fiddling
  -- around and never converted them. Instead of converting them
  -- now, I'm leaving them as they are for this article because this is
  -- what I actually use, and hey, it works ¯\_(ツ)_/¯.
  vim.cmd [[imap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>']]
  vim.cmd [[smap <expr> <C-l> vsnip#available(1) ? '<Plug>(vsnip-expand-or-jump)' : '<C-l>']]

  vim.cmd [[imap <expr> <Tab> vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>']]
  vim.cmd [[smap <expr> <Tab> vsnip#jumpable(1) ? '<Plug>(vsnip-jump-next)' : '<Tab>']]
  vim.cmd [[imap <expr> <S-Tab> vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<S-Tab>']]
  vim.cmd [[smap <expr> <S-Tab> vsnip#jumpable(-1) ? '<Plug>(vsnip-jump-prev)' : '<S-Tab>']]

  vim.cmd [[inoremap <silent><expr> <C-Space> compe#complete()]]
  vim.cmd [[inoremap <silent><expr> <CR> compe#confirm('<CR>')]]
  vim.cmd [[inoremap <silent><expr> <C-e> compe#close('<C-e>')]]
  vim.cmd [[inoremap <silent><expr> <C-f> compe#scroll({ 'delta': +4 })]]
  vim.cmd [[inoremap <silent><expr> <C-d> compe#scroll({ 'delta': -4 })]]
end

-- Finally, let's initialize the Elixir language server

-- Replace the following with the path to your installation
local path_to_elixirls = vim.fn.expand("~/dev/elixir-ls/rel/language_server.sh")

lspconfig.elixirls.setup({
  cmd = {path_to_elixirls},
  capabilities = capabilities,
  on_attach = on_attach,
  settings = {
    elixirLS = {
      -- I choose to disable dialyzer for personal reasons, but
      -- I would suggest you also disable it unless you are well
      -- aquainted with dialzyer and know how to use it.
      dialyzerEnabled = false,
      -- I also choose to turn off the auto dep fetching feature.
      -- It often get's into a weird state that requires deleting
      -- the .elixir_ls directory and restarting your editor.
      fetchDeps = false
    }
  }
})

EOF

For code highlighting I use treesitter.

1 Like

Thanks a lot. I was confused by that tutorial, because it showed Elixir code that was syntax highlighted and didn’t mention the fact that I have to install some extra plugin for that. :slight_smile:

Vim do not provide Elixir highlighting OOtB so you either need to use TreeSitter in NeoVim or use “classic” syntax definition from vim-elixir in Vim or NeoVim.

2 Likes

Hey everyone. Converting my configs from vim to neovim with Lua.

As far as elixir experience goes, I don’t get very accurate references, even though it’s in the list of references in elixir_sense lsp. Im for sure getting a better experience with the coc-nvim plugin, but I’ve decided to switch – the programmable lsp is pretty amazing and is the clear path forward. I plan on testing the lsp, coc, and nvim-lsp to see what the deal is and if I can help in any way.

For anyone who’s converted to nvim-lsp, maybe throw out what issues you’re having on the supported features (from the list of provided functionality).

A plug for a library that I didn’t write, but just committed some elixir queries to, is nvim-treesitter-textobjects, allowing you to make text-objects out of the supported ts-capture (hopefully it auto-merges tonight). I’ve made captures for parameters, comments, and a few more things.

Hey, I’ve been using neovim LSP for a while now too. Just recently discovered/decided to try using a dialyzer, but I failed to make it work just by enabling the correct flags. Has anyone had any success with that so far?

I’ve just decided to jump back into vim (nvim to be exact) after a few years of vscode. I’ve gone with NVChad as a base config as I didn’t want to have to start from scratch. Mostly it all seems fine but I’m not seeing any completions from the Ex LSP. According to my LspInfo it’s running, but I’m at a bit of a loss.

I installed it using Mason and configured it as so:

lspconfig.elixirls.setup {
  on_attach = on_attach,
  capabilities = capabilities,
  cmd = { "/Users/blah/.local/share/nvim/mason/packages/elixir-ls/launch.sh" }
}

Any help appreciated!

The elixirls is not attached to the current buffer, so it’s not actually running. Try execute :LspStart elixirls to first check if the lsp server can be started correctly.

2 Likes

Thanks for that. That helped me trace the problen. I was using the wrong launch cmd, just for future reference. Should’ve been language_server.sh.

1 Like

I’ve just ported my old Neovim vimscript config to Lua, and the elixir-ls config was really tripping me up. I finally realized that using the ~/ alias in my cmd path was tripping it up. Hope this helps anyone else who comes across this.

require('lspconfig').elixirls.setup {
    cmd = {"/Full/Path/to/elixir-ls/release/language_server.sh"},
    on_attach = on_attach
}