From 07f7174cab77e09e6e1d95c7baf712a828c9dc00 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Sat, 9 Sep 2023 08:42:27 +0200 Subject: [PATCH] Fix flags not tab-completing unless string is empty (#460) --- .../cloud/commandframework/CommandTree.java | 7 +--- .../CommandSuggestionsTest.java | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java index 169d8609e..8caa710b4 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java @@ -42,7 +42,6 @@ import cloud.commandframework.permission.CommandPermission; import cloud.commandframework.permission.OrPermission; import cloud.commandframework.types.tuples.Pair; -import io.leangen.geantyref.GenericTypeReflector; import io.leangen.geantyref.TypeToken; import java.util.ArrayList; import java.util.Arrays; @@ -631,10 +630,6 @@ private CommandTree(final @NonNull CommandManager commandManager) { if (!lastFlag.isPresent()) { commandContext.remove(FlagArgument.FLAG_META_KEY); } - } else if (GenericTypeReflector.erase(child.getValue().getValueType().getType()).isArray()) { - while (commandQueue.size() > 1) { - commandQueue.remove(); - } } else if (commandQueue.size() <= child.getValue().getParser().getRequestedArgumentCount()) { for (int i = 0; i < child.getValue().getParser().getRequestedArgumentCount() - 1 && commandQueue.size() > 1; i++) { @@ -654,7 +649,7 @@ private CommandTree(final @NonNull CommandManager commandManager) { } else if (child.getValue() instanceof CompoundArgument) { return this.directSuggestions(commandContext, child, ((LinkedList) commandQueue).getLast()); } - } else if (commandQueue.size() == 1 && commandQueue.peek().isEmpty()) { + } else if (commandQueue.size() == 1) { return this.directSuggestions(commandContext, child, commandQueue.peek()); } diff --git a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java index dd51bd30d..8a9880668 100644 --- a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java +++ b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java @@ -23,6 +23,7 @@ // package cloud.commandframework; +import cloud.commandframework.arguments.CommandArgument; import cloud.commandframework.arguments.compound.ArgumentTriplet; import cloud.commandframework.arguments.parser.ArgumentParseResult; import cloud.commandframework.arguments.standard.BooleanArgument; @@ -37,8 +38,10 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.junit.Ignore; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static cloud.commandframework.util.TestUtils.createManager; @@ -635,6 +638,41 @@ void testFlagYieldingStringArrayWithLiberalFlagArgument() { assertThat(suggestions6).isEmpty(); } + @Test + void testTextFlagCompletion() { + // Arrange + final CommandManager manager = createManager(); + manager.setSetting(CommandManager.ManagerSettings.LIBERAL_FLAG_PARSING, true); + manager.command( + manager.commandBuilder("command") + .flag(manager.flagBuilder("flag").withAliases("f") + .withArgument(EnumArgument.of(TestEnum.class, "test")).build()) + .flag(manager.flagBuilder("flog").build()) + ); + + // Act + final List suggestions1 = suggest(manager, "command "); + final List suggestions2 = suggest(manager, "command --"); + final List suggestions3 = suggest(manager, "command --f"); + final List suggestions4 = suggest(manager, "command --fla"); + final List suggestions5 = suggest(manager, "command -f"); + final List suggestions6 = suggest(manager, "command -"); + + final List suggestions7 = suggest(manager, "command -f "); + final List suggestions8 = suggest(manager, "command -f b"); + + // Assert + assertThat(suggestions1).containsExactly("--flag", "--flog", "-f"); + assertThat(suggestions2).containsExactly("--flag", "--flog"); + assertThat(suggestions3).containsExactly("--flag", "--flog"); + assertThat(suggestions4).containsExactly("--flag"); + assertThat(suggestions5).containsExactly("-f"); + assertThat(suggestions6).containsExactly("--flag", "--flog", "-f"); + assertThat(suggestions7).containsExactly("foo", "bar"); + assertThat(suggestions8).containsExactly("bar"); + } + + private List suggest(CommandManager manager, String command) { return manager.suggest(new TestCommandSender(), command); }