Skip to content

Commit

Permalink
Allow factory suggestions to have parser context (#2613)
Browse files Browse the repository at this point in the history
* 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 5c257f6.

* Revert default swap and add deprecation note
  • Loading branch information
Zeranny committed Mar 15, 2024
1 parent 10dc64e commit 37d4e9b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
Expand Up @@ -53,7 +53,7 @@ private static Predicate<String> validPrefix(String other) {
}

@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
Expand All @@ -65,7 +65,7 @@ private Function<String, Stream<? extends String>> extractArguments(String input
}
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);
Expand Down Expand Up @@ -95,7 +95,7 @@ public List<String> getMatchedAliases() {
public Stream<String> getSuggestions(String input) {
return Arrays.stream(this.prefixes)
.filter(validPrefix(input))
.flatMap(extractArguments(input));
.flatMap(extractArguments(input, new ParserContext()));
}

@Override
Expand All @@ -122,8 +122,25 @@ public E parseFromInput(String input, ParserContext context) throws InputParseEx
* @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.
Expand Down
Expand Up @@ -97,11 +97,11 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
)),
() -> {
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));
}
);
}
Expand Down Expand Up @@ -164,11 +164,11 @@ public Mask parseFromInput(String input, ParserContext context) throws InputPars
)),
() -> {
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));
}
);
}
Expand Down
Expand Up @@ -113,8 +113,7 @@ public static void register(WorldEdit worldEdit, CommandManager commandManager)
);
}

@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);
Expand All @@ -139,6 +138,13 @@ public ConversionResult<T> convert(String argument, InjectedValueAccess context)
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)
Expand All @@ -150,7 +156,9 @@ public ConversionResult<T> convert(String argument, InjectedValueAccess context)

@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
Expand Down
Expand Up @@ -127,13 +127,13 @@ public MaskFactory(WorldEdit worldEdit) {
}

@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
Expand Down
Expand Up @@ -96,9 +96,14 @@ public E parseFromInput(String input, ParserContext context) throws InputParseEx
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());
}

Expand Down
Expand Up @@ -45,9 +45,22 @@ protected InputParser(WorldEdit worldEdit) {
* 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);
}
}

0 comments on commit 37d4e9b

Please sign in to comment.