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

Allow greedy parsers to suggest after a space #414

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 4 additions & 8 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,11 @@ 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);
}
return Collections.emptyList();
String input = commandQueue.size() == 1 ? commandQueue.peek()
jpenilla marked this conversation as resolved.
Show resolved Hide resolved
: child.getValue() instanceof CompoundArgument ? ((LinkedList<String>) commandQueue).getLast()
: String.join(" ", commandQueue);
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