nvim-cmp.luaattempt to index field ‘menu‘ (a nil value)

28 篇文章 1 订阅
25 篇文章 2 订阅

《06_[nvim0.5+从0单排]_Native LSP 自动补全、语法检查、code action、代码段—TypeScript篇》
https://www.bilibili.com/video/BV19T4y1Z7VB/

有朋友使用我的配置,遇到了如下的错。

报错信息

在这里插入图片描述

E5108: Error executing lua /Users/itkey/.config/nvim/after/plugin/nvim-cmp.lua:81: attempt to index field ‘menu’ (a nil value)

问题分析

一开始我也很纳闷,相同的配置怎么我的是正常的呢?

终于发现原因了,由于nvim-cmp插件升级导致。我录制视频之时,这段代码还是可以正常用的。等到大家看视频的时候。nvim-cmp插件升级了,我的这段配置不兼容了。[笑哭] 临时解决办法就是先删除这段配置。

https://github.com/hrsh7th/nvim-cmp

解决办法

方法一:直接删除报错的代码

很抱歉给你带来困扰。79-104行代码出自我手,用于function对象按回车自动补全(),删了不影响什么。

删除75行至104行,也就是删除如下代码。问题解决!

--重写插件方法,为了实现function 后,自动追加()
local core = require("cmp.core")
local keymap = require("cmp.utils.keymap")
--local cmp_confirm = cmp.confirm
cmp.confirm = function(option)
  option = option or {}
  local e = core.menu:get_selected_entry() or (option.select and core.menu:get_first_entry() or nil)
  if e then
    core.confirm(
      e,
      {
        behavior = option.behavior
      },
      function()
        local myContext = core.get_context({reason = cmp.ContextReason.TriggerOnly})
        core.complete(myContext)
        --function() 自动增加()
        if
          e and e.resolved_completion_item and
            (e.resolved_completion_item.kind == 3 or e.resolved_completion_item.kind == 2)
         then
          vim.api.nvim_feedkeys(keymap.t("()<Left>"), "n", true)
        end
      end
    )
    return true
  else
    return false
  end
end

方法二:升级代码

如果你觉得自动补全()有用的话,把75行至104行代码换成如下代码:

------修复2021年10月12日 nvim-cmp.luaattempt to index field 'menu' (a nil value)---------
--重写插件方法,为了实现function 后,自动追加()
local keymap = require("cmp.utils.keymap")
cmp.confirm = function(option)
  option = option or {}
  local e = cmp.core.view:get_selected_entry() or (option.select and cmp.core.view:get_first_entry() or nil)
  if e then
    cmp.core:confirm(e, {
      behavior = option.behavior,
    }, function()
        local myContext = cmp.core:get_context({ reason = cmp.ContextReason.TriggerOnly })
        cmp.core:complete(myContext)
                --function() 自动增加()
                if
                  e and e.resolved_completion_item and
                    (e.resolved_completion_item.kind == 3 or e.resolved_completion_item.kind == 2)
                 then
                  vim.api.nvim_feedkeys(keymap.t("()<Left>"), "n", true)
                end
    end)
    return true
  else
    if vim.fn.complete_info({ 'selected' }).selected ~= -1 then
      keymap.feedkeys(keymap.t('<C-y>'), 'n')
      return true
    end
    return false
  end
end
---------------

新版nvim-cmp升级后导致补全时按tab无效的解决办法

这个我挺难受的,升级个插件,原来的配置竟然不能用了。然后我看了一下nvim-cmp.lua项目的主页说明。貌似配置被减化了。

把原来的这一堆代码,

["<Tab>"] = function(fallback)
      if vim.fn.pumvisible() == 1 then
        vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<C-n>", true, true, true), "n")
      elseif luasnip.expand_or_jumpable() then
        vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-expand-or-jump", true, true, true), "")
      else
        fallback()
      end
    end

现在换成

['<Tab>'] = function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<Plug>luasnip-expand-or-jump', true, true, true), '')
      else
        fallback()
      end
    end,

原来很多行解决的问题,现在一行就搞定了。

同理

["<S-Tab>"] = function(fallback)
      if vim.fn.pumvisible() == 1 then
        vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<C-p>", true, true, true), "n")
      elseif luasnip.jumpable(-1) then
        vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-jump-prev", true, true, true), "")
      else
        fallback()
      end
    end

替换成

['<S-Tab>'] = function(fallback)
      if cmp.visible() then
        cmp.select_prev_item()
      elseif luasnip.jumpable(-1) then
        vim.fn.feedkeys(vim.api.nvim_replace_termcodes('<Plug>luasnip-jump-prev', true, true, true), '')
      else
        fallback()
      end
    end,

参考

https://github.com/neovim/nvim-lspconfig/wiki/Snippets

https://github.com/L3MON4D3/LuaSnip

https://github.com/hrsh7th/nvim-cmp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值