Search in sources :

Example 11 with Completer

use of jline.console.completer.Completer in project grails-core by grails.

the class GrailsConsole method resetCompleters.

public void resetCompleters() {
    final ConsoleReader reader = getReader();
    if (reader != null) {
        Collection<Completer> completers = reader.getCompleters();
        for (Completer completer : completers) {
            reader.removeCompleter(completer);
        }
        // for some unknown reason / bug in JLine you have to iterate over twice to clear the completers (WTF)
        completers = reader.getCompleters();
        for (Completer completer : completers) {
            reader.removeCompleter(completer);
        }
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) Completer(jline.console.completer.Completer)

Example 12 with Completer

use of jline.console.completer.Completer in project hive by apache.

the class CliDriver method getCommandCompleter.

public static Completer[] getCommandCompleter() {
    // StringsCompleter matches against a pre-defined wordlist
    // We start with an empty wordlist and build it up
    List<String> candidateStrings = new ArrayList<String>();
    // parenthesis at the end.
    for (String s : FunctionRegistry.getFunctionNames()) {
        if (s.matches("[a-z_]+")) {
            candidateStrings.add(s + "(");
        } else {
            candidateStrings.add(s);
        }
    }
    // We add Hive keywords, including lower-cased versions
    for (String s : HiveParser.getKeywords()) {
        candidateStrings.add(s);
        candidateStrings.add(s.toLowerCase());
    }
    StringsCompleter strCompleter = new StringsCompleter(candidateStrings);
    // Because we use parentheses in addition to whitespace
    // as a keyword delimiter, we need to define a new ArgumentDelimiter
    // that recognizes parenthesis as a delimiter.
    ArgumentDelimiter delim = new AbstractArgumentDelimiter() {

        @Override
        public boolean isDelimiterChar(CharSequence buffer, int pos) {
            char c = buffer.charAt(pos);
            return (Character.isWhitespace(c) || c == '(' || c == ')' || c == '[' || c == ']');
        }
    };
    // The ArgumentCompletor allows us to match multiple tokens
    // in the same line.
    final ArgumentCompleter argCompleter = new ArgumentCompleter(delim, strCompleter);
    // By default ArgumentCompletor is in "strict" mode meaning
    // a token is only auto-completed if all prior tokens
    // match. We don't want that since there are valid tokens
    // that are not in our wordlist (eg. table and column names)
    argCompleter.setStrict(false);
    // ArgumentCompletor always adds a space after a matched token.
    // This is undesirable for function names because a space after
    // the opening parenthesis is unnecessary (and uncommon) in Hive.
    // We stack a custom Completor on top of our ArgumentCompletor
    // to reverse this.
    Completer customCompletor = new Completer() {

        @Override
        public int complete(String buffer, int offset, List completions) {
            List<String> comp = completions;
            int ret = argCompleter.complete(buffer, offset, completions);
            // is exactly one valid completion, so we ignore other cases.
            if (completions.size() == 1) {
                if (comp.get(0).endsWith("( ")) {
                    comp.set(0, comp.get(0).trim());
                }
            }
            return ret;
        }
    };
    List<String> vars = new ArrayList<String>();
    for (HiveConf.ConfVars conf : HiveConf.ConfVars.values()) {
        vars.add(conf.varname);
    }
    StringsCompleter confCompleter = new StringsCompleter(vars) {

        @Override
        public int complete(final String buffer, final int cursor, final List<CharSequence> clist) {
            int result = super.complete(buffer, cursor, clist);
            if (clist.isEmpty() && cursor > 1 && buffer.charAt(cursor - 1) == '=') {
                HiveConf.ConfVars var = HiveConf.getConfVars(buffer.substring(0, cursor - 1));
                if (var == null) {
                    return result;
                }
                if (var.getValidator() instanceof Validator.StringSet) {
                    Validator.StringSet validator = (Validator.StringSet) var.getValidator();
                    clist.addAll(validator.getExpected());
                } else if (var.getValidator() != null) {
                    clist.addAll(Arrays.asList(var.getValidator().toDescription(), ""));
                } else {
                    clist.addAll(Arrays.asList("Expects " + var.typeString() + " type value", ""));
                }
                return cursor;
            }
            if (clist.size() > DELIMITED_CANDIDATE_THRESHOLD) {
                Set<CharSequence> delimited = new LinkedHashSet<CharSequence>();
                for (CharSequence candidate : clist) {
                    Iterator<String> it = Splitter.on(".").split(candidate.subSequence(cursor, candidate.length())).iterator();
                    if (it.hasNext()) {
                        String next = it.next();
                        if (next.isEmpty()) {
                            next = ".";
                        }
                        candidate = buffer != null ? buffer.substring(0, cursor) + next : next;
                    }
                    delimited.add(candidate);
                }
                clist.clear();
                clist.addAll(delimited);
            }
            return result;
        }
    };
    StringsCompleter setCompleter = new StringsCompleter("set") {

        @Override
        public int complete(String buffer, int cursor, List<CharSequence> clist) {
            return buffer != null && buffer.equals("set") ? super.complete(buffer, cursor, clist) : -1;
        }
    };
    ArgumentCompleter propCompleter = new ArgumentCompleter(setCompleter, confCompleter) {

        @Override
        public int complete(String buffer, int offset, List<CharSequence> completions) {
            int ret = super.complete(buffer, offset, completions);
            if (completions.size() == 1) {
                completions.set(0, ((String) completions.get(0)).trim());
            }
            return ret;
        }
    };
    return new Completer[] { propCompleter, customCompletor };
}
Also used : AbstractArgumentDelimiter(jline.console.completer.ArgumentCompleter.AbstractArgumentDelimiter) ArgumentDelimiter(jline.console.completer.ArgumentCompleter.ArgumentDelimiter) LinkedHashSet(java.util.LinkedHashSet) ArgumentCompleter(jline.console.completer.ArgumentCompleter) ArrayList(java.util.ArrayList) Completer(jline.console.completer.Completer) StringsCompleter(jline.console.completer.StringsCompleter) ArgumentCompleter(jline.console.completer.ArgumentCompleter) ConfVars(org.apache.hadoop.hive.conf.HiveConf.ConfVars) StringsCompleter(jline.console.completer.StringsCompleter) List(java.util.List) ArrayList(java.util.ArrayList) HiveConf(org.apache.hadoop.hive.conf.HiveConf) AbstractArgumentDelimiter(jline.console.completer.ArgumentCompleter.AbstractArgumentDelimiter) Validator(org.apache.hadoop.hive.conf.Validator)

Example 13 with Completer

use of jline.console.completer.Completer in project hive by apache.

the class BeeLineCommandCompleter method getCompleters.

public static List<Completer> getCompleters(Iterable<CommandHandler> handlers) {
    List<Completer> completers = new LinkedList<Completer>();
    for (CommandHandler handler : handlers) {
        String[] commandNames = handler.getNames();
        if (commandNames != null) {
            for (String commandName : commandNames) {
                List<Completer> compl = new LinkedList<Completer>();
                compl.add(new StringsCompleter(BeeLine.COMMAND_PREFIX + commandName));
                compl.addAll(Arrays.asList(handler.getParameterCompleters()));
                // last param no complete
                compl.add(new NullCompleter());
                completers.add(new AggregateCompleter(compl.toArray(new Completer[compl.size()])));
            }
        }
    }
    return completers;
}
Also used : NullCompleter(jline.console.completer.NullCompleter) StringsCompleter(jline.console.completer.StringsCompleter) AggregateCompleter(jline.console.completer.AggregateCompleter) Completer(jline.console.completer.Completer) AggregateCompleter(jline.console.completer.AggregateCompleter) StringsCompleter(jline.console.completer.StringsCompleter) NullCompleter(jline.console.completer.NullCompleter) LinkedList(java.util.LinkedList)

Example 14 with Completer

use of jline.console.completer.Completer in project spring-boot by spring-projects.

the class CommandCompleter method complete.

@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    int completionIndex = super.complete(buffer, cursor, candidates);
    int spaceIndex = buffer.indexOf(' ');
    String commandName = ((spaceIndex != -1) ? buffer.substring(0, spaceIndex) : "");
    if (StringUtils.hasText(commandName)) {
        for (Command command : this.commands) {
            if (command.getName().equals(commandName)) {
                if (cursor == buffer.length() && buffer.endsWith(" ")) {
                    printUsage(command);
                    break;
                }
                Completer completer = this.commandCompleters.get(command.getName());
                if (completer != null) {
                    completionIndex = completer.complete(buffer, cursor, candidates);
                    break;
                }
            }
        }
    }
    return completionIndex;
}
Also used : Command(org.springframework.boot.cli.command.Command) Completer(jline.console.completer.Completer) StringsCompleter(jline.console.completer.StringsCompleter) AggregateCompleter(jline.console.completer.AggregateCompleter) FileNameCompleter(jline.console.completer.FileNameCompleter) ArgumentCompleter(jline.console.completer.ArgumentCompleter)

Aggregations

Completer (jline.console.completer.Completer)14 StringsCompleter (jline.console.completer.StringsCompleter)8 ArgumentCompleter (jline.console.completer.ArgumentCompleter)7 AggregateCompleter (jline.console.completer.AggregateCompleter)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)4 ConsoleReader (jline.console.ConsoleReader)4 FileNameCompleter (jline.console.completer.FileNameCompleter)4 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 AliasCustomCommandFactory (org.fagu.fmv.cli.command.AliasCustomCommandFactory)2 JCommander (com.beust.jcommander.JCommander)1 ParameterDescription (com.beust.jcommander.ParameterDescription)1 AppInfo (com.datatorrent.stram.client.AppPackage.AppInfo)1 DTConfiguration (com.datatorrent.stram.client.DTConfiguration)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1