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},
|
"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
36
rl.go
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue