Neovim: reworked most of the completion stuff, 80% done, will continue tmr

This commit is contained in:
Kyren223
2024-07-08 01:13:30 +03:00
parent fb07fbdefd
commit b491f9e3a7
5 changed files with 127 additions and 51 deletions

View File

@@ -1,6 +1,9 @@
# Plans for the future
- TODO: Refactor stuff to `plugin` folder (keymaps and options) and also `after` folder for filetype-specific things
p.s if some1 reads this and gets triggered by folders, GET REKT
- TODO: Look into bufferline.nvim for "tabs"
- TODO: Look at configuring Noice.nvim and decide if u like it or not
- TODO: Look at buffer-management plugins that automatically delete old buffers (and also ways to close buffers without exiting nvim, and be able to exit nvim with 1 keypress)
@@ -8,8 +11,6 @@
- TODO: Add refactoring keys like <leader>iv for introduce variable, extract method, introduce constant etc
- TODO: Decided which outline-plugin to use (lspsaga or aerial, or smthing else)
- TODO: Finish configuring lsp saga (I think it has supertypes and stuff which aren't in the doc)
- TODO: Remove the vsc*de snippets/completions and make my own luasnip snippets [tutorial](https://www.youtube.com/watch?v=FmHhonPjvvA)
- TODO: Setup [nvim spectre](https://github.com/nvim-pack/nvim-spectre)
- TODO: learn what dot repeat is
@@ -22,14 +23,11 @@ Alt enter should show a list of code actions
A lightbulb somewhere ONLY if the cursor (not line) has a code action and ONLY if it's a quickfix
## TODO: Completions and snippets
Remove all the bs vscode snippets
Make my own snippets per file type
Learn how to navigate between snippet params
HAVE ACTUAL GOOD SORTING FOR SNIPPETS (hopefully this is possible)
when there is an lsp active, it shouldn't show any other types like regex
*when there is an lsp active, it shouldn't show any other types like regex
it should just show lsp stuff
# Look at in the future
- Database integration plugins

View File

@@ -10,8 +10,8 @@ vim.api.nvim_create_autocmd('FileType', {
group = vim.api.nvim_create_augroup('disable-auto-comment', { clear = true }),
pattern = '*',
callback = function()
-- autowrap using textwidth | enter in insert mode | o/O in normal mode
-- c - autowrap using textwidth | r - enter in insert mode | o - o/O in normal mode
-- vim.opt.formatoptions:remove({ 'c', 'r', 'o' })
vim.cmd('set formatoptions-=cro')
vim.cmd('set formatoptions-=co')
end,
})

View File

@@ -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')

View File

@@ -1,50 +1,19 @@
return {
{
'hrsh7th/cmp-nvim-lsp',
},
{
'L3MON4D3/LuaSnip',
dependencies = {
'saadparwaiz1/cmp_luasnip',
'rafamadriz/friendly-snippets',
},
},
{
'hrsh7th/nvim-cmp',
opts = function(_, opts)
-- Setup options for lazydev.nvim
opts.sources = opts.sources or {}
table.insert(opts.sources, {
name = 'lazydev',
group_index = 0,
})
end,
lazy = false,
priority = 100,
dependencies = {
-- 'onsails/lspkind.nvim',
-- 'hrsh7th/cmp-path',
-- 'hrsh7th/cmp-buffer',
'hrsh7th/cmp-nvim-lsp', -- lsp source for nvim-cmp
'saadparwaiz1/cmp_luasnip', -- snippets source for nvim-cmp
{ 'L3MON4D3/LuaSnip', build = 'make install_jsregexp' }, -- snippets plugin
},
-- TODO: Move to setup opts
config = function()
local cmp = require('cmp')
require('luasnip.loaders.from_vscode').lazy_load()
cmp.setup({
snippet = {
expand = function(args)
require('luasnip').lsp_expand(args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-k>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' },
{ name = 'luasnip' },
}, { { name = 'buffer' } }),
})
require('custom.completion')
end,
},
}

View File

@@ -0,0 +1,10 @@
require('luasnip.session.snippet_collection').clear_snippets('lua')
local ls = require('luasnip')
local s = ls.snippet
local i = ls.insert_node
local fmt = require('luasnip.extras.fmt').fmt
ls.add_snippets('lua', {
s('fu', fmt('function {}({})\n {}\nend', { i(1), i(2), i(0) })),
})