mirror of https://github.com/Hilbis/Hilbish
feat: implement syntax highlight and hints
parent
ad183a7208
commit
6848b59cbf
33
api.go
33
api.go
|
@ -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
36
rl.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue