nvim: after/plugin/snipcomp: LuaSnip companion plugin for omni completion

The reddit announcement
https://www.reddit.com/r/neovim/comments/rddugs/snipcomplua_luasnip_companion_plugin_for_omni/
This commit is contained in:
Sanchayan Maity 2021-12-11 11:32:16 +05:30
parent abc00380fc
commit 20c3b7dd7d

View file

@ -0,0 +1,61 @@
--[[
Companion plugin for the LuaSnip snippet engine. Defines a completion
function which can be used for built-in insert mode completion (e.g. omni
completion). See 'ins-completion' for details. After completion the snippet
is expanded.
https://github.com/potamides/dotfiles/blob/master/.config/nvim/plugin/snipcomp.lua
--]]
-- lazy load LuaSnip, only useful when LuaSnip wasn't already loaded elsewhere
local luasnip = setmetatable({}, {__index = function(_, key) return require("luasnip")[key] end})
vim.luasnip = {}
-- https://github.com/hrsh7th/nvim-cmp/issues/180#issuecomment-915405589
require'luasnip'.config.set_config { history = true }
require('luasnip.loaders.from_vscode').load {}
local function snippet2completion(snippet)
return {
word = snippet.trigger,
menu = snippet.name,
info = vim.trim(table.concat(vim.tbl_flatten({snippet.dscr or "", "", snippet:get_docstring()}), "\n")),
dup = true,
user_data = "luasnip"
}
end
local function snippetfilter(line_to_cursor, base)
return function(s)
return not s.hidden and vim.startswith(s.trigger, base) and s.show_condition(line_to_cursor)
end
end
-- Set 'completefunc' or 'omnifunc' to 'v:lua.vim.luasnip.completefunc' to get
-- completion.
function vim.luasnip.completefunc(findstart, base)
local line_to_cursor = vim.fn.getline("."):sub(1, vim.fn.col("."))
if findstart == 1 then
return vim.fn.match(line_to_cursor, '\\k*$')
end
local snippets = vim.list_extend(vim.list_slice(luasnip.snippets.all), luasnip.snippets[vim.bo.filetype] or {})
snippets = vim.tbl_filter(snippetfilter(line_to_cursor, base), snippets)
snippets = vim.tbl_map(snippet2completion, snippets)
table.sort(snippets, function(s1, s2) return s1.word < s2.word end)
return snippets
end
function vim.luasnip.completion_expand(item)
if item.user_data == "luasnip" and luasnip.expandable() then
luasnip.expand()
end
end
vim.cmd([[
augroup luasnip_completion_expand
autocmd!
autocmd CompleteDone * call v:lua.vim.luasnip.completion_expand(v:completed_item)
augroup END
]])
vim.o.completefunc = 'v:lua.vim.luasnip.completefunc'