Skip to content

Commit

Permalink
Add AutoCompleter#filterToClosestInput()
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Mar 12, 2023
1 parent 3e2f8b2 commit 28e488f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ public interface AutoCompleter {
*/
List<String> complete(@NotNull CommandActor actor, @NotNull String buffer);

/**
* Sets whether should this auto-completer filter suggestions
* to include only the closest suggestions to the user input.
* <p>
* By default, this is true.
*
* @param filterToClosestInput Whether should suggestions be
* filtered to the closest input
*/
void filterToClosestInput(boolean filterToClosestInput);

/**
* Returns the containing {@link CommandHandler} of this auto completer.
* This will allow for writing fluent and readable code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ final class BaseAutoCompleter implements AutoCompleter {
private final BaseCommandHandler handler;
final Map<String, SuggestionProvider> suggestionKeys = new HashMap<>();
final List<SuggestionProviderFactory> factories = new ArrayList<>();
private boolean filterToClosestInput = true;

public BaseAutoCompleter(BaseCommandHandler handler) {
this.handler = handler;
Expand Down Expand Up @@ -148,6 +149,11 @@ public SuggestionProvider getProvider(CommandParameter parameter) {
return complete(actor, ArgumentStack.parseForAutoCompletion(buffer));
}

@Override
public void filterToClosestInput(boolean filterToClosestInput) {
this.filterToClosestInput = filterToClosestInput;
}

private ExecutableCommand searchForCommand(CommandPath path, CommandActor actor) {
ExecutableCommand found = handler.getCommand(path);
if (found != null && !found.isSecret() && found.getPermission().canExecute(actor)) return found;
Expand Down Expand Up @@ -223,7 +229,7 @@ private CommandCategory getLastCategory(CommandPath path) {
@NotNull private List<String> getParamCompletions(Collection<String> provider, ArgumentStack args) {
return provider
.stream()
.filter(c -> c.toLowerCase().startsWith(args.getLast().toLowerCase()))
.filter(c -> !filterToClosestInput || c.toLowerCase().startsWith(args.getLast().toLowerCase()))
.sorted(String.CASE_INSENSITIVE_ORDER)
.distinct()
.collect(Collectors.toList());
Expand Down

0 comments on commit 28e488f

Please sign in to comment.