Skip to content

Commit

Permalink
Auto-complete arguments only if they're starting with the current sub…
Browse files Browse the repository at this point in the history
…command or argument. (#35)
  • Loading branch information
Rollczi authored Jan 22, 2022
1 parent 056df8a commit 8265efb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.panda_lang.utilities.inject.Injector;
import panda.utilities.text.Formatter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
Expand All @@ -46,7 +47,7 @@ public class LiteCommandsBuilder {
private final LiteRegisterResolvers registerResolvers = new LiteRegisterResolvers();
private final Formatter placeholders = new Formatter();
private ExecutionResultHandler executionResultHandler;
private LitePlatformManager platformManager;
private LitePlatformManager<?> platformManager;
private Injector baseInjector = DependencyInjection.createInjector();
private Logger logger = Logger.getLogger("LiteCommands");

Expand Down Expand Up @@ -137,7 +138,7 @@ public LiteCommandsBuilder placeholders(Map<String, Supplier<String>> placeholde
return this;
}

public LiteCommandsBuilder platform(LitePlatformManager platformManager) {
public LiteCommandsBuilder platform(LitePlatformManager<?> platformManager) {
this.platformManager = platformManager;
return this;
}
Expand Down Expand Up @@ -245,7 +246,25 @@ private DefaultSuggester(LiteComponent resolver) {

@Override
public List<String> suggest(LiteInvocation invocation) {
return resolver.resolveCompletion(LiteComponent.ContextOfResolving.create(invocation));
List<String> completions = resolver.resolveCompletion(LiteComponent.ContextOfResolving.create(invocation));
String[] arguments = invocation.arguments();

if (arguments.length == 0) {
return completions;
}

String argument = arguments[arguments.length - 1];
List<String> completionsStartsWith = new ArrayList<>();

for (String completion : completions) {
if (!completion.startsWith(argument)) {
continue;
}

completionsStartsWith.add(completion);
}

return completionsStartsWith;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Option<LiteExecution> createExecution(Object instance, Method executionMe
MethodExecutor methodExecutor = new MethodExecutor(executionMethod, instance, injector, parser);

return parser.parse(executionMethod)
.map((scope) -> new LiteExecution(logger, parser, scope, methodExecutor));
.map((scope) -> new LiteExecution(logger, scope, methodExecutor));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.rollczi.litecommands.component;

import dev.rollczi.litecommands.annotations.parser.AnnotationParser;
import dev.rollczi.litecommands.scope.ScopeMetaData;
import dev.rollczi.litecommands.valid.ValidationCommandException;
import dev.rollczi.litecommands.valid.ValidationInfo;
Expand All @@ -16,13 +15,11 @@
public final class LiteExecution extends AbstractComponent {

private final Logger logger;
private final AnnotationParser parser;
private final MethodExecutor executor;

LiteExecution(Logger logger, AnnotationParser parser, ScopeMetaData scopeMetaData, MethodExecutor executor) {
LiteExecution(Logger logger, ScopeMetaData scopeMetaData, MethodExecutor executor) {
super(scopeMetaData);
this.logger = logger;
this.parser = parser;
this.executor = executor;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.rollczi.litecommands.component;

import dev.rollczi.litecommands.LiteInvocation;
import dev.rollczi.litecommands.scope.ScopeMetaData;
import dev.rollczi.litecommands.valid.ValidationInfo;
import panda.std.Option;
Expand Down Expand Up @@ -77,6 +76,7 @@ public ExecutionResult resolveExecution(ContextOfResolving context) {
public List<String> resolveCompletion(ContextOfResolving data) {
ContextOfResolving currentContextOfResolving = data.resolverNestingTracing(this);

// /command subcommand|
if (data.isLastResolver()) {
ArrayList<String> suggestions = new ArrayList<>(resolvers.keySet());

Expand All @@ -89,18 +89,20 @@ public List<String> resolveCompletion(ContextOfResolving data) {

String partCommand = data.getNextPredictedPartOfSuggestion();

// /command subcommand |litecomponent
if (partCommand.isEmpty()) {
List<String> suggestions = new ArrayList<>(resolvers.keySet());
List<String> suggestions = new ArrayList<>(resolvers.keySet()); // suggestions (subcommands)
LiteComponent executor = resolvers.get(StringUtils.EMPTY);
suggestions.remove(StringUtils.EMPTY);

if (executor != null) {
suggestions.addAll(executor.resolveCompletion(currentContextOfResolving));
suggestions.remove(StringUtils.EMPTY);
suggestions.addAll(executor.resolveCompletion(currentContextOfResolving)); // suggestions (arguments)
}

return suggestions;
}

// /command subcommand| litecomponent
for (Map.Entry<String, LiteComponent> entry : resolvers.entrySet()) {
if (!partCommand.equalsIgnoreCase(entry.getKey())) {
continue;
Expand All @@ -112,11 +114,12 @@ public List<String> resolveCompletion(ContextOfResolving data) {
String[] arguments = data.invocation.arguments();
LiteComponent component = resolvers.get(StringUtils.EMPTY);

if (component != null && arguments.length != 0 && component instanceof LiteExecution) {
// /command subcommand argument |[...]
if (component instanceof LiteExecution && arguments.length != 0) {
LiteExecution liteExecution = (LiteExecution) component;
LiteInvocation invocation = data.getInvocation();
List<String> oldSuggestions = liteExecution.generateCompletion(currentContextOfResolving.getCurrentArgsCount(this) - 1, currentContextOfResolving);

// /command subcommand argument |argument
if (oldSuggestions.contains(arguments[arguments.length - 2])) {
return component.resolveCompletion(currentContextOfResolving);
}
Expand Down

0 comments on commit 8265efb

Please sign in to comment.