From 9c9e13e8b8d6488bcfc205e88786abb59f53937e Mon Sep 17 00:00:00 2001 From: jmp Date: Mon, 26 Oct 2020 23:21:25 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Allow=20for=20use=20of=20`@Completi?= =?UTF-8?q?ons`=20annotation=20with=20argument=20types=20other=20than=20St?= =?UTF-8?q?ring?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +++ .../annotations/AnnotationParser.java | 11 +++++++++-- .../parser/StandardParserRegistry.java | 17 ----------------- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b24e1700..4c3a1cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cloud-annotations/src/main/java/cloud/commandframework/annotations/AnnotationParser.java b/cloud-annotations/src/main/java/cloud/commandframework/annotations/AnnotationParser.java index 6b3a8655..491157e7 100644 --- a/cloud-annotations/src/main/java/cloud/commandframework/annotations/AnnotationParser.java +++ b/cloud-annotations/src/main/java/cloud/commandframework/annotations/AnnotationParser.java @@ -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 { } 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 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, String, List>> suggestionsFunction = this.manager.getParserRegistry().getSuggestionProvider(suggestionProviderName); diff --git a/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java b/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java index 917e3f69..fbbda6b9 100644 --- a/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java +++ b/cloud-core/src/main/java/cloud/commandframework/arguments/parser/StandardParserRegistry.java @@ -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 implements ParserRegistry { public StandardParserRegistry() { /* Register standard mappers */ this.registerAnnotationMapper(Range.class, new RangeMapper<>()); - this.registerAnnotationMapper(Completions.class, new CompletionsMapper()); this.registerAnnotationMapper(Greedy.class, new GreedyMapper()); /* Register standard types */ @@ -322,21 +320,6 @@ public final class StandardParserRegistry implements ParserRegistry { } - 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> {