perchance fix suggestions not showing when the literal is valid
Some checks failed
/ deploy (push) Has been cancelled
/ test (push) Has been cancelled

This commit is contained in:
kento2 2026-08-30 12:10:14 +02:00
parent 21c1bf72c6
commit c22f707556
4 changed files with 20 additions and 12 deletions

View file

@ -2,7 +2,6 @@ package site.lab0x13.scrow.commands;
import site.lab0x13.scrow.commands.model.CommandContext;
import site.lab0x13.scrow.commands.model.literal.LiteralMeta;
import site.lab0x13.scrow.commands.model.literal.MessageStyle;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
import site.lab0x13.scrow.commands.parser.NodeTreeWalker;
import site.lab0x13.scrow.commands.parser.WalkResult;
@ -45,23 +44,18 @@ public class CommandHandler<C extends CommandContext<C>> {
String input = null;
switch (walkResult) {
case WalkResult.LiteralUnknown<C> res -> {
stream = res.lastLiteral().subLiteralMetas().stream()
.filter(literalMeta -> platformAdapter.checkPermission(ctx, literalMeta.permission()))
.map(LiteralMeta::names)
.flatMap(Collection::stream);
stream = filterMetas(ctx, res.lastLiteral().subLiteralMetas());
input = res.input();
}
case WalkResult.LiteralExpected<C> res when trailingSpace ->
stream = res.lastLiteral().subLiteralMetas().stream()
.filter(literalMeta -> platformAdapter.checkPermission(ctx, literalMeta.permission()))
.map(LiteralMeta::names)
.flatMap(Collection::stream);
stream = filterMetas(ctx, res.lastLiteral().subLiteralMetas());
case WalkResult.ArgumentIllegal<C> res -> {
stream = res.arg().suggestionProvider().suggest(ctx, res.input()).stream();
input = res.input();
}
case WalkResult.ArgumentExpected<C> res when trailingSpace ->
stream = res.arg().suggestionProvider().suggest(ctx, "").stream();
case WalkResult.Complete<C> res when !trailingSpace -> stream = filterMetas(ctx, res.alternativeLiterals());
default -> {
return Collections.emptyList();
}
@ -73,4 +67,11 @@ public class CommandHandler<C extends CommandContext<C>> {
.filter(s -> s.startsWith(finalInput))
.toList();
}
private Stream<String> filterMetas(C ctx, List<? extends LiteralMeta> metas) {
return metas.stream()
.filter(literalMeta -> platformAdapter.checkPermission(ctx, literalMeta.permission()))
.map(LiteralMeta::names)
.flatMap(Collection::stream);
}
}

View file

@ -7,6 +7,7 @@ import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
import java.util.Collections;
import java.util.Iterator;
import static java.util.Objects.requireNonNull;
@ -25,6 +26,7 @@ public class NodeTreeWalker<C extends CommandContext<C>> {
Iterator<String> iter
) {
var cursor = new Cursor(iter);
Literal<C> previousLiteral = null;
Literal<C> literal = rootLiteral;
while (true) {
@ -41,6 +43,7 @@ public class NodeTreeWalker<C extends CommandContext<C>> {
var nextLiteral = literal.getSubLiteral(literalName);
if (nextLiteral == null || !platformAdapter.checkPermission(ctx, nextLiteral.permission()))
return new WalkResult.LiteralUnknown<>(ctx, literal, literalName);
previousLiteral = literal;
literal = nextLiteral;
}
@ -49,7 +52,7 @@ public class NodeTreeWalker<C extends CommandContext<C>> {
return new WalkResult.LiteralExpected<>(ctx, literal, isOptional);
}
return new WalkResult.Complete<>(ctx, literal);
return new WalkResult.Complete<>(ctx, literal, previousLiteral == null ? Collections.emptyList() : previousLiteral.subLiteralMetas());
}
private @Nullable WalkResult<C> walkArgument(

View file

@ -4,6 +4,9 @@ import org.jetbrains.annotations.Nullable;
import site.lab0x13.scrow.commands.model.CommandContext;
import site.lab0x13.scrow.commands.model.argument.Argument;
import site.lab0x13.scrow.commands.model.literal.Literal;
import site.lab0x13.scrow.commands.model.literal.LiteralMeta;
import java.util.List;
public sealed interface WalkResult<C extends CommandContext<C>> {
C ctx();
@ -65,7 +68,8 @@ public sealed interface WalkResult<C extends CommandContext<C>> {
record Complete<C extends CommandContext<C>>(
C ctx,
Literal<C> lastLiteral
Literal<C> lastLiteral,
List<? extends LiteralMeta> alternativeLiterals
) implements WalkResult<C> {
@Override
public boolean isValidUsage() {