【问题标题】:How do I setup elixir-ls using nvim-lspconfig with autocompletion in neovim?如何在 neovim 中使用带有自动完成功能的 nvim-lspconfig 设置 elixir-ls?
【发布时间】:2022-06-16 17:14:48
【问题描述】:

我想使用内置的语言服务器客户端和nvim-lspconfig在 Neovim 中设置 Elixir 语言服务器。

这方面的文档似乎分布在多个地方:

  1. nvim-lspconfig README
  2. nvim-lspconfig wiki about autocomplete
  3. nvim-lspconfig elixir-ls server configuration documentation
  4. elixir-ls installation instructions

我有点不知所措,并且多次尝试这样做,但总是没有成功就放弃了。我还找到了一个有用的指南:How to Set Up Neovim for Elixir Development,但它做了很多假设,似乎错误地做了两次配置,并且还中途切换了配置格式,所以对我来说不是一个可用的总结(按照说明操作后) ,文档弹出窗口不起作用,并且我无法在自动完成弹出窗口中滚动 - 我还有很多我不理解的复制/粘贴配置)。

到目前为止,我了解所需的步骤是:

  1. 安装 neovim
  2. 手动安装 elixir-ls (由于无法询问 elixir-ls 的版本,目前似乎无法通过 asdf 安装)
  3. 安装所需的 neovim 插件:nvim-lspconfig + 自动完成所需的任何内容
  4. 为 nvim-lspconfig 和自动完成设置必要的配置。

我已经成功完成了第 3 步,但还没有成功地计算出自动完成所需的依赖项和配置。

我需要做些什么才能在 neovim 中使用 nvim-lspconfig 和 neovim 的内置语言服务器客户端设置具有自动完成功能的 elixir-ls?

【问题讨论】:

  • 作为另一个参考,ElixirForum 中有设置 neovim 和 Elixir 的详细指南:elixirforum.com/t/…

标签: autocomplete elixir neovim nvim-lspconfig elixirls


【解决方案1】:

我设法通过一些插件的组合来完成所有工作,以便在 neovim 中完成。它们是:

这是来自neovim's Wikinvim-cmp 推荐的插件列表。

我使用packer 来管理插件。我还在配置插件之前安装它们。 这是安装线:

use 'neovim/nvim-lspconfig'
use 'nvim-treesitter/completion-treesitter' -- Only if you are using TS
use 'hrsh7th/nvim-cmp' -- Autocompletion plugin
use 'hrsh7th/cmp-nvim-lsp' -- LSP source for nvim-cmp
use 'saadparwaiz1/cmp_luasnip' -- Snippets source for nvim-cmp
use 'L3MON4D3/LuaSnip' -- Snippets plugin

然后是配置部分:

-- Add additional capabilities supported by nvim-cmp
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)

-- Configure ElixirLS as the LSP server for Elixir.
require'lspconfig'.elixirls.setup{
  cmd = { "/home/my-user/path-to/elixir-ls/release/language_server.sh" },
  -- on_attach = custom_attach, -- this may be required for extended functionalities of the LSP
  capabilities = capabilities,
  flags = {
    debounce_text_changes = 150,
  },
  elixirLS = {
    dialyzerEnabled = false,
    fetchDeps = false,
  };
}

local luasnip = require 'luasnip'
-- nvim-cmp
local cmp = require 'cmp'

cmp.setup {
  snippet = {
    expand = function(args)
      require('luasnip').lsp_expand(args.body)
    end,
  },
  mapping = {
    ['<C-p>'] = cmp.mapping.select_prev_item(),
    ['<C-n>'] = cmp.mapping.select_next_item(),
    ['<C-d>'] = cmp.mapping.scroll_docs(-4),
    ['<C-f>'] = cmp.mapping.scroll_docs(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    },
    ['<Tab>'] = function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        luasnip.expand_or_jump()
      else
        fallback()
      end
    end,
    ['<S-Tab>'] = function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      elseif luasnip.jumpable(-1) then
        luasnip.jump(-1)
      else
        fallback()
      end
    end,
  },
  sources = {
    { name = 'nvim_lsp' },
    { name = 'luasnip' },
  },
}

你可以在我的 dotfiles repo 中找到我所有的 neovim 配置:https://github.com/philss/dotfiles

作为参考,这是我正在使用的 nvim 版本(从源安装):

NVIM v0.7.0-dev+864-g2818de8b7
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Features: +acl +iconv +tui

【讨论】:

  • 谢谢菲利普。配置是我的杀手锏。我最终选择了 coc.nvim,因为它有很多适合我的默认设置,并且只需要大约 5 行配置(并且不需要 lua)就可以让它按照我喜欢的方式工作。我认为 lspconfig 顶部的包装器具有合理的默认值/更简单的配置可能是最佳选择。
【解决方案2】:

除了接受的答案之外,我建议通过 Homebrew 或类似方式安装语言服务器,而不是克隆 repo 和编译。菲利普的回答是正确的。使用包管理器安装语言服务器,让您的生活更轻松。

如果您使用类似于 NeoVim IDE 的 LunarVim,则配置最少。

几乎所有用于完成的依赖项都已安装。

我还发现 Mac 用户最好像这样使用 Homebrew 安装 Elixir 语言服务器:

brew install elixir-ls

您无需手动跟踪更新。它通常通过brew update &amp;&amp; brew upgrade 与其他开发依赖项一起升级。

试试这个LunarVim

-- generic LSP settings
--
-- ---@usage disable automatic installation of servers
-- lvim.lsp.automatic_servers_installation = false
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


-- ---configure a server manually. !!Requires `:LvimCacheReset` to take effect!!
-- ---see the full default list `:lua print(vim.inspect(lvim.lsp.automatic_configuration.skipped_servers))`
-- vim.list_extend(lvim.lsp.automatic_configuration.skipped_servers, { "pyright" })
-- local opts = {} -- check the lspconfig documentation for a list of all possible options
-- require("lvim.lsp.manager").setup("pyright", opts)
-- Elixir LS
lspconfig.elixirls.setup {
  cmd = { "/usr/local/Cellar/elixir-ls/0.10.0/libexec/language_server.sh" },
  -- on_attach = custom_attach, -- this may be required for extended functionalities of the LSP
  capabilities = capabilities,
  flags = {
    debounce_text_changes = 150,
  },
  elixirLS = {
    dialyzerEnabled = false,
    fetchDeps = false,
  };
}

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多