Search in sources :

Example 1 with Completion

use of org.springframework.shell.core.Completion in project geode by apache.

the class LogLevelConverterTest method testCompletionContainsOnlyLog4jLevels.

@Test
public void testCompletionContainsOnlyLog4jLevels() throws Exception {
    LogLevelConverter converter = new LogLevelConverter();
    List<Completion> completions = new ArrayList();
    converter.getAllPossibleValues(completions, null, null, null, null);
    assertThat(completions.size()).isEqualTo(8);
    for (Completion completion : completions) {
        String level = completion.getValue();
        assertThat(Level.getLevel(level)).isNotNull();
    }
}
Also used : Completion(org.springframework.shell.core.Completion) ArrayList(java.util.ArrayList) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 2 with Completion

use of org.springframework.shell.core.Completion in project geode by apache.

the class IndexTypeConverter method getAllPossibleValues.

@Override
public boolean getAllPossibleValues(List<Completion> completions, Class<?> targetType, String existingData, String optionContext, MethodTarget target) {
    if (String.class.equals(targetType) && ConverterHint.INDEX_TYPE.equals(optionContext)) {
        completions.add(new Completion("range"));
        completions.add(new Completion("key"));
        completions.add(new Completion("hash"));
    }
    return !completions.isEmpty();
}
Also used : Completion(org.springframework.shell.core.Completion)

Example 3 with Completion

use of org.springframework.shell.core.Completion in project geode by apache.

the class GfshParser method completeAdvanced.

/**
   *
   * The super class's completeAdvanced has the following limitations: 1) for option name
   * completion, you need to end your buffer with --. 2) For command name completion, you need to
   * end your buffer with a space. 3) the above 2 completions, the returned value is always 0, and
   * the completion is the entire command 4) for value completion, you also need to end your buffer
   * with space, the returned value is the length of the original string, and the completion strings
   * are the possible values.
   *
   * With these limitations, we will need to overwrite this command with some customization
   *
   * @param userInput
   * @param cursor this input is ignored, we always move the cursor to the end of the userInput
   * @param candidates
   * @return the cursor point at which the candidate string will begin, this is important if you
   *         have only one candidate, cause tabbing will use it to complete the string for you.
   */
