mirror of https://github.com/Hilbis/Hilbish
refactor: rewrite completion handler in lua
parent
bcf02a6b0e
commit
4a4c4d8c74
79
complete.go
79
complete.go
|
@ -131,84 +131,9 @@ func completionLoader(rtm *rt.Runtime) *rt.Table {
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// left as a shim, might doc in the same way as hilbish functions
|
||||||
func completionHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func completionHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
if err := c.CheckNArgs(2); err != nil {
|
return c.Next(), nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
line, err := c.StringArg(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// just for validation
|
|
||||||
_, err = c.IntArg(1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := strings.TrimLeft(line, " ")
|
|
||||||
if len(ctx) == 0 {
|
|
||||||
return c.PushingNext(t.Runtime, rt.TableValue(rt.NewTable()), rt.StringValue("")), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = aliases.Resolve(ctx)
|
|
||||||
fields := strings.Split(ctx, " ")
|
|
||||||
query := fields[len(fields) - 1]
|
|
||||||
|
|
||||||
luaFields := rt.NewTable()
|
|
||||||
|
|
||||||
for i, f := range fields {
|
|
||||||
luaFields.Set(rt.IntValue(int64(i + 1)), rt.StringValue(f))
|
|
||||||
}
|
|
||||||
|
|
||||||
compMod := hshMod.Get(rt.StringValue("completion")).AsTable()
|
|
||||||
var term *rt.Termination
|
|
||||||
if len(fields) == 1 {
|
|
||||||
term = rt.NewTerminationWith(t.CurrentCont(), 2, false)
|
|
||||||
err := rt.Call(t, compMod.Get(rt.StringValue("bins")), []rt.Value{
|
|
||||||
rt.StringValue(query),
|
|
||||||
rt.StringValue(ctx),
|
|
||||||
rt.TableValue(luaFields),
|
|
||||||
}, term)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
gterm := rt.NewTerminationWith(t.CurrentCont(), 2, false)
|
|
||||||
err := rt.Call(t, compMod.Get(rt.StringValue("call")), []rt.Value{
|
|
||||||
rt.StringValue("commands." + fields[0]),
|
|
||||||
rt.StringValue(query),
|
|
||||||
rt.StringValue(ctx),
|
|
||||||
rt.TableValue(luaFields),
|
|
||||||
}, gterm)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
groups := gterm.Get(0)
|
|
||||||
pfx := gterm.Get(1)
|
|
||||||
|
|
||||||
return c.PushingNext(t.Runtime, groups, pfx), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// error means there isnt a command handler - default to files in that case
|
|
||||||
term = rt.NewTerminationWith(t.CurrentCont(), 2, false)
|
|
||||||
err = rt.Call(t, compMod.Get(rt.StringValue("files")), []rt.Value{
|
|
||||||
rt.StringValue(query),
|
|
||||||
rt.StringValue(ctx),
|
|
||||||
rt.TableValue(luaFields),
|
|
||||||
}, term)
|
|
||||||
}
|
|
||||||
|
|
||||||
comps := term.Get(0)
|
|
||||||
pfx := term.Get(1)
|
|
||||||
|
|
||||||
groups := rt.NewTable()
|
|
||||||
|
|
||||||
compGroup := rt.NewTable()
|
|
||||||
compGroup.Set(rt.StringValue("items"), comps)
|
|
||||||
compGroup.Set(rt.StringValue("type"), rt.StringValue("grid"))
|
|
||||||
|
|
||||||
groups.Set(rt.IntValue(1), rt.TableValue(compGroup))
|
|
||||||
return c.PushingNext(t.Runtime, rt.TableValue(groups), pfx), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func callLuaCompleter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func callLuaCompleter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
function hilbish.completion.handler(line, pos)
|
||||||
|
if type(line) ~= 'string' then error '#1 must be a string' end
|
||||||
|
if type(pos) ~= 'number' then error '#2 must be a number' end
|
||||||
|
|
||||||
|
-- trim leading whitespace
|
||||||
|
local ctx = line:gsub('^%s*(.-)$', '%1')
|
||||||
|
if ctx:len() == 0 then return {}, '' end
|
||||||
|
|
||||||
|
ctx = hilbish.aliases.resolve(ctx)
|
||||||
|
local fields = string.split(ctx, ' ')
|
||||||
|
local query = fields[#fields]
|
||||||
|
|
||||||
|
if #fields == 1 then
|
||||||
|
local comps, pfx = hilbish.completion.bins(query, ctx, fields)
|
||||||
|
local compGroup = {
|
||||||
|
items = comps,
|
||||||
|
type = 'grid'
|
||||||
|
}
|
||||||
|
|
||||||
|
return {compGroup}, pfx
|
||||||
|
else
|
||||||
|
local ok, compGroups, pfx = pcall(hilbish.completion.call,
|
||||||
|
'command.' .. #fields[1], query, ctx, fields)
|
||||||
|
if ok then
|
||||||
|
return compGroups, pfx
|
||||||
|
end
|
||||||
|
|
||||||
|
local comps, pfx = hilbish.completion.files(query, ctx, fields)
|
||||||
|
local compGroup = {
|
||||||
|
items = comps,
|
||||||
|
type = 'grid'
|
||||||
|
}
|
||||||
|
|
||||||
|
return {compGroup}, pfx
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue