summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/custom/completion.lua
diff options
context:
space:
mode:
authorKyren223 <ulmliad223@gmail.com>2024-07-08 01:13:30 +0300
committerKyren223 <ulmliad223@gmail.com>2024-07-08 01:13:30 +0300
commitb491f9e3a73367dfd54ca8f8bc74008332c80f34 (patch)
treebf9c2f515d71291d941979d462176b9266c6308f /.config/nvim/lua/custom/completion.lua
parentfb07fbdefd9cbe7d3d8a185812d0a930d7ddc4ab (diff)
Neovim: reworked most of the completion stuff, 80% done, will continue tmr
Diffstat (limited to '.config/nvim/lua/custom/completion.lua')
-rw-r--r--.config/nvim/lua/custom/completion.lua99
1 files changed, 99 insertions, 0 deletions
diff --git a/.config/nvim/lua/custom/completion.lua b/.config/nvim/lua/custom/completion.lua
index e69de29..50df64b 100644
--- a/.config/nvim/lua/custom/completion.lua
+++ b/.config/nvim/lua/custom/completion.lua
@@ -0,0 +1,99 @@
+require('custom.snippets.lua')
+local luasnip = require('luasnip')
+local cmp = require('cmp')
+
+-- Enable paretheses for function completions
+cmp.event:on('confirm_done', require('nvim-autopairs.completion.cmp').on_confirm_done())
+
+-- Helper functions
+local function tab(is_forward)
+ cmp.mapping(function(fallback)
+ local jump_amount = is_forward and 1 or -1
+ if cmp.visible() then
+ local behaviour = cmp.SelectBehavior.Select
+ local select_item = is_forward and cmp.select_next_item or cmp.select_prev_item
+ select_item({ behaviour = behaviour })
+ elseif luasnip.locally_jumpable(jump_amount) then
+ luasnip.jump(jump_amount)
+ else
+ fallback()
+ end
+ end, { 'i', 's' })
+end
+
+local function enter() end
+
+local function is_enabled()
+ local context = require('cmp.config.context')
+
+ local is_in_comment = context.in_treesitter_capture('comment') or context.in_syntax_group('Comment')
+ if is_in_comment then
+ return false
+ end
+
+ return true
+end
+
+cmp.setup({
+ enabled = is_enabled,
+ completion = { completeopt = 'menu,menuone,noinsert' },
+ sources = cmp.config.sources({
+ { name = 'nvim_lsp' },
+ { name = 'luasnip' },
+ { name = 'lazydev', group_index = 0 }, -- for lazydev.nvim
+ { name = 'path' }, -- for filesystem
+ }),
+ snippet = {
+ expand = function(args)
+ luasnip.lsp_expand(args.body)
+ end,
+ },
+ window = {
+ -- TODO: Decide if I like borders or not
+ completion = cmp.config.window.bordered(),
+ documentation = cmp.config.window.bordered(),
+ },
+ mapping = cmp.mapping.preset.insert({
+ -- TODO: Make good keybindings
+ ['<Tab>'] = tab(true),
+ ['<S-Tab>'] = tab(false),
+ ['<CR>'] = cmp.mapping(function(fallback)
+ if cmp.visible() then
+ if luasnip.expandable() then
+ luasnip.expand()
+ else
+ cmp.confirm({ select = true })
+ end
+ else
+ fallback()
+ end
+ end),
+
+ -- NOTE: Select makes it so it doesn't put the text in the buffer (like Insert or defualt does)
+ -- Ex: typing "p" and then select "private" will not write "private" in your buffer, but keep it "p"
+ -- Only after accepting the suggestion then it'll become "private"
+ ['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
+ ['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
+
+ ['<CR>'] = cmp.mapping(
+ cmp.mapping.confirm({ select = true, behavior = cmp.ConfirmBehavior.Insert }),
+ { 'i', 'c' }
+ ),
+
+ ['<C-Space>'] = cmp.mapping.complete(), -- invoke completion
+ ['<C-u>'] = cmp.mapping.scroll_docs(-4),
+ ['<C-d>'] = cmp.mapping.scroll_docs(4),
+
+ -- ['<C-k>'] = cmp.mapping.abort(),
+ -- ['<CR>'] = cmp.mapping.confirm({ select = true }),
+ -- ['<C-y>'] = cmp.mapping(
+ -- cmp.mapping.confirm({
+ -- behavior = cmp.ConfirmBehavior.Insert,
+ -- select = true,
+ -- }),
+ -- { 'i', 'c' }
+ -- ),
+ }),
+})
+
+print('Sourced lsp.lua')