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

local luasnip_ok, luasnip = pcall(require, "luasnip")
if not luasnip_ok then
    vim.notify("Luasnip is required for nvim-cmp to work", vim.log.levels.WARN)
end

local lspkind = require "lspkind"

cmp.setup {
    snippet = {
        expand = function(args)
            luasnip.lsp_expand(args.body)
        end,
    },
    mapping = cmp.mapping.preset.insert {
        ["<C-f>"] = cmp.mapping(cmp.mapping.scroll_docs(4, { "i", "c" })),
        ["<C-b>"] = cmp.mapping(cmp.mapping.scroll_docs(-4, { "i", "c" })),
        ["<C-Space>"] = cmp.mapping(cmp.mapping.complete {}, { "i", "c" }),
        ["<C-c>"] = cmp.mapping.close(),
        ["<CR>"] = cmp.mapping.confirm { select = true },
    },
    sources = cmp.config.sources({
        { name = "nvim_lsp" },
    }, {
        { name = "buffer" },
        { name = "path" },
    }),
    formatting = {
        format = lspkind.cmp_format(),
    },
}

cmp.setup.cmdline(":", {
    mapping = cmp.mapping.preset.cmdline(),
    sources = cmp.config.sources({
        { name = "path" },
    }, {
        { name = "cmdline" },
    }),
})