Skip to content

Commit

Permalink
Let CommandRegistry create Candidate for completion (fixes #1119) (#1120
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gnodet authored Dec 10, 2024
1 parent f88c742 commit ef44e24
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

2 comments on commit ef44e24

@iflan
Copy link

@iflan iflan commented on ef44e24 Dec 18, 2024

Choose a reason for hiding this comment

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

I think that this change is OK, but it creates a difficult upgrade path for people that aren't using a CommandRegistry. Would it be possible to create an overload for compile with no arguments that does the same thing it used to?

@gnodet
Copy link
Member Author

@gnodet gnodet commented on ef44e24 Dec 20, 2024

Choose a reason for hiding this comment

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

Can you provide a PR for that please ?

Please sign in to comment.