Skip to content

Commit

Permalink
Rework on the SUR
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 7, 2024
1 parent 3e9d16e commit 9385116
Show file tree
Hide file tree
Showing 49 changed files with 1,005 additions and 1,155 deletions.
48 changes: 12 additions & 36 deletions core/src/main/java/dev/velix/imperat/ResolverRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import dev.velix.imperat.annotations.base.element.ParameterElement;
import dev.velix.imperat.command.ContextResolverFactory;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.ParameterType;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.Source;
import dev.velix.imperat.context.internal.CommandInputStream;
import dev.velix.imperat.placeholders.Placeholder;
import dev.velix.imperat.placeholders.PlaceholderResolver;
import dev.velix.imperat.resolvers.*;
import dev.velix.imperat.resolvers.ContextResolver;
import dev.velix.imperat.resolvers.PermissionResolver;
import dev.velix.imperat.resolvers.SourceResolver;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.TypeWrap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

Expand Down Expand Up @@ -72,7 +77,7 @@ public sealed interface ResolverRegistrar<S extends Source> permits Imperat {
* Fetches the {@link ContextResolver} suitable for the {@link CommandParameter}
*
* @param commandParameter the parameter of a command's usage
* @param <T> the valueType of value that will be resolved by {@link ValueResolver}
* @param <T> the valueType of value that will be resolved by {@link ParameterType#resolve(ExecutionContext, CommandInputStream)}
* @return the context resolver for this parameter's value valueType
*/
default <T> ContextResolver<S, T> getContextResolver(CommandParameter<S> commandParameter) {
Expand All @@ -89,37 +94,23 @@ default <T> ContextResolver<S, T> getContextResolver(CommandParameter<S> command
<T> void registerContextResolver(Type type, @NotNull ContextResolver<S, T> resolver);

/**
* Fetches {@link ValueResolver} for a certain value
* Fetches {@link ParameterType} for a certain value
*
* @param resolvingValueType the value that the resolver ends providing it from the context
* @return the value resolver of a certain valueType
*/
@Nullable
ValueResolver<S, ?> getValueResolver(Type resolvingValueType);
ParameterType<S, ?> getParameterType(Type resolvingValueType);

/**
* Fetches the {@link ValueResolver} suitable for the {@link CommandParameter}
*
* @param commandParameter the parameter of a command's usage
* @return the value resolver for this parameter's value valueType
*/
default ValueResolver<S, ?> getValueResolver(CommandParameter<S> commandParameter) {
return getValueResolver(commandParameter.valueType());
}

/**
* Registers {@link ValueResolver}
* Registers {@link ParameterType}
*
* @param type the class-valueType of value being resolved from context
* @param resolver the resolver for this value
* @param <T> the valueType of value being resolved from context
*/
<T> void registerValueResolver(Type type, @NotNull ValueResolver<S, T> resolver);
<T> void registerParamType(Type type, @NotNull ParameterType<S, T> resolver);

/**
* @return all currently registered {@link ValueResolver}
*/
Collection<? extends ValueResolver<S, ?>> getRegisteredValueResolvers();

/**
* Fetches the suggestion provider/resolver for a specific valueType of
Expand Down Expand Up @@ -158,21 +149,6 @@ default <T> ContextResolver<S, T> getContextResolver(CommandParameter<S> command
@Nullable
SuggestionResolver<S> getNamedSuggestionResolver(String name);

/**
* Registers a suggestion resolver
*
* @param suggestionResolver the suggestion resolver to register
* @param <T> the valueType of value that the suggestion resolver will work with.
*/
<T> void registerSuggestionResolver(TypeSuggestionResolver<S, T> suggestionResolver);

/**
* Registers a suggestion resolver to a valueType
*
* @param type the valueType
* @param suggestionResolver the suggestion resolver.
*/
void registerSuggestionResolver(Type type, SuggestionResolver<S> suggestionResolver);

/**
* Registers a suggestion resolver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.NumericRange;
import dev.velix.imperat.command.parameters.StrictParameterList;
import dev.velix.imperat.command.parameters.type.ParameterTypes;
import dev.velix.imperat.command.parameters.type.ParameterType;
import dev.velix.imperat.command.processors.CommandPostProcessor;
import dev.velix.imperat.command.processors.CommandPreProcessor;
import dev.velix.imperat.context.Source;
Expand Down Expand Up @@ -422,7 +422,11 @@ private <T> CommandParameter<S> loadParameter(
throw new IllegalStateException("both @Flag and @Switch at the same time !");
}

TypeWrap<T> parameterType = TypeWrap.of((Class<T>) parameter.getParameterizedType());
TypeWrap<T> parameterTypeWrap = TypeWrap.of((Class<T>) parameter.getParameterizedType());
var type = (ParameterType<S, T>) imperat.getParameterType(parameterTypeWrap.getType());
if (type == null) {
throw new IllegalArgumentException("Unknown type detected '" + parameterTypeWrap.getType().getTypeName() + "'");
}

String name = AnnotationHelper.getParamName(imperat, parameter, named, flag, switchAnnotation);
boolean optional = flag != null || switchAnnotation != null
Expand All @@ -440,7 +444,7 @@ private <T> CommandParameter<S> loadParameter(

if (suggestAnnotation != null) {
suggestionResolver = SuggestionResolver.type(
parameterType,
parameterTypeWrap,
imperat.replacePlaceholders(suggestAnnotation.value())
);
} else if (suggestionProvider != null) {
Expand Down Expand Up @@ -479,7 +483,7 @@ else if (namedResolver != null)
permission = imperat.replacePlaceholders(permAnn.value());
}

OptionalValueSupplier<T> optionalValueSupplier = OptionalValueSupplier.empty(parameterType);
OptionalValueSupplier<T> optionalValueSupplier = OptionalValueSupplier.empty(parameterTypeWrap);
if (optional) {
Default defaultAnnotation = parameter.getAnnotation(Default.class);
DefaultProvider provider = parameter.getAnnotation(DefaultProvider.class);
Expand All @@ -491,8 +495,9 @@ else if (namedResolver != null)
if (suggestAnnotation != null) {
suggestionResolver = SuggestionResolver.type(TypeWrap.of(parameter.getParameterizedType()), imperat.replacePlaceholders(suggestAnnotation.value()));
}

return AnnotationParameterDecorator.decorate(
CommandParameter.<S, T>flag(name, (Class<T>) parameterType.getType())
CommandParameter.flag(name, type)
.suggestForInputValue((TypeSuggestionResolver<S, T>) suggestionResolver)
.aliases(getAllExceptFirst(flagAliases))
.flagDefaultInputValue(optionalValueSupplier)
Expand All @@ -516,7 +521,7 @@ else if (namedResolver != null)
CommandParameter<S> param =
AnnotationParameterDecorator.decorate(
CommandParameter.of(
name, ParameterTypes.from(parameterType), permission, desc,
name, type, permission, desc,
optional, greedy, optionalValueSupplier, suggestionResolver
), element
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public final class NumericParameterDecorator<S extends Source> extends InputPara

NumericParameterDecorator(CommandParameter<S> parameter, NumericRange range) {
super(
parameter.name(), parameter.wrappedType(), parameter.permission(),
parameter.name(), parameter.type(), parameter.permission(),
parameter.description(), parameter.isOptional(), parameter.isFlag(),
parameter.isFlag(), parameter.getDefaultValueSupplier(),
parameter.getSuggestionResolver()
Expand Down
56 changes: 17 additions & 39 deletions core/src/main/java/dev/velix/imperat/command/BaseImperat.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.velix.imperat.annotations.base.AnnotationReplacer;
import dev.velix.imperat.annotations.base.element.ParameterElement;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.ParameterType;
import dev.velix.imperat.command.processors.CommandPostProcessor;
import dev.velix.imperat.command.processors.CommandPreProcessor;
import dev.velix.imperat.command.processors.CommandProcessor;
Expand All @@ -20,7 +21,10 @@
import dev.velix.imperat.placeholders.Placeholder;
import dev.velix.imperat.placeholders.PlaceholderRegistry;
import dev.velix.imperat.placeholders.PlaceholderResolver;
import dev.velix.imperat.resolvers.*;
import dev.velix.imperat.resolvers.ContextResolver;
import dev.velix.imperat.resolvers.PermissionResolver;
import dev.velix.imperat.resolvers.SourceResolver;
import dev.velix.imperat.resolvers.SuggestionResolver;
import dev.velix.imperat.util.ImperatDebugger;
import dev.velix.imperat.util.Preconditions;
import dev.velix.imperat.util.TypeWrap;
Expand All @@ -37,7 +41,7 @@
public abstract class BaseImperat<S extends Source> implements Imperat<S> {

private final ContextResolverRegistry<S> contextResolverRegistry;
private final ValueResolverRegistry<S> valueResolverRegistry;
private final ParamTypeRegistry<S> paramTypeRegistry;
private final SuggestionResolverRegistry<S> suggestionResolverRegistry;
private final PlaceholderRegistry<S> placeholderRegistry;
private final SourceResolverRegistry<S> sourceResolverRegistry;
Expand All @@ -61,8 +65,8 @@ protected BaseImperat(@NotNull PermissionResolver<S> permissionResolver) {

contextFactory = ContextFactory.defaultFactory(this);
contextResolverRegistry = ContextResolverRegistry.createDefault(this);
valueResolverRegistry = ValueResolverRegistry.createDefault();
suggestionResolverRegistry = SuggestionResolverRegistry.createDefault();
paramTypeRegistry = ParamTypeRegistry.createDefault();
suggestionResolverRegistry = SuggestionResolverRegistry.createDefault(this);
sourceResolverRegistry = SourceResolverRegistry.createDefault();
placeholderRegistry = PlaceholderRegistry.createDefault(this);
verifier = UsageVerifier.typeTolerantVerifier();
Expand Down Expand Up @@ -390,33 +394,26 @@ public <T> void registerContextResolver(Type type,
}

/**
* Registers {@link ValueResolver}
* Registers {@link dev.velix.imperat.command.parameters.type.ParameterType}
*
* @param type the class-valueType of value being resolved from context
* @param resolver the resolver for this value
*/
@Override
public <T> void registerValueResolver(Type type, @NotNull ValueResolver<S, T> resolver) {
valueResolverRegistry.registerResolver(type, resolver);
public <T> void registerParamType(Type type, @NotNull ParameterType<S, T> resolver) {
paramTypeRegistry.registerResolver(type, resolver);
}

/**
* @return all currently registered {@link ValueResolver}
*/
@Override
public Collection<? extends ValueResolver<S, ?>> getRegisteredValueResolvers() {
return valueResolverRegistry.getAll();
}

/**
* Fetches {@link ValueResolver} for a certain value
* Fetches {@link ParameterType} for a certain value
*
* @param resolvingValueType the value that the resolver ends providing it from the context
* @return the context resolver of a certain valueType
*/
@Override
public @Nullable ValueResolver<S, ?> getValueResolver(Type resolvingValueType) {
return valueResolverRegistry.getResolver(resolvingValueType);
public @Nullable ParameterType<S, ?> getParameterType(Type resolvingValueType) {
return paramTypeRegistry.getResolver(resolvingValueType).orElse(null);
}

/**
Expand All @@ -428,7 +425,9 @@ public <T> void registerValueResolver(Type type, @NotNull ValueResolver<S, T> re
*/
@Override
public @Nullable SuggestionResolver<S> getSuggestionResolverByType(Type type) {
return suggestionResolverRegistry.getResolver(type);
return paramTypeRegistry.getResolver(type)
.map(ParameterType::getSuggestionResolver)
.orElse(null);
}

/**
Expand All @@ -442,27 +441,6 @@ public boolean canBeSender(Type type) {
return TypeWrap.of(Source.class).isSupertypeOf(type);
}

/**
* Registers a suggestion resolver
*
* @param suggestionResolver the suggestion resolver to register
*/
@Override
public <T> void registerSuggestionResolver(TypeSuggestionResolver<S, T> suggestionResolver) {
suggestionResolverRegistry.registerResolverForType(suggestionResolver);
}

/**
* Registers a suggestion resolver to a valueType
*
* @param type the valueType
* @param suggestionResolver the suggestion resolver.
*/
@Override
public void registerSuggestionResolver(Type type, SuggestionResolver<S> suggestionResolver) {
suggestionResolverRegistry.registerResolverForType(type, suggestionResolver);
}

/**
* Fetches the suggestion provider/resolver for a specific argument
*
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/dev/velix/imperat/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import dev.velix.imperat.Imperat;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.type.ParameterType;
import dev.velix.imperat.command.parameters.type.ParameterTypes;
import dev.velix.imperat.command.processors.CommandPostProcessor;
import dev.velix.imperat.command.processors.CommandPreProcessor;
import dev.velix.imperat.command.suggestions.AutoCompleter;
Expand Down Expand Up @@ -82,6 +84,11 @@ default void addAliases(String... aliases) {
addAliases(List.of(aliases));
}

@Override
default @NotNull ParameterType<S, ?> type() {
return ParameterTypes.command();
}

/**
* Sets the position of this command in a syntax
* DO NOT USE THIS FOR ANY REASON unless it's necessary to do so
Expand All @@ -93,6 +100,7 @@ default void position(int position) {
throw new UnsupportedOperationException("You can't modify the position of a command");
}


/**
* @return the default value if it's input is not present
* in case of the parameter being optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import dev.velix.imperat.command.cooldown.UsageCooldown;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.command.parameters.ParameterBuilder;
import dev.velix.imperat.context.CommandFlag;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.FlagData;
import dev.velix.imperat.context.Source;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -58,7 +58,7 @@ static <S extends Source> Builder<S> builder() {
* @return the flag from the raw input, null if it cannot be a flag
*/
@Nullable
CommandFlag getFlagFromRaw(String rawInput);
FlagData getFlagFromRaw(String rawInput);

/**
* Adds parameters to the usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import dev.velix.imperat.command.cooldown.DefaultCooldownHandler;
import dev.velix.imperat.command.cooldown.UsageCooldown;
import dev.velix.imperat.command.parameters.CommandParameter;
import dev.velix.imperat.context.CommandFlag;
import dev.velix.imperat.context.ExecutionContext;
import dev.velix.imperat.context.FlagData;
import dev.velix.imperat.context.Source;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -96,7 +96,7 @@ public boolean hasFlag(String input) {
* @return the flag from the raw input, null if it cannot be a flag
*/
@Override
public @Nullable CommandFlag getFlagFromRaw(String rawInput) {
public @Nullable FlagData getFlagFromRaw(String rawInput) {
boolean isSingle = SINGLE_FLAG.matcher(rawInput).matches();
boolean isDouble = DOUBLE_FLAG.matcher(rawInput).matches();

Expand All @@ -108,7 +108,7 @@ public boolean hasFlag(String input) {

for (var param : parameters) {
if (!param.isFlag()) continue;
CommandFlag flag = param.asFlagParameter().flagData();
FlagData flag = param.asFlagParameter().flagData();
if (flag.acceptsInput(inputFlagAlias)) {
return flag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static <S extends Source> ParameterBuilder<S, String> optionalGreedy(String name

static <S extends Source, T> FlagBuilder<S, T> flag(
String name,
Class<T> inputType
ParameterType<S, T> inputType
) {
return FlagBuilder.ofFlag(name, inputType);
}
Expand Down Expand Up @@ -268,4 +268,7 @@ default NumericParameter<S> asNumeric() {
*/
boolean similarTo(CommandParameter<?> parameter);

default boolean isRequired() {
return !isOptional();
}
}
Loading

0 comments on commit 9385116

Please sign in to comment.