Skip to content

Commit

Permalink
OptionCompleter: improved short option value completion, #485
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 15, 2019
1 parent b1ec62c commit dfc070d
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Expand Up @@ -833,7 +833,7 @@ public OptionCompleter(org.jline.reader.Completer completer, Map<String,List<Str
public OptionCompleter(Map<String,List<String>> optionValues, Collection<String> options, int startPos) {
this(OptDesc.compile(optionValues, options), startPos);
}

public OptionCompleter(org.jline.reader.Completer completer, Collection<OptDesc> options, int startPos) {
this(options, startPos);
this.argsCompleters.add(completer);
Expand Down Expand Up @@ -901,13 +901,13 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
if ((buffer.contains("=") && !buffer.endsWith("=") && !valueCandidates) || addbuff) {
candidates.add(new Candidate(buffer, buffer, null, null, null, null, true));
}
} else if (words.size() > 1 && words.get(words.size() - 2).matches("-[a-zA-Z]{1}") && findOptDesc(command, words.get(words.size() - 2)).hasValue()) {
findOptDesc(command, words.get(words.size() - 2)).valueCompleter().complete(reader, commandLine, candidates);
} else if (words.size() > 1 && shortOptionValueCompleter(command, words.get(words.size() - 2)) != null) {
shortOptionValueCompleter(command, words.get(words.size() - 2)).complete(reader, commandLine, candidates);
} else if (!argsCompleters.isEmpty()) {
int args = -1;
for (int i = startPos; i < words.size(); i++) {
if (!words.get(i).startsWith("-")) {
if (i > 0 && (!words.get(i - 1).matches("-[a-zA-Z]{1}") || !findOptDesc(command, words.get(i - 1)).hasValue())) {
if (i > 0 && shortOptionValueCompleter(command, words.get(i - 1)) == null) {
args++;
}
}
Expand All @@ -922,8 +922,33 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
}
}

private org.jline.reader.Completer shortOptionValueCompleter(String command, String opt) {
if (!opt.matches("-[a-zA-Z]+")) {
return null;
}
org.jline.reader.Completer out = null;
Collection<OptDesc> optDescs = commandOptions == null ? options : commandOptions.apply(command);
if (opt.length() == 2) {
out = findOptDesc(optDescs, opt).valueCompleter();
} else if (opt.length() > 2) {
for (int i = 1; i < opt.length(); i++) {
OptDesc o = findOptDesc(optDescs, "-" + opt.charAt(i));
if (o.shortOption() == null) {
return null;
} else if (out == null) {
out = o.valueCompleter();
}
}
}
return out;
}

private OptDesc findOptDesc(String command, String opt) {
for (OptDesc o : commandOptions == null ? options : commandOptions.apply(command)) {
return findOptDesc(commandOptions == null ? options : commandOptions.apply(command), opt);
}

private OptDesc findOptDesc(Collection<OptDesc> optDescs, String opt) {
for (OptDesc o : optDescs) {
if (o.match(opt)) {
return o;
}
Expand Down

0 comments on commit dfc070d

Please sign in to comment.