String buffer = op.getBuffer();
List<String> candidates = op.getCompletionCandidates();
if(buffer.isEmpty()) {
// Nothing in the buffer, return all commands
for(String name : context.getCommandRegistry().getCommandNames()) {
Command command = context.getCommandRegistry().getCommand(name);
if(command.isAvailable(context)) {
candidates.add(name);
}
}
} else {
ProcessedCommand procCmd = new ProcessedCommand(buffer, op.getCursor());
if(!procCmd.isCommandComplete()) {
// A possibly incomplete command in the buffer, return the commands that match
for(String name : context.getCommandRegistry().getCommandNames()) {
Command command = context.getCommandRegistry().getCommand(name);
if(command.isAvailable(context) && name.startsWith(procCmd.getCommand())) {
candidates.add(name);
}
}
} else {
Command command = context.getCommandRegistry().getCommand(procCmd.getCommand());
if(command.isAvailable(context)) {
op.setOffset(op.getCursor());
for(Argument arg : procCmd.getArguments()) {
if(arg.getOffset()<op.getCursor()) {
op.setOffset(arg.getOffset());
} else {
break;
}
}
addPrefixMatches(procCmd.getCurrentArgument(), command.getOptions(), candidates);
command.complete(context, procCmd, candidates);
}
}
}
Collections.sort(candidates);
}