Skip to content

Commit

Permalink
GH-36 Aliases execute annotation (#37)
Browse files Browse the repository at this point in the history
* Add aliases to `@Execute` annotation.

* Add handling aliases for LiteExecution.
  • Loading branch information
Rollczi authored Feb 6, 2022
1 parent 92e5449 commit b0a4a0a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

String route() default "";

String[] aliases() default {};

int required() default -1;

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ public Option<ScopeMetaData> parse(AnnotatedElement annotatedElement) {

if (annotation instanceof Execute) {
Execute execute = (Execute) annotation;
builder.name(placeholders.format(execute.route()));
List<String> aliases = Arrays.stream(execute.aliases())
.map(placeholders::format)
.collect(Collectors.toList());

builder
.name(placeholders.format(execute.route()))
.aliases(aliases);

if (execute.required() > - 1) {
builder.amountValidator(validator -> validator.required(execute.required()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ExecutionResult resolveExecution(ContextOfResolving context) {
ContextOfResolving currentContext = context.resolverNestingTracing(this);

List<LiteComponent> filteredComponents = PandaStream.of(this.resolvers.values())
.filter(component -> component.getScope().getName().equalsIgnoreCase(command))
.filter(component -> component.getScope().equalsNameOrAliasIgnoreCase(command))
.toList();

if (filteredComponents.isEmpty()) {
Expand Down Expand Up @@ -80,6 +80,10 @@ public List<String> resolveCompletion(ContextOfResolving data) {
if (data.isLastResolver()) {
ArrayList<String> suggestions = new ArrayList<>(resolvers.keySet());

for (LiteComponent component : resolvers.values()) {
suggestions.addAll(component.getScope().getAliases());
}

if (suggestions.remove(StringUtils.EMPTY)) {
suggestions.addAll(resolvers.get(StringUtils.EMPTY).resolveCompletion(currentContextOfResolving));
}
Expand All @@ -94,6 +98,10 @@ public List<String> resolveCompletion(ContextOfResolving data) {
List<String> suggestions = new ArrayList<>(resolvers.keySet()); // suggestions (subcommands)
LiteComponent executor = resolvers.get(StringUtils.EMPTY);

for (LiteComponent component : resolvers.values()) {
suggestions.addAll(component.getScope().getAliases());
}

if (executor != null) {
suggestions.remove(StringUtils.EMPTY);
suggestions.addAll(executor.resolveCompletion(currentContextOfResolving)); // suggestions (arguments)
Expand All @@ -103,12 +111,12 @@ public List<String> resolveCompletion(ContextOfResolving data) {
}

// /command subcommand| litecomponent
for (Map.Entry<String, LiteComponent> entry : resolvers.entrySet()) {
if (!partCommand.equalsIgnoreCase(entry.getKey())) {
for (LiteComponent component : resolvers.values()) {
if (!component.getScope().equalsNameOrAliasIgnoreCase(partCommand)) {
continue;
}

return entry.getValue().resolveCompletion(currentContextOfResolving);
return component.resolveCompletion(currentContextOfResolving);
}

String[] arguments = data.invocation.arguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ public Set<String> getPermissionsExclude() {
return Collections.unmodifiableSet(permissionsExclude);
}

public boolean equalsNameOrAliasIgnoreCase(String text) {
if (name.equalsIgnoreCase(text)) {
return true;
}

for (String alias : aliases) {
if (alias.equalsIgnoreCase(text)) {
return true;
}
}

return false;
}

public static Builder builder() {
return new Builder();
}
Expand Down

0 comments on commit b0a4a0a

Please sign in to comment.