-
Notifications
You must be signed in to change notification settings - Fork 3
/
completion.lua
101 lines (88 loc) · 2.38 KB
/
completion.lua
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
90
91
92
93
94
95
96
97
98
99
100
101
local t = {}
local ti_units = require 'ti.units'
local ti_functions = require 'ti.functions'
local stats = require 'config.stats'
local completion_stats = stats.completion
local function score_match(str)
return completion_stats[str]
end
local function match_and_add(str, prefix, tab)
local len = prefix:len()
if str:find(prefix, 1) == 1 then
table.insert(tab, { str, str:sub(len + 1), score = score_match(str) })
end
end
function t.complete_unit(prefix, tab)
for _, cat in pairs(ti_units.tab) do
for _, v in ipairs(cat) do
match_and_add(v[1], prefix, tab)
end
end
end
function t.complete_variable(prefix, tab)
for _, v in ipairs(_G.var.list()) do
match_and_add(v, prefix, tab)
end
end
function t.complete_function(prefix, tab)
for k, _ in pairs(ti_functions.tab) do
match_and_add(k, prefix, tab)
end
end
function t.complete_number(_, tab)
t.complete_unit('', tab)
end
function t.complete_word(prefix, tab)
t.complete_function(prefix, tab)
t.complete_variable(prefix, tab)
end
function t.sort_completion(tab)
table.sort(tab, function(a, b)
return (a.score or 0) > (b.score or 0)
end)
return tab
end
function t.complete_smart(prefix)
local lexer = require 'ti.lexer'
local ok, res = pcall(function()
return lexer.tokenize(prefix)
end)
if ok and res and #res > 0 then
local text, kind = table.unpack(res[#res])
local tab = {}
if t['complete_' .. kind] then
t['complete_' .. kind](text, tab)
end
return t.sort_completion(tab)
elseif prefix == '_' or prefix:find('[^%a]_$') then
local tab = {}
t.complete_unit('_', tab)
return t.sort_completion(tab)
else
print('Error parsing prefix!')
end
return {}
end
-- Setup completion
function t.setup_edit(edit)
edit.on_complete = function(_, prefix)
return t.complete_smart(prefix)
end
edit.on_complete_done = function(_, title, _)
if title and #title > 0 then
completion_stats[title] = (completion_stats[title] or 0) + 1
end
end
end
-- Setup completion for variables only
function t.setup_edit_store(edit)
edit.on_complete = function(_, prefix)
local tab = {}
for n = 1, 9 do
match_and_add(string.format('f%d(x)', n), prefix, tab)
end
t.complete_variable(prefix, tab)
return t.sort_completion(tab)
end
end
return t