feat: expose syntax highlighting (closes #125)

pull/128/head
TorchedSammy 2022-03-26 18:25:19 -04:00
parent 0cad0e7e66
commit 76f100ca77
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 31 additions and 0 deletions

8
api.go
View File

@ -28,6 +28,7 @@ var exports = map[string]lua.LGFunction {
"exec": hlexec, "exec": hlexec,
"runnerMode": hlrunnerMode, "runnerMode": hlrunnerMode,
"goro": hlgoro, "goro": hlgoro,
"highlighter": hlhighlighter,
"hinter": hlhinter, "hinter": hlhinter,
"multiprompt": hlmlprompt, "multiprompt": hlmlprompt,
"prependPath": hlprependPath, "prependPath": hlprependPath,
@ -517,3 +518,10 @@ func hlhinter(L *lua.LState) int {
return 0 return 0
} }
func hlhighlighter(L *lua.LState) int {
highlighterCb := L.CheckFunction(1)
highlighter = highlighterCb
return 0
}

23
rl.go
View File

@ -14,6 +14,7 @@ type lineReader struct {
} }
var fileHist *fileHistory var fileHist *fileHistory
var hinter lua.LValue = lua.LNil var hinter lua.LValue = lua.LNil
var highlighter lua.LValue = lua.LNil
// other gophers might hate this naming but this is local, shut up // other gophers might hate this naming but this is local, shut up
func newLineReader(prompt string, noHist bool) *lineReader { func newLineReader(prompt string, noHist bool) *lineReader {
@ -68,6 +69,28 @@ func newLineReader(prompt string, noHist bool) *lineReader {
return []rune(hintText) return []rune(hintText)
} }
rl.SyntaxHighlighter = func(line []rune) string {
if highlighter == lua.LNil {
return string(line)
}
err := l.CallByParam(lua.P{
Fn: highlighter,
NRet: 1,
Protect: true,
}, lua.LString(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()
}
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