summary refs log tree commit diff
path: root/.config/nvim/lua/config/templates.lua
blob: a01efa1b1a4134e38ca55776286a6c8310ff9255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- template snippets
require('luasnip').setup_snip_env()
local templates = {}

-- check if the current buffer is empty
local bufempty = function(bufnr)
  bufnr = bufnr or 0
  if vim.api.nvim_buf_get_offset(bufnr, 0) <= 0 then
    local handle = io.open(vim.api.nvim_buf_get_name(bufnr))
    if handle == nil then
      return true
    end
    local eof = handle:read(0)
    handle:close()
    if eof == nil then
      return true
    end
  end
  return false
end

local register_template = function(pattern, snip)
  templates[pattern] = snip

  vim.api.nvim_create_autocmd({ 'BufEnter', 'BufNewFile' }, {
    pattern = pattern,
    group = vim.api.nvim_create_augroup('Template' .. pattern, { clear = true }),
    callback = function(opt)
      if bufempty() then
        require('luasnip').snip_expand(templates[pattern])
        require('luasnip').unlink_current()
      end
    end,
  })
end

register_template(
  'deb/DEBIAN/changelog_blobs/*.txt',
  s(
    'debian-changelog',
    fmt(
      [[
ukp-agent (${VERSION}) unstable; urgency=medium

  * feat: <>
]],
      { i(0) },
      {
        delimiters = '<>',
      }
    )
  )
)