peterh / liner

Pure Go line editor with history, inspired by linenoise
MIT License
1.05k stars 132 forks source link

complete anywhere in the line #8

Closed rif closed 10 years ago

rif commented 10 years ago

Would it possible to have autocompletion for multiple words in the line?

For example: prompt> get_cost ArticleId=123

to be written like: type ge[TAB] than Ar[TAB]=123 and so on.

peterh commented 10 years ago

Yes, it is possible. Your completion function can (and should!) parse the input line to provide the best possible completion candidates.

For example, I have something like this at the top of my completion function:

if strings.HasPrefix(line, "load ") || strings.HasPrefix(line, "save ") {
        return completeFile(line)
}

if strings.HasPrefix(line, "set ") {
        list = make([]string, 0)
        // Get a list of all the command-line flags
        flag.VisitAll(func(f *flag.Flag) {
                candidate := "set "+f.Name+"="
                if strings.HasPrefix(candidate, line) {
                        list = append(list, candidate)
                }
        })
        return list
}

// normal first-word completion here

where completeFile reads the current directory for possible matches to the partial file name already typed (or all files if "load " is the entire line).

Treating all words equally for the purpose of completion would be a simple exercise in strings.Split() and strings.Join().