-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.lua
More file actions
44 lines (38 loc) · 1.21 KB
/
autocomplete.lua
File metadata and controls
44 lines (38 loc) · 1.21 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
local completion = require "cc.shell.completion"
---@class Autocomplete
local Autocomplete = {}
Autocomplete.structure = nil
---@param structure table
---@param name string | nil
---@return table
function Autocomplete:registerAutoComplete(structure, name)
self.structure = structure
shell.setCompletionFunction(
name or shell.getRunningProgram(),
function(shell, index, argument, previous)
local commands = {}
for k, _ in pairs(self.structure) do
commands[k] = self.structure[k] or {}
end
local currArg = commands
for i = 2, #previous do
if currArg[previous[i]] then
currArg = currArg[previous[i]]
else
return nil
end
end
local results = {}
for word, value in pairs(currArg) do
if type(value) ~= "table" then
word = value
end
if word:sub(1, #argument) == argument then
results[#results + 1] = word:sub(#argument + 1)
end
end
return results
end
)
end
return Autocomplete