0

I'm trying to enable snippets in my auto-completion but they are only working for lua files and nothing else. I ve been messing around with nvim-cmp and luasnip itself and even downloaded friendly snippets but still can't get them to work.

LSP-ZERO/CMP CONFIG:

local lsp = require('lsp-zero').preset('recommended')

lsp.on_attach(function(client, bufnr)
  lsp.default_keymaps({ buffer = bufnr, })
end)

-- (Optional) Configure lua language server for neovim
require('lspconfig').lua_ls.setup(lsp.nvim_lua_ls())
lsp.format_on_save()
lsp.setup()

local status, cmp = pcall(require, "cmp")
if (not status) then return end

cmp.setup({
  snippet = {
    expand = function(args)
      require('luasnip').lsp_expand(args.body)
    end,
  },
  mapping = cmp.mapping.preset.insert({
    ['<C-d>'] = cmp.mapping.scroll_docs(-4),
    ['<C-f>'] = cmp.mapping.scroll_docs(4),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true
    }),
  }),
  sources = cmp.config.sources({
    { name = 'nvim_lsp' },
    { name = 'buffer' },
  }),


})


LUASNIP CONFIG

require('luasnip').config.set_config({
  region_check_events = 'InsertEnter',
  delete_check_events = 'InsertLeave'
})

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


I'm not sure what I'm missing.

1 Answer 1

1

You need to add cmp_luasnip as a source to nvim-cmp. You can find a minimal example in lsp-zero's documentation.

Side note: format_on_save() doesn't do anything if you don't give it any arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.