最小的Neovim LuA配置,用于与LSP一起使用NVIM-CMP

发布于 2025-02-10 18:02:11 字数 1738 浏览 2 评论 0原文

我正在尝试编写自己的基于Neovim Lua的配置,将其剥离至最低限度,并至少了解至少大多数配置。 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) 

在某种启动延迟后,完成的意义是我根据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. LSP setup is working fine and is the only source I configured for 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.

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.

Could somebody show me a minimal configuration for this setup or point me to more "basic" docs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2025-02-17 18:02:11

NVIM-CMP要求您明确设置TAB和其他键的映射,以下是我的工作示例

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]",
      }),
    }),
  },
})

这对我有用。映射零件对应于表中的键映射的配置。您可以根据我的配置调整conf,以使其适合您。

Nvim-cmp requires you to set the mapping for tab and other keys explicitly, here is my working example:

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. You can tweak your conf based on my config to make it work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文