Skip to content

Commit

Permalink
Fix flags not tab-completing unless string is empty (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 authored Sep 9, 2023
1 parent 7efbec3 commit 07f7174
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -631,10 +630,6 @@ private CommandTree(final @NonNull CommandManager<C> 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++) {
Expand All @@ -654,7 +649,7 @@ private CommandTree(final @NonNull CommandManager<C> commandManager) {
} else if (child.getValue() instanceof CompoundArgument) {
return this.directSuggestions(commandContext, child, ((LinkedList<String>) commandQueue).getLast());
}
} else if (commandQueue.size() == 1 && commandQueue.peek().isEmpty()) {
} else if (commandQueue.size() == 1) {
return this.directSuggestions(commandContext, child, commandQueue.peek());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -635,6 +638,41 @@ void testFlagYieldingStringArrayWithLiberalFlagArgument() {
assertThat(suggestions6).isEmpty();
}

@Test
void testTextFlagCompletion() {
// Arrange
final CommandManager<TestCommandSender> 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<String> suggestions1 = suggest(manager, "command ");
final List<String> suggestions2 = suggest(manager, "command --");
final List<String> suggestions3 = suggest(manager, "command --f");
final List<String> suggestions4 = suggest(manager, "command --fla");
final List<String> suggestions5 = suggest(manager, "command -f");
final List<String> suggestions6 = suggest(manager, "command -");

final List<String> suggestions7 = suggest(manager, "command -f ");
final List<String> 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<String> suggest(CommandManager<TestCommandSender> manager, String command) {
return manager.suggest(new TestCommandSender(), command);
}
Expand Down

0 comments on commit 07f7174

Please sign in to comment.