简体   繁体   English

使用 nvim-cmp 和 lsp 的最小 NeoVim lua 配置

[英]Minimal NeoVim lua config for using nvim-cmp with lsp

I'm trying to write my own NeoVim Lua based config, stripped down to the minimum I need and with the goal to understand at least most of the config.我正在尝试编写自己的基于 NeoVim Lua 的配置,精简到我需要的最低限度,目标是至少了解大部分配置。 LSP setup is working fine and is the only source I configured for nvim-cmp: LSP 设置工作正常,是我为 nvim-cmp 配置的唯一源:

local cmp = require("cmp")                                                                                                                                                              
 
cmp.setup {                                                                                                                                                                             
  sources = {                                                                                                                                                                           
    { name = 'nvim_lsp' }                                                                                                                                                               
   }                                                                                                                                                                                     
}                                                                                                                                                                                       
local capabilities = vim.lsp.protocol.make_client_capabilities()                                                                                                                        
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) 

After some startup delay the completion is working in the sense that I see popups with proposed completions, based on information from LSP.根据来自 LSP 的信息,在一些启动延迟之后,完成工作在我看到带有建议完成的弹出窗口的意义上。

But I cannot select any of the proposed completions.但我不能选择任何建议的完成。 I can just continue to type which reduces the proposed completions, but I cannot use tab, arrow keys, ... to select an entry from the popup.我可以继续输入以减少建议的完成,但我不能使用制表符、箭头键...从弹出窗口中选择一个条目。 I saw in the docs that one can define keyboard mappings but cannot make sense out of them.我在文档中看到可以定义键盘映射但无法理解它们。 They are all rather sophisticated, require a snippet package to be installed, ...它们都相当复杂,需要安装一个片段包,......

I would prefer to select the next completion via tab and to navigate them via arrow key.我更愿意通过选项卡选择下一个完成并通过箭头键导航它们。 "Enter" should select the current one. “Enter”应该选择当前的一个。

Could somebody show me a minimal configuration for this setup or point me to more "basic" docs?有人可以向我展示此设置的最小配置或指向更多“基本”文档吗?

Nvim-cmp requires you to set the mapping for tab and other keys explicitly, here is my working example : Nvim-cmp 要求您明确设置制表符和其他键的映射,这是我的工作示例

local cmp = require'cmp'
local lspkind = require'lspkind'

cmp.setup({
  snippet = {
    expand = function(args)
      -- For `ultisnips` user.
      vim.fn["UltiSnips#Anon"](args.body)
    end,
  },
  mapping = cmp.mapping.preset.insert({
          ['<Tab>'] = function(fallback)
            if cmp.visible() then
              cmp.select_next_item()
            else
              fallback()
            end
          end,
          ['<S-Tab>'] = function(fallback)
            if cmp.visible() then
              cmp.select_prev_item()
            else
              fallback()
            end
          end,
          ['<CR>'] = cmp.mapping.confirm({ select = true }),
          ['<C-e>'] = cmp.mapping.abort(),
          ['<Esc>'] = cmp.mapping.close(),
          ['<C-d>'] = cmp.mapping.scroll_docs(-4),
          ['<C-f>'] = cmp.mapping.scroll_docs(4),
        }),
  sources = {
    { name = 'nvim_lsp' }, -- For nvim-lsp
    { name = 'ultisnips' }, -- For ultisnips user.
    { name = 'nvim_lua' }, -- for nvim lua function
    { name = 'path' }, -- for path completion
    { name = 'buffer', keyword_length = 4 }, -- for buffer word completion
    { name = 'omni' },
    { name = 'emoji', insert = true, } -- emoji completion
  },
  completion = {
    keyword_length = 1,
    completeopt = "menu,noselect"
  },
  view = {
    entries = 'custom',
  },
  formatting = {
    format = lspkind.cmp_format({
      mode = "symbol_text",
      menu = ({
        nvim_lsp = "[LSP]",
        ultisnips = "[US]",
        nvim_lua = "[Lua]",
        path = "[Path]",
        buffer = "[Buffer]",
        emoji = "[Emoji]",
          omni = "[Omni]",
      }),
    }),
  },
})

This is working great for me.这对我很有用。 The mapping part corresponds to the config for key mapping in the table.映射部分对应于表中键mapping的配置。 You can tweak your conf based on my config to make it work for you.您可以根据我的配置调整您的 conf 以使其适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM