From 42c34c56a0caf60f5c707e52fba27139ccf8d189 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 19 Dec 2015 12:20:52 +0100 Subject: [PATCH] Fix command formatting after refactor - Fix spacing issues (too few or too many spaces) - Handle base commands with invalid arguments properly - Add highlighting function for command syntax --- .../xephi/authme/command/CommandHandler.java | 20 ++++--- .../command/help/CommandSyntaxHelper.java | 36 +++++++++++++ .../authme/command/help/HelpProvider.java | 54 ++++--------------- 3 files changed, 57 insertions(+), 53 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/command/help/CommandSyntaxHelper.java diff --git a/src/main/java/fr/xephi/authme/command/CommandHandler.java b/src/main/java/fr/xephi/authme/command/CommandHandler.java index 09ccbd3c..78a8e78b 100644 --- a/src/main/java/fr/xephi/authme/command/CommandHandler.java +++ b/src/main/java/fr/xephi/authme/command/CommandHandler.java @@ -189,17 +189,21 @@ public class CommandHandler { } private FoundCommandResult getCommandWithSmallestDifference(CommandDescription base, List parts) { - final String childLabel = parts.size() >= 2 ? parts.get(1) : null; + // Return the base command with incorrect arg count error if we only have one part + if (parts.size() <= 1) { + return new FoundCommandResult( + base, parts, new ArrayList(), 0.0, FoundResultStatus.INCORRECT_ARGUMENTS); + } + + final String childLabel = parts.get(1); double minDifference = Double.POSITIVE_INFINITY; CommandDescription closestCommand = null; - if (childLabel != null) { - for (CommandDescription child : base.getChildren()) { - double difference = getLabelDifference(child, childLabel); - if (difference < minDifference) { - minDifference = difference; - closestCommand = child; - } + for (CommandDescription child : base.getChildren()) { + double difference = getLabelDifference(child, childLabel); + if (difference < minDifference) { + minDifference = difference; + closestCommand = child; } } diff --git a/src/main/java/fr/xephi/authme/command/help/CommandSyntaxHelper.java b/src/main/java/fr/xephi/authme/command/help/CommandSyntaxHelper.java new file mode 100644 index 00000000..40bb2185 --- /dev/null +++ b/src/main/java/fr/xephi/authme/command/help/CommandSyntaxHelper.java @@ -0,0 +1,36 @@ +package fr.xephi.authme.command.help; + +import fr.xephi.authme.command.CommandArgumentDescription; +import fr.xephi.authme.command.CommandDescription; +import org.bukkit.ChatColor; + +import java.util.List; + +/** + * Helper class for displaying the syntax of a command properly to a user. + */ +class CommandSyntaxHelper { + + private CommandSyntaxHelper() { + } + + public static String getSyntax(CommandDescription command, List correctLabels) { + String commandSyntax = ChatColor.WHITE + "/" + correctLabels.get(0) + ChatColor.YELLOW; + for (int i = 1; i < correctLabels.size(); ++i) { + commandSyntax += " " + correctLabels.get(i); + } + for (CommandArgumentDescription argument : command.getArguments()) { + commandSyntax += " " + formatArgument(argument); + } + return commandSyntax; + } + + /** Format a command argument with the proper type of brackets. */ + private static String formatArgument(CommandArgumentDescription argument) { + if (argument.isOptional()) { + return "[" + argument.getName() + "]"; + } + return "<" + argument.getName() + ">"; + } + +} diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index 73fd957a..37bbb9e5 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -12,15 +12,13 @@ import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.util.CollectionUtils; -import fr.xephi.authme.util.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.List; +import static java.util.Arrays.asList; import static java.util.Collections.singletonList; /** @@ -67,7 +65,7 @@ public final class HelpProvider { List correctLabels = ImmutableList.copyOf(filterCorrectLabels(command, labels)); if (!hasFlag(HIDE_COMMAND, options)) { - printCommand(command, correctLabels, lines); + lines.add(ChatColor.GOLD + "Command: " + CommandSyntaxHelper.getSyntax(command, correctLabels)); } if (hasFlag(SHOW_LONG_DESCRIPTION, options)) { printDetailedDescription(command, lines); @@ -88,15 +86,6 @@ public final class HelpProvider { return lines; } - private static void printCommand(CommandDescription command, List correctLabels, List lines) { - // FIXME: Create highlight logic to mark arguments and the 2nd label as yellow - String syntaxLine = "/" + CommandUtils.labelsToString(correctLabels); - for (CommandArgumentDescription argument : command.getArguments()) { - syntaxLine += " " + formatArgument(argument); - } - lines.add(syntaxLine); - } - private static void printDetailedDescription(CommandDescription command, List lines) { lines.add(ChatColor.GOLD + "Short Description: " + ChatColor.WHITE + command.getDescription()); lines.add(ChatColor.GOLD + "Detailed Description:"); @@ -104,7 +93,7 @@ public final class HelpProvider { } private static void printArguments(CommandDescription command, List lines) { - if (!command.getArguments().isEmpty()) { + if (command.getArguments().isEmpty()) { return; } @@ -122,39 +111,22 @@ public final class HelpProvider { } private static void printAlternatives(CommandDescription command, List correctLabels, List lines) { + // TODO ljacqu 20151219: Need to show alternatives for base labels too? E.g. /r for /register if (command.getLabels().size() <= 1) { return; } lines.add(ChatColor.GOLD + "Alternatives:"); // Get the label used - final String usedLabel = correctLabels.get(correctLabels.size() - 1); + final String parentLabel = correctLabels.get(0); + final String childLabel = correctLabels.get(1); // Create a list of alternatives - List alternatives = new ArrayList<>(); for (String entry : command.getLabels()) { - if (!entry.equalsIgnoreCase(usedLabel)) { - alternatives.add(entry); + if (!entry.equalsIgnoreCase(childLabel)) { + lines.add(" " + CommandSyntaxHelper.getSyntax(command, asList(parentLabel, entry))); } } - - // Sort the alternatives - Collections.sort(alternatives, new Comparator() { - // TODO ljacqu 20151212: This computes the difference each time anew. It might make sense to compute the - // difference once and to store it in some map-like structure (Guava has some interesting ones) - @Override - public int compare(String o1, String o2) { - return Double.compare(StringUtils.getDifference(usedLabel, o1), - StringUtils.getDifference(usedLabel, o2)); - } - }); - - // Print each alternative with proper syntax - for (String alternative : alternatives) { - // fixme add highlight functionality (see commented old line) - // sender.sendMessage(" " + _HelpSyntaxHelper.getCommandSyntax(command, commandReference, alternative, true)); - lines.add(" " + CommandUtils.labelsToString(correctLabels) + " " + alternative); - } } private static void printPermissions(CommandDescription command, CommandSender sender, @@ -199,19 +171,11 @@ public final class HelpProvider { lines.add(ChatColor.GOLD + "Commands:"); String parentCommandPath = CommandUtils.labelsToString(parentLabels); for (CommandDescription child : command.getChildren()) { - lines.add(" " + parentCommandPath + child.getLabels().get(0) + lines.add(" /" + parentCommandPath + " " + child.getLabels().get(0) + ChatColor.GRAY + ChatColor.ITALIC + ": " + child.getDescription()); } } - /** Format a command argument with the proper type of brackets. */ - private static String formatArgument(CommandArgumentDescription argument) { - if (argument.isOptional()) { - return "[" + argument.getName() + "]"; - } - return "<" + argument.getName() + ">"; - } - private static boolean hasFlag(int flag, int options) { return (flag & options) != 0; }