Compare commits

..

7 Commits

Author SHA1 Message Date
Renzix 61c9e12a4a
feat: control k to delete the rest of the line (#128)
Co-authored-by: Renzix <DanielDeBruno@renzix.com>
2022-03-26 23:43:30 -04:00
TorchedSammy 0aba60b5de docs: [ci] generate new docs 2022-03-26 22:28:27 +00:00
TorchedSammy 62a6cc56b9
docs: document hilbish.highlighter 2022-03-26 18:28:01 -04:00
TorchedSammy e5d841a0a7 docs: [ci] generate new docs 2022-03-26 22:26:10 +00:00
TorchedSammy 3e50e608c1
chore: merge from remote 2022-03-26 18:25:35 -04:00
TorchedSammy 76f100ca77
feat: expose syntax highlighting (closes #125) 2022-03-26 18:25:19 -04:00
TorchedSammy 0cad0e7e66
fix: only try to run hinter function if it isnt nil 2022-03-26 18:24:49 -04:00
6 changed files with 70 additions and 1 deletions

14
api.go
View File

@ -28,6 +28,7 @@ var exports = map[string]lua.LGFunction {
"exec": hlexec,
"runnerMode": hlrunnerMode,
"goro": hlgoro,
"highlighter": hlhighlighter,
"hinter": hlhinter,
"multiprompt": hlmlprompt,
"prependPath": hlprependPath,
@ -517,3 +518,16 @@ func hlhinter(L *lua.LState) int {
return 0
}
// highlighter(cb)
// Sets the highlighter function. This is mainly for syntax hightlighting, but in
// reality could set the input of the prompt to display anything. The callback
// 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)
highlighter = highlighterCb
return 0
}

View File

@ -16,6 +16,11 @@ exec(cmd) > Replaces running hilbish with `cmd`
goro(fn) > Puts `fn` in a goroutine
highlighter(cb) > Sets the highlighter function. This is mainly for syntax hightlighting, but in
reality could set the input of the prompt to display anything. The callback
is passed the current line as typed and is expected to return a line that will
be used to display in the line.
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

View File

@ -33,6 +33,13 @@ function hilbish.exec(cmd) end
--- @param fn function
function hilbish.goro(fn) end
--- Sets the highlighter function. This is mainly for syntax hightlighting, but in
--- reality could set the input of the prompt to display anything. The callback
--- 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
function hilbish.highlighter(cb) end
--- 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

View File

@ -176,3 +176,9 @@ func (rl *Instance) deleteToBeginning() {
rl.line = rl.line[rl.pos:]
rl.pos = 0
}
func (rl *Instance) deleteToEnd() {
rl.resetVirtualComp(false)
// Keep everything before the cursor
rl.line = rl.line[:rl.pos]
}

View File

@ -189,6 +189,16 @@ func (rl *Instance) Readline() (string, error) {
rl.resetHelpers()
rl.updateHelpers()
case charCtrlK:
if rl.modeTabCompletion {
rl.resetVirtualComp(true)
}
// Delete everything after the cursor position
rl.saveBufToRegister(rl.line[rl.pos:])
rl.deleteToEnd()
rl.resetHelpers()
rl.updateHelpers()
case charBackspace, charBackspace2:
// When currently in history completion, we refresh and automatically
// insert the first (filtered) candidate, virtually

29
rl.go
View File

@ -13,7 +13,8 @@ type lineReader struct {
rl *readline.Instance
}
var fileHist *fileHistory
var hinter lua.LValue
var hinter lua.LValue = lua.LNil
var highlighter lua.LValue = lua.LNil
// other gophers might hate this naming but this is local, shut up
func newLineReader(prompt string, noHist bool) *lineReader {
@ -46,6 +47,10 @@ 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 {
return []rune{}
}
err := l.CallByParam(lua.P{
Fn: hinter,
NRet: 1,
@ -64,6 +69,28 @@ func newLineReader(prompt string, noHist bool) *lineReader {
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) {
ctx := string(line)
var completions []string