✨ Add named suggestion providers
This allows for pre-registration of command suggestion providers, that can then be used in annotated command methods.
This commit is contained in:
parent
37e0b4e91b
commit
3c7bd63f07
6 changed files with 112 additions and 8 deletions
|
|
@ -621,7 +621,7 @@ public final class CommandTree<C> {
|
|||
Objects.requireNonNull(
|
||||
Objects.requireNonNull(
|
||||
node.value,
|
||||
"node.value"
|
||||
"node.value: "
|
||||
).getOwningCommand(),
|
||||
"owning command"
|
||||
).getCommandPermission()
|
||||
|
|
|
|||
|
|
@ -23,11 +23,13 @@
|
|||
//
|
||||
package cloud.commandframework.arguments.parser;
|
||||
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
|
@ -121,4 +123,30 @@ public interface ParserRegistry<C> {
|
|||
@NonNull ParserParameters parserParameters
|
||||
);
|
||||
|
||||
/**
|
||||
* Register a new named suggestion provider
|
||||
*
|
||||
* @param name Name of the suggestions provider. The name is case independent.
|
||||
* @param suggestionsProvider The suggestions provider
|
||||
* @see #getSuggestionProvider(String) Get a suggestion provider
|
||||
* @since 1.1.0
|
||||
*/
|
||||
void registerSuggestionProvider(
|
||||
@NonNull String name,
|
||||
@NonNull BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionsProvider
|
||||
);
|
||||
|
||||
/**
|
||||
* Get a named suggestion provider, if a suggestion provider with the given name exists in the registry
|
||||
*
|
||||
* @param name Suggestion provider name. The name is case independent.
|
||||
* @return Optional that either contains the suggestion provider name, or nothing ({@link Optional#empty()}) if no
|
||||
* suggestion provider is registered with the given name
|
||||
* @see #registerSuggestionProvider(String, BiFunction) Register a suggestion provider
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@NonNull Optional<BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>>> getSuggestionProvider(
|
||||
@NonNull String name
|
||||
);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import cloud.commandframework.arguments.standard.ShortArgument;
|
|||
import cloud.commandframework.arguments.standard.StringArgument;
|
||||
import cloud.commandframework.arguments.standard.StringArrayArgument;
|
||||
import cloud.commandframework.arguments.standard.UUIDArgument;
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
import io.leangen.geantyref.GenericTypeReflector;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
|
@ -45,6 +46,8 @@ import java.lang.annotation.Annotation;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
|
@ -75,6 +78,8 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
private final Map<TypeToken<?>, Function<ParserParameters, ArgumentParser<C, ?>>> parserSuppliers = new HashMap<>();
|
||||
private final Map<Class<? extends Annotation>, BiFunction<? extends Annotation, TypeToken<?>, ParserParameters>>
|
||||
annotationMappers = new HashMap<>();
|
||||
private final Map<String, BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>>>
|
||||
namedSuggestionProviders = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Construct a new {@link StandardParserRegistry} instance. This will also
|
||||
|
|
@ -221,6 +226,24 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
|
|||
return Optional.of(parser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerSuggestionProvider(
|
||||
@NonNull final String name,
|
||||
@NonNull final BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionsProvider
|
||||
) {
|
||||
this.namedSuggestionProviders.put(name.toLowerCase(Locale.ENGLISH), suggestionsProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Optional<BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>>> getSuggestionProvider(
|
||||
@NonNull final String name
|
||||
) {
|
||||
final BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionProvider =
|
||||
this.namedSuggestionProviders.get(name.toLowerCase(Locale.ENGLISH));
|
||||
return Optional.ofNullable(suggestionProvider);
|
||||
}
|
||||
|
||||
|
||||
private static final class RangeMapper<T> implements BiFunction<@NonNull Range, @NonNull TypeToken<?>,
|
||||
@NonNull ParserParameters> {
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue