aboutsummaryrefslogtreecommitdiffstats
path: root/nvim/lua/plugins/nvim-cmp.lua
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2024-08-16 16:28:22 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2024-08-16 16:28:22 -0400
commite184d7cee358abc277a51f040cacf236f1eaceab (patch)
treebfc7ced143a04d6d79f955ca7c37b962a01773f3 /nvim/lua/plugins/nvim-cmp.lua
parentd3230ab29e56f97966f05cf2232a2e92d6a1302f (diff)
removed lspzero, updated keymaps
Diffstat (limited to 'nvim/lua/plugins/nvim-cmp.lua')
-rw-r--r--nvim/lua/plugins/nvim-cmp.lua452
1 files changed, 183 insertions, 269 deletions
diff --git a/nvim/lua/plugins/nvim-cmp.lua b/nvim/lua/plugins/nvim-cmp.lua
index ac588f6..3f13d1f 100644
--- a/nvim/lua/plugins/nvim-cmp.lua
+++ b/nvim/lua/plugins/nvim-cmp.lua
@@ -1,288 +1,202 @@
--- modified from https://github.com/VonHeikemen/dotfiles
+local icons = {
+ Text = "",
+ Method = "󰆧",
+ Function = "󰊕",
+ Constructor = "",
+ Field = "󰇽",
+ Variable = "󰂡",
+ Class = "󰠱",
+ Interface = "",
+ Module = "",
+ Property = "󰜢",
+ Unit = "",
+ Value = "󰎠",
+ Enum = "",
+ Keyword = "󰌋",
+ Snippet = "",
+ Color = "󰏘",
+ File = "󰈙",
+ Reference = "",
+ Folder = "󰉋",
+ EnumMember = "",
+ Constant = "󰏿",
+ Struct = "",
+ Event = "",
+ Operator = "󰆕",
+ TypeParameter = "󰅲",
+}
-local Plugins = {}
+local luasnip = {
+ "L3MON4D3/LuaSnip",
+ version = "v2.*",
+ build = "make install_jsregexp",
+ keys = {
+ {
+ '<c-j>',
+ function()
+ local ls = require('luasnip')
+ if ls.expand_or_jumpable() then
+ ls.expand_or_jump()
+ end
+ end,
+ mode = { 'i', 's' },
+ desc = 'Previous snippet jump'
+ },
+ {
+ '<c-k>',
+ function()
+ local ls = require('luasnip')
+ if ls.jumpable(-1) then
+ ls.jump(-1)
+ end
+ end,
+ mode = { 'i', 's' },
+ desc = 'Next snippet jump'
+ }
+ },
+ lazy = true
+}
--- FIXME: plugin not loading correctly
+local cmp_path = {
+ 'hrsh7th/cmp-path',
+ event = 'InsertEnter'
+}
--- 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',
+local cmp_luasnip = {
+ 'saadparwaiz1/cmp_luasnip',
+ dependencies = { "L3MON4D3/LuaSnip" },
+ event = 'InsertEnter'
+}
- -- Snippets
- {'L3MON4D3/LuaSnip'},
+local cmp_lua = {
+ 'hrsh7th/cmp-nvim-lua',
+ ft = {
+ "lua", "vim"
}
}
-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'},
- { name = 'buffer', keyword_length = 4},
- { name = 'luasnip'},
- { name = 'cmp_pandoc' },
- { name = 'emoji' },
- },
- window = {
- documentation = {
- border = 'rounded',
- max_height = 15,
- max_width = 50,
- zindex = 50,
- }
+local cmp_pandoc = {
+ 'aspeddro/cmp-pandoc.nvim',
+ -- FIXME: weird issues when using bibliographies
+ enabled = false,
+ dependencies = {
+ 'nvim-lua/plenary.nvim',
},
- 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]',
- emoiji = '[Emoji]'
+ -- ft = { 'pandoc', 'markdown', 'rmd' }
+ opts = {
+ bibliography = {
+ documentation = true,
+ },
+ crossref = {
+ documentation = true,
+ enable_nabla = false
}
-
- item.menu = menu[entry.source.name]
- return item
- end,
- },
- mapping = {
- ['<C-k>'] = cmp.mapping.scroll_docs(-5),
- ['<C-j>'] = cmp.mapping.scroll_docs(5),
-
- ['<Up>'] = cmp.mapping.select_prev_item(select_opts),
- ['<Down>'] = cmp.mapping.select_next_item(select_opts),
-
- -- TODO: test
- -- ['<M-k>'] = cmp.mapping.select_prev_item(select_opts),
- -- ['<M-j>'] = cmp.mapping.select_next_item(select_opts),
-
- ['<C-a>'] = cmp.mapping(function(fallback)
- if luasnip.jumpable(-1) then
- luasnip.jump(-1)
- else
- fallback()
- end
- end, {'i', 's'}),
-
- ['<C-d>'] = cmp.mapping(function(fallback)
- if luasnip.jumpable(1) then
- luasnip.jump(1)
- else
- fallback()
- end
- end, {'i', 's'}),
-
- ['<M-u>'] = cmp.mapping(function()
- if cmp.visible() then
- user.set_autocomplete(false)
- cmp.abort()
- else
- user.set_autocomplete(true)
- cmp.complete()
- end
- end),
-
- ['<Tab>'] = 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'}),
-
- ['<S-Tab>'] = 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',
- '<space>',
- '<cmd>UserCmpEnable<cr><space>',
- {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', '<Space>')
- user.set_autocomplete(true)
-end
+local cmp_lsp = {
+ -- TODO: lazy load on lsp load
+ 'hrsh7th/cmp-nvim-lsp',
+ event = 'LspAttach',
+ dependencies = {
+ 'neovim/nvim-lspconfig'
+ },
+}
-function user.insert_tab()
- vim.api.nvim_feedkeys(
- vim.api.nvim_replace_termcodes('<Tab>', true, true, true),
- 'n',
- true
- )
-end
+local cmp_buffer = {
+ 'hrsh7th/cmp-buffer',
+ event = 'BufEnter'
+}
--- return Plugins
+local cmp = {
+ 'hrsh7th/nvim-cmp',
+ config = function()
+ local cmp = require('cmp')
+ cmp.setup({
+ snippet = {
+ expand = function(args)
+ require('luasnip').lsp_expand(args.body)
+ end
+ },
+ sources = {
+ { name = 'nvim_lsp' },
+ { name = 'luasnip' },
+ { name = 'path', keyword_length = 2 },
+ { name = 'buffer', keyword_length = 4 },
+ },
+ window = {
+ -- completion = cmp.config.window.bordered(),
+ -- documentation = cmp.config.window.bordered()
+ },
+ formatting = {
+ -- fields = {'menu', 'abbr', 'kind'},
+ format = function(entry, item)
+ -- item.kind = string.format('%s %s', icons[item.kind], item.kind)
+ item.kind = icons[item.kind] .. ' ' .. item.kind
+ local menu = {
+ nvim_lsp = '[LSP]',
+ luasnip = '[Snip]',
+ buffer = '[Buffer]',
+ path = '[Path] foo',
+ nvim_lua = '[Lua]',
+ cmp_pandoc = '[Pandoc]',
+ emoiji = '[Emoji]',
+ }
+ menu['vim-dadbod-completion'] = '[dadbod]'
+ item.menu = menu[entry.source.name]
+ return item
+ end
+ },
+ mapping = cmp.mapping.preset.insert({
+ ['<C-k>'] = cmp.mapping.scroll_docs(-5),
+ ['<C-j>'] = cmp.mapping.scroll_docs(5),
+ ['<TAB>'] = cmp.mapping(function(fallback)
+ if cmp.visible() then
+ cmp.confirm({ select = true })
+ else
+ fallback()
+ end
+ end, { 'i', 's' })
+ })
+ })
+ cmp.setup.filetype({ 'pandoc', 'markdown', 'rmd' }, {
+ sources = {
+ { name = 'luasnip' },
+ { name = 'path', keyword_length = 3 },
+ { name = 'buffer' },
+ { name = 'emoji' },
+ -- { name = 'cmp_pandoc' },
+ }
+ })
+ cmp.setup.filetype({ "lua", "vim" }, {
+ sources = {
+ { name = 'luasnip' },
+ { name = 'nvim_lsp' },
+ { name = 'nvim_lua' },
+ { name = 'luansip' },
+ { name = 'buffer' },
+ { name = 'path' }
+ }
+ })
+ cmp.setup.filetype({ "sql" }, {
+ sources = {
+ { name = 'vim-dadbod-completion' },
+ { name = 'buffer' }
+ }
+ })
+ end
+}
return {
- {
- "L3MON4D3/LuaSnip",
- version = "v2.*",
- build = "make install_jsregexp"
- },
+ luasnip,
-- completion sources
- {
- 'hrsh7th/cmp-path',
- },
- {
- 'saadparwaiz1/cmp_luasnip'
- },
- { -- TODO: lazy load on filtetype
- 'hrsh7th/cmp-nvim-lua'
- },
- {
- -- TODO: lazy load on lsp load
- 'hrsh7th/cmp-nvim-lsp',
- dependencies= {
- 'neovim/nvim-lspconfig'
- },
- },
- {
- 'hrsh7th/cmp-buffer',
- },
- {
- 'hrsh7th/nvim-cmp',
- config = function()
- local cmp = require('cmp')
- cmp.setup({
- snippet = {
- expand = function(args)
- require('luasnip').lsp_expand(args.body)
- end
- },
- sources = {
- { name = 'nvim_lsp'},
- { name = 'luasnip'},
- { name = 'path' },
- { name = 'nvim_lua' },
- { name = 'buffer', keyword_length = 4 },
- { name = 'emoji' }
- },
- window = {
- -- TODO: style cmp windows
- completion = cmp.config.window.bordered(),
- documentation = cmp.config.window.bordered()
- },
- mapping = cmp.mapping.preset.insert({
- ['<C-k>'] = cmp.mapping.scroll_docs(-5),
- ['<C-j>'] = cmp.mapping.scroll_docs(5),
- ['<TAB>'] = cmp.mapping(function(fallback)
- if cmp.visible() then
- cmp.confirm({select = true })
- else
- fallback()
- end
- end, {'i', 's'})
- })
- })
- end
- },
+ cmp_path,
+ cmp_luasnip,
+ cmp_lua,
+ cmp_lsp,
+ cmp_buffer,
+ -- cmp_pandoc,
+ cmp,
-- {
-- 'kdheepak/cmp-latex-symbols',