neovim configuration using rocks.nvim plugin manager
1require("luasnip.session.snippet_collection").clear_snippets("norg")
2
3local ls = require("luasnip")
4local s = ls.snippet
5local sn = ls.snippet_node
6local i = ls.insert_node
7local f = ls.function_node
8local d = ls.dynamic_node
9local t = ls.text_node
10local k = require("luasnip.nodes.key_indexer").new_key
11local rep = require("luasnip.extras").rep
12local fmt = require("luasnip.extras.fmt").fmt
13
14local DATE_FORMAT = "!%Y-%m-%dT%H:%M:%S+09:00"
15
16local function today_fn()
17 return os.date(DATE_FORMAT)
18end
19
20-- TODO: reimplement these with git
21local function btime_fn()
22 local path = vim.fn.expand("%")
23 local stat = vim.uv.fs_stat(path)
24 assert(stat)
25 local btime = (stat.birthtime or stat.mtime).sec
26 return os.date(DATE_FORMAT, btime)
27end
28
29local function mtime_fn()
30 local path = vim.fn.expand("%")
31 local stat = vim.uv.fs_stat(path)
32 assert(stat)
33 local mtime = stat.mtime.sec
34 return os.date(DATE_FORMAT, mtime)
35end
36
37local function file_index_fn(_, snip)
38 local filename = vim.fn.expand("%:t:r")
39 local title
40 -- TODO: rewrite this with `vim.fs` lua api
41 if filename == "index" then
42 title = vim.fn.expand("%"):match("([^/]+)/[^/]+$")
43 else
44 title = vim.fn.substitute(snip.env.TM_FILENAME, "\\..*$", "", "g")
45 end
46 title = vim.fn.substitute(title, "-", " ", "g")
47 return sn(nil, { i(1, title) })
48end
49
50-- stylua: ignore
51ls.add_snippets("norg", {
52 s("!", fmt(
53 [[
54 @document.meta
55 title: {}
56 created: {}
57 updated: {}
58 @end
59
60 ]],
61 {
62 d(1, file_index_fn),
63 f(btime_fn),
64 f(mtime_fn),
65 }
66 )),
67 -- cool ranged-tag snippet
68 s(
69 { trig = "(@+)([^ ]+)(.*)", regTrig = true, wordTrig = false, name = "Dynamic Code Trigger" },
70 {
71 f(function(_, snip)
72 return snip.captures[1]
73 end, {}, { key = "prefix" }),
74 f(function(_, snip)
75 return snip.captures[2]
76 end),
77 d(1, function(_, snip)
78 local args = snip.captures[3]
79 return sn(nil, { i(1, args) })
80 end),
81 t{"", ""},
82 f(function(_, snip)
83 local current_line = snip.env.TM_CURRENT_LINE
84 local trigger_column = current_line:find(snip.trigger, 1, true)
85 return string.rep(" ", trigger_column - 1)
86 end, {}, { key = "indent" }),
87 i(2),
88 t{"", ""},
89 rep(k("indent")),
90 rep(k("prefix")),
91 t{"end", ""},
92 }
93 ),
94})