mirror of
https://github.com/aikar/commands.git
synced 2026-05-31 06:11:55 +00:00
Add support for multi word tab completion (#200)
I added support for multi word tab completion.
Current problem:
the replacement "example" contains completions with whitespaces
```java
@CommandCompletion("@example")
public void test(Player player, String takesTheRest) {...}
```
"takesTheRest" takes all the whole rest howewer the tab completion only supports one word.
I don't use the ACFUtils much and maybe there are some ugly checks, feel free to refactor.
This commit is contained in:
@@ -685,7 +685,11 @@ public abstract class BaseCommand {
|
||||
* @return All results to complete the command.
|
||||
*/
|
||||
private List<String> completeCommand(CommandIssuer issuer, RegisteredCommand cmd, String[] args, String commandLabel, boolean isAsync) {
|
||||
if (!cmd.hasPermission(issuer) || args.length > cmd.consumeInputResolvers || args.length == 0) {
|
||||
if (!cmd.hasPermission(issuer) || args.length == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (!cmd.parameters[cmd.parameters.length - 1].consumesRest && args.length > cmd.consumeInputResolvers) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
package co.aikar.commands;
|
||||
|
||||
import co.aikar.commands.apachecommonslang.ApacheCommonsLangUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -191,6 +192,8 @@ public class CommandCompletions<C extends CommandCompletionContext> {
|
||||
String last = completions[completions.length - 1];
|
||||
if (last.startsWith("repeat@")) {
|
||||
completion = last;
|
||||
} else if (argIndex >= completions.length && cmd.parameters[cmd.parameters.length - 1].consumesRest) {
|
||||
completion = last;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,7 +233,8 @@ public class CommandCompletions<C extends CommandCompletionContext> {
|
||||
CommandOperationContext<?> ctx = CommandManager.getCurrentCommandOperationContext();
|
||||
return ctx.enumCompletionValues;
|
||||
}
|
||||
if (completion.startsWith("repeat@")) {
|
||||
boolean repeat = completion.startsWith("repeat@");
|
||||
if (repeat) {
|
||||
completion = completion.substring(6);
|
||||
}
|
||||
completion = manager.getCommandReplacements().replace(completion);
|
||||
@@ -252,6 +256,22 @@ public class CommandCompletions<C extends CommandCompletionContext> {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
Collection<String> completions = handler.getCompletions(context);
|
||||
|
||||
//Handle completions with more than one word:
|
||||
if (!repeat &&
|
||||
command.parameters[command.parameters.length - 1].consumesRest
|
||||
&& args.length > ACFPatterns.SPACE.split(command.complete).length) {
|
||||
String start = String.join(" ", args);
|
||||
completions = completions.stream()
|
||||
.filter(s -> s.split(" ").length >= args.length)
|
||||
.filter(s -> ApacheCommonsLangUtil.startsWithIgnoreCase(s, start))
|
||||
.map(s -> {
|
||||
String[] completionArgs = s.split(" ");
|
||||
return String.join(" ",
|
||||
Arrays.copyOfRange(completionArgs, args.length - 1, completionArgs.length));
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
if (completions != null) {
|
||||
allCompletions.addAll(completions);
|
||||
continue;
|
||||
|
||||
Reference in New Issue
Block a user