@Override
public int completeAdvanced(String userInput, int cursor, final List<Completion> candidates) {
    // move the cursor to the end of the input
    cursor = userInput.length();
    List<String> inputTokens = splitUserInput(userInput);
    // check if the input is before any option is specified, e.g. (start, describe)
    boolean inputIsBeforeOption = true;
    for (String token : inputTokens) {
        if (token.startsWith("--")) {
            inputIsBeforeOption = false;
            break;
        }
    }
    // in the case of we are still trying to complete the command name
    if (inputIsBeforeOption) {
        List<Completion> potentials = getCandidates(userInput);
        if (potentials.size() == 1 && potentials.get(0).getValue().equals(userInput)) {
            potentials = getCandidates(userInput.trim() + " ");
        }
        if (potentials.size() > 0) {
            candidates.addAll(potentials);
            return 0;
        }
    // otherwise, falling down to the potentials.size==0 case below
    }
    // now we are either trying to complete the option or a value
    // trying to get candidates using the converted input
    String buffer = getSimpleParserInputFromTokens(inputTokens);
    String lastToken = inputTokens.get(inputTokens.size() - 1);
    boolean lastTokenIsOption = lastToken.startsWith("--");
    // In the original user input, where to begin the candidate string for completion
    int candidateBeginAt;
    // initially assume we are trying to complete the last token
    List<Completion> potentials = getCandidates(buffer);
    // last token is complete, then add either space or " --" and try again
    if (potentials.size() == 0 || userInput.endsWith(" ")) {
        candidateBeginAt = buffer.length();
        // last token is an option
        if (lastTokenIsOption) {
            // add a space to the buffer to get the option value candidates
            potentials = getCandidates(buffer + " ");
            lastTokenIsOption = false;
        } else // last token is a value, we need to add " --" to it and retry to get the next list of options
        {
            potentials = getCandidates(buffer + " --");
            lastTokenIsOption = true;
        }
    } else {
        if (lastTokenIsOption) {
            candidateBeginAt = buffer.length() - lastToken.length();
        } else {
            // need to return the index before the "=" sign, since later on we are going to add the
            // "=" sign to the completion candidates
            candidateBeginAt = buffer.length() - lastToken.length() - 1;
        }
    }
    // manipulate the candidate strings
    if (lastTokenIsOption) {
        // strip off the beginning part of the candidates from the cursor point
        potentials.replaceAll(completion -> new Completion(completion.getValue().substring(candidateBeginAt)));
    } else // if the completed values are option, and the userInput doesn't ends with an "=" sign,
    if (!userInput.endsWith("=")) {
        // these potentials do not have "=" in front of them, manually add them
        potentials.replaceAll(completion -> new Completion("=" + completion.getValue()));
    }
    candidates.addAll(potentials);
    // usually we want to return the cursor at candidateBeginAt, but since we consolidated
    // --J options into one, and added quotes around we need to consider the length difference
    // between userInput and the converted input
    cursor = candidateBeginAt + (userInput.trim().length() - buffer.length());
    return cursor;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Parser(org.springframework.shell.core.Parser) Properties(java.util.Properties) ParseResult(org.springframework.shell.event.ParseResult) ArrayConverter(org.springframework.shell.converters.ArrayConverter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Matcher(java.util.regex.Matcher) SimpleParser(org.springframework.shell.core.SimpleParser) Completion(org.springframework.shell.core.Completion) Converter(org.springframework.shell.core.Converter) Pattern(java.util.regex.Pattern) CommandMarker(org.springframework.shell.core.CommandMarker) Completion(org.springframework.shell.core.Completion)

Example 4 with Completion

use of org.springframework.shell.core.Completion in project geode by apache.

the class GfshParser method getCandidates.

/**
   * @param buffer use the buffer to find the completion candidates
   *
   *        Note the cursor maynot be the size the buffer
   */
private List<Completion> getCandidates(String buffer) {
    List<Completion> candidates = new ArrayList<>();
    // always pass the buffer length as the cursor position for simplicity purpose
    super.completeAdvanced(buffer, buffer.length(), candidates);
    // trimming the candidates
    candidates.replaceAll(completion -> new Completion(completion.getValue().trim()));
    return candidates;
}
Also used : Completion(org.springframework.shell.core.Completion) ArrayList(java.util.ArrayList)

Example 5 with Completion

use of org.springframework.shell.core.Completion in project geode by apache.

the class RegionPathConverter method getAllPossibleValues.

@Override
public boolean getAllPossibleValues(List<Completion> completions, Class<?> targetType, String existingData, String optionContext, MethodTarget target) {
    Set<String> regionPathSet = getAllRegionPaths();
    Gfsh gfsh = Gfsh.getCurrentInstance();
    String currentContextPath = "";
    if (gfsh != null) {
        currentContextPath = gfsh.getEnvProperty(Gfsh.ENV_APP_CONTEXT_PATH);
        if (currentContextPath != null && !org.apache.geode.management.internal.cli.converters.RegionPathConverter.DEFAULT_APP_CONTEXT_PATH.equals(currentContextPath)) {
            regionPathSet.remove(currentContextPath);
            regionPathSet.add(org.apache.geode.management.internal.cli.converters.RegionPathConverter.DEFAULT_APP_CONTEXT_PATH);
        }
    }
    for (String regionPath : regionPathSet) {
        if (existingData != null) {
            if (regionPath.startsWith(existingData)) {
                completions.add(new Completion(regionPath));
            }
        } else {
            completions.add(new Completion(regionPath));
        }
    }
    return !completions.isEmpty();
}
Also used : Completion(org.springframework.shell.core.Completion) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh)

Aggregations

Completion (org.springframework.shell.core.Completion)6 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 StringUtils (org.apache.commons.lang.StringUtils)1 Helper (org.apache.geode.management.internal.cli.help.Helper)1 Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1 Test (org.junit.Test)1 ArrayConverter (org.springframework.shell.converters.ArrayConverter)1 CommandMarker (org.springframework.shell.core.CommandMarker)1 Converter (org.springframework.shell.core.Converter)1 Parser (org.springframework.shell.core.Parser)1 SimpleParser (org.springframework.shell.core.SimpleParser)1 ParseResult (org.springframework.shell.event.ParseResult)1