Skip to content

Commit

Permalink
Added OptionCompleter class
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 16, 2019
1 parent 06eb15f commit 9370290
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
28 changes: 24 additions & 4 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.jline.builtins.Commands;
import org.jline.builtins.Completers.FilesCompleter;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Completers.SystemCompleter;
import org.jline.builtins.TTop;
import org.jline.builtins.Options.HelpException;
Expand Down Expand Up @@ -327,26 +328,45 @@ private Set<String> allWidgets() {

private List<Completer> nanoCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(new StringsCompleter(name), new FilesCompleter(workDir.get(), true)));
completers.add(new ArgumentCompleter(new StringsCompleter(name)
, new OptionCompleter(new FilesCompleter(workDir.get(), true)
, new HashMap<String,List<String>>()
, new ArrayList<String>()
, 1)
));
return completers;
}

private List<Completer> lessCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(new StringsCompleter(name), new FilesCompleter(workDir.get(), true)));
completers.add(new ArgumentCompleter(new StringsCompleter(name)
, new OptionCompleter(new FilesCompleter(workDir.get(), true)
, new HashMap<String,List<String>>()
, new ArrayList<String>()
, 1)
));
return completers;
}

private List<Completer> historyCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(new StringsCompleter(name), new NullCompleter()));
completers.add(new ArgumentCompleter(new StringsCompleter(name)
, new StringsCompleter(Arrays.asList("-A", "-W", "-R")), new FilesCompleter(workDir.get(), true), new NullCompleter()));
, new OptionCompleter(new NullCompleter()
, new HashMap<String,List<String>>()
, new ArrayList<String>()
, 1)
));
completers.add(new ArgumentCompleter(new StringsCompleter(name)
, new StringsCompleter(Arrays.asList("-A", "-W", "-R", "-AI", "-RI", "-WI")), new FilesCompleter(workDir.get(), true), new NullCompleter()));
return completers;
}

private List<Completer> widgetCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(new StringsCompleter(name)
, new StringsCompleter("-l", "-la", "-N", "-U")
, new NullCompleter()
));
completers.add(new ArgumentCompleter(new StringsCompleter(name)
, new StringsCompleter("-A"), new StringsCompleter(() -> allWidgets())
, new StringsCompleter(() -> reader.getWidgets().keySet()), new NullCompleter()));
Expand Down
97 changes: 97 additions & 0 deletions builtins/src/main/java/org/jline/builtins/Completers.java
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,101 @@ public Map<String,List<org.jline.reader.Completer>> getCompleters() {
}
}

public static class OptionCompleter implements org.jline.reader.Completer {
private Map<String,List<String>> optionValues = new HashMap<>();
private List<String> options = new ArrayList<String>();

private List<org.jline.reader.Completer> argsCompleters = new ArrayList<>();
private int startPos;

public OptionCompleter(List<org.jline.reader.Completer> completers, Map<String,List<String>> optionValues, Collection<String> options, int startPos){
this(optionValues, options, startPos);
this.argsCompleters = new ArrayList<>(completers);
}

public OptionCompleter(org.jline.reader.Completer completer, Map<String,List<String>> optionValues, Collection<String> options, int startPos){
this(optionValues, options, startPos);
this.argsCompleters.add(completer);
}

private OptionCompleter(Map<String,List<String>> optionValues, Collection<String> options, int startPos) {
this.optionValues = new HashMap<>(optionValues);
this.options.addAll(options);
this.startPos = startPos;
}

@Override
public void complete(LineReader reader, final ParsedLine commandLine, List<Candidate> candidates) {
assert commandLine != null;
assert candidates != null;
List<String> words = commandLine.words();
String buffer = commandLine.word().substring(0, commandLine.wordCursor());
if (buffer.startsWith("-")) {
boolean addbuff = true;
boolean valueCandidates = false;
int eq = buffer.indexOf('=');
if (eq < 0) {
List<String> usedOptions = new ArrayList<>();
for (int i = startPos; i < words.size(); i++) {
if (words.get(i).startsWith("-")) {
String w = words.get(i);
int ind = w.indexOf('=');
if (ind < 0) {
usedOptions.add(w);
} else {
usedOptions.add(w.substring(0,ind));
}
}
}
for (String o: optionValues.keySet()) {
if (usedOptions.contains(o)) {
continue;
}
if (o.startsWith(buffer)) {
addbuff = false;
}
candidates.add(new Candidate(o+"=", o, null, null, null, null, false));
}
for (String o: options) {
if (usedOptions.contains(o)) {
continue;
}
if (o.startsWith(buffer)) {
addbuff = false;
}
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);
String opt = buffer.substring(0, eq);
if (optionValues.containsKey(opt) && !optionValues.get(opt).isEmpty()) {
for (String v: optionValues.get(opt)) {
if (v.startsWith(value)) {
valueCandidates = true;
addbuff = false;
}
candidates.add(new Candidate(curBuf+v, v, null, null, null, null, true));
}
}
}
if ((buffer.contains("=") && !buffer.endsWith("=") && !valueCandidates) || addbuff) {
candidates.add(new Candidate(buffer, buffer, null, null, null, null, true));
}
} else if (argsCompleters.size() > 1){
int args = 0;
for (int i = startPos; i < words.size(); i++) {
if (!words.get(i).startsWith("-")) {
args++;
}
}
if (args < argsCompleters.size()) {
argsCompleters.get(args).complete(reader, commandLine, candidates);
}
} else {
argsCompleters.get(0).complete(reader, commandLine, candidates);
}
}
}

}

1 comment on commit 9370290

@mattirn
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.