3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-28 11:50:05 +01:00

Add context to rich parser

Dieser Commit ist enthalten in:
Zeranny 2024-03-10 18:04:15 +00:00
Ursprung 9fa3385edc
Commit b38d97ec5d
6 geänderte Dateien mit 48 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -53,7 +53,7 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
} }
@Nonnull @Nonnull
private Function<String, Stream<? extends String>> extractArguments(String input) { private Function<String, Stream<? extends String>> extractArguments(String input, ParserContext context) {
return prefix -> { return prefix -> {
if (input.length() > prefix.length() && input.startsWith(prefix + "[")) { if (input.length() > prefix.length() && input.startsWith(prefix + "[")) {
// input already contains argument(s) -> extract them // input already contains argument(s) -> extract them
@ -65,7 +65,7 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
} }
String previous = prefix + builder; String previous = prefix + builder;
// read the suggestions for the last argument // read the suggestions for the last argument
return getSuggestions(strings[strings.length - 1], strings.length - 1) return getSuggestions(strings[strings.length - 1], strings.length - 1, context)
.map(suggestion -> previous + "[" + suggestion); .map(suggestion -> previous + "[" + suggestion);
} else { } else {
return Stream.of(prefix); return Stream.of(prefix);
@ -95,7 +95,7 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
public Stream<String> getSuggestions(String input) { public Stream<String> getSuggestions(String input) {
return Arrays.stream(this.prefixes) return Arrays.stream(this.prefixes)
.filter(validPrefix(input)) .filter(validPrefix(input))
.flatMap(extractArguments(input)); .flatMap(extractArguments(input, new ParserContext()));
} }
@Override @Override
@ -123,7 +123,13 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
* @param index the index of the argument to get suggestions for. * @param index the index of the argument to get suggestions for.
* @return a stream of suggestions matching the given input for the argument at the given index. * @return a stream of suggestions matching the given input for the argument at the given index.
*/ */
protected abstract Stream<String> getSuggestions(String argumentInput, int index); protected Stream<String> getSuggestions(String argumentInput, int index) {
return Stream.empty();
}
protected Stream<String> getSuggestions(String argumentInput, int index, ParserContext context) {
return getSuggestions(argumentInput, index);
}
/** /**
* Parses the already split arguments. * Parses the already split arguments.

Datei anzeigen

@ -97,11 +97,11 @@ public class RichMaskParser extends FaweParser<Mask> {
)), )),
() -> { () -> {
if (full.length() == 1) { if (full.length() == 1) {
return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("")); return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("", context));
} }
return new ArrayList<>(worldEdit return new ArrayList<>(worldEdit
.getMaskFactory() .getMaskFactory()
.getSuggestions(command.toLowerCase(Locale.ROOT))); .getSuggestions(command.toLowerCase(Locale.ROOT), context));
} }
); );
} }
@ -164,11 +164,11 @@ public class RichMaskParser extends FaweParser<Mask> {
)), )),
() -> { () -> {
if (full.length() == 1) { if (full.length() == 1) {
return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("")); return new ArrayList<>(worldEdit.getMaskFactory().getSuggestions("", context));
} }
return new ArrayList<>(worldEdit return new ArrayList<>(worldEdit
.getMaskFactory() .getMaskFactory()
.getSuggestions(command.toLowerCase(Locale.ROOT))); .getSuggestions(command.toLowerCase(Locale.ROOT), context));
} }
); );
} }

Datei anzeigen

@ -37,6 +37,7 @@ import com.sk89q.worldedit.internal.annotation.ClipboardMask;
import com.sk89q.worldedit.internal.registry.AbstractFactory; import com.sk89q.worldedit.internal.registry.AbstractFactory;
import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.session.ClipboardHolder; import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.session.request.RequestExtent;
import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.world.World; import com.sk89q.worldedit.world.World;
@ -113,8 +114,7 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
); );
} }
@Override private ParserContext createContext(InjectedValueAccess context) {
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
Actor actor = context.injectedValue(Key.of(Actor.class)) Actor actor = context.injectedValue(Key.of(Actor.class))
.orElseThrow(() -> new IllegalStateException("No actor")); .orElseThrow(() -> new IllegalStateException("No actor"));
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor); LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
@ -139,6 +139,13 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
contextTweaker.accept(parserContext); contextTweaker.accept(parserContext);
} }
return parserContext;
}
@Override
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
ParserContext parserContext = createContext(context);
try { try {
return SuccessfulConversion.fromSingle( return SuccessfulConversion.fromSingle(
factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext) factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext)
@ -150,7 +157,9 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
@Override @Override
public List<String> getSuggestions(String input, InjectedValueAccess context) { public List<String> getSuggestions(String input, InjectedValueAccess context) {
return factoryExtractor.apply(worldEdit).getSuggestions(input); ParserContext parserContext = createContext(context);
return factoryExtractor.apply(worldEdit).getSuggestions(input, parserContext);
} }
@Override @Override

Datei anzeigen

@ -127,13 +127,13 @@ public final class MaskFactory extends AbstractFactory<Mask> {
} }
@Override @Override
public List<String> getSuggestions(String input) { public List<String> getSuggestions(String input, final ParserContext parserContext) {
final String[] split = input.split(" "); final String[] split = input.split(" ");
if (split.length > 1) { if (split.length > 1) {
String prev = input.substring(0, input.lastIndexOf(" ")) + " "; String prev = input.substring(0, input.lastIndexOf(" ")) + " ";
return super.getSuggestions(split[split.length - 1]).stream().map(s -> prev + s).collect(Collectors.toList()); return super.getSuggestions(split[split.length - 1], parserContext).stream().map(s -> prev + s).collect(Collectors.toList());
} }
return super.getSuggestions(input); return super.getSuggestions(input, parserContext);
} }
@Override @Override

Datei anzeigen

@ -96,9 +96,14 @@ public abstract class AbstractFactory<E> {
throw new NoMatchException(Caption.of("worldedit.error.no-match", TextComponent.of(input))); throw new NoMatchException(Caption.of("worldedit.error.no-match", TextComponent.of(input)));
} }
@Deprecated
public List<String> getSuggestions(String input) { public List<String> getSuggestions(String input) {
return getSuggestions(input, new ParserContext());
}
public List<String> getSuggestions(String input, ParserContext context) {
return parsers.stream().flatMap( return parsers.stream().flatMap(
p -> p.getSuggestions(input) p -> p.getSuggestions(input, context)
).collect(Collectors.toList()); ).collect(Collectors.toList());
} }

Datei anzeigen

@ -45,9 +45,22 @@ public abstract class InputParser<E> {
* Gets a stream of suggestions of input to this parser. * Gets a stream of suggestions of input to this parser.
* *
* @return a stream of suggestions * @return a stream of suggestions
* @deprecated Use the version that takes a {@link ParserContext}, {@link #getSuggestions(String, ParserContext)}
*/ */
@Deprecated
public Stream<String> getSuggestions(String input) { public Stream<String> getSuggestions(String input) {
return Stream.empty(); return Stream.empty();
} }
/**
* Gets a stream of suggestions of input to this parser.
*
* @param input The string input
* @param context The parser context
*
* @return a stream of suggestions
*/
public Stream<String> getSuggestions(String input, ParserContext context) {
return getSuggestions(input);
}
} }