feat: implement syntax highlight and hints

lua5.4
TorchedSammy 2022-03-29 20:57:29 -04:00
parent ad183a7208
commit 6848b59cbf
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 34 additions and 35 deletions

33
api.go
View File

@ -35,8 +35,10 @@ var exports = map[string]util.LuaExport{
"runnerMode": util.LuaExport{hlrunnerMode, 1, false},
/*
"goro": hlgoro,
"highlighter": hlhighlighter,
"hinter": hlhinter,
*/
"highlighter": util.LuaExport{hlhighlighter, 1, false},
"hinter": util.LuaExport{hlhinter, 1, false},
/*
"multiprompt": hlmlprompt,
"prependPath": hlprependPath,
*/
@ -574,19 +576,23 @@ func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
/*
// hinter(cb)
// Sets the hinter function. This will be called on every key insert to determine
// what text to use as an inline hint. The callback is passed 2 arguments:
// the current line and the position. It is expected to return a string
// which will be used for the hint.
// --- @param cb function
func hlhinter(L *lua.LState) int {
hinterCb := L.CheckFunction(1)
func hlhinter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
hinterCb, err := c.ClosureArg(0)
if err != nil {
return nil, err
}
hinter = hinterCb
return 0
return c.Next(), err
}
// highlighter(cb)
@ -595,10 +601,15 @@ func hlhinter(L *lua.LState) int {
// is passed the current line as typed and is expected to return a line that will
// be used to display in the line.
// --- @param cb function
func hlhighlighter(L *lua.LState) int {
highlighterCb := L.CheckFunction(1)
func hlhighlighter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
highlighterCb, err := c.ClosureArg(0)
if err != nil {
return nil, err
}
highlighter = highlighterCb
return 0
return c.Next(), err
}
*/

36
rl.go
View File

@ -13,10 +13,8 @@ type lineReader struct {
rl *readline.Instance
}
var fileHist *fileHistory
/*
var hinter lua.LValue = lua.LNil
var highlighter lua.LValue = lua.LNil
*/
var hinter *rt.Closure
var highlighter *rt.Closure
func newLineReader(prompt string, noHist bool) *lineReader {
rl := readline.NewInstance()
@ -47,53 +45,43 @@ func newLineReader(prompt string, noHist bool) *lineReader {
}
hooks.Em.Emit("hilbish.vimAction", actionStr, args)
}
/*
rl.HintText = func(line []rune, pos int) []rune {
if hinter == lua.LNil {
if hinter == nil {
return []rune{}
}
err := l.CallByParam(lua.P{
Fn: hinter,
NRet: 1,
Protect: true,
}, lua.LString(string(line)), lua.LNumber(pos))
retVal, err := rt.Call1(l.MainThread(), rt.FunctionValue(highlighter),
rt.StringValue(string(line)), rt.IntValue(int64(pos)))
if err != nil {
fmt.Println(err)
return []rune{}
}
retVal := l.Get(-1)
hintText := ""
if luaStr, ok := retVal.(lua.LString); retVal != lua.LNil && ok {
hintText = luaStr.String()
if luaStr, ok := retVal.TryString(); ok {
hintText = luaStr
}
return []rune(hintText)
}
rl.SyntaxHighlighter = func(line []rune) string {
if highlighter == lua.LNil {
if highlighter == nil {
return string(line)
}
err := l.CallByParam(lua.P{
Fn: highlighter,
NRet: 1,
Protect: true,
}, lua.LString(string(line)))
retVal, err := rt.Call1(l.MainThread(), rt.FunctionValue(highlighter),
rt.StringValue(string(line)))
if err != nil {
fmt.Println(err)
return string(line)
}
retVal := l.Get(-1)
highlighted := ""
if luaStr, ok := retVal.(lua.LString); retVal != lua.LNil && ok {
highlighted = luaStr.String()
if luaStr, ok := retVal.TryString(); ok {
highlighted = luaStr
}
return highlighted
}
*/
rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
ctx := string(line)
var completions []string