Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for custom command completion in the annotation method of command registration #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ Sub-commands are supported. This is because all `Dispatcher`s are also
`CommandCallable`s, so you can add a dispatcher to another dispatcher to another
dispatcher!

In addition, Intake supports completion of arguments, although currently the
annotation method of command registration does not support the completion of
parameters in a command. You can complete sub-commands, however.
In addition, Intake supports completion of arguments through
`CommandCallable`s and `CommandCompleter`s

The API supports a rich amount of metadata about each command, allowing the
inspection of registered commands, their parameters, their permissions, and
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.sk89q</groupId>
<artifactId>intake</artifactId>
<version>3.1.1-SNAPSHOT</version>
<version>3.1.2-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/sk89q/intake/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,11 @@
*/
boolean anyFlags() default false;

/**
* The command completer.
*
* @return the command completer, or {@link Class} if not set
*/
Class<?> completer() default Class.class;

}
21 changes: 21 additions & 0 deletions src/main/java/com/sk89q/intake/parametric/ParametricBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class ParametricBuilder {
private final Map<Type, Binding> bindings = new HashMap<Type, Binding>();
private final List<InvokeListener> invokeListeners = new ArrayList<InvokeListener>();
private final List<ExceptionConverter> exceptionConverters = new ArrayList<ExceptionConverter>();
private final Map<Class, CommandCompleter> completers = new HashMap<Class, CommandCompleter>();
private Authorizer authorizer = new NullAuthorizer();
private CommandCompleter defaultCompleter = new NullCompleter();

Expand Down Expand Up @@ -214,6 +215,15 @@ List<ExceptionConverter> getExceptionConverters() {
return exceptionConverters;
}

/**
* Get the map of custom completers.
*
* @return a map of custom completers.
*/
Map<Class, CommandCompleter> getCompleters() {
return completers;
}

/**
* Get the authorizer.
*
Expand Down Expand Up @@ -254,4 +264,15 @@ public void setDefaultCompleter(CommandCompleter defaultCompleter) {
this.defaultCompleter = defaultCompleter;
}

/**
* Add a custom command suggestions provider that will be used if
* specified in a {@link Command}
*
* @param completer the custom command completer
*/
public void addCompleter(CommandCompleter completer) {
checkNotNull(completer);
completers.put(completer.getClass(), completer);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.sk89q.intake.Parameter;
import com.sk89q.intake.Require;
import com.sk89q.intake.SettableDescription;
import com.sk89q.intake.completion.CommandCompleter;
import com.sk89q.intake.context.CommandContext;
import com.sk89q.intake.context.CommandLocals;
import com.sk89q.intake.parametric.annotation.Optional;
Expand Down Expand Up @@ -65,6 +66,7 @@ class ParametricCallable implements CommandCallable {
private final Set<Character> legacyFlags = new HashSet<Character>();
private final SettableDescription description = new SettableDescription();
private final Require permission;
private final CommandCompleter commandCompleter;

/**
* Create a new instance.
Expand Down Expand Up @@ -182,6 +184,20 @@ class ParametricCallable implements CommandCallable {

// Get permissions annotation
permission = method.getAnnotation(Require.class);

// Set command completer
if (definition.completer() == Class.class) {
// Set to default
commandCompleter = builder.getDefaultCompleter();
} else {
// Set a custom completer
CommandCompleter customCompleter = builder.getCompleters().get(definition.completer());
if (customCompleter == null) {
throw new ParametricException("Cannot find custom completer for " + definition.completer().getCanonicalName());
} else {
commandCompleter = customCompleter;
}
}
}

@Override
Expand Down Expand Up @@ -288,7 +304,7 @@ public boolean call(String stringArguments, CommandLocals locals, String[] paren

@Override
public List<String> getSuggestions(String arguments, CommandLocals locals) throws CommandException {
return builder.getDefaultCompleter().getSuggestions(arguments, locals);
return commandCompleter.getSuggestions(arguments, locals);
}

/**
Expand Down