Skip to content

Commit

Permalink
Let CommandRegistry create Candidate for completion (fixes jline#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 10, 2024
1 parent 1ab0792 commit b7467cd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
15 changes: 14 additions & 1 deletion console/src/main/java/org/jline/console/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.PrintStream;
import java.util.*;

import org.jline.reader.Candidate;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;

Expand Down Expand Up @@ -42,10 +43,22 @@ static SystemCompleter aggregateCompleters(CommandRegistry... commandRegistries)
*/
static SystemCompleter compileCompleters(CommandRegistry... commandRegistries) {
SystemCompleter out = aggregateCompleters(commandRegistries);
out.compile();
out.compile(s -> createCandidate(commandRegistries, s));
return out;
}

static Candidate createCandidate(CommandRegistry[] commandRegistries, String command) {
String group = null, desc = null;
for (CommandRegistry registry : commandRegistries) {
if (registry.hasCommand(command)) {
group = registry.name();
desc = registry.commandInfo(command).stream().findFirst().orElse(null);
break;
}
}
return new Candidate(command, command, group, desc, null, null, true);
}

/**
* Returns the name of this registry.
* @return the name of the registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private SystemCompleter _compileCompleters() {
}
local.add(customSystemCompleter);
out.add(local);
out.compile();
out.compile(s -> CommandRegistry.createCandidate(commandRegistries, s));
return out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
package org.jline.reader.impl.completer;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
Expand All @@ -24,6 +26,7 @@
public class SystemCompleter implements Completer {
private Map<String, List<Completer>> completers = new HashMap<>();
private Map<String, String> aliasCommand = new HashMap<>();
private Map<String, String> descriptions = new HashMap<>();
private StringsCompleter commands;
private boolean compiled = false;

Expand Down Expand Up @@ -123,7 +126,7 @@ private Map<String, String> getAliases() {
return aliasCommand;
}

public void compile() {
public void compile(Function<String, Candidate> candidateBuilder) {
if (compiled) {
return;
}
Expand All @@ -139,7 +142,7 @@ public void compile() {
completers = compiledCompleters;
Set<String> cmds = new HashSet<>(completers.keySet());
cmds.addAll(aliasCommand.keySet());
commands = new StringsCompleter(cmds);
commands = new StringsCompleter(cmds.stream().map(candidateBuilder).collect(Collectors.toList()));
compiled = true;
}

Expand Down

0 comments on commit b7467cd

Please sign in to comment.