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( + [[ + +
+ + +