private ArgumentWithValue ln;
public BatchEditLineHandler() {
super("batch-edit-line");
ln = new ArgumentWithValue(this, 0, "--line-number");
ln.addCantAppearAfter(helpArg);
ArgumentWithValue line = new ArgumentWithValue(this, new CommandLineCompleter() {
@Override
public int complete(CommandContext ctx, String buffer, int cursor, List<String> candidates) {
final String lnStr = ln.getValue(ctx.getParsedCommandLine());
if(lnStr == null) {
return -1;
}
final String originalLine = ctx.getParsedCommandLine().getOriginalLine();
boolean skipWS;
int wordCount;
if(Character.isWhitespace(originalLine.charAt(0))) {
skipWS = true;
wordCount = 0;
} else {
skipWS = false;
wordCount = 1;
}
int cmdStart = 1;
while(cmdStart < originalLine.length()) {
if(skipWS) {
if(!Character.isWhitespace(originalLine.charAt(cmdStart))) {
skipWS = false;
++wordCount;
if(wordCount == 3) {
break;
}
}
} else if(Character.isWhitespace(originalLine.charAt(cmdStart))) {
skipWS = true;
}
++cmdStart;
}
final String cmd;
if(wordCount == 2) {
cmd = "";
} else if(wordCount != 3) {
return -1;
} else {
cmd = originalLine.substring(cmdStart);
}
int cmdResult = ctx.getDefaultCommandCompleter().complete(ctx, cmd, 0, candidates);
if(cmdResult < 0) {
return cmdResult;
}
// escaping index correction
int escapeCorrection = 0;
int start = originalLine.length() - 1 - buffer.length();
while(start - escapeCorrection >= 0) {
final char ch = originalLine.charAt(start - escapeCorrection);
if(Character.isWhitespace(ch) || ch == '=') {
break;
}
++escapeCorrection;
}
return buffer.length() + escapeCorrection - (cmd.length() - cmdResult);
}}, Integer.MAX_VALUE, "--line") {
};
line.addRequiredPreceding(ln);
}