🐛 Fix wrong suggestions following an invalid literal (#72)

*  Add a test for suggestions after an invalid literal

* 🐛 Fix wrong suggestions following an invalid literal

* 📚 Document literal suggestion fix in CHANGELOG

Co-authored-by: Alexander Söderberg <sauilitired@gmail.com>
This commit is contained in:
Jason 2020-10-18 12:34:27 -07:00 committed by Alexander Söderberg
parent 27d228cb4c
commit c051ff20df
3 changed files with 25 additions and 2 deletions

View file

@ -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

View file

@ -541,14 +541,23 @@ public final class CommandTree<C> {
}
}
}
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<String> suggestions = new LinkedList<>();
for (final Node<CommandArgument<C, ?>> 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<String> suggestionsToAdd = argument.getValue().getSuggestionsProvider()
.apply(commandContext, stringOrEmpty(commandQueue.peek()));
suggestions.addAll(suggestionsToAdd);
}
return suggestions;
}

View file

@ -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<String> suggestions = manager.suggest(new TestCommandSender(), input);
Assertions.assertEquals(Collections.singletonList("one"), suggestions);
final String input2 = "test o ";
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
Assertions.assertEquals(Collections.emptyList(), suggestions2);
final String input3 = "test o abc123xyz";
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
Assertions.assertEquals(Collections.emptyList(), suggestions3);
}
public enum TestEnum {
FOO,