Skip to content

Instantly share code, notes, and snippets.

@runiq
Created August 6, 2020 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runiq/73417d58446b83f6ed1ca9631a8f7dcd to your computer and use it in GitHub Desktop.
Save runiq/73417d58446b83f6ed1ca9631a8f7dcd to your computer and use it in GitHub Desktop.
Integrating snippets.nvim and completion-nvim
diff --git i/lua/completion.lua w/lua/completion.lua
index 1fed4f4..4c9774b 100644
--- i/lua/completion.lua
+++ w/lua/completion.lua
@@ -107,6 +107,8 @@ local function hasConfirmedCompletion()
api.nvim_input("<c-r>".."=neosnippet#expand('"..completed_item.word.."')".."<CR>")
elseif completed_item.kind == 'vim-vsnip' then
api.nvim_call_function('vsnip#expand', {})
+ elseif completed_item.kind == 'snippets.nvim' then
+ require'snippets'.expand_at_cursor()
end
end
diff --git i/lua/completion/source.lua w/lua/completion/source.lua
index e769c42..5ddbf09 100644
--- i/lua/completion/source.lua
+++ w/lua/completion/source.lua
@@ -34,6 +34,9 @@ local complete_items_map = {
},
['Neosnippet'] = {
item = snippet.getNeosnippetItems
+ },
+ ['snippets.nvim'] = {
+ item = snippet.getSnippetsNvimItems
}
}
diff --git i/lua/completion/source/snippet.lua w/lua/completion/source/snippet.lua
index edde842..179b3a0 100644
--- i/lua/completion/source/snippet.lua
+++ w/lua/completion/source/snippet.lua
@@ -76,6 +76,33 @@ M.getVsnipItems = function(prefix)
return complete_items
end
+M.getSnippetsNvimItems = function(prefix)
+ local snippets = require 'snippets'
+ if not snippets then return {} end
+ local ft = vim.bo.filetype
+ local snippetsList = vim.tbl_extend('force', snippets.snippets._global, snippets.snippets[ft])
+ local complete_items = {}
+ if vim.tbl_isempty(snippetsList) == 0 then
+ return {}
+ end
+ local priority = vim.g.completion_items_priority['snippets.nvim'] or 1
+ for short, long in pairs(snippetsList) do
+ local user_data = { norcalli = { ['snippets.nvim'] = { [short] = long } } }
+ local item = {}
+ -- We need to keep this thing short for now and expand later.
+ -- Also, lookup/parsing/validation should be done later, since we also want
+ -- to expand LSP snippets (on CompleteDone or something).
+ item.word = short
+ item.kind = 'snippets.nvim'
+ item.menu = short
+ -- TODO: Turn actual snippet text into label/description?
+ item.priority = priority
+ item.user_data = user_data
+ match.matching(complete_items, prefix, item)
+ end
+ return complete_items
+end
+
M.getCompletionItems = function(prefix)
local source = vim.g.completion_enable_snippet
local snippet_list = {}
@@ -85,6 +112,8 @@ M.getCompletionItems = function(prefix)
snippet_list = M.getNeosnippetItems(prefix)
elseif source == 'vim-vsnip' then
snippet_list = M.getVsnipItems(prefix)
+ elseif source == 'snippets.nvim' then
+ snippet_list = M.getSnippetsNvimItems(prefix)
end
return snippet_list
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment