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 5c257f60ea9ec002e407b2a4a8a7c6ede6bee5e7.

* Revert default swap and add deprecation note
This commit is contained in:
Zeranny 2024-03-15 17:41:53 +00:00 committed by GitHub
parent 10dc64eeaf
commit 37d4e9bb6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 58 additions and 15 deletions

View File

@ -53,7 +53,7 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
}
@Nonnull
private Function<String, Stream<? extends String>> extractArguments(String input) {
private Function<String, Stream<? extends String>> extractArguments(String input, ParserContext context) {
return prefix -> {
if (input.length() > prefix.length() && input.startsWith(prefix + "[")) {
// 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;
// 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);
} else {
return Stream.of(prefix);
@ -95,7 +95,7 @@ public abstract class RichParser<E> extends InputParser<E> implements AliasedPar
public Stream<String> getSuggestions(String input) {
return Arrays.stream(this.prefixes)
.filter(validPrefix(input))
.flatMap(extractArguments(input));
.flatMap(extractArguments(input, new ParserContext()));
}
@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 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.
*
* @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.

View File

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

View File

@ -113,8 +113,7 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
);
}
@Override
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
private ParserContext createContext(InjectedValueAccess context) {
Actor actor = context.injectedValue(Key.of(Actor.class))
.orElseThrow(() -> new IllegalStateException("No actor"));
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
@ -139,6 +138,13 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
contextTweaker.accept(parserContext);
}
return parserContext;
}
@Override
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
ParserContext parserContext = createContext(context);
try {
return SuccessfulConversion.fromSingle(
factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext)
@ -150,7 +156,9 @@ public class FactoryConverter<T> implements ArgumentConverter<T> {
@Override
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

View File

@ -127,13 +127,13 @@ public final class MaskFactory extends AbstractFactory<Mask> {
}
@Override
public List<String> getSuggestions(String input) {
public List<String> getSuggestions(String input, final ParserContext parserContext) {
final String[] split = input.split(" ");
if (split.length > 1) {
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

View File

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

View File

@ -45,9 +45,22 @@ public abstract class InputParser<E> {
* Gets a stream of suggestions of input to this parser.
*
* @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) {
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);
}
}