|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: derklaro < [email protected]> |
| 3 | +Date: Wed, 2 Nov 2022 15:03:51 +0100 |
| 4 | +Subject: [PATCH] fix invalid suggestions caused by dynamic arguments |
| 5 | + |
| 6 | + |
| 7 | +diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java |
| 8 | +index 668239c78e25c999c6fbc9c32251b794e09cd80e..f4b95ea61e77ac0da7abed7f25588328e27238f0 100644 |
| 9 | +--- a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java |
| 10 | ++++ b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java |
| 11 | +@@ -702,9 +702,25 @@ public final class CommandTree<C> { |
| 12 | + // START: Parsing |
| 13 | + commandContext.setCurrentArgument(child.getValue()); |
| 14 | + final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue); |
| 15 | +- if (result.getParsedValue().isPresent() && !commandQueue.isEmpty()) { |
| 16 | +- commandContext.store(child.getValue().getName(), result.getParsedValue().get()); |
| 17 | +- return this.getSuggestions(commandContext, commandQueue, child); |
| 18 | ++ // CloudNet start - fix suggestions for dynamic arguments |
| 19 | ++ final Optional<?> parsedValue = result.getParsedValue(); |
| 20 | ++ final boolean parseSuccess = parsedValue.isPresent(); |
| 21 | ++ |
| 22 | ++ if ((parseSuccess && !commandQueue.isEmpty()) || (!parseSuccess && commandQueue.size() > 1)) { |
| 23 | ++ if (parseSuccess) { |
| 24 | ++ // the current argument at the position is parsable and there are more arguments following |
| 25 | ++ commandContext.store(child.getValue().getName(), parsedValue.get()); |
| 26 | ++ return this.getSuggestions(commandContext, commandQueue, child); |
| 27 | ++ } else { |
| 28 | ++ // at this point there should normally be no need to reset the command queue as we expect |
| 29 | ++ // users to only take out an argument if the parse succeeded. Just to be sure we do it anyway |
| 30 | ++ commandQueue.clear(); |
| 31 | ++ commandQueue.addAll(commandQueueOriginal); |
| 32 | ++ // there are more arguments following but the current argument isn't matching - there |
| 33 | ++ // is no need to collect any further suggestions |
| 34 | ++ return Collections.emptyList(); |
| 35 | ++ } |
| 36 | ++ // CloudNet end |
| 37 | + } |
| 38 | + // END: Parsing |
| 39 | + } |
| 40 | +@@ -713,6 +729,15 @@ public final class CommandTree<C> { |
| 41 | + commandQueue.clear(); |
| 42 | + commandQueue.addAll(commandQueueOriginal); |
| 43 | + |
| 44 | ++ // CloudNet start - fix suggestions for dynamic arguments |
| 45 | ++ if (!preParseSuccess && commandQueue.size() > 1) { |
| 46 | ++ // The preprocessor denied the argument, and there are more arguments following the current one |
| 47 | ++ // Therefore we shouldn't list the suggestions of the current argument, as clearly the suggestions of |
| 48 | ++ // one of the following arguments is requested |
| 49 | ++ return Collections.emptyList(); |
| 50 | ++ } |
| 51 | ++ // CloudNet end |
| 52 | ++ |
| 53 | + // Fallback: use suggestion provider of argument |
| 54 | + commandContext.setCurrentArgument(child.getValue()); |
| 55 | + return child.getValue().getSuggestionsProvider().apply(commandContext, this.stringOrEmpty(commandQueue.peek())); |
0 commit comments