pluginsluaneovimnvim.cmp

Nvim-cmp throwing error when expanding snippets


I have nvim-cmp installed and configured for Neovim. It works well and suggests code completion for all of my projects, until I recently noticed it throws an error and crashes with the Lua language.

The snippets and suggestions appear correctly completion suggestions working correctly

but when any of them are selected, I receive this error: nvim-cmp error

I'm having trouble debugging the specific issue causing this and why it is only an issue with the Lua language.

Here is my nvim-cmp.lua config file (using lazy as my plugin loader):

return {
    "hrsh7th/nvim-cmp",
    event = "InsertEnter",
    dependencies = {
        "hrsh7th/cmp-buffer",
        "hrsh7th/cmp-path",
        "onsails/lspkind.nvim",
        "L3MON4D3/LuaSnip",
        "saadparwaiz1/cmp_luasnip",
        "rafamadriz/friendly-snippets",
        "hrsh7th/cmp-nvim-lsp",
    },
    config = function()
        local cmp = require("cmp")
        local luasnip = require("luasnip")
        local lspkind = require("lspkind")

        require("luasnip.loaders.from_vscode").lazy_load()

        vim.opt.completeopt = "menu,menuone,preview,noselect"
        cmp.setup({
            snippet = {
                expand = function(args)
                    luasnip.lspexpand(args.body)
                end,
            },
            mapping = cmp.mapping.preset.insert({
                ["<C-k>"] = cmp.mapping.select_prev_item(),
                ["<C-j>"] = cmp.mapping.select_next_item(),
                ["<C-b>"] = cmp.mapping.scroll_docs(-4),
                ["<C-f>"] = cmp.mapping.scroll_docs(4),
                ["<C-Space>"] = cmp.mapping.complete(),
                ["<C-e>"] = cmp.mapping.abort(),
                ["<CR>"] = cmp.mapping.confirm({ select = false }),
            }),
            sources = cmp.config.sources({
                { name = "nvim_lsp" },
                { name = "luasnip" },
                { name = "buffer" },
                { name = "path" },
            }),
            formatting = {
                format = lspkind.cmp_format({
                    maxwidth = 50,
                    ellipsis_char = "...",
                }),
            },
        })
    end,
}

Please let me know any further context I can add.


Solution

  • I think you call wrong function.

    luasnip.lspexpand(args.body)

    It should be

    luasnip.lsp_expand(args.body)