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:
parent
abc00380fc
commit
20c3b7dd7d
1 changed files with 61 additions and 0 deletions
61
nvim/.config/nvim/after/plugin/snipcomp.lua
Normal file
61
nvim/.config/nvim/after/plugin/snipcomp.lua
Normal 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'
|
Loading…
Reference in a new issue