Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow factory suggestions to have parser context #2613

Merged
merged 7 commits into from Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 Down Expand Up @@ -123,7 +123,21 @@ public E parseFromInput(String input, ParserContext context) throws InputParseEx
* @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.
*/
protected abstract Stream<String> getSuggestions(String argumentInput, int index);
protected Stream<String> getSuggestions(String argumentInput, int index) {
return Stream.empty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should default to the new method with an empty parser context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should default to the new method with an empty parser context

I've swapped them round 5c257f6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that they should both default to each other so parsers can implement one of either method and it will still work. Similar is already done in extents https://github.com/IntellectualSites/FastAsyncWorldEdit/blob/main/worldedit-core/src/main/java/com/sk89q/worldedit/extent/InputExtent.java#L52-L59

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a dumb question, but would there be any risk if someone did not overwrite one or both of them when using the API? Would the getSuggestions not just loop?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I suppose we're not necessarily going to always do suggestions? Return it to how it was but add a deprecation to it I think, as that method should never be called; overriding is fine but calling is not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the Deprecated tag & a note: 2873359

}

/**
* 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);
}
}