Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added switch and value flag suggestions #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 -")));
}

private static CommandManager create() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
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.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -69,14 +70,40 @@ public void parse(CommandContext context, ArgumentStack stack, CommandPart calle

@Override
public List<String> getSuggestions(CommandContext context, ArgumentStack stack) {

Iterator<CommandPart> partIterator = parts.iterator();
List<CommandPart> 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) {
FixedDev marked this conversation as resolved.
Show resolved Hide resolved
flagParts.add(part);

if (!nextCanBeFlag) {
continue;
}
}

List<String> suggestions = part.getSuggestions(context, stack);

if (nextCanBeFlag) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.kyori.text.TextComponent;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

public class SwitchPart implements CommandPart {
Expand Down Expand Up @@ -85,28 +85,31 @@ public void parse(CommandContext context, ArgumentStack stack, CommandPart paren

@Override
public List<String> getSuggestions(CommandContext commandContext, ArgumentStack stack) {
StackSnapshot snapshot = stack.getSnapshot();
String nextArgument = stack.hasNext() ? stack.next() : "";

while (stack.hasNext()) {
String arg = stack.next();
List<String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,6 +94,35 @@ public void parse(CommandContext context, ArgumentStack stack, CommandPart paren
stack.applySnapshot(snapshot, false);
}

@Override
public List<String> getSuggestions(CommandContext commandContext, ArgumentStack stack) {
String nextArgument = stack.hasNext() ? stack.next() : "";

List<String> 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();
Expand Down