From fbeb45e70be06b71bb3c5372db89d43a1209bde3 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Wed, 1 Jan 2025 13:59:07 -0800 Subject: [PATCH] feat: LuaSnip Haven't figured out personal snippets yet. --- nix/neovim-overlay.nix | 1 + nvim/lua/plugins/coding/blink-cmp.lua | 23 +++- nvim/lua/plugins/coding/init.lua | 1 + nvim/lua/plugins/coding/luasnip.lua | 21 ++++ nvim/lua/snippets/fish.lua | 107 +++++++++++++++++++ nvim/lua/snippets/gitconfig.lua | 8 ++ nvim/lua/snippets/helpers.lua | 40 +++++++ nvim/lua/snippets/html.lua | 29 +++++ nvim/lua/snippets/lua.lua | 11 ++ nvim/lua/snippets/nix.lua | 148 ++++++++++++++++++++++++++ 10 files changed, 388 insertions(+), 1 deletion(-) create mode 100644 nvim/lua/plugins/coding/luasnip.lua create mode 100644 nvim/lua/snippets/fish.lua create mode 100644 nvim/lua/snippets/gitconfig.lua create mode 100644 nvim/lua/snippets/helpers.lua create mode 100644 nvim/lua/snippets/html.lua create mode 100644 nvim/lua/snippets/lua.lua create mode 100644 nvim/lua/snippets/nix.lua diff --git a/nix/neovim-overlay.nix b/nix/neovim-overlay.nix index 210f564..c30515e 100644 --- a/nix/neovim-overlay.nix +++ b/nix/neovim-overlay.nix @@ -93,6 +93,7 @@ with final.pkgs.lib; let # Coding blink-cmp + luasnip friendly-snippets lexima-vim (mkNvimPlugin inputs.neotab-nvim "neotab.nvim") diff --git a/nvim/lua/plugins/coding/blink-cmp.lua b/nvim/lua/plugins/coding/blink-cmp.lua index e72f78c..6075572 100644 --- a/nvim/lua/plugins/coding/blink-cmp.lua +++ b/nvim/lua/plugins/coding/blink-cmp.lua @@ -2,7 +2,10 @@ return { 'blink.cmp', event = 'InsertEnter', before = function() - require('lz.n').trigger_load({ 'mini.icons' }) + require('lz.n').trigger_load({ + 'mini.icons', + 'luasnip', + }) end, after = function() ---@module 'blink.cmp' @@ -48,6 +51,24 @@ return { }, }, + snippets = { + expand = function(snippet) + require('luasnip').lsp_expand(snippet) + end, + + active = function(filter) + if filter and filter.direction then + return require('luasnip').jumpable(filter.direction) + end + + return require('luasnip').in_snippet() + end, + + jump = function(direction) + require('luasnip').jump(direction) + end, + }, + sources = { default = { 'lsp', 'path', 'snippets', 'buffer', 'luasnip' }, }, diff --git a/nvim/lua/plugins/coding/init.lua b/nvim/lua/plugins/coding/init.lua index 4b986d7..52bb2db 100644 --- a/nvim/lua/plugins/coding/init.lua +++ b/nvim/lua/plugins/coding/init.lua @@ -4,6 +4,7 @@ return { req('blink-cmp'), req('friendly-snippets'), req('lexima-vim'), + req('luasnip'), req('neotab-nvim'), req('ts-comments-nvim'), req('mini-ai'), diff --git a/nvim/lua/plugins/coding/luasnip.lua b/nvim/lua/plugins/coding/luasnip.lua new file mode 100644 index 0000000..d001c15 --- /dev/null +++ b/nvim/lua/plugins/coding/luasnip.lua @@ -0,0 +1,21 @@ +return { + 'luasnip', + lazy = true, + before = function() + require('lz.n').trigger_load({ + 'friendly-snippets', + }) + end, + after = function() + require('luasnip').setup({ + history = true, + delete_check_events = 'TextChanged', + }) + + -- friendly-snippets + require('luasnip.loaders.from_vscode').lazy_load() + + -- Personal snippets + require('luasnip.loaders.from_lua').load({ path = 'lua/snippets' }) + end, +} diff --git a/nvim/lua/snippets/fish.lua b/nvim/lua/snippets/fish.lua new file mode 100644 index 0000000..911294f --- /dev/null +++ b/nvim/lua/snippets/fish.lua @@ -0,0 +1,107 @@ +local ls = require("luasnip") +local s = ls.snippet +local t = ls.text_node +local i = ls.insert_node +local f = ls.function_node +local fmt = require("luasnip.extras.fmt").fmt +local fill = require("snippets.helpers").fill + +return { + s( + { trig = "dc", desc = "divider comment" }, + f(fill, {}, { user_args = { "#%- " } }) + ), + + s({ trig = "#!", desc = "shebang" }, t("#!/usr/bin/env fish")), + + s( + { trig = "if", desc = "if ... end" }, + fmt( + [[ + if {} + {} + end + ]], + { i(1), i(0) } + ) + ), + + s( + { trig = "elif", desc = "else if ..." }, + fmt( + [[ + else if {} + {} + ]], + { i(1), i(0) } + ) + ), + + s( + { trig = "fori", desc = "for ... in ... end" }, + fmt( + [[ + for {} in {} + {} + end + ]], + { i(1), i(2), i(0) } + ) + ), + + s( + { trig = "wh", desc = "while ... end" }, + fmt( + [[ + while {} + {} + end + ]], + { i(1), i(0) } + ) + ), + + s( + { trig = "wht", desc = "while true ... end" }, + fmt( + [[ + while true + {} + end + ]], + { i(0) } + ) + ), + + s( + { trig = "sw", desc = "switch ... case ... end" }, + fmt( + [[ + switch {} + case {} + {} + end + ]], + { i(1), i(2), i(0) } + ) + ), + + s( + { trig = "func", desc = "function ... end" }, + fmt( + [[ + function {} + {} + end + ]], + { i(1), i(0) } + ) + ), + + s({ trig = "dn", desc = "&>/dev/null" }, t("&>/dev/null")), + + s( + { trig = "abbr", desc = "abbr -a ... --position ..." }, + fmt('abbr -a {} --position {} "{}"', { i(1), i(2, "command"), i(3) }) + ), +} diff --git a/nvim/lua/snippets/gitconfig.lua b/nvim/lua/snippets/gitconfig.lua new file mode 100644 index 0000000..088e0ef --- /dev/null +++ b/nvim/lua/snippets/gitconfig.lua @@ -0,0 +1,8 @@ +local ls = require("luasnip") +local s = ls.snippet +local f = ls.function_node +local helpers = require("snippets.helpers") + +return { + s({ trig = "dc", desc = "divider comment" }, f(helpers.fill, {}, { user_args = { "#%- " } })), +} diff --git a/nvim/lua/snippets/helpers.lua b/nvim/lua/snippets/helpers.lua new file mode 100644 index 0000000..03ca79f --- /dev/null +++ b/nvim/lua/snippets/helpers.lua @@ -0,0 +1,40 @@ +local ls = require("luasnip") +local sn = ls.snippet_node +local i = ls.insert_node + +local M = {} + +M.fill = function(_, parent, args) + local title = "" + local chars = {} + + if #parent.snippet.env.LS_SELECT_RAW > 0 then + for _, ele in ipairs(parent.snippet.env.LS_SELECT_RAW) do + title = title .. ele .. " " + end + end + + for str in string.gmatch(args, "([^%%]+)") do + table.insert(chars, str) + end + + local snip = chars[1] .. " " .. title + + local _, c = unpack(vim.api.nvim_win_get_cursor(0)) + + while #snip < (vim.bo.tw - c - 1) do + snip = snip .. chars[2] + end + + return snip +end + +M.get_visual = function(_, parent) + if #parent.snippet.env.LS_SELECT_RAW > 0 then + return sn(nil, i(1, parent.snippet.env.LS_SELECT_RAW)) + else -- If LS_SELECT_RAW is empty, return a blank insert node + return sn(nil, i(1)) + end +end + +return M diff --git a/nvim/lua/snippets/html.lua b/nvim/lua/snippets/html.lua new file mode 100644 index 0000000..5a34b0d --- /dev/null +++ b/nvim/lua/snippets/html.lua @@ -0,0 +1,29 @@ +local ls = require("luasnip") +local s = ls.snippet +local i = ls.insert_node +local fmt = require("luasnip.extras.fmt").fmt + +return { + s( + { trig = "sd!", desc = "Saudade page" }, + fmt( + [[ + + + + + {} + + + + + + + {} + + + ]], + { i(1), i(0) } + ) + ), +} diff --git a/nvim/lua/snippets/lua.lua b/nvim/lua/snippets/lua.lua new file mode 100644 index 0000000..347fede --- /dev/null +++ b/nvim/lua/snippets/lua.lua @@ -0,0 +1,11 @@ +local ls = require('luasnip') +local s = ls.snippet +local f = ls.function_node +local fill = require('snippets.helpers').fill + +return { + s( + { trig = 'dc', desc = 'divider comment' }, + f(fill, {}, { user_args = { '--%-- ' } }) + ), +} diff --git a/nvim/lua/snippets/nix.lua b/nvim/lua/snippets/nix.lua new file mode 100644 index 0000000..b9b89c9 --- /dev/null +++ b/nvim/lua/snippets/nix.lua @@ -0,0 +1,148 @@ +local ls = require("luasnip") +local s = ls.snippet +local i = ls.insert_node +local f = ls.function_node +local fmta = require("luasnip.extras.fmt").fmta +local rep = require("luasnip.extras").rep +local fill = require("snippets.helpers").fill + +return { + s( + { trig = "dc", desc = "divider comment" }, + f(fill, {}, { user_args = { "#%- " } }) + ), + + s( + { trig = "mexpr", desc = "Basic module expression" }, + fmta( + [[ + { <>... }: + { + <> + } + ]], + { i(1), i(0) } + ) + ), + + s( + { trig = "ovr", desc = "Override" }, + fmta( + [[ + <> = prev.<>.override { + <> + }; + ]], + { i(1, "pkg"), rep(1), i(0) } + ) + ), + + s( + { trig = "ovra", desc = "OverrideAttrs" }, + fmta( + [[ + <> = prev.<>.overrideAttrs (old: { + <> + }); + ]], + { i(1, "pkg"), rep(1), i(0) } + ) + ), + + s( + { trig = "hpkg", desc = "home.packages list" }, + fmta( + [[ + home.packages = with pkgs; [ + <> + ];<> + ]], + { i(1), i(0) } + ) + ), + + s( + { trig = "shm", desc = "Snowfall lib home module" }, + fmta( + [[ + { + lib, + config,<> + ... + }: + let + inherit (lib) mkEnableOption mkIf;<> + + cfg = config.marleyos.<>.<>;<> + in + { + options.marleyos.<>.<>.enable = mkEnableOption "<>"; + + config = mkIf cfg.enable { + <> + }; + } + ]], + { + i(1), -- extra args + i(2, { "", " inherit (lib.marleyos) enabled;" }), + i(3), -- group + i(4), -- mod + i(5, { "", " inherit (config.marleyos.theme) colors;" }), + rep(3), -- group + rep(4), -- mod + rep(4), -- mod + i(0), -- end + } + ) + ), + + s( + { trig = "ith", desc = "Inherit theme definition" }, + fmta( + [[ + inherit (config.marleyos.theme) colors;<> + ]], + { i(0) } + ) + ), + + s( + { trig = "erpth", desc = "Enable rose-pine theme module option" }, + fmta( + [[ + rose-pine = mkIf colors.isRosePine enabled;<> + ]], + { i(0) } + ) + ), + + s( + { trig = "sov", desc = "Snowfall lib overlay" }, + fmta( + [[ + <>: final: prev: { + <> + } + ]], + { i(1, "_"), i(0) } + ) + ), + + s( + { trig = "nvk", desc = "NixVim keymap attr set" }, + fmta( + [[ + { + mode = [ "<>"<> ]; + key = "<>"; + action = "<>"; + options = { + desc = "<>";<> + }; + }<> + ]], + { i(1), i(2), i(3), i(4), i(5), i(6), i(0) } + ) + ), +}