This repository was archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathsnippet.lua
More file actions
89 lines (83 loc) · 2.32 KB
/
snippet.lua
File metadata and controls
89 lines (83 loc) · 2.32 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local vim = vim
local api = vim.api
local util = require 'utility'
local M = {}
local getUltisnipItems = function(prefix, score_func)
local snippetsList = api.nvim_call_function('UltiSnips#SnippetsInCurrentScope', {})
local complete_items = {}
if vim.tbl_isempty(snippetsList) then
return {}
end
for key, val in pairs(snippetsList) do
-- fix lua parsing issue
if key == true then
key = 'true'
end
local score = score_func(prefix, key)
local user_data = {hover = val}
if score < #prefix/2 then
table.insert(complete_items, {
word = key,
kind = 'UltiSnips',
menu = val,
score = score,
icase = 1,
dup = 1,
empty = 1,
user_data = vim.fn.json_encode(user_data)
})
end
end
return complete_items
end
local getNeosnippetItems = function(prefix, score_func)
local snippetsList = api.nvim_call_function('neosnippet#helpers#get_completion_snippets', {})
local complete_items = {}
if vim.tbl_isempty(snippetsList) == 0 then
return {}
end
for key, val in pairs(snippetsList) do
if key == true then
key = 'true'
end
local user_data = {hover = val.description}
local score = score_func(prefix, key)
if score < #prefix/2 then
table.insert(complete_items, {
word = key,
kind = 'Neosnippet',
menu = val.description,
score = score,
icase = 1,
dup = 1,
empty = 1,
user_data = vim.fn.json_encode(user_data)
})
end
end
return complete_items
end
M.getCompletionItems = function(prefix, score_func, _)
local source = api.nvim_get_var('completion_enable_snippet')
local snippet_list = {}
if source == 'UltiSnips' then
snippet_list = getUltisnipItems(prefix, score_func)
elseif source == 'Neosnippet' then
snippet_list = getNeosnippetItems(prefix, score_func)
end
return snippet_list
end
M.triggerCompletion = function(manager, _, prefix, textMatch)
local snippet_list = M.getCompletionItemsItem(prefix)
util.sort_completion_items(snippet_list)
if manager.insertChar == true then
vim.fn.complete(textMatch+1, snippet_list)
if #snippet_list ~= 0 then
manager.insertChar = false
manager.changeSource = false
else
manager.changeSource = true
end
end
end
return M