Search in sources :

Example 6 with SortedMatchIterator

use of henplus.view.util.SortedMatchIterator in project henplus by neurolabs.

the class AliasCommand method complete.

@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
    final StringTokenizer st = new StringTokenizer(partialCommand);
    final String cmd = (String) st.nextElement();
    final int argc = st.countTokens();
    // list-aliases gets no names.
    if ("list-aliases".equals(cmd)) {
        return null;
    }
    /*
         * some completion within the alias/unalias commands.
         */
    if ("alias".equals(cmd) || "unalias".equals(cmd)) {
        final HashSet<String> alreadyGiven = new HashSet<String>();
        if ("alias".equals(cmd)) {
            // do not complete beyond first word.
            if (argc > ("".equals(lastWord) ? 0 : 1)) {
                return null;
            }
        } else {
            /*
                 * remember all aliases, that have already been given on the
                 * commandline and exclude from completion.. cool, isn't it ?
                 */
            while (st.hasMoreElements()) {
                alreadyGiven.add(st.nextToken());
            }
        }
        // ok, now return the list.
        return new SortedMatchIterator(lastWord, _aliases) {

            @Override
            protected boolean exclude(final String current) {
                return alreadyGiven.contains(current);
            }
        };
    }
    /*
         * ok, someone tries to complete something that is a command. try to
         * find the actual command and ask that command to do the completion.
         */
    final String toExecute = _aliases.get(cmd);
    if (toExecute != null) {
        final Command c = disp.getCommandFrom(toExecute);
        if (c != null) {
            int i = 0;
            final String param = partialCommand;
            while (param.length() < i && Character.isWhitespace(param.charAt(i))) {
                ++i;
            }
            while (param.length() < i && !Character.isWhitespace(param.charAt(i))) {
                ++i;
            }
            return c.complete(disp, toExecute + param.substring(i), lastWord);
        }
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) SortedMatchIterator(henplus.view.util.SortedMatchIterator) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command) HashSet(java.util.HashSet)

Example 7 with SortedMatchIterator

use of henplus.view.util.SortedMatchIterator in project henplus by neurolabs.

the class AbstractPropertyCommand method complete.

/**
     * complete property names.
     */
@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
    final StringTokenizer st = new StringTokenizer(partialCommand);
    final String cmd = st.nextToken();
    final int argc = st.countTokens();
    if (argc > ("".equals(lastWord) ? 0 : 1)) {
        /* one arg given */
        if (getSetCommand().equals(cmd)) {
            final String name = st.nextToken();
            PropertyHolder holder;
            holder = getRegistry().getPropertyMap().get(name);
            if (holder == null) {
                return null;
            }
            return holder.completeValue(lastWord);
        }
        return null;
    }
    return new SortedMatchIterator(lastWord, getRegistry().getPropertyMap());
}
Also used : StringTokenizer(java.util.StringTokenizer) PropertyHolder(henplus.property.PropertyHolder) SortedMatchIterator(henplus.view.util.SortedMatchIterator)

Aggregations

SortedMatchIterator (henplus.view.util.SortedMatchIterator)7 StringTokenizer (java.util.StringTokenizer)5 AbstractCommand (henplus.AbstractCommand)2 Command (henplus.Command)2 HashSet (java.util.HashSet)2 PropertyHolder (henplus.property.PropertyHolder)1