3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-26 02:50:06 +01:00

Allow factory suggestions to have parser context (#2613)

* Add context to rich parser

* Description for getSuggestions with context

* Tidy up imports

* Swap which getSuggestions is primary

* Revert "Swap which getSuggestions is primary"

This reverts commit 5c257f60ea.

* Revert default swap and add deprecation note
Dieser Commit ist enthalten in:
Zeranny 2024-03-15 17:41:53 +00:00 committet von GitHub
Ursprung 10dc64eeaf
Commit 37d4e9bb6f
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
6 geänderte Dateien mit 58 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
@ -122,8 +122,25 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
* @param argumentInput the already provided input for the argument at the given index. * @param argumentInput the already provided input for the argument at the given index.
* @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.
*
* @deprecated Use the version that takes a {@link ParserContext}, {@link #getSuggestions(String, int, ParserContext)}
*/ */
protected abstract Stream<String> getSuggestions(String argumentInput, int index); @Deprecated
protected Stream<String> getSuggestions(String argumentInput, int index) {
return Stream.empty();
}
/**
* Returns a stream of suggestions for the argument at the given index.
*
* @param argumentInput the already provided input for the argument at the given index.
* @param index the index of the argument to get suggestions for.
* @param context the context which may optionally be provided by a parser.
* @return a stream of suggestions matching the given input for the argument at the given index.
*/
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

@ -113,8 +113,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 +138,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 +156,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);
}
} }