Search in sources :

Example 1 with CommandLineFormat

use of org.jboss.as.cli.CommandLineFormat in project wildfly-core by wildfly.

the class OperationRequestCompleter method completeArgumentValueAndPropertyNames.

private int completeArgumentValueAndPropertyNames(CommandContext ctx, ParsedCommandLine parsedCmd, Collection<CommandArgument> allArgs, List<String> candidates, String chunk, int result) {
    // lastArg will be not null if its name is equals to the last typed one.
    CommandArgument lastArg = null;
    // All property present means proposing end of list instead of property
    // separator when the last property is a boolean one.
    boolean allPropertiesPresent = true;
    // e.g.: recursive and recursive-depth
    for (CommandArgument arg : allArgs) {
        try {
            if (arg.canAppearNext(ctx)) {
                allPropertiesPresent = false;
            } else if (arg.getFullName().equals(chunk)) {
                lastArg = arg;
            }
        } catch (CommandFormatException e) {
            return -1;
        }
    }
    boolean needNeg = false;
    // We must first check that we have no matches for option names
    // prior to call the argument (option with no value) value completers
    // Otherwise we can enter the case where value completer would complete
    // an option name. That is possible because value completer parsers are not strict.
    // for example: ls --hel<TAB> ==> --hel will be completed by the node-path completer
    // and this can give strange completion result.
    boolean optionMatch = false;
    for (CommandArgument arg : allArgs) {
        try {
            if (arg.canAppearNext(ctx)) {
                if (arg.getIndex() < 0) {
                    String argFullName = arg.getFullName();
                    if (chunk != null && argFullName.startsWith(chunk)) {
                        if (!parsedCmd.isLastPropertyNegated()) {
                            optionMatch = true;
                            break;
                        } else // We can only add candidates that are of type boolean
                        if (!arg.isValueRequired()) {
                            optionMatch = true;
                            break;
                        }
                    }
                }
            }
        } catch (CommandFormatException e) {
            return -1;
        }
    }
    for (CommandArgument arg : allArgs) {
        try {
            if (arg.canAppearNext(ctx)) {
                if (arg.getIndex() >= 0) {
                    if (optionMatch) {
                        continue;
                    }
                    CommandLineCompleter valCompl = arg.getValueCompleter();
                    if (valCompl != null) {
                        final String value = chunk == null ? "" : chunk;
                        valCompl.complete(ctx, value, value.length(), candidates);
                    // Values have been added as candidate.
                    // If there are some options to propose, they will be mixed
                    // with the values. That only applies to commands.
                    }
                } else {
                    String argFullName = arg.getFullName();
                    if (chunk == null || argFullName.startsWith(chunk)) {
                        /* The following complexity is due to cases like:
                                 recursive and recursive-depth. Both start with the same name
                                 but are of different types. Completion can't propose
                                 recursive-depth of !recursive has been typed.
                                 If the last property is not negated,
                                 we can add all properties with the same name.
                             */
                        if (!parsedCmd.isLastPropertyNegated()) {
                            candidates.add(arg.getDecoratedName());
                        } else // We can only add candidates that are of type boolean
                        {
                            if (!arg.isValueRequired()) {
                                candidates.add(arg.getDecoratedName());
                            }
                        }
                    }
                    // and this property is not already negated.
                    if (!arg.isValueRequired() && !parsedCmd.isLastPropertyNegated()) {
                        needNeg = true;
                    }
                }
            }
        } catch (CommandFormatException e) {
            return -1;
        }
    }
    // Propose not operator only after a property separator
    if (needNeg && parsedCmd.endsOnPropertySeparator()) {
        candidates.add(Util.NOT_OPERATOR);
    }
    if (lastArg != null) {
        if (lastArg.isValueRequired()) {
            candidates.add(lastArg.getFullName() + "=");
        } else if (lastArg instanceof ArgumentWithoutValue) {
            ArgumentWithoutValue argWithoutValue = (ArgumentWithoutValue) lastArg;
            // If the last argument is exclusive, no need to add any separator
            if (!argWithoutValue.isExclusive()) {
                // to make completion propose next argument
                if (!allPropertiesPresent) {
                    CommandLineFormat format = parsedCmd.getFormat();
                    if (format != null && format.getPropertySeparator() != null) {
                        candidates.add(lastArg.getFullName() + format.getPropertySeparator());
                    }
                }
            }
        } else {
            // We are completing implicit values for operation.
            CommandLineFormat format = parsedCmd.getFormat();
            // enough.
            if (!parsedCmd.isLastPropertyNegated()) {
                candidates.add("=" + Util.FALSE);
                result = recalculateResult(parsedCmd, candidates, chunk, result);
            }
            if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
                candidates.add(format.getPropertyListEnd());
                result = recalculateResult(parsedCmd, candidates, chunk, result);
                if (!allPropertiesPresent) {
                    candidates.add(format.getPropertySeparator());
                    result = recalculateResult(parsedCmd, candidates, chunk, result);
                }
            }
        }
    }
    if (candidates.isEmpty()) {
        if (chunk == null && !parsedCmd.endsOnSeparator()) {
            final CommandLineFormat format = parsedCmd.getFormat();
            if (format != null && format.getPropertyListEnd() != null && format.getPropertyListEnd().length() > 0) {
                candidates.add(format.getPropertyListEnd());
                result = recalculateResult(parsedCmd, candidates, chunk, result);
            }
        }
    } else {
        Collections.sort(candidates);
    }
    return result;
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) CommandLineCompleter(org.jboss.as.cli.CommandLineCompleter) CommandLineFormat(org.jboss.as.cli.CommandLineFormat) ArgumentWithoutValue(org.jboss.as.cli.impl.ArgumentWithoutValue)

