Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let CommandRegistry create Candidate for completion (fixes #1119) #1120

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading