Skip to content

Commit

Permalink
Improved OptionCompleter long options value completion
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jun 6, 2020
1 parent de55886 commit c6a09bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 13 additions & 7 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,22 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.LineReader.Option;
import org.jline.reader.impl.completer.AggregateCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.ParsedLine;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.jline.utils.OSUtils;
import org.jline.utils.StyleResolver;

Expand Down Expand Up @@ -871,11 +865,14 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
}
} 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 (words.size() > 1 && longOptionValueCompleter(command, words.get(words.size() - 2)) != null) {
longOptionValueCompleter(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 && shortOptionValueCompleter(command, words.get(i - 1)) == null) {
if (i > 0 && shortOptionValueCompleter(command, words.get(i - 1)) == null
&& longOptionValueCompleter(command, words.get(i - 1)) == null) {
args++;
}
}
Expand All @@ -890,6 +887,15 @@ public void complete(LineReader reader, final ParsedLine commandLine, List<Candi
}
}

private org.jline.reader.Completer longOptionValueCompleter(String command, String opt) {
if (!opt.matches("--[a-zA-Z]+")) {
return null;
}
Collection<OptDesc> optDescs = commandOptions == null ? options : commandOptions.apply(command);
OptDesc option = findOptDesc(optDescs, opt);
return option.hasValue() ? option.valueCompleter() : null;
}

private org.jline.reader.Completer shortOptionValueCompleter(String command, String opt) {
if (!opt.matches("-[a-zA-Z]+")) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
*/
package org.jline.builtins;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.OptDesc;
import org.jline.reader.LineReader;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.ArgumentCompleter;
Expand All @@ -42,6 +39,11 @@ public void testOptions() throws Exception {
assertBuffer("command ", new TestBuffer("c").tab());
assertBuffer("command -s", new TestBuffer("command -").tab());
assertBuffer("command -s val ", new TestBuffer("command -s v").tab());
assertBuffer("command -sval ", new TestBuffer("command -sv").tab());
assertBuffer("command --sopt val ", new TestBuffer("command --sopt v").tab());
assertBuffer("command --sopt=", new TestBuffer("command --sop").tab());
assertBuffer("command --sopt=val ", new TestBuffer("command --sopt=v").tab());
assertBuffer("command -sval ", new TestBuffer("command -sv").tab());
assertBuffer("command -s val bar ", new TestBuffer("command -s val b").tab());
assertBuffer("command -s val bar --option ", new TestBuffer("command -s val bar --o").tab());
assertBuffer("command -s val bar --option foo ", new TestBuffer("command -s val bar --option f").tab());
Expand Down

0 comments on commit c6a09bd

Please sign in to comment.