btw i use nix
1local ls = require('luasnip')
2require("luasnip.loaders.from_vscode").lazy_load()
3local s = ls.snippet
4local f = ls.function_node
5local t = ls.text_node
6local i = ls.insert_node
7local c = ls.choice_node
8local sn = ls.snippet_node
9local d = ls.dynamic_node
10
11local function date_input()
12 return os.date('%Y-%m-%d')
13end
14
15local function amount_spacing(args)
16 local account_length = #args[1][1]
17 local amount = args[2][1]
18 if amount == '' then
19 return ''
20 end
21 local desired_column = vim.g.ledger_align_at or 50
22 local current_column = 2 + account_length + 2 -- 2 spaces after account
23 local period_position = amount:find('%.') or (#amount + 1)
24 local pre_period_length = period_position - 1
25 local total_length = current_column + pre_period_length
26 local spaces_needed = desired_column - total_length
27 if spaces_needed < 0 then spaces_needed = 1 end
28 return string.rep(' ', spaces_needed)
29end
30
31local function recursive_accounts()
32 return sn(nil, {
33 t({ '', ' ' }), i(1, 'Account'), f(amount_spacing, { 1, 2 }),
34 c(2, {
35 t(''),
36 sn(nil, { i(1, '£') })
37 }),
38 c(3, {
39 t(''),
40 d(nil, recursive_accounts)
41 })
42 })
43end
44
45ls.add_snippets('all', {
46 s('d', {
47 f(date_input)
48 }),
49 s('ledger', {
50 i(1, f(date_input)), t(' '), i(2, 'Description'),
51 c(3, {
52 sn(nil, { t({ '', ' ; ' }), i(1, 'Comment') }),
53 t(''),
54 }),
55 t({ '', ' ' }), i(4, 'Account'), f(amount_spacing, { 4, 5 }),
56 c(5, {
57 sn(nil, { i(1, '£') }),
58 t(''),
59 }),
60 d(6, recursive_accounts),
61 t({ '', '', '' }),
62 }),
63})
64ls.add_snippets('mail', {
65 s('attach', {
66 t({ '<#part filename=' }),
67 i(1, '~/'),
68 c(2, {
69 sn(nil, { t({ ' description="', }), i(1, ''), t({ '"', }) }),
70 t(''),
71 }),
72 t({ '><#/part>', '' }),
73 }),
74})
75
76
77vim.keymap.set({ 'i' }, '<C-k>', function() ls.expand() end, { silent = true; desc = 'Snip complete' })
78vim.keymap.set({ 'i', 's' }, '<C-l>', function() ls.jump(1) end, { silent = true; desc = 'Snip next' })
79vim.keymap.set({ 'i', 's' }, '<C-h>', function() ls.jump(-1) end, { silent = true; desc = 'Snip prev' })
80vim.keymap.set({ 'i', 's' }, '<C-j>', function()
81 if ls.choice_active() then
82 ls.change_choice(1)
83 end
84end, { silent = true; desc = 'Snip choice' })