Commit a52244ac authored by gwenn's avatar gwenn

Replace radix trees by FTS tables for autocompletion

parent 0d695f4e
This diff is collapsed.
......@@ -12,18 +12,30 @@ import (
. "github.com/gwenn/gosqlite/shell"
)
func createCache(t *testing.T) *CompletionCache {
cc, err := CreateCache()
assert.Tf(t, err == nil, "%v", err)
return cc
}
func TestPragmaNames(t *testing.T) {
pragmas := CompletePragma("fo")
cc := createCache(t)
pragmas, err := cc.CompletePragma("fo")
assert.Tf(t, err == nil, "%v", err)
assert.Equalf(t, 3, len(pragmas), "got %d pragmas; expected %d", len(pragmas), 3)
assert.Equal(t, []string{"foreign_key_check", "foreign_key_list(", "foreign_keys"}, pragmas, "unexpected pragmas")
}
func TestFuncNames(t *testing.T) {
funcs := CompleteFunc("su")
cc := createCache(t)
funcs, err := cc.CompleteFunc("su")
assert.Tf(t, err == nil, "%v", err)
assert.Equal(t, 2, len(funcs), "got %d functions; expected %d", len(funcs), 2)
assert.Equal(t, []string{"substr(", "sum("}, funcs, "unexpected functions")
}
func TestCmdNames(t *testing.T) {
cmds := CompleteCmd(".h")
cc := createCache(t)
cmds, err := cc.CompleteCmd(".h")
assert.Tf(t, err == nil, "%v", err)
assert.Equal(t, 2, len(cmds), "got %d commands; expected %d", len(cmds), 2)
assert.Equal(t, []string{".headers", ".help"}, cmds, "unexpected commands")
}
......@@ -31,7 +43,7 @@ func TestCache(t *testing.T) {
db, err := sqlite.Open(":memory:")
assert.Tf(t, err == nil, "%v", err)
defer db.Close()
cc := CreateCache(db)
cc := createCache(t)
err = cc.Update(db)
assert.Tf(t, err == nil, "%v", err)
}
......@@ -118,16 +118,18 @@ func catchInterrupt() {
signal.Notify(ch, syscall.SIGINT)
}
func completion(line string, pos int) (string, []string, string) {
func completion(cc *shell.CompletionCache, line string, pos int) (string, []string, string) {
if isBlank(line) {
return line[:pos], nil, line[pos:]
}
prefix := line[:pos]
var err error
var matches []string
if isCommand(line) {
i := strings.LastIndex(prefix, " ")
if i == -1 {
matches = shell.CompleteCmd(prefix)
matches, err = cc.CompleteCmd(prefix)
check(err)
if len(matches) > 0 {
prefix = ""
}
......@@ -135,7 +137,8 @@ func completion(line string, pos int) (string, []string, string) {
} else {
fields := strings.Fields(prefix)
if strings.EqualFold("PRAGMA", fields[0]) { // TODO check pos
matches = shell.CompletePragma(fields[1])
matches, err = cc.CompletePragma(fields[1])
check(err)
}
}
return prefix, matches, line[pos:]
......@@ -156,7 +159,11 @@ func main() {
}
state.Close()
}()
state.SetWordCompleter(completion)
completionCache, err := shell.CreateCache()
check(err)
state.SetWordCompleter(func(line string, pos int) (string, []string, string) {
return completion(completionCache, line, pos)
})
err = loadHistory(state, historyFileName)
check(err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment