edit: Make argCompleter a field of Editor.

This commit is contained in:
Qi Xiao 2017-05-22 01:15:52 +01:00
parent e36b0d8371
commit b6d57a8be2
3 changed files with 11 additions and 5 deletions

View File

@ -67,7 +67,8 @@ func installModules(modules map[string]eval.Namespace, ed *Editor) {
}
ns["binding"] = eval.NewRoVariable(binding)
ns["completer"] = argCompleter
ns["completer"] = ed.argCompleter
ns[eval.FnPrefix+"complete-getopt"] = eval.NewRoVariable(
&eval.BuiltinFn{"le:&complete-getopt", complGetopt})
for _, bac := range argCompletersData {

View File

@ -17,20 +17,20 @@ var (
"": {"complete-filename", complFilename},
"sudo": {"complete-sudo", complSudo},
}
argCompleter eval.Variable
)
func init() {
func makeCompleterMap() eval.Map {
m := map[eval.Value]eval.Value{}
for k, v := range argCompletersData {
m[eval.String(k)] = v
}
argCompleter = eval.NewPtrVariableWithValidator(eval.NewMap(m), eval.ShouldBeMap)
return eval.NewMap(m)
}
func completeArg(words []string, ev *eval.Evaler) ([]*candidate, error) {
logger.Printf("completing argument: %q", words)
m := argCompleter.Get().(eval.Map)
// XXX(xiaq): not the best way to get argCompleter.
m := ev.Editor.(*Editor).argCompleter.Get().(eval.Map)
var v eval.Value
if m.HasKey(eval.String(words[0])) {
v = m.IndexOne(eval.String(words[0]))

View File

@ -47,6 +47,8 @@ type Editor struct {
beforeReadLine eval.Variable
afterReadLine eval.Variable
argCompleter eval.Variable
historyMutex sync.RWMutex
active bool
@ -126,6 +128,9 @@ func NewEditor(file *os.File, sigs chan os.Signal, ev *eval.Evaler, st *store.St
beforeReadLine: eval.NewPtrVariableWithValidator(eval.NewList(), eval.ShouldBeList),
afterReadLine: eval.NewPtrVariableWithValidator(eval.NewList(), eval.ShouldBeList),
argCompleter: eval.NewPtrVariableWithValidator(
makeCompleterMap(), eval.ShouldBeMap),
}
ev.Editor = ed