Update bindings + luasnip config

This commit is contained in:
Sheldon Lee 2024-12-25 18:35:17 +08:00
parent de9fd67b6f
commit 3a066b4c75
6 changed files with 99 additions and 15 deletions

View File

@ -197,8 +197,8 @@ vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' }
vim.keymap.set('n', '<leader>pv', vim.cmd.Ex, { desc = 'File explorer' })
-- Quick fix list prev and next
vim.keymap.set('n', '<C-j>', vim.cmd.cnext, { desc = 'Quick fix list next' })
vim.keymap.set('n', '<C-k>', vim.cmd.cprev, { desc = 'Quick fix list previous' })
vim.keymap.set('n', '<C-j>', vim.cmd.cnext, { desc = 'Quick fix list next', silent = true })
vim.keymap.set('n', '<C-k>', vim.cmd.cprev, { desc = 'Quick fix list previous', silent = true })
-- [[ User Commands ]]
@ -340,6 +340,7 @@ require('lazy').setup({
{ '<leader>l', group = '[L]SP Options' },
{ '<leader>h', group = 'Git [H]unk', mode = { 'n', 'v' } },
{ '<leader>m', group = '[M]iscellaneous', mode = { 'n', 'v' } },
{ '<leader>n', group = 'Extra harpon layer', mode = { 'n', 'v' } },
},
},
},
@ -765,12 +766,12 @@ require('lazy').setup({
-- `friendly-snippets` contains a variety of premade snippets.
-- See the README about individual language/framework/plugin snippets:
-- https://github.com/rafamadriz/friendly-snippets
-- {
-- 'rafamadriz/friendly-snippets',
-- config = function()
-- require('luasnip.loaders.from_vscode').lazy_load()
-- end,
-- },
{
'rafamadriz/friendly-snippets',
config = function()
require('luasnip.loaders.from_vscode').lazy_load()
end,
},
},
},
'saadparwaiz1/cmp_luasnip',
@ -785,7 +786,15 @@ require('lazy').setup({
-- See `:help cmp`
local cmp = require 'cmp'
local luasnip = require 'luasnip'
luasnip.config.setup {}
luasnip.config.setup {
history = true,
}
require 'snippets'
-- TODO: Find out how to reload snippets properly.
-- Currently returning false at the end of each file required by
-- init.lua below to invalidate cache entry for require.
vim.keymap.set('n', '<leader>ms', '<cmd>source ~/.config/nvim/lua/snippets/init.lua<CR>', { desc = 'Reload snippets' })
cmp.setup {
snippet = {
@ -988,10 +997,10 @@ require('lazy').setup({
{
'ThePrimeagen/harpoon',
config = function()
vim.keymap.set('n', '<leader>mq', function()
vim.keymap.set('n', '<leader>nq', function()
vim.cmd 'lua require("harpoon.ui").toggle_quick_menu()'
end, { desc = 'Harpoon Menu' })
vim.keymap.set('n', '<leader>mm', function()
vim.keymap.set('n', '<leader>nn', function()
vim.cmd 'lua require("harpoon.mark").add_file()'
end, { desc = 'Harpoon add file' })
vim.keymap.set('n', '<leader>a', function()
@ -1006,16 +1015,16 @@ require('lazy').setup({
vim.keymap.set('n', '<leader>t', function()
vim.cmd 'lua require("harpoon.ui").nav_file(4)'
end, { desc = 'Harpoon file 4' })
vim.keymap.set('n', '<leader>ma', function()
vim.keymap.set('n', '<leader>na', function()
vim.cmd 'lua require("harpoon.ui").nav_file(5)'
end, { desc = 'Harpoon file 5' })
vim.keymap.set('n', '<leader>mr', function()
vim.keymap.set('n', '<leader>nr', function()
vim.cmd 'lua require("harpoon.ui").nav_file(6)'
end, { desc = 'Harpoon file 6' })
vim.keymap.set('n', '<leader>ms', function()
vim.keymap.set('n', '<leader>ns', function()
vim.cmd 'lua require("harpoon.ui").nav_file(7)'
end, { desc = 'Harpoon file 7' })
vim.keymap.set('n', '<leader>mt', function()
vim.keymap.set('n', '<leader>nt', function()
vim.cmd 'lua require("harpoon.ui").nav_file(8)'
end, { desc = 'Harpoon file 8' })
end,

View File

@ -5,6 +5,7 @@
"cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" },
"conform.nvim": { "branch": "master", "commit": "339b3e4519ec49312d34fcfa275aa15bfaa67025" },
"fidget.nvim": { "branch": "main", "commit": "9238947645ce17d96f30842e61ba81147185b657" },
"friendly-snippets": { "branch": "main", "commit": "efff286dd74c22f731cdec26a70b46e5b203c619" },
"gitsigns.nvim": { "branch": "main", "commit": "5f808b5e4fef30bd8aca1b803b4e555da07fc412" },
"gruvbox.nvim": { "branch": "main", "commit": "68c3460a5d1d1a362318960035c9f3466d5011f5" },
"harpoon": { "branch": "master", "commit": "1bc17e3e42ea3c46b33c0bbad6a880792692a1b3" },

23
lua/snippets/c-cpp.lua Normal file
View File

@ -0,0 +1,23 @@
ls.add_snippets('c', {
s('fn', {
i(1, 'void'),
t ' ',
i(2, 'func_name'),
t '(',
i(3),
t ') ',
t '{ ',
i(4, '...'),
t ' }',
}),
s('test', {
t 'printf("',
i(1),
t '");',
}),
})
ls.filetype_extend('cpp', { 'c' })
-- Return false to invalidate cache entry for require()
-- This is so sourcing snippets/init.lua also "reloads" this file
-- https://stackoverflow.com/questions/72242451/sourcing-nvim-init-does-not-source-required-lua-files
return false

27
lua/snippets/common.lua Normal file
View File

@ -0,0 +1,27 @@
ls = require 'luasnip'
s = ls.snippet
sn = ls.snippet_node
isn = ls.indent_snippet_node
t = ls.text_node
i = ls.insert_node
f = ls.function_node
c = ls.choice_node
d = ls.dynamic_node
r = ls.restore_node
events = require 'luasnip.util.events'
ai = require 'luasnip.nodes.absolute_indexer'
extras = require 'luasnip.extras'
l = extras.lambda
rep = extras.rep
p = extras.partial
m = extras.match
n = extras.nonempty
dl = extras.dynamic_lambda
fmt = require('luasnip.extras.fmt').fmt
fmta = require('luasnip.extras.fmt').fmta
conds = require 'luasnip.extras.expand_conditions'
postfix = require('luasnip.extras.postfix').postfix
types = require 'luasnip.util.types'
parse = require('luasnip.util.parser').parse_snippet
ms = ls.multi_snippet
k = require('luasnip.nodes.key_indexer').new_key

4
lua/snippets/init.lua Normal file
View File

@ -0,0 +1,4 @@
require 'snippets.common'
require('luasnip.session.snippet_collection').clear_snippets()
require 'snippets.lua'
require 'snippets.c-cpp'

20
lua/snippets/lua.lua Normal file
View File

@ -0,0 +1,20 @@
require('luasnip.session.snippet_collection').clear_snippets()
ls.add_snippets('lua', {
s('func', {
t 'function() ',
i(1),
t ' end',
}),
s('if', {
t 'if ',
i(1, 'true'),
t ' then ',
i(2),
t ' end',
}),
})
-- Return false to invalidate cache entry for require()
-- This is so sourcing snippets/init.lua also "reloads" this file
-- https://stackoverflow.com/questions/72242451/sourcing-nvim-init-does-not-source-required-lua-files
return false