feat: extras.coding.luasnip

This commit is contained in:
punkfairie 2025-03-07 19:03:25 -08:00
parent 744b1ad145
commit b70909f429
Signed by: punkfairie
GPG key ID: B3C5488E9A1A7CA6
5 changed files with 311 additions and 9 deletions

View file

@ -21,13 +21,15 @@ local lazyOptions = {
},
}
-- NOTE: this the lazy wrapper. Use it like require('lazy').setup() but with an extra
-- argument, the path to lazy.nvim as downloaded by nix, or nil, before the normal arguments.
-- NOTE: this the lazy wrapper. Use it like require('lazy').setup() but with an
-- extra argument, the path to lazy.nvim as downloaded by nix, or nil, before
-- the normal arguments.
require('nixCatsUtils.lazyCat').setup(nixCats.pawsible { 'allPlugins', 'start', 'lazy.nvim' }, {
{ 'LazyVim/LazyVim', import = 'lazyvim.plugins' },
-- disable mason.nvim while using nix
-- precompiled binaries do not agree with nixos, and we can just make nix install this stuff for us.
-- Disable mason.nvim while using nix.
-- Precompiled binaries do not agree with nixos, and we can just make nix
-- install this stuff for us.
{ 'williamboman/mason-lspconfig.nvim', enabled = require('nixCatsUtils').lazyAdd(true, false) },
{ 'williamboman/mason.nvim', enabled = require('nixCatsUtils').lazyAdd(true, false) },
@ -36,23 +38,26 @@ require('nixCatsUtils.lazyCat').setup(nixCats.pawsible { 'allPlugins', 'start',
ft = 'lua',
opts = {
library = {
-- adds type hints for nixCats global, but LazyDev is just nice in general
-- Adds type hints for nixCats global, but LazyDev is just nice in
-- general.
{ path = (nixCats.nixCatsPath or '') .. '/lua', words = { 'nixCats' } },
},
},
},
-- extras
-- Extras.
{ import = 'lazyvim.plugins.extras.coding.luasnip' },
{ import = 'lazyvim.plugins.extras.lang.nix' },
-- this needs to be after extras to prevent extras from overwriting it
-- This needs to be after extras to prevent extras from overwriting it.
{
'nvim-treesitter/nvim-treesitter',
build = require('nixCatsUtils').lazyAdd ':TSUpdate',
opts_extend = require('nixCatsUtils').lazyAdd(nil, false),
opts = {
-- nix already ensured they were installed, and we would need to change the parser_install_dir if we wanted to use it instead.
-- so we just disable install and do it via nix.
-- Nix already ensured they were installed, and we would need to change
-- the parser_install_dir if we wanted to use it instead.
-- So we just disable install and do it via nix.
ensure_installed = require('nixCatsUtils').lazyAdd({ 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'vim', 'vimdoc' }, false),
auto_install = require('nixCatsUtils').lazyAdd(true, false),
},

14
lua/plugins/luasnip.lua Normal file
View file

@ -0,0 +1,14 @@
return {
'L3MON4D3/LuaSnip',
opts = {
store_selection_keys = '<TAB>',
update_events = 'TextChanged,TextChangedI',
},
config = function(_, opts)
require('luasnip').setup(opts)
require('luasnip.loaders.from_lua').load { paths = {
nixCats.configDir .. '/lua/snippets',
} }
end,
}

101
lua/snippets/fish.lua Normal file
View file

@ -0,0 +1,101 @@
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) })),
}

40
lua/snippets/helpers.lua Normal file
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 type(parent.snippet.env.LS_SELECT_RAW) == 'table' 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

142
lua/snippets/nix.lua Normal file
View file

@ -0,0 +1,142 @@
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 = 'sov', desc = 'Snowfall lib overlay' },
fmta(
[[
<>: final: prev: {
<>
}
]],
{ i(1, '_'), i(0) }
)
),
s(
{ trig = 'sovi', desc = 'Snowfall lib overlay pulling from inputs' },
fmta(
[[
{inputs, ...}: final: prev: {
<> = inputs.<>.<>."${prev.system}".<>;
}
]],
{ i(1, 'pkg'), i(2, 'input'), i(3, 'packages'), i(4, 'default') }
)
),
s(
{ trig = 'sovc', desc = 'Snowfall lib overlay pulling from channels' },
fmta(
[[
{channels, ...}: final: prev: {
inherit (channels.<>) <>;
}
]],
{ i(1, 'unstable'), i(2, 'pkg') }
)
),
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 = 'ospkg', desc = 'NixOS environment.systemPackages list' },
fmta(
[[
environment.systemPackages = with pkgs; [
<>
];<>
]],
{ i(1), i(0) }
)
),
s(
{ trig = 'smod', desc = 'Snowfall lib module' },
fmta(
[[
{
lib,
config,<>
...
}:
let<>
cfg = config.marleyos.<>.<>;
in
{
options.marleyos.<>.<>.enable = mkEnableOption "<>";
config = lib.mkIf cfg.enable {
<>
};
}
]],
{
i(1), -- extra args
i(2, { '', ' inherit (lib.marleyos) enabled;' }),
i(3), -- group
i(4), -- mod
rep(3), -- group
rep(4), -- mod
rep(4), -- mod
i(0), -- end
}
)
),
}