neovim

Personal neovim configuration files
git clone git://gtms.dev/neovim
Log | Files | Refs

snippet_source.lua (1574B)


      1 local M = {}
      2 
      3 function M:is_available()
      4   return true
      5 end
      6 
      7 function M:get_debug_name()
      8   return 'snippets'
      9 end
     10 
     11 function M:complete(_, callback)
     12   local snippets = require 'tms.snippets'[vim.bo.filetype] or {}
     13 
     14   local response = {}
     15 
     16   for key in pairs(snippets) do
     17     local snippet = snippets[key]
     18     local body = snippet.body
     19     if type(body) == 'function' then
     20       body = body()
     21     end
     22     if type(body) == 'table' then
     23       body = table.concat(body, '\n')
     24     end
     25 
     26     local prefix = snippets[key].prefix
     27     if type(prefix) == 'table' then
     28       for _, p in ipairs(prefix) do
     29         table.insert(response, {
     30           label = p,
     31           kind = require 'cmp'.lsp.CompletionItemKind.Snippet,
     32           insertText = p,
     33           data = { prefix = p, body = body },
     34         })
     35       end
     36     else
     37       table.insert(response, {
     38         label = prefix,
     39         kind = require 'cmp'.lsp.CompletionItemKind.Snippet,
     40         insertText = prefix,
     41         data = { prefix = prefix, body = body },
     42       })
     43     end
     44   end
     45   callback(response)
     46 end
     47 
     48 function M:resolve(completion_item, callback)
     49   completion_item.documentation = { kind = require 'cmp'.lsp.MarkupKind.Markdown, value = completion_item.data.body }
     50   callback(completion_item)
     51 end
     52 
     53 function M:execute(completion_item, callback)
     54   callback(completion_item)
     55   local cursor = vim.api.nvim_win_get_cursor(0)
     56   cursor[1] = cursor[1] - 1
     57 
     58   vim.api.nvim_buf_set_text(0, cursor[1], cursor[2] - #completion_item.data.prefix, cursor[1], cursor[2], { '' })
     59   vim.snippet.expand(completion_item.data.body)
     60 end
     61 
     62 return M