Skip to content

Commit

Permalink
Allow greedy parsers to suggest after a space (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 authored and jpenilla committed Dec 13, 2022
1 parent 99d388b commit bde084c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
15 changes: 8 additions & 7 deletions cloud-core/src/main/java/cloud/commandframework/CommandTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,16 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {

if (commandQueue.isEmpty()) {
return Collections.emptyList();
} else if (child.isLeaf() && commandQueue.size() < 2) {
return this.directSuggestions(commandContext, child, commandQueue.peek());
} else if (child.isLeaf()) {
if (child.getValue() instanceof CompoundArgument) {
final String last = ((LinkedList<String>) commandQueue).getLast();
commandContext.setCurrentArgument(child.getValue());
return child.getValue().getSuggestionsProvider().apply(commandContext, last);
final String input;
if (commandQueue.size() == 1) {
input = commandQueue.peek();
} else {
input = child.getValue() instanceof CompoundArgument
? ((LinkedList<String>) commandQueue).getLast()
: String.join(" ", commandQueue);
}
return Collections.emptyList();
return this.directSuggestions(commandContext, child, input);
} else if (commandQueue.peek().isEmpty()) {
return this.directSuggestions(commandContext, child, commandQueue.peek());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.arguments.standard.StringArrayArgument;
import cloud.commandframework.execution.FilteringCommandSuggestionProcessor;
import cloud.commandframework.types.tuples.Pair;
import cloud.commandframework.types.tuples.Triplet;
import java.util.Arrays;
Expand Down Expand Up @@ -511,6 +512,38 @@ void testFlagYieldingStringArrayFollowedByFlagArgument() {
assertThat(suggestions6).isEmpty();
}

@Test
void testGreedyArgumentSuggestsAfterSpace() {
// Arrange
final CommandManager<TestCommandSender> manager = createManager();
manager.command(
manager.commandBuilder("command")
.argument(
StringArgument.<TestCommandSender>newBuilder("string")
.greedy()
.withSuggestionsProvider((context, input) -> Collections.singletonList("hello world"))
.build())
);
manager.commandSuggestionProcessor(
new FilteringCommandSuggestionProcessor<>(
FilteringCommandSuggestionProcessor.Filter.<TestCommandSender>startsWith(true).andTrimBeforeLastSpace()));

// Act
final List<String> suggestions1 = suggest(manager, "command ");
final List<String> suggestions2 = suggest(manager, "command hello");
final List<String> suggestions3 = suggest(manager, "command hello ");
final List<String> suggestions4 = suggest(manager, "command hello wo");
final List<String> suggestions5 = suggest(manager, "command hello world");
final List<String> suggestions6 = suggest(manager, "command hello world ");

// Assert
assertThat(suggestions1).containsExactly("hello world");
assertThat(suggestions2).containsExactly("hello world");
assertThat(suggestions3).containsExactly("world");
assertThat(suggestions4).containsExactly("world");
assertThat(suggestions5).containsExactly("world");
assertThat(suggestions6).isEmpty();
}

@Test
void testFlagYieldingGreedyStringWithLiberalFlagArgument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import cloud.commandframework.ArgumentDescription;
import cloud.commandframework.Command;
import cloud.commandframework.CommandHelpHandler;
import cloud.commandframework.CommandTree;
import cloud.commandframework.annotations.AnnotationParser;
import cloud.commandframework.annotations.Argument;
Expand Down Expand Up @@ -56,6 +57,7 @@
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.execution.FilteringCommandSuggestionProcessor;
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
import cloud.commandframework.keys.SimpleCloudKey;
import cloud.commandframework.meta.CommandMeta;
Expand All @@ -79,6 +81,7 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.format.NamedTextColor;
Expand Down Expand Up @@ -149,6 +152,12 @@ public void onEnable() {
this.getServer().getPluginManager().disablePlugin(this);
return;
}

// Use contains to filter suggestions instead of default startsWith
this.manager.commandSuggestionProcessor(new FilteringCommandSuggestionProcessor<>(
FilteringCommandSuggestionProcessor.Filter.<CommandSender>contains(true).andTrimBeforeLastSpace()
));

//
// Create a BukkitAudiences instance (adventure) in order to use the minecraft-extras
// help system
Expand Down Expand Up @@ -483,11 +492,21 @@ private void registerNamespacedKeyUsingCommand() {
}));
}

@Suggestions("help_queries")
public @NonNull List<String> suggestHelpQueries(
final @NonNull CommandContext<CommandSender> ctx,
final @NonNull String input
) {
return this.manager.createCommandHelpHandler().queryRootIndex(ctx.getSender()).getEntries().stream()
.map(CommandHelpHandler.VerboseHelpEntry::getSyntaxString)
.collect(Collectors.toList());
}

@CommandMethod("example|e|ex help [query]")
@CommandDescription("Help menu")
public void commandHelp(
final @NonNull CommandSender sender,
final @Argument("query") @Greedy String query
final @Argument(value = "query", suggestions = "help_queries") @Greedy String query
) {
this.minecraftHelp.queryCommands(query == null ? "" : query, sender);
}
Expand Down

0 comments on commit bde084c

Please sign in to comment.