Skip to content

Commit

Permalink
Add support for custom argument node types
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed May 14, 2022
1 parent b2728f2 commit a8c5e9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.hedgehog1029.frame.dispatcher.bindings;

import io.github.hedgehog1029.frame.annotation.Text;
import io.github.hedgehog1029.frame.dispatcher.arguments.CommandArgumentsDeque;
import io.github.hedgehog1029.frame.dispatcher.exception.DispatcherException;
import io.github.hedgehog1029.frame.dispatcher.exception.NotEnoughArgumentsException;
Expand Down Expand Up @@ -81,17 +80,11 @@ public List<ExecutionPlan> getExecutionPlans() {
int i = 0;
while (arity > 0) {
ParameterWrapper pw = visibleParams.get(i++);
boolean isGreedy = pw.isAnnotationPresent(Text.class);

for (int n = 0; n < pw.getWillConsume(); n++) {
String name = pw.getName() + (n == 0 ? "" : n);

if (isGreedy) {
nodes.add(new ArgumentNode.GreedyString(name));
// do we assert that getWillConsume() == 1 here?
} else {
nodes.add(new ArgumentNode.SingleString(name));
}
nodes.add(pw.getProvider().makeNode(name, pw));
}

arity -= pw.getWillConsume();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.hedgehog1029.frame.dispatcher.provider;

import io.github.hedgehog1029.frame.annotation.Text;
import io.github.hedgehog1029.frame.dispatcher.arguments.ICommandArguments;
import io.github.hedgehog1029.frame.dispatcher.exception.DispatcherException;
import io.github.hedgehog1029.frame.dispatcher.pipeline.ArgumentNode;
import io.github.hedgehog1029.frame.module.wrappers.ParameterWrapper;
import io.github.hedgehog1029.frame.util.Namespace;

Expand All @@ -18,4 +20,18 @@ public interface Provider<T> {

List<String> getSuggestions(int index, String partial, Namespace namespace);
int argsWanted(ParameterWrapper param);

/**
* Called when the execution planner wants an argument node.
* @param name Name to use for this argument. Use this rather than {@link ParameterWrapper#getName}!
* @param param Parameter this is operating on
* @return An argument node for this parameter, used by implementors for hinting
*/
default ArgumentNode makeNode(String name, ParameterWrapper param) {
if (param.isAnnotationPresent(Text.class)) {
return new ArgumentNode.GreedyString(name);
} else {
return new ArgumentNode.SingleString(name);
}
}
}

0 comments on commit a8c5e9f

Please sign in to comment.