All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javarepl.completion.AggregateCompleter Maven / Gradle / Ivy

There is a newer version: 431
Show newest version
package javarepl.completion;

import com.googlecode.totallylazy.Group;
import com.googlecode.totallylazy.Option;
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.functions.Function1;

import static com.googlecode.totallylazy.Groups.groupKey;
import static com.googlecode.totallylazy.Sequences.empty;

public class AggregateCompleter extends Completer {
    private final Sequence completers;

    public AggregateCompleter(Sequence completers) {
        this.completers = completers;
    }

    public CompletionResult call(String expression) throws Exception {
        Option> group = completeGroup(expression);

        if (group.isEmpty())
            return new CompletionResult(expression, 0, empty(CompletionCandidate.class));

        return new CompletionResult(expression, group.get().key(), group.get().flatMap(CompletionResult::candidates));
    }

    private Option> completeGroup(String expression) {
        return completers.map(complete(expression))
                .unique()
                .groupBy(CompletionResult::position)
                .sortBy(groupKey(Integer.class))
                .reverse()
                .headOption();
    }

    private Function1 complete(final String expression) {
        return completer -> completer.apply(expression);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy