Add asynchronous tab completion to the paper module
This commit is contained in:
parent
9d5f007e37
commit
6f0dba0bf0
14 changed files with 203 additions and 21 deletions
|
|
@ -122,7 +122,7 @@ public abstract class CommandManager<C> {
|
|||
*/
|
||||
@Nonnull
|
||||
public CompletableFuture<CommandResult<C>> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
|
||||
final CommandContext<C> context = this.commandContextFactory.create(commandSender);
|
||||
final CommandContext<C> context = this.commandContextFactory.create(false, commandSender);
|
||||
final LinkedList<String> inputQueue = this.tokenize(input);
|
||||
try {
|
||||
if (this.preprocessContext(context, inputQueue) == State.ACCEPTED) {
|
||||
|
|
@ -147,7 +147,7 @@ public abstract class CommandManager<C> {
|
|||
*/
|
||||
@Nonnull
|
||||
public List<String> suggest(@Nonnull final C commandSender, @Nonnull final String input) {
|
||||
final CommandContext<C> context = this.commandContextFactory.create(commandSender);
|
||||
final CommandContext<C> context = this.commandContextFactory.create(true, commandSender);
|
||||
final LinkedList<String> inputQueue = this.tokenize(input);
|
||||
if (this.preprocessContext(context, inputQueue) == State.ACCEPTED) {
|
||||
return this.commandSuggestionProcessor.apply(new CommandPreprocessingContext<>(context, inputQueue),
|
||||
|
|
|
|||
|
|
@ -131,7 +131,7 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
|
||||
private static <C> BiFunction<CommandContext<C>, String, List<String>> buildDefaultSuggestionsProvider(
|
||||
@Nonnull final CommandArgument<C, ?> argument) {
|
||||
return (context, s) -> argument.getParser().suggestions(context, s);
|
||||
return new DelegatingSuggestionsProvider<>(argument.getName(), argument.getParser());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -404,8 +404,8 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
|
|||
this.parser = (c, i) -> ArgumentParseResult
|
||||
.failure(new UnsupportedOperationException("No parser was specified"));
|
||||
}
|
||||
if (suggestionsProvider == null) {
|
||||
suggestionsProvider = this.parser::suggestions;
|
||||
if (this.suggestionsProvider == null) {
|
||||
this.suggestionsProvider = new DelegatingSuggestionsProvider<>(this.name, this.parser);
|
||||
}
|
||||
return new CommandArgument<>(this.required, this.name, this.parser,
|
||||
this.defaultValue, this.valueType, this.suggestionsProvider);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package com.intellectualsites.commands.arguments;
|
||||
|
||||
import com.intellectualsites.commands.arguments.parser.ArgumentParser;
|
||||
import com.intellectualsites.commands.context.CommandContext;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
final class DelegatingSuggestionsProvider<C> implements BiFunction<CommandContext<C>, String, List<String>> {
|
||||
|
||||
private final String argumentName;
|
||||
private final ArgumentParser<C, ?> parser;
|
||||
|
||||
DelegatingSuggestionsProvider(@Nonnull final String argumentName, @Nonnull final ArgumentParser<C, ?> parser) {
|
||||
this.argumentName = argumentName;
|
||||
this.parser = parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> apply(final CommandContext<C> context, final String s) {
|
||||
return this.parser.suggestions(context, s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("DelegatingSuggestionsProvider{name='%s',parser='%s'}", this.argumentName,
|
||||
this.parser.getClass().getCanonicalName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -215,6 +215,14 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
|
|||
}
|
||||
|
||||
if (this.stringMode == StringMode.SINGLE) {
|
||||
if (commandContext.isSuggestions()) {
|
||||
final List<String> suggestions = this.suggestionsProvider.apply(commandContext, inputQueue.peek());
|
||||
if (!suggestions.isEmpty() && !suggestions.contains(input)) {
|
||||
return ArgumentParseResult.failure(new IllegalArgumentException(
|
||||
String.format("'%s' is not one of: %s", input, String.join(", ", suggestions))
|
||||
));
|
||||
}
|
||||
}
|
||||
inputQueue.remove();
|
||||
return ArgumentParseResult.success(input);
|
||||
}
|
||||
|
|
@ -222,7 +230,6 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
|
|||
final StringJoiner sj = new StringJoiner(" ");
|
||||
final int size = inputQueue.size();
|
||||
|
||||
|
||||
boolean started = false;
|
||||
boolean finished = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ public final class CommandContext<C> {
|
|||
|
||||
private final Map<String, Object> internalStorage = new HashMap<>();
|
||||
private final C commandSender;
|
||||
private final boolean suggestions;
|
||||
|
||||
/**
|
||||
* Create a new command context instance
|
||||
|
|
@ -44,7 +45,18 @@ public final class CommandContext<C> {
|
|||
* @param commandSender Sender of the command
|
||||
*/
|
||||
public CommandContext(@Nonnull final C commandSender) {
|
||||
this(false, commandSender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new command context instance
|
||||
*
|
||||
* @param suggestions Whether or not the context is created for command suggestions
|
||||
* @param commandSender Sender of the command
|
||||
*/
|
||||
public CommandContext(final boolean suggestions, @Nonnull final C commandSender) {
|
||||
this.commandSender = commandSender;
|
||||
this.suggestions = suggestions;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -57,6 +69,15 @@ public final class CommandContext<C> {
|
|||
return this.commandSender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this context was created for tab completion purposes
|
||||
*
|
||||
* @return {@code true} if this context is requesting suggestions, else {@code false}
|
||||
*/
|
||||
public boolean isSuggestions() {
|
||||
return this.suggestions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a value in the context map
|
||||
*
|
||||
|
|
|
|||
|
|
@ -35,10 +35,11 @@ public interface CommandContextFactory<C> {
|
|||
/**
|
||||
* Create a new command context
|
||||
*
|
||||
* @param suggestions Whether or not the sender is requesting suggestions
|
||||
* @param sender Command sender
|
||||
* @return Command context
|
||||
*/
|
||||
@Nonnull
|
||||
CommandContext<C> create(@Nonnull C sender);
|
||||
CommandContext<C> create(boolean suggestions, @Nonnull C sender);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,13 +30,14 @@ public final class StandardCommandContextFactory<C> implements CommandContextFac
|
|||
/**
|
||||
* Construct a new command context
|
||||
*
|
||||
* @param sender Command sender
|
||||
* @param suggestions Whether or not the sender is requesting suggestions
|
||||
* @param sender Command sender
|
||||
* @return Created context
|
||||
*/
|
||||
@Nonnull
|
||||
@Override
|
||||
public CommandContext<C> create(@Nonnull final C sender) {
|
||||
return new CommandContext<>(sender);
|
||||
public CommandContext<C> create(final boolean suggestions, @Nonnull final C sender) {
|
||||
return new CommandContext<>(suggestions, sender);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,6 +73,9 @@ public class CommandSuggestionsTest {
|
|||
final String input2 = "test ";
|
||||
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
|
||||
Assertions.assertEquals(Arrays.asList("alt", "comb", "one", "two", "var"), suggestions2);
|
||||
final String input3 = "test a";
|
||||
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
|
||||
Assertions.assertEquals(Collections.singletonList("alt"), suggestions3);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -82,7 +85,7 @@ public class CommandSuggestionsTest {
|
|||
Assertions.assertTrue(suggestions.isEmpty());
|
||||
final String input2 = "test var one";
|
||||
final List<String> suggestions2 = manager.suggest(new TestCommandSender(), input2);
|
||||
Assertions.assertTrue(suggestions2.isEmpty());
|
||||
Assertions.assertEquals(Collections.emptyList(), suggestions2);
|
||||
final String input3 = "test var one f";
|
||||
final List<String> suggestions3 = manager.suggest(new TestCommandSender(), input3);
|
||||
Assertions.assertEquals(Collections.singletonList("foo"), suggestions3);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue