
javarepl.completion.CommandCompleter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javarepl Show documentation
Show all versions of javarepl Show documentation
Java REPL is a simple Read-Eval-Print-Loop for Java language.
package javarepl.completion;
import com.googlecode.totallylazy.Sequence;
import static com.googlecode.totallylazy.Sequences.*;
import static com.googlecode.totallylazy.Strings.startsWith;
import static javarepl.completion.CompletionCandidate.asCompletionCandidate;
public class CommandCompleter extends Completer {
private final String command;
private final Sequence candidates;
public CommandCompleter(String command) {
this(command, empty(String.class));
}
public CommandCompleter(String command, Sequence candidates) {
this.command = command;
this.candidates = candidates;
}
public CompletionResult call(String expression) throws Exception {
Sequence parts = sequence(expression.split(" "));
if (parts.isEmpty()) {
return new CompletionResult(expression, 0, empty(CompletionCandidate.class));
}
if (command.equals(parts.head())) {
String nextCommandPart = parts.tail().headOption().getOrElse("");
return new CompletionResult(expression, candidates.isEmpty() ? 0 : command.length() + 1, candidates.filter(startsWith(nextCommandPart)).map(asCompletionCandidate()));
}
if (command.startsWith(parts.head())) {
return new CompletionResult(expression, 0, one(asCompletionCandidate().apply(command)));
}
return new CompletionResult(expression, 0, empty(CompletionCandidate.class));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy