summary refs log tree commit diff
path: root/.vim/lua/snips.lua
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-04-04 12:00:29 +0200
committerRobert Günzler <r@gnzler.io>2022-04-04 12:00:29 +0200
commit06a1cd6ede3ee876c4b1206769985b79e3559c5d (patch)
tree3319602c85184c56410ab0da4f0114cfb905aafd /.vim/lua/snips.lua
parentadad823f99bea2a20bae2efbd92e5cd5bae8eb8b (diff)
vim: use luasnip
Diffstat (limited to '')
-rw-r--r--.vim/lua/snips.lua78
1 files changed, 46 insertions, 32 deletions
diff --git a/.vim/lua/snips.lua b/.vim/lua/snips.lua
index b8c8b77..c4a58af 100644
--- a/.vim/lua/snips.lua
+++ b/.vim/lua/snips.lua
@@ -1,36 +1,50 @@
-local M = {}
 
-M.exec = function(cmd)
-  return io.popen(cmd):read'*l'
-end
+local ls = require("luasnip")
+local fmt = require('luasnip.extras.fmt').fmt
 
-local function find_sub(s, replacement, pattern, start, special)
-  local x1, x2 = s:find(pattern, start, special)
-  if x1 then
-    return s:sub(1, x1-1) .. replacement .. s:sub(x2+1), x1, x2
-  end
-  return s
-end
--- Only works with prefix comments.
-local function get_line_comment()
-  local cms = vim.bo.commentstring
-  -- This whole pesc dance is a bummer.
-  local pattern = '^(%s*)' ..
-  (find_sub(vim.pesc((find_sub(cms, '\0', '%s', 1, true))), '(%s*).*', '\0', 1, true))
-  local pre_ws, inner_ws = vim.api.nvim_get_current_line():match(pattern)
-  if pre_ws then
-    return pre_ws .. cms:format('') .. inner_ws
-  end
-  return ''
-end
-M.comment = function(str)
-  -- add space
-  local prefix = vim.bo.commentstring:format(''):gsub('%S$', '%0 ')
-  local comment = get_line_comment()
-  if comment ~= '' then
-    prefix = ''
-  end
-  return prefix .. str
+local s = ls.s
+local t = ls.text_node
+local f = ls.function_node
+-- local i = ls.insert_node
+
+local exec = function(command)
+  return f(function(_, _, arg)
+    local p = io.popen(arg, 'r')
+    local res = {}
+    for line in p:lines() do
+      table.insert(res, line)
+    end
+    return res
+  end, {}, { user_args = { command }})
 end
 
-return M
+-- snipmate snippets
+require('luasnip.loaders.from_snipmate').load({ paths = {'~/.config/nvim/after/snippets'} })
+
+-- luasnip snippets
+ls.add_snippets('all', {
+  s(
+    'sign',
+    t('Robert Günzler <r@gnzler.io>')
+  ),
+
+  s(
+    'me',
+    fmt(
+      [[{} <{}>]], {
+        exec('git config --get user.name'),
+        exec('git config --get user.email'),
+      }
+    )
+  ),
+
+  s(
+    'today',
+    f(function() return os.date('%Y-%m-%d') end, {})
+  ),
+
+  s(
+    'now',
+    f(function() return os.date('%Y-%m-%dT%H:%M:%SZ') end, {})
+  )
+})