diff --git a/CHANGELOG.md b/CHANGELOG.md index 9556d057..3be4ac56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed quoted parsing in StringArgument + - Fixed wrong suggestions following invalid literals ## [1.0.1] - 2020-10-14 diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java index 7e8bb5fb..6985f116 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java @@ -541,14 +541,23 @@ public final class CommandTree { } } } + if (commandQueue.size() > 1) { + /* + * In this case we were unable to match any of the literals, and so we cannot + * possibly attempt to match any of its children (which is what we want, according + * to the input queue). Because of this, we terminate immediately + */ + return Collections.emptyList(); + } } final List suggestions = new LinkedList<>(); for (final Node> argument : root.getChildren()) { if (argument.getValue() == null || this.isPermitted(commandContext.getSender(), argument) != null) { continue; } - suggestions.addAll(argument.getValue().getSuggestionsProvider() - .apply(commandContext, stringOrEmpty(commandQueue.peek()))); + final List suggestionsToAdd = argument.getValue().getSuggestionsProvider() + .apply(commandContext, stringOrEmpty(commandQueue.peek())); + suggestions.addAll(suggestionsToAdd); } return suggestions; } diff --git a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java index c667b554..4aaf0e50 100644 --- a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java +++ b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java @@ -191,6 +191,19 @@ public class CommandSuggestionsTest { Assertions.assertEquals(Arrays.asList("5", "6", "7", "8", "9"), suggestions5); } + @Test + void testInvalidLiteralThenSpace() { + final String input = "test o"; + final List suggestions = manager.suggest(new TestCommandSender(), input); + Assertions.assertEquals(Collections.singletonList("one"), suggestions); + final String input2 = "test o "; + final List suggestions2 = manager.suggest(new TestCommandSender(), input2); + Assertions.assertEquals(Collections.emptyList(), suggestions2); + final String input3 = "test o abc123xyz"; + final List suggestions3 = manager.suggest(new TestCommandSender(), input3); + Assertions.assertEquals(Collections.emptyList(), suggestions3); + } + public enum TestEnum { FOO,