From 3a066b4c75550a65dc6986160a087cb74b4d2cd4 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Wed, 25 Dec 2024 18:35:17 +0800 Subject: [PATCH] Update bindings + luasnip config --- init.lua | 39 ++++++++++++++++++++++++--------------- lazy-lock.json | 1 + lua/snippets/c-cpp.lua | 23 +++++++++++++++++++++++ lua/snippets/common.lua | 27 +++++++++++++++++++++++++++ lua/snippets/init.lua | 4 ++++ lua/snippets/lua.lua | 20 ++++++++++++++++++++ 6 files changed, 99 insertions(+), 15 deletions(-) create mode 100644 lua/snippets/c-cpp.lua create mode 100644 lua/snippets/common.lua create mode 100644 lua/snippets/init.lua create mode 100644 lua/snippets/lua.lua diff --git a/init.lua b/init.lua index b962c91..0086139 100644 --- a/init.lua +++ b/init.lua @@ -197,8 +197,8 @@ vim.keymap.set('t', '', '', { desc = 'Exit terminal mode' } vim.keymap.set('n', 'pv', vim.cmd.Ex, { desc = 'File explorer' }) -- Quick fix list prev and next -vim.keymap.set('n', '', vim.cmd.cnext, { desc = 'Quick fix list next' }) -vim.keymap.set('n', '', vim.cmd.cprev, { desc = 'Quick fix list previous' }) +vim.keymap.set('n', '', vim.cmd.cnext, { desc = 'Quick fix list next', silent = true }) +vim.keymap.set('n', '', vim.cmd.cprev, { desc = 'Quick fix list previous', silent = true }) -- [[ User Commands ]] @@ -340,6 +340,7 @@ require('lazy').setup({ { 'l', group = '[L]SP Options' }, { 'h', group = 'Git [H]unk', mode = { 'n', 'v' } }, { 'm', group = '[M]iscellaneous', mode = { 'n', 'v' } }, + { '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', 'ms', 'source ~/.config/nvim/lua/snippets/init.lua', { desc = 'Reload snippets' }) cmp.setup { snippet = { @@ -988,10 +997,10 @@ require('lazy').setup({ { 'ThePrimeagen/harpoon', config = function() - vim.keymap.set('n', 'mq', function() + vim.keymap.set('n', 'nq', function() vim.cmd 'lua require("harpoon.ui").toggle_quick_menu()' end, { desc = 'Harpoon Menu' }) - vim.keymap.set('n', 'mm', function() + vim.keymap.set('n', 'nn', function() vim.cmd 'lua require("harpoon.mark").add_file()' end, { desc = 'Harpoon add file' }) vim.keymap.set('n', 'a', function() @@ -1006,16 +1015,16 @@ require('lazy').setup({ vim.keymap.set('n', 't', function() vim.cmd 'lua require("harpoon.ui").nav_file(4)' end, { desc = 'Harpoon file 4' }) - vim.keymap.set('n', 'ma', function() + vim.keymap.set('n', 'na', function() vim.cmd 'lua require("harpoon.ui").nav_file(5)' end, { desc = 'Harpoon file 5' }) - vim.keymap.set('n', 'mr', function() + vim.keymap.set('n', 'nr', function() vim.cmd 'lua require("harpoon.ui").nav_file(6)' end, { desc = 'Harpoon file 6' }) - vim.keymap.set('n', 'ms', function() + vim.keymap.set('n', 'ns', function() vim.cmd 'lua require("harpoon.ui").nav_file(7)' end, { desc = 'Harpoon file 7' }) - vim.keymap.set('n', 'mt', function() + vim.keymap.set('n', 'nt', function() vim.cmd 'lua require("harpoon.ui").nav_file(8)' end, { desc = 'Harpoon file 8' }) end, diff --git a/lazy-lock.json b/lazy-lock.json index 6a38f97..7f421bf 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -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" }, diff --git a/lua/snippets/c-cpp.lua b/lua/snippets/c-cpp.lua new file mode 100644 index 0000000..ccc07c6 --- /dev/null +++ b/lua/snippets/c-cpp.lua @@ -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 diff --git a/lua/snippets/common.lua b/lua/snippets/common.lua new file mode 100644 index 0000000..92402e3 --- /dev/null +++ b/lua/snippets/common.lua @@ -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 diff --git a/lua/snippets/init.lua b/lua/snippets/init.lua new file mode 100644 index 0000000..d2afcb1 --- /dev/null +++ b/lua/snippets/init.lua @@ -0,0 +1,4 @@ +require 'snippets.common' +require('luasnip.session.snippet_collection').clear_snippets() +require 'snippets.lua' +require 'snippets.c-cpp' diff --git a/lua/snippets/lua.lua b/lua/snippets/lua.lua new file mode 100644 index 0000000..a7a4441 --- /dev/null +++ b/lua/snippets/lua.lua @@ -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