Skip to content

Commit

Permalink
fix: filter suggestions properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Dec 31, 2023
1 parent d2be0bc commit 4b002c3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import cloud.commandframework.exceptions.InvalidSyntaxException;
import cloud.commandframework.exceptions.NoPermissionException;
import cloud.commandframework.exceptions.NoSuchCommandException;
import cloud.commandframework.execution.FilteringCommandSuggestionProcessor;
import cloud.commandframework.keys.CloudKey;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -87,6 +88,9 @@ public SpringCommandManager(
this.commandSenderMapper = commandSenderMapper;
this.suggestionFactory = super.suggestionFactory().mapped(CloudCompletionProposal::fromSuggestion);
this.parameterInjectorRegistry().registerInjectionService(new SpringInjectionService<>(applicationContext));
this.commandSuggestionProcessor(new FilteringCommandSuggestionProcessor<>(
FilteringCommandSuggestionProcessor.Filter.<C>startsWith(true).andTrimBeforeLastSpace()
));

this.registerDefaultExceptionHandlers();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import cloud.commandframework.Command;
import cloud.commandframework.CommandBean;
import cloud.commandframework.CommandProperties;
import cloud.commandframework.arguments.aggregate.AggregateCommandParser;
import cloud.commandframework.arguments.flags.CommandFlag;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.suggestion.SuggestionProvider;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.meta.CommandMeta;
Expand All @@ -42,6 +44,7 @@
import org.springframework.stereotype.Component;

import static cloud.commandframework.CommandDescription.commandDescription;
import static cloud.commandframework.arguments.standard.IntegerParser.integerParser;
import static cloud.commandframework.arguments.standard.StringParser.stringParser;

@Component
Expand Down Expand Up @@ -74,21 +77,28 @@ public AddCatCommand(final @NonNull CatService catService) {
protected Command.@NonNull Builder<SpringCommandSender> configure(
final Command.@NonNull Builder<SpringCommandSender> builder
) {
return builder.literal("add")
.required("name", stringParser(), SuggestionProvider.blocking((ctx, in) -> List.of(
final AggregateCommandParser<SpringCommandSender, Cat> catParser = AggregateCommandParser.<SpringCommandSender>builder()
.withComponent("name", stringParser(), SuggestionProvider.blocking((ctx, in) -> List.of(
CloudCompletionProposal.of("Missy").displayText("Missy (A cute cat name)"),
CloudCompletionProposal.of("Donald").displayText("Donald (Old man name = CUTE!)"),
CloudCompletionProposal.of("Fluffy").displayText("Fluffy (A classic :))")
)))
.withComponent("age", integerParser(0))
.withDirectMapper(Cat.class, (cmxCtx, ctx) -> ArgumentParseResult.success(new Cat(ctx.get("name"),
ctx.get("age"))))
.build();

return builder.literal("add")
.required("cat", catParser)
.flag(CommandFlag.builder("override"))
.commandDescription(commandDescription("Add a cat"));
}

@Override
public void execute(final @NonNull CommandContext<SpringCommandSender> commandContext) {
final String name = commandContext.get("name");
final Cat cat = commandContext.get("cat");
final boolean override = commandContext.flags().hasFlag("override");
final Cat cat = this.catService.addCat(name, override);
this.catService.addCat(cat, override);

// We can either write the output to the context:
commandContext.set(SpringCommandManager.OUTPUT, String.format("Added cat: %s", cat.name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ public ListCatCommand(final @NonNull CatService catService) {
@CommandMethod("cat list")
public void listCats(final @NonNull SpringCommandSender sender) {
sender.writeLine("Cats");
this.catService.cats().forEach(cat -> sender.writeLine(String.format("- %s", cat.name())));
this.catService.cats().forEach(cat -> sender.writeLine(String.format("- %s (Age: %d)", cat.name(), cat.age())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
//
package org.incendo.cloud.spring.example.model;

import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;

/**
* A kitty.
*
* @param name the name of the cat
* @param age the age of the cat
*/
public record Cat(@NonNull String name) {
public record Cat(@NonNull String name, @NonNegative int age) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,14 @@ public CatService(final @NonNull CatRepository catRepository) {
/**
* Adds a cat with the given {@code name}.
*
* @param name the cat
* @param cat the cat
* @param override whether to override an existing cat
* @return the cat
*/
public Cat addCat(final @NonNull String name, final boolean override) {
final Cat cat = new Cat(name);
public void addCat(final @NonNull Cat cat, final boolean override) {
if (override) {
this.removeCat(name);
this.removeCat(cat.name());
}
this.catRepository.addCat(cat);
return cat;
}

/**
Expand Down

0 comments on commit 4b002c3

Please sign in to comment.