Add ArrayLength visual update for tabcompletions
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2023-01-22 22:04:00 +01:00
Ursprung a560ddd689
Commit 4e6f73c637
2 geänderte Dateien mit 42 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -26,7 +26,10 @@ import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.*; import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public abstract class AbstractSWCommand<T> { public abstract class AbstractSWCommand<T> {
@ -608,7 +611,8 @@ public abstract class AbstractSWCommand<T> {
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER}) @Target({ElementType.PARAMETER})
@CommandMetaData.Parameter(handler = ArrayLength.Type.class) @CommandMetaData.Parameter(handler = ArrayLength.Type.class)
@CommandMetaData.ImplicitValidator(handler = ArrayLength.Handler.class, order = 1) @CommandMetaData.ImplicitTypeMapper(handler = ArrayLength.HandlerTypeMapper.class)
@CommandMetaData.ImplicitValidator(handler = ArrayLength.HandlerValidator.class, order = 1)
protected @interface ArrayLength { protected @interface ArrayLength {
int min() default 0; int min() default 0;
@ -621,12 +625,40 @@ public abstract class AbstractSWCommand<T> {
} }
} }
class Handler<T> implements AbstractValidator<T, Object> { class HandlerTypeMapper<T> implements AbstractTypeMapper<T, Object> {
private int max;
private AbstractTypeMapper<T, Object> inner;
public HandlerTypeMapper(ArrayLength arrayLength, AbstractTypeMapper<T, Object> inner) {
this.max = arrayLength.max();
this.inner = inner;
}
@Override
public Object map(T sender, PreviousArguments previousArguments, String s) {
return inner.map(sender, previousArguments, s);
}
@Override
public Collection<String> tabCompletes(T sender, PreviousArguments previousArguments, String s) {
Object[] mapped = previousArguments.getMappedArg(0);
if (mapped.length >= max) return Collections.emptyList();
return inner.tabCompletes(sender, previousArguments, s);
}
@Override
public String normalize(T sender, String s) {
return inner.normalize(sender, s);
}
}
class HandlerValidator<T> implements AbstractValidator<T, Object> {
private int min; private int min;
private int max; private int max;
public Handler(ArrayLength arrayLength) { public HandlerValidator(ArrayLength arrayLength) {
this.min = arrayLength.min(); this.min = arrayLength.min();
this.max = arrayLength.max(); this.max = arrayLength.max();
} }

Datei anzeigen

@ -129,11 +129,16 @@ class CommandPart<T> {
public void generateTabComplete(List<String> current, T sender, String[] args, List<Object> mappedArgs, int startIndex) { public void generateTabComplete(List<String> current, T sender, String[] args, List<Object> mappedArgs, int startIndex) {
if (varArgType != null) { if (varArgType != null) {
List<Object> currentArgs = new ArrayList<>(mappedArgs);
List<Object> varArgs = new ArrayList<>();
for (int i = startIndex; i < args.length - 1; i++) { for (int i = startIndex; i < args.length - 1; i++) {
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, mappedArgs, i); CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, mappedArgs, i);
if (!validArgument.success) return; if (!validArgument.success) return;
varArgs.add(validArgument.value);
} }
Collection<String> strings = tabCompletes(sender, args, mappedArgs, args.length - 1);
currentArgs.add(varArgs.toArray());
Collection<String> strings = tabCompletes(sender, args, currentArgs, args.length - 1);
if (strings != null) { if (strings != null) {
current.addAll(strings); current.addAll(strings);
} }