Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Unknown function: UltiSnips#CanExpandSnippet, and it's workaround #102

Open
D029-W opened this issue Dec 23, 2024 · 0 comments
Open

Comments

@D029-W
Copy link

D029-W commented Dec 23, 2024

Greetings. I set up ultisnips' global variables, cmp_nvim_ultisnips, and neovim-cmp, in their respective order with Lazy.vim plug-in manager. (I will provide the full configuration code at the end.)

When I hit <tab> in a document, the following error pops up:

E5108: Error executing lua Vim:E117: Unknown function: UltiSnips#CanExpandSnippet
stack traceback:
[C]: at 0x55a4871cf190
...y/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/mappings.lua:8: in function <...y/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/mappings.lua:7>
...y/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/mappings.lua:52: in function <...y/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/mappings.lua:39>
...y/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/mappings.lua:62: in function 'expand_or_jump_forwards' ~/.config/nvim/lua/plugins/nvim-cmp.lua:34: in function 'on_keymap'
.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:144: in function 'callback'
.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:118: in function <.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:112>
.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:248: in function <.../.local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/keymap.lua:247>

Based on Lazy.vim info panel, the ultisnips' version info and cmp-nvim-ultisnips' version info are here:

    ● ultisnips
        dir     ~/.local/share/nvim/lazy/ultisnips
        URL     https://github.com/SirVer/ultisnips
        version 3.2.0
        tag     3.2
        branch  master
        commit  7dc30c5
        ...
    ● cmp-nvim-ultisnips
        dir    ~/.local/share/nvim/lazy/cmp-nvim-ultisnips
        URL    https://github.com/quangnguyen30192/cmp-nvim-ultisnips
        branch main
        commit 2be0eda
        ...

The function 'UltiSnips#CanExpandSnippet' used in the mapping-related features for the "cmp-nvim-ultsnips" plugin is not available in this version of UltiSnips. Therefore, the following lines won't work:

    local cmp_ultisnips_mappings = require("cmp_nvim_ultisnips.mappings")
    local cmp = require("cmp")
    local cmp_opts = {
        -- Other options
        mapping = cmp.mapping.preset.insert({
            ["<Tab>"] = cmp.mapping(function(fallback)
                cmp_ultisnips_mappings.expand_or_jump_forwards(fallback)
            end, { "i", "s" }),
        }),
        -- Other options
    }

    cmp.setup(cmp_opts)

After researching a little bit, my workaround is using the method mentioned in :h UltiSnips-trigger-functions as follows:

        mapping = cmp.mapping.preset.insert({
            ["<Tab>"] = cmp.mapping(function(fallback)
                -- cmp_ultisnips_mappings.expand_or_jump_forwards(fallback)
                vim.fn["UltiSnips#ExpandSnippetOrJump"]()
                if vim.g.ulti_expand_or_jump_res == 0 then
                    -- Operation failed, input tab, and call fallback
                    vim.fn["feedkeys"](vim.api.nvim_replace_termcodes("<Tab>", true, true, true), 'n')
                    fallback()
                end
            end, { "i", "s" }),
            ["<S-Tab>"] = cmp.mapping(function(fallback)
                -- cmp_ultisnips_mappings.jump_backwards(fallback)
                vim.fn["UltiSnips#JumpBackwards"]()
                if vim.g.ulti_jump_backwards_res == 0 then
                    -- Operation failed, input tab, and call fallback
                    vim.fn["feedkeys"](vim.api.nvim_replace_termcodes("<S-Tab>", true, true, true), 'n')
                    fallback()
                end
            end, { "i", "s" }),
        }),

I hope this could help someone facing the same issue. Below are the codes I used. If there were better solutions or my code did not follow good practices, please mention it.

Sincerely


-- Ultisnips.lua
local function init()
    local snip_folder = '/.config/nvim/UltiSnips'

    vim.g.UltiSnipsExpandTrigger = '<Tab>'
    vim.g.UltiSnipsJumpForwardTrigger = '<Tab>'
    vim.g.UltiSnipsJumpBackwardTrigger = '<S-Tab>'
    vim.g.UltiSnipsSnippetDirectories = {(os.getenv("HOME") .. snip_folder), "UltiSnips"}

    -- Setup operation result variables for expand and jump operations
    vim.g.ulti_expand_or_jump_res = 0
    vim.g.ulti_jump_backwards_res = 0
end