Example 2 with CommandLineFormat

use of org.jboss.as.cli.CommandLineFormat in project wildfly-core by wildfly.

the class OperationRequestCompleter method completeWithValueCompleter.

private int completeWithValueCompleter(CommandContext ctx, ParsedCommandLine parsedCmd, Collection<CommandArgument> allArgs, String argName, String buffer, List<String> candidates, String chunk, int result, CommandLineCompleter valueCompleter) {
    if (chunk == null) {
        // Complete with false if boolean
        for (CommandArgument arg : allArgs) {
            String argFullName = arg.getFullName();
            if (argFullName.equals(argName)) {
                if (!arg.isValueRequired()) {
                    candidates.add(Util.FALSE);
                    return buffer.length();
                }
            }
        }
    }
    // Call the value completer.
    // Completion is done with substituted line, so returned offset
    // is possibly wrong.
    // Some completers are reseting the CommandContext referenced parsed
    // command. We need to keep a local reference to substitution
    // to compute substitution index after completion occured.
    SubstitutedLine substitutions = parsedCmd.getSubstitutions();
    final String normalizedChunk = chunk == null ? "" : chunk;
    int valueResult = valueCompleter.complete(ctx, normalizedChunk, normalizedChunk.length(), candidates);
    // No proposition.
    if (valueResult < 0) {
        return valueResult;
    } else {
        // or the propertyListEnd if no more properties.
        if (suggestionEqualsUserEntry(candidates, chunk, valueResult)) {
            final CommandLineFormat format = parsedCmd.getFormat();
            if (format != null) {
                for (CommandArgument arg : allArgs) {
                    try {
                        if (arg.canAppearNext(ctx)) {
                            candidates.set(0, "" + format.getPropertySeparator());
                            return buffer.length();
                        }
                    } catch (CommandFormatException e) {
                        return -1;
                    }
                }
                // inline the end of properties.
                candidates.set(0, format.getPropertyListEnd());
                // at the end of the input.
                return buffer.length();
            }
        }
        // WFCORE-3190 ignore trailing spaces after the cursor position
        int trailOffset = substitutions.getSubstitued().substring(result).length() - normalizedChunk.length();
        return result + valueResult + trailOffset;
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) CommandArgument(org.jboss.as.cli.CommandArgument) SubstitutedLine(org.jboss.as.cli.parsing.StateParser.SubstitutedLine) CommandLineFormat(org.jboss.as.cli.CommandLineFormat)

Aggregations

CommandArgument (org.jboss.as.cli.CommandArgument)2 CommandFormatException (org.jboss.as.cli.CommandFormatException)2 CommandLineFormat (org.jboss.as.cli.CommandLineFormat)2 CommandLineCompleter (org.jboss.as.cli.CommandLineCompleter)1 ArgumentWithoutValue (org.jboss.as.cli.impl.ArgumentWithoutValue)1 SubstitutedLine (org.jboss.as.cli.parsing.StateParser.SubstitutedLine)1