Skip to content

Commit

Permalink
IGNITE-21674 Fix subcommand completion in CLI
Browse files Browse the repository at this point in the history
Add a workaround for jline/jline3#956
  • Loading branch information
valepakh committed Mar 21, 2024
1 parent a643a9f commit 217fde1
Showing 1 changed file with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.ignite.internal.cli.core.repl.completer.DynamicCompleter;
import org.apache.ignite.internal.cli.core.repl.completer.DynamicCompleterRegistry;
import org.apache.ignite.internal.cli.core.repl.completer.filter.CompleterFilter;
import org.jline.builtins.Options.HelpException;
import org.jline.console.ArgDesc;
import org.jline.console.CmdDesc;
import org.jline.console.CommandRegistry;
import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.completer.ArgumentCompleter;
Expand Down Expand Up @@ -179,7 +178,7 @@ public Map<String, String> commandAliases() {
return aliasCommand;
}

private class IgnitePicocliCompleter extends ArgumentCompleter implements Completer {
private class IgnitePicocliCompleter extends ArgumentCompleter {

public IgnitePicocliCompleter() {
super(NullCompleter.INSTANCE);
Expand All @@ -199,35 +198,31 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>
0,
commandLine.cursor(),
cs);
String[] staticCandidates = cs.stream()
.map(CharSequence::toString)
.toArray(String[]::new);

List<DynamicCompleter> completers = completerRegistry.findCompleters(words);
if (!completers.isEmpty()) {
try {
completers.stream()
.map(c -> c.complete(words))
.flatMap(List::stream)
.map(this::dynamicCandidate)
.forEach(candidates::add);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

if (!candidates.isEmpty()) {
return;
try {
completerRegistry.findCompleters(words).stream()
.map(c -> c.complete(words))
.flatMap(List::stream)
.map(this::dynamicCandidate)
.forEach(candidates::add);
} catch (Exception e) {
throw new RuntimeException(e);
}

String[] filteredCandidates = staticCandidates;
for (CompleterFilter filter : completerFilters) {
filteredCandidates = filter.filter(words, filteredCandidates);
}
if (candidates.isEmpty()) {
String[] filteredCandidates = cs.stream()
.map(CharSequence::toString)
.toArray(String[]::new);
for (CompleterFilter filter : completerFilters) {
filteredCandidates = filter.filter(words, filteredCandidates);
}

for (String c : filteredCandidates) {
candidates.add(staticCandidate(c));
for (String c : filteredCandidates) {
candidates.add(staticCandidate(c));
}
}

sortCandidates(candidates);
}

private Candidate dynamicCandidate(String one) {
Expand All @@ -241,5 +236,36 @@ private Candidate dynamicCandidate(String one) {
private Candidate staticCandidate(String one) {
return new Candidate(one, one, null, null, null, null, true, 10);
}

/**
* When custom sort order is used, sort candidates list and reassign sort order according to candidates order.
* TODO https://issues.apache.org/jira/browse/IGNITE-21824
*
* @param candidates List of candidates.
*/
private void sortCandidates(List<Candidate> candidates) {
boolean customOrder = candidates.stream().anyMatch(c -> c.sort() != 0);
if (!customOrder) {
return;
}
Collections.sort(candidates);
List<Candidate> newCandidates = new ArrayList<>(candidates.size());
for (int i = 0; i < candidates.size(); i++) {
Candidate candidate = candidates.get(i);
Candidate newCandidate = new Candidate(
candidate.value(),
candidate.displ(),
candidate.group(),
candidate.descr(),
candidate.suffix(),
candidate.key(),
candidate.complete(),
i // override sort order
);
newCandidates.add(newCandidate);
}
candidates.clear();
candidates.addAll(newCandidates);
}
}
}

0 comments on commit 217fde1

Please sign in to comment.