Add more suggestion tests

This commit is contained in:
Hugo Manrique
2021-06-09 22:22:15 +02:00
parent bcb68c8d0f
commit ba34e4729b
7 changed files with 407 additions and 69 deletions

View File

@@ -121,16 +121,15 @@ final class SuggestionsProvider<S> {
}
/**
* Returns whether a literal node with the given name should be considered for
* Returns whether a literal node with the given lowercase name should be considered for
* suggestions given the specified input.
*
* @param name the literal name
* @param name the lowercase literal name
* @param input the partial input
* @return true if the literal should be considered; false otherwise
*/
private static boolean shouldConsider(final String name, final String input) {
// TODO (perf) If we expect input to be lowercase, no need to ignore case
return name.regionMatches(true, 0, input, 0, input.length());
return name.regionMatches(false, 0, input, 0, input.length());
}
/**
@@ -143,7 +142,9 @@ final class SuggestionsProvider<S> {
private CompletableFuture<Suggestions> provideAliasSuggestions(
final StringReader reader, final CommandContextBuilder<S> contextSoFar) {
final S source = contextSoFar.getSource();
final String input = reader.getRead();
// Lowercase the alias here so all comparisons can be case-sensitive (cheaper)
// TODO Is this actually faster?
final String input = reader.getRead().toLowerCase(Locale.ENGLISH);
final Collection<CommandNode<S>> aliases = contextSoFar.getRootNode().getChildren();
@SuppressWarnings("unchecked")
@@ -188,7 +189,13 @@ final class SuggestionsProvider<S> {
// This is a BrigadierCommand, fallback to regular suggestions
reader.setCursor(0);
final ParseResults<S> parse = this.dispatcher.parse(reader, source);
return this.dispatcher.getCompletionSuggestions(parse);
try {
return this.dispatcher.getCompletionSuggestions(parse);
} catch (final Throwable e) {
// Ugly, ugly swallowing of everything Throwable, because plugins are naughty.
LOGGER.error("Command node cannot provide suggestions for " + fullInput, e);
return Suggestions.empty();
}
}
if (!argsNode.canUse(source)) {
@@ -243,7 +250,7 @@ final class SuggestionsProvider<S> {
try {
return node.listSuggestions(built, new SuggestionsBuilder(fullInput, start));
} catch (final Throwable e) {
// Ugly, ugly swallowing of everything Throwable, because plugins are naughty.
// Again, plugins are naughty
LOGGER.error("Arguments node cannot provide suggestions", e);
return Suggestions.empty();
}
@@ -266,7 +273,7 @@ final class SuggestionsProvider<S> {
try {
return this.dispatcher.getCompletionSuggestions(parse);
} catch (final Throwable e) {
// Again, plugins are naughty.
// Yet again, plugins are naughty.
LOGGER.error("Hint node cannot provide suggestions", e);
return Suggestions.empty();
}

View File

@@ -25,6 +25,7 @@ import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion;
import com.mojang.brigadier.tree.RootCommandNode;
import com.spotify.futures.CompletableFutures;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandManager;
@@ -150,8 +151,9 @@ public class VelocityCommandManager implements CommandManager {
Preconditions.checkNotNull(cmdLine, "cmdLine");
final String normalizedInput = VelocityCommands.normalizeInput(cmdLine, true);
final ParseResults<CommandSource> parse = this.parse(normalizedInput, source);
try {
// The parse can fail if the requirement predicates throw
final ParseResults<CommandSource> parse = this.parse(normalizedInput, source);
return dispatcher.execute(parse) != BrigadierCommand.FORWARD;
} catch (final CommandSyntaxException e) {
boolean isSyntaxError = !e.getType().equals(
@@ -207,8 +209,14 @@ public class VelocityCommandManager implements CommandManager {
Preconditions.checkNotNull(cmdLine, "cmdLine");
final String normalizedInput = VelocityCommands.normalizeInput(cmdLine, false);
return suggestionsProvider.provideSuggestions(normalizedInput, source)
.thenApply(suggestions -> Lists.transform(suggestions.getList(), Suggestion::getText));
try {
return suggestionsProvider.provideSuggestions(normalizedInput, source)
.thenApply(suggestions -> Lists.transform(suggestions.getList(), Suggestion::getText));
} catch (final Throwable e) {
// Again, plugins are naughty
return CompletableFutures.exceptionallyCompletedFuture(
new RuntimeException("Unable to provide suggestions for " + cmdLine + " for " + source, e));
}
}
/**