summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.vim/after/snippets/c.snippets1
-rw-r--r--.vim/after/snippets/go.snippets20
-rw-r--r--.vim/lua/snips.lua188
-rw-r--r--.vim/snippets/all.lua53
-rw-r--r--.vim/snippets/gitcommit.lua4
-rw-r--r--.vim/snippets/go.lua28
-rw-r--r--.vim/snippets/lua.lua18
7 files changed, 186 insertions, 126 deletions
diff --git a/.vim/after/snippets/c.snippets b/.vim/after/snippets/c.snippets
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/.vim/after/snippets/c.snippets
@@ -0,0 +1 @@
+
diff --git a/.vim/after/snippets/go.snippets b/.vim/after/snippets/go.snippets
new file mode 100644
index 0000000..84df9a1
--- /dev/null
+++ b/.vim/after/snippets/go.snippets
@@ -0,0 +1,20 @@
+snippet memfd
+	func newMemFd(data []byte) (*io.File, error) {
+	  fd, err := unix.MemfdCreate("memfd", 0)
+	  if err != nil {
+		  return nil, err
+	  }
+	  if err := unix.Ftruncate(fd, int64(len(data))); err != nil {
+		  return nil, err
+	  }
+	  var mem []byte
+	  mem, err = unix.Mmap(fd, 0, len(data), unix.PROT_READ|unix.PROT_WRITE, unix.MAP_SHARED)
+	  if err != nil {
+		  return nil, err
+	  }
+	  copy(mem, data)
+	  if err := unix.Munmap(mem); err != nil {
+		  return nil, err
+	  }
+	  return os.NewFile(uintptr(fd), fmt.Sprintf("/proc/self/fd/%d", fd)), nil
+	}
diff --git a/.vim/lua/snips.lua b/.vim/lua/snips.lua
index 31d9dd4..b22d945 100644
--- a/.vim/lua/snips.lua
+++ b/.vim/lua/snips.lua
@@ -1,95 +1,22 @@
 
