我最终实现了一个新的接口(它是用groovy编写的):
class MatchAnyCompleter implements Completer {
protected final List<Candidate> candidateList = []
MatchAnyCompleter(List<String> list) {
assert list
list.each {
candidateList << new Candidate(AttributedString.stripAnsi(it), it, null, null, null, null, true)
}
}
@Override
void complete(final LineReader reader, final ParsedLine commandLine, final List<Candidate> selected) {
assert commandLine != null
assert selected != null
selected.addAll(candidateList.findAll {
Candidate candidate ->
commandLine.words().stream().allMatch {
String keyword ->
candidate.value().contains(keyword)
}
})
}
}
测试:
class MatchAnyCompleterTest extends Specification {
def "Testing matches"() {
setup:
def mac = new MatchAnyCompleter([
"somevalue",
"welcome_trashcan",
"pecan_seaweed",
"yeswecan",
"canwest",
"nomatchhere"
])
def cmdLine = new ParsedLine() {
// non-implemented methods were removed for simplicity
@Override
List<String> words() {
return ["we","can"]
}
}
List<Candidate> selected = []
mac.complete(null, cmdLine, selected)
expect:
selected.each {
println it.value()
}
assert selected.size() == 4
}
}
输出:
welcome_trashcan
pecan_seaweed
yeswecan
canwest