Skip to content

Commit

Permalink
Fix tab completions not working for first arguments with empty buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Oct 8, 2021
1 parent 2c18229 commit d922d38
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public BukkitCommandExecutor(BukkitHandler handler) {
@NotNull String alias,
@NotNull String[] args) {
BukkitCommandActor actor = new BukkitActor(sender);
ArgumentStack arguments = ArgumentStack.of(args);
ArgumentStack arguments = ArgumentStack.ofUnsafe(args);

arguments.addFirst(command.getName());
return handler.getAutoCompleter().complete(actor, arguments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BungeeCommand(String name, BungeeHandler handler) {
}

@Override public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
ArgumentStack arguments = ArgumentStack.of(args);
ArgumentStack arguments = ArgumentStack.ofUnsafe(args);
arguments.addFirst(getName());

BungeeCommandActor actor = new BungeeActor(sender);
Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/revxrsal/commands/command/ArgumentStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import org.jetbrains.annotations.UnmodifiableView;
import revxrsal.commands.autocomplete.AutoCompleter;
import revxrsal.commands.core.LinkedArgumentStack;
import revxrsal.commands.util.tokenize.QuotedStringTokenizer;

Expand Down Expand Up @@ -103,6 +104,20 @@ public interface ArgumentStack extends Deque<String>, List<String>, Cloneable {
return new LinkedArgumentStack(QuotedStringTokenizer.tokenize(String.join(" ", arguments)));
}


/**
* Returns a new {@link ArgumentStack} with the specified arguments. This
* will not remove trailing space, and hence may give incorrect
* measures. This should only be used with {@link AutoCompleter#complete(CommandActor, ArgumentStack)}.
*
* @param arguments Arguments to clone from
* @return The newly created argument stack.
*/
static @NotNull ArgumentStack ofUnsafe(@NotNull String... arguments) {
if (arguments.length == 0) return ArgumentStack.empty();
return new LinkedArgumentStack(QuotedStringTokenizer.tokenizeUnsafe(String.join(" ", arguments)));
}

/**
* Returns a new {@link ArgumentStack} with the specified arguments,
* from splitting the string by whitespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
*/
public final class QuotedStringTokenizer {

public static final List<String> EMPTY_TEXT = Collections.singletonList("");

private QuotedStringTokenizer() {}

private static final int CHAR_BACKSLASH = '\\';
Expand Down Expand Up @@ -74,6 +76,20 @@ public static List<String> tokenize(String arguments) {
return returnedArgs;
}

/**
* Returns a list of tokens from parsing the given input,
* respecting quotes and breaks.
*
* @param arguments Argument string to parse
* @return A list of tokens.
*/
public static List<String> tokenizeUnsafe(String arguments) {
if (arguments.length() == 0) {
return EMPTY_TEXT;
}
return tokenize(arguments);
}

// Parsing methods

private static void skipWhiteSpace(TokenizerState state) throws ArgumentParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;
import java.util.Optional;

import static revxrsal.commands.util.tokenize.QuotedStringTokenizer.EMPTY_TEXT;

// i'm not sure if we are supposed to be providing implementations
// for testPermission(), getHelp() and getUsage().
final class SpongeCommandCallable implements CommandCallable {
Expand All @@ -37,7 +39,7 @@ public SpongeCommandCallable(SpongeCommandHandler handler, String name) {

@Override public @NotNull List<String> getSuggestions(@NotNull CommandSource source, @NotNull String arguments, @Nullable Location<World> targetPosition) {
CommandActor actor = new SpongeActor(source);
ArgumentStack args = ArgumentStack.fromString(arguments);
ArgumentStack args = arguments.isEmpty() ? ArgumentStack.exactly(EMPTY_TEXT) : ArgumentStack.fromString(arguments);
return handler.getAutoCompleter().complete(actor, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public VelocitySimpleCommand(VelocityHandler handler) {
}

@Override public List<String> suggest(Invocation invocation) {
ArgumentStack arguments = ArgumentStack.of(invocation.arguments());
ArgumentStack arguments = ArgumentStack.ofUnsafe(invocation.arguments());
arguments.addFirst(invocation.alias());
VelocityCommandActor actor = new VelocityActor(invocation.source(), handler.getServer());

Expand Down

0 comments on commit d922d38

Please sign in to comment.