local status, cmp = pcall(require, "cmp") if (not status) then return end local function has_words_before() local line, col = unpack(vim.api.nvim_win_get_cursor(0)) return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil end -- nvim-cmp setup local cmp = require 'cmp' cmp.setup { completion = { autocomplete = false }, snippet = { expand = function(args) require('luasnip').lsp_expand(args.body) end, }, mapping = { [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif has_words_before() then cmp.complete() else fallback() end end, { "i", "s" }), [''] = cmp.mapping.select_prev_item(), [''] = cmp.mapping.scroll_docs(-4), [''] = cmp.mapping.scroll_docs(4), [''] = cmp.mapping.close(), [''] = cmp.mapping.confirm { behavior = cmp.ConfirmBehavior.Replace, select = true, }, }, sources = { { name = 'nvim_lsp' }, { name = 'luasnip' }, }, }