Skip to content

Commit

Permalink
OptionCompleter: complete values of short option, #485
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 11, 2019
1 parent 04ec072 commit 064aa77
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Expand Up @@ -734,7 +734,9 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
assert candidates != null;
List<String> words = commandLine.words();
String buffer = commandLine.word().substring(0, commandLine.wordCursor());
if (buffer.startsWith("-")) {
if (startPos >= words.size()) {
candidates.add(new Candidate(buffer, buffer, null, null, null, null, true));
} else if (buffer.startsWith("-")) {
boolean addbuff = true;
boolean valueCandidates = false;
if (commandOptions != null) {
Expand All @@ -748,7 +750,7 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
}
}
} else {
int eq = buffer.indexOf('=');
int eq = buffer.matches("-[a-zA-Z]{1}[a-zA-Z0-9]+") ? 2 : buffer.indexOf('=');
if (eq < 0) {
List<String> usedOptions = new ArrayList<>();
for (int i = startPos; i < words.size(); i++) {
Expand Down Expand Up @@ -781,8 +783,9 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
candidates.add(new Candidate(o, o, null, null, null, null, true));
}
} else {
String value = buffer.substring(eq + 1);
String curBuf = buffer.substring(0, eq + 1);
int nb = buffer.contains("=") ? 1 : 0;
String value = buffer.substring(eq + nb);
String curBuf = buffer.substring(0, eq + nb);
String opt = buffer.substring(0, eq);
if (optionValues.containsKey(opt) && !optionValues.get(opt).isEmpty()) {
for (String v: optionValues.get(opt)) {
Expand All @@ -798,22 +801,24 @@ 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 (argsCompleters.size() > 1) {
} else if (words.size() > 1 && words.get(words.size() - 2).matches("-[a-zA-Z]{1}") && optionValues.containsKey(words.get(words.size() - 2))) {
new StringsCompleter(optionValues.get(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("-")) {
args++;
if (i > 0 && (!words.get(i - 1).matches("-[a-zA-Z]{1}") || !optionValues.containsKey(words.get(i - 1)))) {
args++;
}
}
}
if (args == -1) { // alternatively could set argumentCompleter strict = false;
if (args == -1) {
candidates.add(new Candidate(buffer, buffer, null, null, null, null, true));
} else if (args < argsCompleters.size()) {
argsCompleters.get(args).complete(reader, commandLine, candidates);
} else {
argsCompleters.get(argsCompleters.size() - 1).complete(reader, commandLine, candidates);
}
} else {
argsCompleters.get(0).complete(reader, commandLine, candidates);
}
}
}
Expand Down

0 comments on commit 064aa77

Please sign in to comment.