if (state.getPlugin() != null)
{
Queue<String> tokens = state.getTokens();
state.setIndex(state.getBuffer().length());
PluginMetadata plugin = state.getPlugin();
if (tokens.size() > 1)
{
// there must be a command, or a string of arguments for the
// default command
String peek = tokens.peek();
if (plugin.hasCommand(peek, shell))
{
CommandMetadata command = plugin.getCommand(tokens.remove());
state.setCommand(command);
// TODO this should probably be tokenComplete`?` sensitive
// complete the command, remove the last token
}
else if (plugin.hasDefaultCommand())
{
CommandMetadata defaultCommand = plugin.getDefaultCommand();
state.setCommand(defaultCommand);
}
else
{
// bad input, not a command and there is no default command
}
}
else if (!tokens.isEmpty())
{
// just one more token, it's either a command or an argument
// for the default command
String peek = tokens.peek();
if (plugin.hasCommand(peek, shell))
{
CommandMetadata command = plugin.getCommand(tokens.remove());
state.setCommand(command);
}
else if (couldBeCommand(plugin, peek))
{
state.setIndex(state.getBuffer().lastIndexOf(peek));
addCommandCandidates(plugin, state);
}
}
else if (plugin.hasCommands())
{
addCommandCandidates(plugin, state);
}
if (plugin.hasDefaultCommand())
{
if ((state.getCommand() == null))
{
CommandMetadata defaultCommand = plugin.getDefaultCommand();
state.setCommand(defaultCommand);
}
if (tokens.isEmpty() && !state.hasSuggestions() && plugin.getDefaultCommand().equals(state.getCommand()))
{
if (plugin.hasCommands())
{
addCommandCandidates(plugin, state);
}
}
}