feat: LuaSnip

Haven't figured out personal snippets yet.
This commit is contained in:
punkfairie 2025-01-01 13:59:07 -08:00
parent 6275e43282
commit fbeb45e70b
Signed by: punkfairie
GPG key ID: A509E8F77FB9D696
10 changed files with 388 additions and 1 deletions

View file

@ -93,6 +93,7 @@ with final.pkgs.lib; let
# Coding
blink-cmp
luasnip
friendly-snippets
lexima-vim
(mkNvimPlugin inputs.neotab-nvim "neotab.nvim")

View file

@ -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' },
},

View file

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

View file

@ -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,
}

107
nvim/lua/snippets/fish.lua Normal file
View file

@ -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) })
),
}

View file

@ -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 = { "#%- " } })),
}

View file

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

View file

@ -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(
[[
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{}</title>
<link rel="stylesheet" href="/style.css" />
<!-- saudade includes -->
<script src="/saudade.js@root" type="module"></script>
</head>
<body>
{}
</body>
</html>
]],
{ i(1), i(0) }
)
),
}

11
nvim/lua/snippets/lua.lua Normal file
View file

@ -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 = { '--%-- ' } })
),
}

148
nvim/lua/snippets/nix.lua Normal file
View file

@ -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) }
)
),
}