From 8cac4b6ea21fa90e1d0d7304b21adc98d3e0e6c5 Mon Sep 17 00:00:00 2001 From: FixedDev Date: Tue, 24 Aug 2021 16:03:02 -0500 Subject: [PATCH 1/4] Added switch and value flag suggestions --- .../examples/basic/SwitchArguments.java | 2 ++ .../examples/basic/ValueFlagArguments.java | 4 +++ .../part/defaults/SequentialCommandPart.java | 36 +++++++++++++++++-- .../commandflow/part/defaults/SwitchPart.java | 36 ++++++++++--------- .../part/defaults/ValueFlagPart.java | 32 +++++++++++++++++ 5 files changed, 92 insertions(+), 18 deletions(-) diff --git a/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java b/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java index 5b12e682..4f271479 100644 --- a/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java +++ b/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java @@ -27,6 +27,8 @@ public static void main(String[] args) { commandManager.execute(namespace, "test -g Fixed"); // Prints Goodbye Fixed commandManager.execute(namespace, "test Fixed -g"); // Prints Goodbye Fixed // due to missing arguments. + + System.out.println(String.join(",", commandManager.getSuggestions(namespace, "test -g"))); } private static CommandManager create() { diff --git a/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/ValueFlagArguments.java b/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/ValueFlagArguments.java index a6c149f6..e304f844 100644 --- a/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/ValueFlagArguments.java +++ b/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/ValueFlagArguments.java @@ -26,11 +26,15 @@ public static void main(String[] args) { commandManager.execute(namespace, "test Fixed"); // Prints Hi Fixed commandManager.execute(namespace, "test Fixed -g GoodBye"); // Prints GoodBye Fixed commandManager.execute(namespace, "test Fixed -g Hello"); // Prints Hello Fixed + + System.out.println(String.join(",", commandManager.getSuggestions(namespace, "test -g"))); + commandManager.execute(namespace, "test -g Fixed"); // Throws a NoMoreArguments exception, meaning that the parsing failed // because the Fixed argument was taken as the value for the flag and no argument // is remaining for the name. commandManager.execute(namespace, "test Fixed -g"); // Throws a NoMoreArguments exception, meaning that the parsing failed // because the flag doesn't has any argument left to use. + } private static CommandManager create() { diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java index 32962d31..49ca06de 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java @@ -6,14 +6,20 @@ import me.fixeddev.commandflow.part.PartsWrapper; import me.fixeddev.commandflow.part.visitor.CommandPartVisitor; import me.fixeddev.commandflow.stack.ArgumentStack; +import me.fixeddev.commandflow.stack.StackSnapshot; import net.kyori.text.Component; import net.kyori.text.TextComponent; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Objects; +import java.util.Set; public class SequentialCommandPart implements CommandPart, PartsWrapper { @@ -69,14 +75,40 @@ public void parse(CommandContext context, ArgumentStack stack, CommandPart calle @Override public List getSuggestions(CommandContext context, ArgumentStack stack) { - Iterator partIterator = parts.iterator(); + List flagParts = new LinkedList<>(); - while (partIterator.hasNext()){ + while (partIterator.hasNext()) { + String nextString = stack.hasNext() ? stack.peek() : ""; CommandPart part = partIterator.next(); + boolean nextCanBeFlag = nextString.startsWith("-"); + + if (part instanceof SwitchPart || part instanceof ValueFlagPart) { + flagParts.add(part); + + if (!nextCanBeFlag) { + continue; + } + } List suggestions = part.getSuggestions(context, stack); + if (nextString.startsWith("-")) { + StackSnapshot snapshot = stack.getSnapshot(); + boolean modified = false; + + for (CommandPart flagPart : flagParts) { + if (suggestions.addAll(flagPart.getSuggestions(context, stack))) { + modified = true; + } + } + + if (!modified) { + stack.applySnapshot(snapshot); + } + } + + if (!suggestions.isEmpty() && !stack.hasNext()) { return suggestions; } diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java index 0c6b9ecc..9adcb546 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java @@ -9,6 +9,7 @@ import net.kyori.text.TextComponent; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -85,28 +86,31 @@ public void parse(CommandContext context, ArgumentStack stack, CommandPart paren @Override public List getSuggestions(CommandContext commandContext, ArgumentStack stack) { - StackSnapshot snapshot = stack.getSnapshot(); + String nextArgument = stack.hasNext() ? stack.next() : ""; - while (stack.hasNext()) { - String arg = stack.next(); + List suggestions = new ArrayList<>(); - if (!arg.startsWith("-")) { - continue; - } + if (!nextArgument.startsWith("-")) { + return suggestions; + } - if (arg.equals("--" + name) && allowFullName) { - stack.remove(); - break; - } + nextArgument = nextArgument.lastIndexOf('-') != 0 ? nextArgument.substring(2) : nextArgument.substring(1); - if (arg.equals("-" + shortName)) { - stack.remove(); - break; - } + if (allowFullName && name.startsWith(nextArgument)) { + suggestions.add("--" + name); } - stack.applySnapshot(snapshot, false); + if (shortName.startsWith(nextArgument)) { + suggestions.add("-" + shortName); + } + + if(nextArgument.equals(shortName) || nextArgument.equals(name)){ + suggestions.clear(); - return Collections.emptyList(); + return suggestions; + } + + return suggestions; } + } diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/ValueFlagPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/ValueFlagPart.java index a1b53389..688b5902 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/ValueFlagPart.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/ValueFlagPart.java @@ -11,6 +11,9 @@ import net.kyori.text.TextComponent; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.List; + public class ValueFlagPart implements SinglePartWrapper { private final CommandPart part; @@ -91,6 +94,35 @@ public void parse(CommandContext context, ArgumentStack stack, CommandPart paren stack.applySnapshot(snapshot, false); } + @Override + public List getSuggestions(CommandContext commandContext, ArgumentStack stack) { + String nextArgument = stack.hasNext() ? stack.next() : ""; + + List suggestions = new ArrayList<>(); + + if (!nextArgument.startsWith("-")) { + return suggestions; + } + + nextArgument = nextArgument.lastIndexOf('-') != 0 ? nextArgument.substring(2) : nextArgument.substring(1); + + if (allowFullName&& name.startsWith(nextArgument)) { + suggestions.add("--" + name); + } + + if (shortName.startsWith(nextArgument)) { + suggestions.add("-" + shortName); + } + + if (nextArgument.equals(shortName) || nextArgument.equals(name)) { + suggestions.clear(); + + suggestions.addAll(part.getSuggestions(commandContext, stack)); + } + + return suggestions; + } + private boolean parseValueFlag(CommandContext context, ArgumentStack stack) { StackSnapshot beforeRemoveFlagStack = stack.getSnapshot(); ContextSnapshot beforeParseContext = context.getSnapshot(); From 309b7a6470776b4c591fb9d00217025f9581f472 Mon Sep 17 00:00:00 2001 From: FixedDev Date: Tue, 24 Aug 2021 16:16:34 -0500 Subject: [PATCH 2/4] Whoops, forgot to remove unused imports --- .../commandflow/part/defaults/SequentialCommandPart.java | 5 ----- .../me/fixeddev/commandflow/part/defaults/SwitchPart.java | 1 - 2 files changed, 6 deletions(-) diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java index 49ca06de..c6e6f2ae 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java @@ -4,22 +4,17 @@ import me.fixeddev.commandflow.exception.ArgumentParseException; import me.fixeddev.commandflow.part.CommandPart; import me.fixeddev.commandflow.part.PartsWrapper; -import me.fixeddev.commandflow.part.visitor.CommandPartVisitor; import me.fixeddev.commandflow.stack.ArgumentStack; import me.fixeddev.commandflow.stack.StackSnapshot; import net.kyori.text.Component; import net.kyori.text.TextComponent; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Objects; -import java.util.Set; public class SequentialCommandPart implements CommandPart, PartsWrapper { diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java index 9adcb546..39b1bd35 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SwitchPart.java @@ -10,7 +10,6 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Collections; import java.util.List; public class SwitchPart implements CommandPart { From 837fd75c87ad5d8b086541a867c4ad644d99b23e Mon Sep 17 00:00:00 2001 From: Gilberto Garcia Date: Fri, 10 Sep 2021 19:38:53 -0500 Subject: [PATCH 3/4] Use the already checked condition --- .../commandflow/part/defaults/SequentialCommandPart.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java index c6e6f2ae..12890fbd 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/SequentialCommandPart.java @@ -88,7 +88,7 @@ public List getSuggestions(CommandContext context, ArgumentStack stack) List suggestions = part.getSuggestions(context, stack); - if (nextString.startsWith("-")) { + if (nextCanBeFlag) { StackSnapshot snapshot = stack.getSnapshot(); boolean modified = false; From 37529f30a1ff05271a308203f3214fa5a90c1674 Mon Sep 17 00:00:00 2001 From: Gilberto Garcia Date: Fri, 10 Sep 2021 19:54:09 -0500 Subject: [PATCH 4/4] Actually demostrate the use of suggestions If we use "-g" directly then the only suggestion available is nothing, so only use "-" --- .../me/fixeddev/commandflow/examples/basic/SwitchArguments.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java b/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java index 4f271479..4239035e 100644 --- a/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java +++ b/Examples/src/main/java/me/fixeddev/commandflow/examples/basic/SwitchArguments.java @@ -28,7 +28,7 @@ public static void main(String[] args) { commandManager.execute(namespace, "test Fixed -g"); // Prints Goodbye Fixed // due to missing arguments. - System.out.println(String.join(",", commandManager.getSuggestions(namespace, "test -g"))); + System.out.println(String.join(",", commandManager.getSuggestions(namespace, "test -"))); } private static CommandManager create() {