From fbf420a0b9c3cd0a20c77757c58b9778f84f8fb5 Mon Sep 17 00:00:00 2001 From: sammyette Date: Tue, 3 Oct 2023 11:44:05 -0400 Subject: [PATCH] refactor: move hilbish.editor function impls to readline side --- cmd/docgen/docgen.go | 7 ++- docs/api/hilbish/hilbish.editor.md | 9 ---- editor.go | 85 +----------------------------- emmyLuaDocs/hilbish.lua | 18 ++----- readline/lua-api.go | 37 +++++++++++++ 5 files changed, 46 insertions(+), 110 deletions(-) create mode 100644 readline/lua-api.go diff --git a/cmd/docgen/docgen.go b/cmd/docgen/docgen.go index aae6202..f12815e 100644 --- a/cmd/docgen/docgen.go +++ b/cmd/docgen/docgen.go @@ -268,7 +268,7 @@ func main() { os.Mkdir("docs/api", 0777) os.Mkdir("emmyLuaDocs", 0777) - dirs := []string{"./"} + dirs := []string{"./readline", "./"} filepath.Walk("golibs/", func (path string, info os.FileInfo, err error) error { if !info.IsDir() { return nil @@ -295,7 +295,7 @@ func main() { pieces := []docPiece{} typePieces := []docPiece{} mod := l - if mod == "main" { + if mod == "main" || mod == "readline" { mod = "hilbish" } var hasInterfaces bool @@ -457,6 +457,9 @@ func main() { htmlSig := typeTag.ReplaceAllStringFunc(strings.Replace(dps.FuncSig, "<", `\<`, -1), func(typ string) string { typName := typ[1:] typLookup := typeTable[strings.ToLower(typName)] + if len(typLookup) == 0 { + typLookup = []string{"WHAT", "WHAT"} + } ifaces := typLookup[0] + "." + typLookup[1] + "/" if typLookup[1] == "" { ifaces = "" diff --git a/docs/api/hilbish/hilbish.editor.md b/docs/api/hilbish/hilbish.editor.md index 30a3842..64b020b 100644 --- a/docs/api/hilbish/hilbish.editor.md +++ b/docs/api/hilbish/hilbish.editor.md @@ -12,15 +12,6 @@ The hilbish.editor interface provides functions to directly interact with the line editor in use. ## Functions -### getLine() -> string -Returns the current input line. - -### getVimRegister(register) -> string -Returns the text that is at the register. - ### insert(text) Inserts text into the line. -### setVimRegister(register, text) -Sets the vim register at `register` to hold the passed text. - diff --git a/editor.go b/editor.go index 3038f07..aae8041 100644 --- a/editor.go +++ b/editor.go @@ -1,8 +1,6 @@ package main import ( - "hilbish/util" - rt "github.com/arnodel/golua/runtime" ) @@ -11,86 +9,5 @@ import ( // The hilbish.editor interface provides functions to // directly interact with the line editor in use. func editorLoader(rtm *rt.Runtime) *rt.Table { - exports := map[string]util.LuaExport{ - "insert": {editorInsert, 1, false}, - "setVimRegister": {editorSetRegister, 1, false}, - "getVimRegister": {editorGetRegister, 2, false}, - "getLine": {editorGetLine, 0, false}, - } - - mod := rt.NewTable() - util.SetExports(rtm, mod, exports) - - return mod -} - -// #interface editor -// insert(text) -// Inserts text into the line. -func editorInsert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { - return nil, err - } - - text, err := c.StringArg(0) - if err != nil { - return nil, err - } - - lr.rl.Insert(text) - - return c.Next(), nil -} - -// #interface editor -// setVimRegister(register, text) -// Sets the vim register at `register` to hold the passed text. -// --- @param register string -// --- @param text string -func editorSetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { - return nil, err - } - - register, err := c.StringArg(0) - if err != nil { - return nil, err - } - - text, err := c.StringArg(1) - if err != nil { - return nil, err - } - - lr.rl.SetRegisterBuf(register, []rune(text)) - - return c.Next(), nil -} - -// #interface editor -// getVimRegister(register) -> string -// Returns the text that is at the register. -// --- @param register string -func editorGetRegister(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { - return nil, err - } - - register, err := c.StringArg(0) - if err != nil { - return nil, err - } - - buf := lr.rl.GetFromRegister(register) - - return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil -} - -// #interface editor -// getLine() -> string -// Returns the current input line. -func editorGetLine(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - buf := lr.rl.GetLine() - - return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil + return lr.rl.SetupLua(rtm) } diff --git a/emmyLuaDocs/hilbish.lua b/emmyLuaDocs/hilbish.lua index c26c7ec..d7c37d0 100644 --- a/emmyLuaDocs/hilbish.lua +++ b/emmyLuaDocs/hilbish.lua @@ -2,6 +2,9 @@ local hilbish = {} +--- Inserts text into the line. +function hilbish.editor.insert(text) end + --- This is an alias (ha) for the `hilbish.alias` function. --- @param alias string --- @param cmd string @@ -30,21 +33,6 @@ function hilbish.completions.call(name, query, ctx, fields) end --- @param pos string function hilbish.completions.handler(line, pos) end ---- Returns the current input line. -function hilbish.editor.getLine() end - ---- Returns the text that is at the register. ---- @param register string -function hilbish.editor.getVimRegister(register) end - ---- Inserts text into the line. -function hilbish.editor.insert(text) end - ---- Sets the vim register at `register` to hold the passed text. ---- @param register string ---- @param text string -function hilbish.editor.setVimRegister(register, text) end - --- Sets an alias of `cmd` to `orig` --- @param cmd string --- @param orig string diff --git a/readline/lua-api.go b/readline/lua-api.go new file mode 100644 index 0000000..29feb5f --- /dev/null +++ b/readline/lua-api.go @@ -0,0 +1,37 @@ +package readline + +import ( + "hilbish/util" + + rt "github.com/arnodel/golua/runtime" +) + +func (rl *Instance) SetupLua(rtm *rt.Runtime) *rt.Table { + exports := map[string]util.LuaExport{ + "insert": {rl.editorInsert, 1, false}, + } + + mod := rt.NewTable() + util.SetExports(rtm, mod, exports) + + return mod +} + +// #interface editor +// insert(text) +// Inserts text into the line. +func (rl *Instance) editorInsert(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + + text, err := c.StringArg(0) + if err != nil { + return nil, err + } + + rl.Insert(text) + + return c.Next(), nil +} +