return {
    "SirVer/ultisnips",
    version = "*",
    init = init,
    lazy = false,
}
-- nvim-cmp.lua
-- This file sets up auto-completion with Ultisnips and others
local function config()
    -- Load cmp_nvim_ultisnips plugin first
    local cmp_ultisnips = require("cmp_nvim_ultisnips")
    local cmp_ultisnips_opts = {
        filetype_source = "treesitter",
        show_snippets = "expandable",
        documentation = function(snippet)
            return snippet.description .. "\n\n" .. snippet.value
        end,
    }
    cmp_ultisnips.setup(cmp_ultisnips_opts)

    -- Config nvim-cmp
    -- local cmp_ultisnips_mappings = require("cmp_nvim_ultisnips.mappings")
    local cmp = require("cmp")
    local cmp_opts = {
        snippet = {
            expand = function(args)
                vim.fn["UltiSnips#Anon"](args.body)
            end,
        },
        -- windows = {
        --     -- completion = cmp.config.window.bordered(),
        --     -- documentation = cmp.config.window.bordered(),
        -- },
        mapping = cmp.mapping.preset.insert({
            ['<C-n>'] = cmp.mapping.select_next_item(),
            ['<C-p>'] = cmp.mapping.select_prev_item(),
            ['<C-d>'] = cmp.mapping.scroll_docs(3),
            ['<C-f>'] = cmp.mapping.scroll_docs(3),
            ['<CR>'] = cmp.mapping.confirm({ select = true }),
            ["<Tab>"] = cmp.mapping(function(fallback)
                -- cmp_ultisnips_mappings.expand_or_jump_forwards(fallback)
                vim.fn["UltiSnips#ExpandSnippetOrJump"]()
                if vim.g.ulti_expand_or_jump_res == 0 then
                    -- Operation failed, input tab, and call fallback
                    vim.fn["feedkeys"](vim.api.nvim_replace_termcodes("<Tab>", true, true, true), 'n')
                    fallback()
                end
            end, { "i", "s" }),
            ["<S-Tab>"] = cmp.mapping(function(fallback)
                -- cmp_ultisnips_mappings.jump_backwards(fallback)
                vim.fn["UltiSnips#JumpBackwards"]()
                if vim.g.ulti_jump_backwards_res == 0 then
                    -- Operation failed, input tab, and call fallback
                    vim.fn["feedkeys"](vim.api.nvim_replace_termcodes("<S-Tab>", true, true, true), 'n')
                    fallback()
                end
            end, { "i", "s" }),
        }),
        sources = cmp.config.sources({
            { name = 'ultisnips' },
            { name = 'nvim_lsp' },
        }, {
            { name = 'buffer' },
            { name = 'path' },
        }),
    }

    cmp.setup(cmp_opts)

    -- To use git you need to install the plugin petertriho/cmp-git and uncomment lines below
    -- Set configuration for specific filetype.
    --[[ cmp.setup.filetype('gitcommit', {
        sources = cmp.config.sources({
            { name = 'git' },
        }, {
            { name = 'buffer' },
        })
    })
    require("cmp_git").setup() ]]-- 

    -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
    cmp.setup.cmdline({ '/', '?' }, {
        mapping = cmp.mapping.preset.cmdline(),
        sources = {
            { name = 'buffer' }
        }
    })

    -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
    cmp.setup.cmdline(':', {
        mapping = cmp.mapping.preset.cmdline(),
        sources = cmp.config.sources({
            { name = 'path' }
        }, {
            {
                name = 'cmdline',
                option = {
                    ignore_cmds = { 'Man', '!' }
                },
            }
        }),
        matching = { disallow_symbol_nonprefix_matching = false }
    })

    -- -- Set up lspconfig.
    -- local capabilities = require('cmp_nvim_lsp').default_capabilities()
    -- -- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
    -- require('lspconfig')['<YOUR_LSP_SERVER>'].setup {
    --     capabilities = capabilities
    -- }
end

return {
    "hrsh7th/nvim-cmp",
    dependencies = {
        "hrsh7th/cmp-nvim-lsp",
        "hrsh7th/cmp-cmdline",
        "hrsh7th/cmp-buffer",
        "hrsh7th/cmp-path",
        "SirVer/ultisnips",
        "quangnguyen30192/cmp-nvim-ultisnips",
        "neovim/nvim-lspconfig",
    },
    config = config,
    version = "*",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant