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

fix invalid suggestions when parsing of argument fails #401

Merged
merged 3 commits into from
Nov 28, 2022
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
24 changes: 22 additions & 2 deletions cloud-core/src/main/java/cloud/commandframework/CommandTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,22 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
// START: Parsing
commandContext.setCurrentArgument(child.getValue());
final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
if (result.getParsedValue().isPresent() && !commandQueue.isEmpty()) {
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
final Optional<?> parsedValue = result.getParsedValue();
final boolean parseSuccess = parsedValue.isPresent();

if (parseSuccess && !commandQueue.isEmpty()) {
// the current argument at the position is parsable and there are more arguments following
commandContext.store(child.getValue().getName(), parsedValue.get());
return this.getSuggestions(commandContext, commandQueue, child);
} else if (!parseSuccess && commandQueueOriginal.size() > 1) {
// at this point there should normally be no need to reset the command queue as we expect
// users to only take out an argument if the parse succeeded. Just to be sure we reset anyway
commandQueue.clear();
commandQueue.addAll(commandQueueOriginal);

// there are more arguments following but the current argument isn't matching - there
// is no need to collect any further suggestions
return Collections.emptyList();
}
// END: Parsing
}
Expand All @@ -669,6 +682,13 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
commandQueue.clear();
commandQueue.addAll(commandQueueOriginal);

if (!preParseSuccess && commandQueue.size() > 1) {
// The preprocessor denied the argument, and there are more arguments following the current one
// Therefore we shouldn't list the suggestions of the current argument, as clearly the suggestions of
// one of the following arguments is requested
return Collections.emptyList();
}

// Fallback: use suggestion provider of argument
commandContext.setCurrentArgument(child.getValue());
return child.getValue().getSuggestionsProvider().apply(commandContext, this.stringOrEmpty(commandQueue.peek()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package cloud.commandframework;

import cloud.commandframework.arguments.compound.ArgumentTriplet;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.standard.BooleanArgument;
import cloud.commandframework.arguments.standard.EnumArgument;
import cloud.commandframework.arguments.standard.IntegerArgument;
Expand Down Expand Up @@ -128,6 +129,18 @@ static void setupManager() {
manager.command(manager.commandBuilder("literal_with_variable")
.literal("vici")
.literal("later"));

manager.command(manager.commandBuilder("cmd_with_multiple_args")
.argument(IntegerArgument.<TestCommandSender>of("number").addPreprocessor((ctx, input) -> {
String argument = input.peek();
if (argument == null || !argument.equals("1024")) {
return ArgumentParseResult.success(true);
} else {
return ArgumentParseResult.failure(new NullPointerException());
}
}))
.argument(EnumArgument.of(TestEnum.class, "enum"))
.literal("world"));
}

@Test
Expand Down Expand Up @@ -378,6 +391,7 @@ void testStringArgumentWithSuggestionProvider() {
Assertions.assertEquals(Collections.singletonList("literal"), suggestions9);
}

@Test
void testLiteralWithVariable() {
final String input = "literal_with_variable ";
final List<String> suggestions = manager.suggest(new TestCommandSender(), input);
Expand All @@ -390,7 +404,7 @@ void testLiteralWithVariable() {
Assertions.assertEquals(Arrays.asList("vici", "vidi"), suggestions3);
final String input4 = "literal_with_variable vidi";
final List<String> suggestions4 = manager.suggest(new TestCommandSender(), input4);
Assertions.assertEquals(Collections.emptyList(), suggestions4);
Assertions.assertEquals(Collections.singletonList("vidi"), suggestions4);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was disabled & wrong previously. Without my change the test wouldn't pass as well as "vidi" would be given as a suggestion.

final String input5 = "literal_with_variable vidi ";
final List<String> suggestions5 = manager.suggest(new TestCommandSender(), input5);
Assertions.assertEquals(Collections.singletonList("now"), suggestions5);
Expand All @@ -399,6 +413,39 @@ void testLiteralWithVariable() {
Assertions.assertEquals(Collections.singletonList("later"), suggestions6);
}

@Test
void testInvalidArgumentShouldNotCauseFurtherCompletion() {
// pass preprocess
final String input = "cmd_with_multiple_args 512 ";
final List<String> suggestions = manager.suggest(new TestCommandSender(), input);
Assertions.assertEquals(Arrays.asList("foo", "bar"), suggestions);
final String input2 = "cmd_with_multiple_args 512 BAR ";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Collections.singletonList("world"), suggestions2);
final String input3 = "cmd_with_multiple_args test ";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
Assertions.assertEquals(Collections.emptyList(), suggestions3);
final String input4 = "cmd_with_multiple_args 512 f";
final List<String> suggestions4 = manager.suggest(new TestCommandSender(), input4);
Assertions.assertEquals(Collections.singletonList("foo"), suggestions4);
final String input5 = "cmd_with_multiple_args world f";
final List<String> suggestions5 = manager.suggest(new TestCommandSender(), input5);
Assertions.assertEquals(Collections.emptyList(), suggestions5);
// trigger preprocess fail
final String input6 = "cmd_with_multiple_args 1024";
final List<String> suggestions6 = manager.suggest(new TestCommandSender(), input6);
Assertions.assertEquals(11, suggestions6.size());
final String input7 = "cmd_with_multiple_args 1024 ";
final List<String> suggestions7 = manager.suggest(new TestCommandSender(), input7);
Assertions.assertEquals(Collections.emptyList(), suggestions7);
final String input8 = "cmd_with_multiple_args 1024 f";
final List<String> suggestions8 = manager.suggest(new TestCommandSender(), input8);
Assertions.assertEquals(Collections.emptyList(), suggestions8);
final String input9 = "cmd_with_multiple_args 1024 foo w";
final List<String> suggestions9 = manager.suggest(new TestCommandSender(), input9);
Assertions.assertEquals(Collections.emptyList(), suggestions9);
}

@Test
void testFlagYieldingGreedyStringFollowedByFlagArgument() {
// Arrange
Expand Down