Neovim nvim-cmp and LuaSnip: Tab Key Not Navigating Completion Menu or Expanding Snippets

301 views Asked by At

I'm working on setting up my Neovim configuration for development, specifically focusing on integrating nvim-cmp for autocompletion and LuaSnip for snippet support. However, I'm encountering an issue where the Tab key does expand snippets as expected.

Environment:

  • Neovim version: [Your Neovim version]
  • Relevant plugins:
    • nvim-cmp
    • LuaSnip
    • lsp-zero
    • Mason for automatic LSP server installation

Current Configuration:

I've set up nvim-cmp and LuaSnip in my lsp.lua configuration file. Here's the relevant part of my setup:

local lsp = require("lsp-zero")

lsp.preset("recommended")

lsp.ensure_installed({
    'gopls',
})

local cmp = require('cmp')
local cmp_select = {behavior = cmp.SelectBehavior.Select}
local cmp_mappings = lsp.defaults.cmp_mappings({
    ['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
    ['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
    ['<C-y>'] = cmp.mapping.confirm({ select = true }),
    ['<Return>'] = cmp.mapping.confirm({ select = true }),
    ["<C-Space>"] = cmp.mapping.complete(),
})

cmp_mappings['<Tab>'] = cmp.mapping.select_next_item(cmp_select)
cmp_mappings['<S-Tab>'] = cmp.mapping.select_prev_item(cmp_select)

lsp.setup_nvim_cmp({
    mapping = cmp_mappings
})

lsp.set_preferences({
    suggest_lsp_servers = true,
    sign_icons = {
        error = 'E',
        warn = 'W',
        hint = 'H',
        info = 'I'
    }
})

lsp.on_attach(function(client, bufnr)
    local opts = {buffer = bufnr, remap = false}

    vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
    vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
    vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
    vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
    vim.keymap.set("n", "[d", function() vim.diagnostic.goto_next() end, opts)
    vim.keymap.set("n", "]d", function() vim.diagnostic.goto_prev() end, opts)
    vim.keymap.set("n", "<leader>vca", function() vim.lsp.buf.code_action() end, opts)
    vim.keymap.set("n", "<leader>vrr", function() vim.lsp.buf.references() end, opts)
    vim.keymap.set("n", "<leader>vrn", function() vim.lsp.buf.rename() end, opts)
    vim.keymap.set("i", "<C-h>", function() vim.lsp.buf.signature_help() end, opts)
end)

lsp.setup()

vim.diagnostic.config({
    virtual_text = true
})

I've also used mason and mason-lspconfig to manage and automatically install LSP servers, including configuration for automatic installation of the Lua language server.

Issue:

  • When I type a prefix of a snippet (e.g., swi for a switch statement), the nvim-cmp completion menu correctly displays the snippet suggestion.
  • Pressing Tab at this point only inserts a tab space instead of expanding the snippet or navigating through the suggestions.
  • Using Ctrl+n navigates to the keyword in the completion menu, but it then only allows inserting the keyword itself, not expanding the snippet.

Troubleshooting Steps:

  • I've confirmed that all plugins are installed and up-to-date.
  • Running :verbose imap <Tab> indicates that the Tab key is mapped by nvim-cmp.
  • I've reviewed the nvim-cmp and LuaSnip documentation for any missed configuration steps.

Question:

How can I correctly configure the Tab key to navigate the nvim-cmp completion menu and expand snippets with LuaSnip in Neovim? Is there a potential conflict with my setup, or am I missing a critical step in my configuration?

1

There are 1 answers

0
Manjunath Mohan On

Did you try adding luasnip to the sources table while setting up nvim-cmp.

Check here for configuration template.