@Override
public void complete(CompleteOperation completeOperation)
{
String line = completeOperation.getBuffer();
ShellContext shellContext = shell.newShellContext();
final AbstractShellInteraction cmd = shell.findCommand(shellContext, line);
if (cmd == null)
{
Collection<AbstractShellInteraction> commands = shell.findMatchingCommands(shellContext, line);
for (AbstractShellInteraction command : commands)
{
completeOperation.addCompletionCandidate(command.getName());
}
}
else if (line.equals(cmd.getName()))
{
completeOperation.addCompletionCandidate(" ");
}
else
{
try
{
// We are dealing with one-level commands only.
// Eg. new-project-type --named ... instead of new-project-type setup --named ...
// cmd.populateInputs(line, true);
ParsedCompleteObject completeObject = cmd.parseCompleteObject(line);
if (completeObject.doDisplayOptions())
{
List<String> options = cmd.getCompletionOptions(completeObject.getName(), line);
completeOperation.addCompletionCandidates(options);
if (completeOperation.getCompletionCandidates().size() == 1)
{
completeOperation.setOffset(completeOperation.getCursor() - completeObject.getOffset());
}
}
else
{
final InputComponent<?, Object> input;
if (completeObject.isOption())
{
// try to complete an option value. Eg: "--xxx"
input = cmd.getInputs().get(completeObject.getName());
}
// try to complete a argument value Eg: ls . (. is the argument)
else if (completeObject.isArgument())
{
input = cmd.getInputs().get(ARGUMENTS_INPUT_NAME); // default for arguments
}
else
{
input = null;
}
if (input != null)
{
CompletionStrategy completionObj = CompletionStrategyFactory.getCompletionFor(input);
completionObj.complete(completeOperation, input, shellContext, completeObject.getValue(),
shell.getConverterFactory());
}
}
}
catch (ArgumentParserException e)
{
if (!cmd.getInputs().isEmpty())
{
completeOperation.doAppendSeparator(false);
completeOperation.addCompletionCandidate(line + "--");
}
}