-local ls = require("luasnip")
-local fmt = require('luasnip.extras.fmt').fmt
-
-local ls_namespace = vim.api.nvim_create_namespace('luasnip')
-local group = vim.api.nvim_create_augroup('LuaSnip', { clear = true })
-
-vim.api.nvim_create_autocmd('ModeChanged', {
-  callback = function(opts)
-    if ls.session.current_nodes[opts.buf]
-        and ((vim.v.event.old_mode == 's' and vim.v.event.new_mode == 'n') or vim.v.event.old_mode == 'i')
-    then
-      ls.unlink_current()
-      ls.cleanup()
-    end
-  end,
-})
-vim.api.nvim_create_autocmd({'CursorHold', 'CursorHoldI'}, {
-  callback = function(opts)
-    vim.api.nvim_buf_clear_namespace(opts.buf, ls_namespace, 0, -1)
-
-    if ls.session.current_nodes[opts.buf]
-        and ls.choice_active()
-    then
-      vim.notify('current nodes: ' .. #ls.session.current_nodes[opts.buf])
-      local pos = vim.api.nvim_win_get_cursor(0)
-      vim.api.nvim_buf_set_extmark(opts.buf, ls_namespace, pos[1] - 1, pos[2], {
-        virt_text = {{'<-- choice node', 'Comment'}},
-        virt_text_pos = 'eol',
-        hl_mode = 'combine',
-      })
-    end
-  end,
-  group = group
+local ls = require('luasnip')
+local types = require('luasnip.util.types')
+
+vim.api.nvim_set_hl(0, 'LuasnipIndicator', {
+  fg = vim.g.terminal_color_15,
+  bg = vim.g.terminal_color_1,
+  italic = true,
+  nocombine = true,
 })
-vim.api.nvim_create_autocmd('User', {
-  pattern = 'LuasnipCleanup',
-  callback = function(opts)
-    vim.api.nvim_buf_clear_namespace(opts.buf, ls_namespace, 0, -1)
-  end,
-  group = group
-})
-
-local s = ls.s
-local sn = ls.sn
-local t = ls.text_node
-local f = ls.function_node
-local i = ls.insert_node
-local c = ls.choice_node
-local d = ls.dynamic_node
-
-local exec = function(command)
-  return f(function(_, _, command_arg)
-    local results, code = require('plenary.job'):new({
-      command = vim.env.SHELL,
-      args = {'-c', command_arg},
-      enable_recording = true,
-    }):sync()
-    if not code == 0 or #results == 0 then
-      error(string.format('exec "%s" failed', command_arg))
-    end
-    return results
-  end, {}, {user_args = {command}})
-end
-
-local user_email = {
-  exec([[git config --get user.name]]),
-  t(' <'),
-  c(1, {
-    exec([[git config --get user.email]]),
-    t([[robert.gunzler@postman.com]]),
-    t([[robert@gnzler.io]]),
-  }),
-  t('>'),
-}
-
--- snipmate snippets
-require('luasnip.loaders.from_snipmate').load({ paths = {'~/.config/nvim/after/snippets'} })
-
-ls.add_snippets('all', {
-  s('me'   , 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, {}))
-}, { type = 'snippets', key = 'generic_helpers'})
 
+-- autowrap: wraps the function in top and bot if there is any content
 local autowrap = function(top, bot, inner)
   local autoinsert = function(args, _, _, wrap_with)
     local nodes = {}
     if vim.trim(table.concat(args[1] or {})) ~= "" then
       table.insert(nodes, wrap_with)
     end
-    return sn(nil, nodes)
+    return ls.sn(nil, nodes)
   end
 
   local argnodes = {}
@@ -104,13 +31,15 @@ local autowrap = function(top, bot, inner)
   end
 
   local nodes = {}
-  table.insert(nodes, d(maxpos+1, autoinsert, argnodes, {user_args = {top}}))
+  table.insert(nodes, ls.d(maxpos+1, autoinsert, argnodes, {user_args = {top}}))
   for _, e in ipairs(inner) do table.insert(nodes, e) end
-  table.insert(nodes, d(maxpos+2, autoinsert, argnodes, {user_args = {bot}}))
+  table.insert(nodes, ls.d(maxpos+2, autoinsert, argnodes, {user_args = {bot}}))
   return nodes
 end
 
+-- comment: wraps the content in &commentstring (or a blockcomment)
 local comment = function(wrapped, blockcomment)
+  -- commentstring helper
   local get_cstring = function()
     local cs = require('Comment.ft').calculate {
       ctype = blockcomment and 2 or 1,
@@ -127,51 +56,58 @@ local comment = function(wrapped, blockcomment)
   end
 
   local nodes = {}
-  table.insert(nodes, f(function() return get_cstring()[1] end))
+  table.insert(nodes, ls.f(function() return get_cstring()[1] end))
   for _, n in ipairs(wrapped) do table.insert(nodes, n) end
-  table.insert(nodes, f(function() return get_cstring()[2] end))
+  table.insert(nodes, ls.f(function() return get_cstring()[2] end))
   return nodes
 end
 
-local todo_comment = function(key)
-  local nodes = {}
-  if type(key) == 'string' then
-    table.insert(nodes, t(key))
-  elseif type(key) == 'table' then
-    table.insert(nodes, c(1, vim.tbl_map(function(e) return t(e) end, key)))
-  end
-  local wrapped = autowrap(t('('), t(')'), {
-    c(#nodes, {
-      t(''), -- bare
-      exec([[git config --get user.name]]), -- name
-      f(function() return os.date('%Y-%m-%d') end, {}), -- time
-    }),
-  })
-  for _, n in ipairs(wrapped) do table.insert(nodes, n) end
-  table.insert(nodes, t(': '))
-  return comment(nodes)
+-- exec: inserts the output of command
+local exec = function(command)
+  return ls.f(function(_, _, command_arg)
+    local results, code = require('plenary.job'):new({
+      command = vim.env.SHELL,
+      args = {'-c', command_arg},
+      enable_recording = true,
+    }):sync()
+    if not code == 0 or #results == 0 then
+      error(string.format('exec "%s" failed', command_arg))
+    end
+    return results
+  end, {}, {user_args = {command}})
 end
 
-ls.add_snippets('all', {
-  s('todo', todo_comment('TODO')),
-  s('note', todo_comment('NOTE')),
-  s('hack', todo_comment('HACK')),
-  s('warn', todo_comment({'WARN', 'XXX'})),
-  s('fix' , todo_comment({'FIX', 'FIXME', 'ISSUE', 'BUG'})),
-}, { type = 'snippets', key = 'todo_comments'})
-
-ls.add_snippets('gitcommit', {
-  s('sign'  , fmt('Signed-off-by: {}', {sn(1, user_email)})),
-  s('change', fmt('Change-type: {}', {c(1, {t('patch'), t('minor'), t('major')}) }))
-}, { type = 'snippets', key = 'signature_helpers'})
-
-ls.add_snippets('go', {
-  s('pack:main', fmt([[
-    package main
-
-    func main() {{
-	{}
-    }}
-    ]], {i(1, 'println("hello world!")')}))
-}, { type = 'snippets', key = 'golang'})
+ls.setup {
+  ext_opts = {
+    [types.insertNode] = {
+      unvisited = { hl_group = 'LuasnipIndicator' }
+    },
+    [types.choiceNode] = {
+      active = {
+        virt_text = {{'<-- choice node', 'LuasnipIndicator'}},
+      }
+    }
+  },
+
+  snip_env = vim.tbl_extend('keep', ls.session.config.snip_env, {
+    autowrap = autowrap,
+    exec = exec,
+    comment = comment,
+
+    user_email = {
+      exec([[git config --get user.name]]),
+      ls.t(' <'),
+      ls.c(1, {
+        exec([[git config --get user.email]]),
+        ls.t([[robert.gunzler@postman.com]]),
+        ls.t([[robert@gnzler.io]]),
+      }),
+      ls.t('>'),
+    },
+  })
+}
 
+-- snipmate snippets
+require('luasnip.loaders.from_snipmate').lazy_load {paths = './after/snippets'}
+-- lua snippets
+require("luasnip.loaders.from_lua").lazy_load {paths = './snippets'}
diff --git a/.vim/snippets/all.lua b/.vim/snippets/all.lua
new file mode 100644
index 0000000..a8157cc
--- /dev/null
+++ b/.vim/snippets/all.lua
@@ -0,0 +1,53 @@
+
+local todo_comment = function(key)
+  local nodes = {}
+  if type(key) == 'string' then
+    table.insert(nodes, t(key))
+  elseif type(key) == 'table' then
+    table.insert(nodes, c(1, vim.tbl_map(function(e) return t(e) end, key)))
+  end
+  local wrapped = autowrap(t('('), t(')'), {
+    c(#nodes, {
+      t(''), -- bare
+      sn(1, user_email),
+      f(function() return os.date('%Y-%m-%d') end, {}), -- time
+    }),
+  })
+  for _, n in ipairs(wrapped) do table.insert(nodes, n) end
+  table.insert(nodes, t(': '))
+  return comment(nodes)
+end
+
+return {
+  s('me', 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, {})),
+
+  -- todo comments
+  s('todo', todo_comment('TODO')),
+  s('note', todo_comment('NOTE')),
+  s('hack', todo_comment('HACK')),
+  s('warn', todo_comment({'WARN', 'XXX'})),
+  s('fix' , todo_comment({'FIX', 'FIXME', 'ISSUE', 'BUG'})),
+
+  s({trig='#!', name='Shell script skeleton'},
+    fmt([=[
+      #!/bin/sh
+
+      set -e
+      [ -n "$DEBUG" ] && set -x
+      [ -n "$DESC" ] && desc() {{
+      	printf "{}"
+      	exit 0
+      }}
+
+    ]=], {i(1, 'what does this script do')}),
+    {
+      callbacks = {
+        [-1] = {
+          [events.enter] = function(_, _) vim.opt_local.filetype = 'sh' end
+        }
+      }
+    }
+  ),
+}
diff --git a/.vim/snippets/gitcommit.lua b/.vim/snippets/gitcommit.lua
new file mode 100644
index 0000000..fd96216
--- /dev/null
+++ b/.vim/snippets/gitcommit.lua
@@ -0,0 +1,4 @@
+return {
+  s('sign'  , fmt([[Signed-off-by: {}]], {sn(1, user_email)})),
+  s('change', fmt([[Change-type: {}]], {c(1, {t('patch'), t('minor'), t('major')}) })),
+}
diff --git a/.vim/snippets/go.lua b/.vim/snippets/go.lua
new file mode 100644
index 0000000..ee63158
--- /dev/null
+++ b/.vim/snippets/go.lua
@@ -0,0 +1,28 @@
+return {
+  s({trig='package main', name='Go skeleton'},
+    fmt([[
+      package main
+
+      func main() {{
+      	{}
+      }}
+    ]], {i(0, 'println("hello world!")')})
+  ),
+  s({trig='package test', name='Go test skeleton'},
+    fmt([[
+      package main
+
+      import (
+      	"testing"
+      )
+
+      func Test{}(t *testing.T) {{
+      	{}
+      }}
+    ]], {
+        i(1, 'Foobar'),
+        i(0, 't.Logf("Hello world")'),
+      }
+    )
+  ),
+}
diff --git a/.vim/snippets/lua.lua b/.vim/snippets/lua.lua
new file mode 100644
index 0000000..0d2609a
--- /dev/null
+++ b/.vim/snippets/lua.lua
@@ -0,0 +1,18 @@
+return {
+  s('use', fmt([[
+    use {{'{}'{} -- {{{{{{
+      config = function()
+        require('{}').setup {{}}
+      end}} -- }}}}}}
+    ]], {
+      i(1, 'username/repo'),
+      c(2, {
+        t(','),
+        t(', disabled = true,'),
+        fmt([[, cmd = {{'{}'}}, module = {{'{}'}},]],
+          {i(1, 'Cmd'), i(2, 'module')})
+      }),
+      i(3, 'module'),
+    })
+  ),
+}