Allow for use of @Completions annotation with argument types other than String

This commit is contained in:
jmp 2020-10-26 23:21:25 -07:00 committed by Alexander Söderberg
parent 9a5c674f0d
commit 9c9e13e8b8
3 changed files with 12 additions and 19 deletions

View file

@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added parameter injectors
- Store currently parsing command argument in the command context
### Changed
- Allow for use of `@Completions` annotation with argument types other than String
### Fixed
- Use the correct default range for Double and Float parsers in the StandardParserRegistry

View file

@ -28,6 +28,7 @@ import cloud.commandframework.CommandManager;
import cloud.commandframework.Description;
import cloud.commandframework.annotations.injection.ParameterInjectorRegistry;
import cloud.commandframework.annotations.injection.RawArgs;
import cloud.commandframework.annotations.specifier.Completions;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
@ -427,8 +428,14 @@ public final class AnnotationParser<C> {
} else {
argumentBuilder.asRequired();
}
/* Check whether or not a suggestion provider should be set */
if (!argument.suggestions().isEmpty()) {
/* Check for Completions annotation */
final Completions completions = parameter.getDeclaredAnnotation(Completions.class);
if (completions != null) {
final List<String> suggestions = Arrays.asList(
completions.value().replace(" ", "").split(",")
);
argumentBuilder.withSuggestionsProvider((commandContext, input) -> suggestions);
} else if (!argument.suggestions().isEmpty()) { /* Check whether or not a suggestion provider should be set */
final String suggestionProviderName = argument.suggestions();
final Optional<BiFunction<CommandContext<C>, String, List<String>>> suggestionsFunction =
this.manager.getParserRegistry().getSuggestionProvider(suggestionProviderName);

View file

@ -23,7 +23,6 @@
//
package cloud.commandframework.arguments.parser;
import cloud.commandframework.annotations.specifier.Completions;
import cloud.commandframework.annotations.specifier.Greedy;
import cloud.commandframework.annotations.specifier.Range;
import cloud.commandframework.arguments.standard.BooleanArgument;
@ -92,7 +91,6 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
public StandardParserRegistry() {
/* Register standard mappers */
this.<Range, Number>registerAnnotationMapper(Range.class, new RangeMapper<>());
this.<Completions, String>registerAnnotationMapper(Completions.class, new CompletionsMapper());
this.<Greedy, String>registerAnnotationMapper(Greedy.class, new GreedyMapper());
/* Register standard types */
@ -322,21 +320,6 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
}
private static final class CompletionsMapper implements BiFunction<@NonNull Completions, @NonNull TypeToken<?>,
@NonNull ParserParameters> {
@Override
public @NonNull ParserParameters apply(final @NonNull Completions completions, final @NonNull TypeToken<?> token) {
if (GenericTypeReflector.erase(token.getType()).equals(String.class)) {
final String[] splitCompletions = completions.value().replace(" ", "").split(",");
return ParserParameters.single(StandardParameters.COMPLETIONS, splitCompletions);
}
return ParserParameters.empty();
}
}
private static final class GreedyMapper implements BiFunction<@NonNull Greedy, @NonNull TypeToken<?>,
@NonNull ParserParameters> {