-- modified from https://github.com/VonHeikemen/dotfiles local Plugins = {} -- FIXME: plugin not loading correctly -- Autocompletion local cmp_plugin = { 'hrsh7th/nvim-cmp', event = 'InsertEnter', dependencies = { -- Sources 'hrsh7th/cmp-buffer', 'hrsh7th/cmp-path', 'saadparwaiz1/cmp_luasnip', 'hrsh7th/cmp-nvim-lsp', 'hrsh7th/cmp-nvim-lua', -- Snippets {'L3MON4D3/LuaSnip'}, } } local user = {autocomplete = true} table.insert(Plugins, cmp_plugin) table.insert(Plugins, { 'aspeddro/cmp-pandoc.nvim', enabled = true, -- event = 'InsertEnter', ft = {'markdown', 'pandoc', 'rmd'}, dependencies = { 'nvim-lua/plenary.nvim', 'jbyuki/nabla.nvim' }, opts = { crossref = { enable_nabla = true } } }) cmp_plugin.config = function() user.augroup = vim.api.nvim_create_augroup('compe_cmds', {clear = true}) vim.api.nvim_create_user_command('UserCmpEnable', user.enable_cmd, {}) local cmp = require('cmp') local luasnip = require('luasnip') local select_opts = {behavior = cmp.SelectBehavior.Select} local cmp_enable = cmp.get_config().enabled user.config = { enabled = function() if user.autocomplete then return cmp_enable() end return false end, completion = { completeopt = 'menu,menuone', }, snippet = { expand = function(args) luasnip.lsp_expand(args.body) end, }, sources = { {name = 'path'}, {name = 'nvim_lsp', keyword_length = 3}, {name = 'buffer', keyword_length = 3}, {name = 'luasnip', keyword_length = 2}, {name = 'cmp_pandoc' }, }, window = { documentation = { border = 'rounded', max_height = 15, max_width = 50, zindex = 50, } }, formatting = { fields = {'menu', 'abbr', 'kind'}, format = function(entry, item) local menu = { nvim_lsp = '[LSP]', luasnip = '[Snip]', buffer = '[Buffer]', path = '[Path]', nvim_lua = '[Lua]', cmp_pandoc = '[Pandoc]' } item.menu = menu[entry.source.name] return item end, }, mapping = { [''] = cmp.mapping.scroll_docs(-5), [''] = cmp.mapping.scroll_docs(5), [''] = cmp.mapping.select_prev_item(select_opts), [''] = cmp.mapping.select_next_item(select_opts), -- TODO: test -- [''] = cmp.mapping.select_prev_item(select_opts), -- [''] = cmp.mapping.select_next_item(select_opts), [''] = cmp.mapping(function(fallback) if luasnip.jumpable(-1) then luasnip.jump(-1) else fallback() end end, {'i', 's'}), [''] = cmp.mapping(function(fallback) if luasnip.jumpable(1) then luasnip.jump(1) else fallback() end end, {'i', 's'}), [''] = cmp.mapping(function() if cmp.visible() then user.set_autocomplete(false) cmp.abort() else user.set_autocomplete(true) cmp.complete() end end), [''] = cmp.mapping(function(fallback) if cmp.visible() then cmp.confirm({select = true}) elseif luasnip.jumpable(1) then luasnip.jump(1) elseif user.check_back_space() then fallback() else user.set_autocomplete(true) cmp.complete() end end, {'i', 's'}), [''] = cmp.mapping(function() if luasnip.jumpable(-1) then luasnip.jump(-1) else user.insert_tab() end end, {'i', 's'}), } } cmp.setup(user.config) end function user.set_autocomplete(new_value) local old_value = user.autocomplete if new_value == old_value then return end if new_value == false then -- restore autocomplete in the next word vim.api.nvim_buf_set_keymap( 0, 'i', '', 'UserCmpEnable', {noremap = true} ) -- restore when leaving insert mode vim.api.nvim_create_autocmd('InsertLeave', { group = user.augroup, command = 'UserCmpEnable', once = true, }) end user.autocomplete = new_value end function user.check_back_space() local col = vim.fn.col('.') - 1 if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then return true else return false end end function user.enable_cmd() if user.autocomplete then return end pcall(vim.api.nvim_buf_del_keymap, 0, 'i', '') user.set_autocomplete(true) end function user.insert_tab() vim.api.nvim_feedkeys( vim.api.nvim_replace_termcodes('', true, true, true), 'n', true ) end return Plugins