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 ( ...@@ -12,18 +12,30 @@ import (
. "github.com/gwenn/gosqlite/shell" . "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) { 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.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") assert.Equal(t, []string{"foreign_key_check", "foreign_key_list(", "foreign_keys"}, pragmas, "unexpected pragmas")
} }
func TestFuncNames(t *testing.T) { 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, 2, len(funcs), "got %d functions; expected %d", len(funcs), 2)
assert.Equal(t, []string{"substr(", "sum("}, funcs, "unexpected functions") assert.Equal(t, []string{"substr(", "sum("}, funcs, "unexpected functions")
} }
func TestCmdNames(t *testing.T) { 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, 2, len(cmds), "got %d commands; expected %d", len(cmds), 2)
assert.Equal(t, []string{".headers", ".help"}, cmds, "unexpected commands") assert.Equal(t, []string{".headers", ".help"}, cmds, "unexpected commands")
} }
...@@ -31,7 +43,7 @@ func TestCache(t *testing.T) { ...@@ -31,7 +43,7 @@ func TestCache(t *testing.T) {
db, err := sqlite.Open(":memory:") db, err := sqlite.Open(":memory:")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
defer db.Close() defer db.Close()
cc := CreateCache(db) cc := createCache(t)
err = cc.Update(db) err = cc.Update(db)
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
} }
...@@ -118,16 +118,18 @@ func catchInterrupt() { ...@@ -118,16 +118,18 @@ func catchInterrupt() {
signal.Notify(ch, syscall.SIGINT) 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) { if isBlank(line) {
return line[:pos], nil, line[pos:] return line[:pos], nil, line[pos:]
} }
prefix := line[:pos] prefix := line[:pos]
var err error
var matches []string var matches []string
if isCommand(line) { if isCommand(line) {
i := strings.LastIndex(prefix, " ") i := strings.LastIndex(prefix, " ")
if i == -1 { if i == -1 {
matches = shell.CompleteCmd(prefix) matches, err = cc.CompleteCmd(prefix)
check(err)
if len(matches) > 0 { if len(matches) > 0 {
prefix = "" prefix = ""
} }
...@@ -135,7 +137,8 @@ func completion(line string, pos int) (string, []string, string) { ...@@ -135,7 +137,8 @@ func completion(line string, pos int) (string, []string, string) {
} else { } else {
fields := strings.Fields(prefix) fields := strings.Fields(prefix)
if strings.EqualFold("PRAGMA", fields[0]) { // TODO check pos 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:] return prefix, matches, line[pos:]
...@@ -156,7 +159,11 @@ func main() { ...@@ -156,7 +159,11 @@ func main() {
} }
state.Close() 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) err = loadHistory(state, historyFileName)
check(err) 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