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}, "runnerMode": util.LuaExport{hlrunnerMode, 1, false},
/* /*
"goro": hlgoro, "goro": hlgoro,
"highlighter": hlhighlighter, */
"hinter": hlhinter, "highlighter": util.LuaExport{hlhighlighter, 1, false},
"hinter": util.LuaExport{hlhinter, 1, false},
/*
"multiprompt": hlmlprompt, "multiprompt": hlmlprompt,
"prependPath": hlprependPath, "prependPath": hlprependPath,
*/ */
@ -574,19 +576,23 @@ func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil return c.Next(), nil
} }
/*
// hinter(cb) // hinter(cb)
// Sets the hinter function. This will be called on every key insert to determine // 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: // 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 // the current line and the position. It is expected to return a string
// which will be used for the hint. // which will be used for the hint.
// --- @param cb function // --- @param cb function
func hlhinter(L *lua.LState) int { func hlhinter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
hinterCb := L.CheckFunction(1) if err := c.Check1Arg(); err != nil {
return nil, err
}
hinterCb, err := c.ClosureArg(0)
if err != nil {
return nil, err
}
hinter = hinterCb hinter = hinterCb
return 0 return c.Next(), err
} }
// highlighter(cb) // 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 // is passed the current line as typed and is expected to return a line that will
// be used to display in the line. // be used to display in the line.
// --- @param cb function // --- @param cb function
func hlhighlighter(L *lua.LState) int { func hlhighlighter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
highlighterCb := L.CheckFunction(1) if err := c.Check1Arg(); err != nil {
return nil, err
}
highlighterCb, err := c.ClosureArg(0)
if err != nil {
return nil, err
}
highlighter = highlighterCb highlighter = highlighterCb
return 0 return c.Next(), err
} }
*/

36
rl.go
View File

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