Skip to content

Commit 28e488f

Browse files
committed
Add AutoCompleter#filterToClosestInput()
1 parent 3e2f8b2 commit 28e488f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

common/src/main/java/revxrsal/commands/autocomplete/AutoCompleter.java

+11
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ public interface AutoCompleter {
143143
*/
144144
List<String> complete(@NotNull CommandActor actor, @NotNull String buffer);
145145

146+
/**
147+
* Sets whether should this auto-completer filter suggestions
148+
* to include only the closest suggestions to the user input.
149+
* <p>
150+
* By default, this is true.
151+
*
152+
* @param filterToClosestInput Whether should suggestions be
153+
* filtered to the closest input
154+
*/
155+
void filterToClosestInput(boolean filterToClosestInput);
156+
146157
/**
147158
* Returns the containing {@link CommandHandler} of this auto completer.
148159
* This will allow for writing fluent and readable code.

common/src/main/java/revxrsal/commands/core/BaseAutoCompleter.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ final class BaseAutoCompleter implements AutoCompleter {
4646
private final BaseCommandHandler handler;
4747
final Map<String, SuggestionProvider> suggestionKeys = new HashMap<>();
4848
final List<SuggestionProviderFactory> factories = new ArrayList<>();
49+
private boolean filterToClosestInput = true;
4950

5051
public BaseAutoCompleter(BaseCommandHandler handler) {
5152
this.handler = handler;
@@ -148,6 +149,11 @@ public SuggestionProvider getProvider(CommandParameter parameter) {
148149
return complete(actor, ArgumentStack.parseForAutoCompletion(buffer));
149150
}
150151

152+
@Override
153+
public void filterToClosestInput(boolean filterToClosestInput) {
154+
this.filterToClosestInput = filterToClosestInput;
155+
}
156+
151157
private ExecutableCommand searchForCommand(CommandPath path, CommandActor actor) {
152158
ExecutableCommand found = handler.getCommand(path);
153159
if (found != null && !found.isSecret() && found.getPermission().canExecute(actor)) return found;
@@ -223,7 +229,7 @@ private CommandCategory getLastCategory(CommandPath path) {
223229
@NotNull private List<String> getParamCompletions(Collection<String> provider, ArgumentStack args) {
224230
return provider
225231
.stream()
226-
.filter(c -> c.toLowerCase().startsWith(args.getLast().toLowerCase()))
232+
.filter(c -> !filterToClosestInput || c.toLowerCase().startsWith(args.getLast().toLowerCase()))
227233
.sorted(String.CASE_INSENSITIVE_ORDER)
228234
.distinct()
229235
.collect(Collectors.toList());

0 commit comments

Comments
 (0)