CMDAPIArrayLength #41

Zusammengeführt
Lixfel hat 2 Commits von CMDAPIArrayLength nach master 2023-01-23 16:13:55 +01:00 zusammengeführt
4 geänderte Dateien mit 49 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -26,7 +26,10 @@ import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
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;
public abstract class AbstractSWCommand<T> {
@ -608,7 +611,8 @@ public abstract class AbstractSWCommand<T> {
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
@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 {
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 max;
public Handler(ArrayLength arrayLength) {
public HandlerValidator(ArrayLength arrayLength) {
this.min = arrayLength.min();
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) {
if (varArgType != null) {
List<Object> currentArgs = new ArrayList<>(mappedArgs);
List<Object> varArgs = new ArrayList<>();
for (int i = startIndex; i < args.length - 1; i++) {
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, mappedArgs, i);
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) {
current.addAll(strings);
}

Datei anzeigen

@ -67,4 +67,8 @@ public class ArgumentCommand extends TestSWCommand {
public void minAndMaxLengthArgument(String sender, @Length(min = 3, max = 3) @StaticValue({"wo", "world"}) String arg) {
throw new ExecutionIdentifier("RunLengthArgument with String");
}
public void arrayLengthArgument(String sender, @ArrayLength(max = 3) @StaticValue({"one", "two", "three"}) String... args) {
throw new ExecutionIdentifier("RunArrayLengthArgument with String");
}
}

Datei anzeigen

@ -121,5 +121,8 @@ public class ArgumentCommandTest {
strings = cmd.tabComplete("test", "", new String[]{"worl"});
assertTabCompletes(strings, "wor", "worl");
strings = cmd.tabComplete("test", "", new String[]{"one", "two", "three", "one"});
assertTabCompletes(strings);
}
}