summary refs log tree commit diff
path: root/.vim/snippets
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-04-30 13:02:14 +0200
committerRobert Günzler <r@gnzler.io>2025-04-30 13:02:14 +0200
commit1de7aa433c16e217e382c05c8fa4cf8d22b072d2 (patch)
tree3e396602d47cebda063a268e833dd4e7f2c7816d /.vim/snippets
parentf268916ae0e65c5bca2958f27bef55bfeebe514c (diff)
vim: add fancy snippets
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.vim/snippets')
-rw-r--r--.vim/snippets/all.lua83
-rw-r--r--.vim/snippets/sh_script.txt9
2 files changed, 77 insertions, 15 deletions
diff --git a/.vim/snippets/all.lua b/.vim/snippets/all.lua
index 6dfe009..0bcba08 100644
--- a/.vim/snippets/all.lua
+++ b/.vim/snippets/all.lua
@@ -1,4 +1,29 @@
 ---@diagnostic disable: undefined-global
+local events = require('luasnip.util.events')
+
+local fromglob = function(pattern, opts)
+  local choices = {}
+  for _, path in ipairs(vim.fn.glob(pattern, false, true, false)) do
+    choices[#choices + 1] = f(function(_, _, uargs)
+      return vim.fn.readfile(uargs)
+    end, {}, { user_args = { path } })
+  end
+  return sn(nil, { c(1, choices, opts) })
+end
+
+local commentwrap = function(opts)
+  return vim.tbl_extend('error', opts or {}, {
+    callbacks = {
+      [-1] = {
+        [events.leave] = function(node)
+          local start_ln = node:get_buf_position()[1] + 1
+          local end_ln = start_ln + #node:get_text() - 1
+          require('vim._comment').toggle_lines(start_ln, end_ln)
+        end,
+      },
+    },
+  })
+end
 
 return {
   -- mini symbols
@@ -30,26 +55,54 @@ return {
   ),
 
   s(
-    'license',
+    { trig = 'license' },
     d(1, function()
-      local choices = {}
-      for _, path in
-        ipairs(
-          vim.fn.glob(vim.fn.stdpath('config') .. '/snippets/license_*.txt', false, true, false)
-        )
-      do
-        choices[#choices + 1] = f(function(_, _, uargs)
-          return vim.fn.readfile(uargs)
-        end, {}, { user_args = { path } })
-      end
-      return sn(nil, { c(1, choices, { name = 'FOSS licenses' }) })
+      local pattern = vim.fn.stdpath('config') .. '/snippets/license_*.txt'
+      return fromglob(pattern, { name = 'LICENSE templates' })
     end, nil)
   ),
 
   s(
-    'todo',
+    { trig = 'spdx' },
     d(1, function()
-      return sn(nil, fmt(string.format(vim.bo.commentstring, ' TODO: {} '), { i(1) }))
-    end, nil)
+      local pattern = vim.fn.stdpath('config') .. '/snippets/spdx_*.txt'
+      return fromglob(pattern, { name = 'SPDX header templates' })
+    end),
+    commentwrap(nil)
+  ),
+
+  s(
+    { trig = 'todo' },
+    isn(1, {
+      c(1, {
+        sn(nil, { i(1, 'TODO') }),
+        sn(nil, { i(1, 'NOTE') }),
+        sn(nil, { i(1, 'XXX') }),
+      }),
+      c(2, {
+        sn(nil, { i(1) }),
+        sn(nil, { t('('), i(1, 'robertgzr'), t(')') }),
+      }),
+      t(': '),
+      i(3, 'your comment here'),
+    }, ''),
+    commentwrap(nil)
+  ),
+
+  s(
+    { trig = '#!/bin' },
+    d(1, function()
+      local pattern = vim.fn.stdpath('config') .. '/snippets/sh_*.txt'
+      return fromglob(pattern, { name = 'Shell script template' })
+    end, {}),
+    {
+      callbacks = {
+        [-1] = {
+          [events.pre_expand] = function()
+            vim.opt_local.filetype = 'sh'
+          end,
+        },
+      },
+    }
   ),
 }
diff --git a/.vim/snippets/sh_script.txt b/.vim/snippets/sh_script.txt
new file mode 100644
index 0000000..eab45a3
--- /dev/null
+++ b/.vim/snippets/sh_script.txt
@@ -0,0 +1,9 @@
+#!/bin/sh
+set -eu
+[ -n "${DEBUG:-}" ] && set -x
+
+main() {
+  printf 'Hello world\n'
+}
+
+main "$@"