Fixes various tab completion problems

This commit is contained in:
Kristian Knarvik 2022-02-27 14:28:46 +01:00
parent 91d477b45a
commit 3b7268d2ff
8 changed files with 52 additions and 38 deletions

View File

@ -26,7 +26,7 @@ public class AddCommand extends TokenizedCommand {
return false; return false;
} }
String signName = arguments.get(0); String signName = arguments.get(0).trim();
double cost; double cost;
try { try {
cost = Double.parseDouble(arguments.get(1)); cost = Double.parseDouble(arguments.get(1));

View File

@ -27,16 +27,18 @@ public class AddConditionTabCompleter extends TokenizedTabCompleter {
if (lineIndices == null) { if (lineIndices == null) {
initializeValues(); initializeValues();
} }
if (argumentSize < 1) { if (argumentSize == 1) {
return TabCompleteHelper.getPaidSignNames(); return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0));
} else if (argumentSize < 2) { } else if (argumentSize == 2) {
return this.lineIndices; return TabCompleteHelper.filterMatchingStartsWith(this.lineIndices, arguments.get(1));
} else if (argumentSize < 3) { } else if (argumentSize == 3) {
return stringsToMatch; return TabCompleteHelper.filterMatchingStartsWith(stringsToMatch, arguments.get(2));
} else if (argumentSize < 4) { } else if (argumentSize == 4) {
return booleans; return TabCompleteHelper.filterMatchingStartsWith(booleans, arguments.get(3));
} else if (argumentSize < 6) { } else if (argumentSize == 5) {
return optionStates; return TabCompleteHelper.filterMatchingStartsWith(optionStates, arguments.get(4));
} else if (argumentSize == 6) {
return TabCompleteHelper.filterMatchingStartsWith(optionStates, arguments.get(5));
} }
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -35,14 +35,16 @@ public class AddTabCompleter extends TokenizedTabCompleter {
loadAvailablePermissions(); loadAvailablePermissions();
} }
if (argumentSize < 1) { if (argumentSize == 1) {
return names; return TabCompleteHelper.filterMatchingStartsWith(names, arguments.get(0));
} else if (argumentSize < 2) { } else if (argumentSize == 2) {
return costs; return TabCompleteHelper.filterMatchingStartsWith(costs, arguments.get(1));
} else if (argumentSize < 3) { } else if (argumentSize == 3) {
return tabCompletePermission(arguments.get(arguments.size() - 1)); return tabCompletePermission(arguments.get(arguments.size() - 1));
} else if (argumentSize < 5) { } else if (argumentSize == 4) {
return options; return TabCompleteHelper.filterMatchingStartsWith(options, arguments.get(3));
} else if (argumentSize == 5) {
return TabCompleteHelper.filterMatchingStartsWith(options, arguments.get(4));
} }
return new ArrayList<>(); return new ArrayList<>();
} }

View File

@ -20,22 +20,19 @@ public class ListTabCompleter extends TokenizedTabCompleter {
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) { @NotNull String[] args) {
if (argumentSize < 1) { if (argumentSize == 1) {
return TabCompleteHelper.getPaidSignNames(); return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0));
} else if (argumentSize < 2) { } else if (argumentSize == 2) {
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0));
if (sign != null) { if (sign != null) {
List<String> availableConditions = new ArrayList<>(); List<String> availableConditions = new ArrayList<>();
for (Short signLine : sign.getConditions().keySet()) { for (Short signLine : sign.getConditions().keySet()) {
availableConditions.add(String.valueOf(signLine + 1)); availableConditions.add(String.valueOf(signLine + 1));
} }
return availableConditions; return TabCompleteHelper.filterMatchingStartsWith(availableConditions, arguments.get(1));
} else { }
return TabCompleteHelper.getSignLines();
} }
} else {
return new ArrayList<>(); return new ArrayList<>();
} }
}
} }

View File

@ -27,18 +27,16 @@ public class RemoveConditionTabCompleter extends TokenizedTabCompleter {
initializeValues(); initializeValues();
} }
if (argumentSize < 1) { if (argumentSize == 1) {
return TabCompleteHelper.getPaidSignNames(); return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0));
} else if (argumentSize < 2) { } else if (argumentSize == 2) {
PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0));
if (sign != null) { if (sign != null) {
List<String> availableConditions = new ArrayList<>(); List<String> availableConditions = new ArrayList<>();
for (Short signLine : sign.getConditions().keySet()) { for (Short signLine : sign.getConditions().keySet()) {
availableConditions.add(String.valueOf(signLine + 1)); availableConditions.add(String.valueOf(signLine + 1));
} }
return availableConditions; return TabCompleteHelper.filterMatchingStartsWith(availableConditions, arguments.get(1));
} else {
return TabCompleteHelper.getSignLines();
} }
} }
return new ArrayList<>(); return new ArrayList<>();

View File

@ -19,12 +19,16 @@ public class TokenizedTabCompleter implements TabCompleter {
@Nullable @Nullable
@Override @Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias,
@NotNull String[] args) {
arguments = Tokenizer.tokenize(String.join(" ", args)); arguments = Tokenizer.tokenize(String.join(" ", args));
if (args.length == 0) { if (args.length == 0) {
argumentSize = 0; argumentSize = 0;
} else { } else {
argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; if (args[args.length - 1].isEmpty()) {
arguments.add("");
}
argumentSize = arguments.size();
} }
return null; return null;
} }

View File

@ -29,7 +29,7 @@ public class PaidSign {
*/ */
public PaidSign(String name, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) { public PaidSign(String name, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) {
if (name == null || name.trim().isBlank()) { if (name == null || name.trim().isBlank()) {
throw new IllegalArgumentException("Id cannot be empty"); throw new IllegalArgumentException("Name cannot be empty");
} }
if (cost <= 0) { if (cost <= 0) {
throw new IllegalArgumentException("Cost must be larger than 0"); throw new IllegalArgumentException("Cost must be larger than 0");

View File

@ -15,10 +15,21 @@ public final class Tokenizer {
/** /**
* Tokenizes a string * Tokenizes a string
* *
* @param input <p>A string.</p> * @param input <p>A string</p>
* @return <p>A list of tokens.</p> * @return <p>A list of tokens</p>
*/ */
public static List<String> tokenize(String input) { public static List<String> tokenize(String input) {
return tokenize(input, true);
}
/**
* Tokenizes a string
*
* @param input <p>A string</p>
* @param allowEmptyQuotes <p>Whether to treat "" as a token</p>
* @return <p>A list of tokens</p>
*/
public static List<String> tokenize(String input, boolean allowEmptyQuotes) {
List<String> tokens = new ArrayList<>(); List<String> tokens = new ArrayList<>();
boolean startedQuote = false; boolean startedQuote = false;
StringBuilder currentToken = new StringBuilder(); StringBuilder currentToken = new StringBuilder();
@ -33,7 +44,7 @@ public final class Tokenizer {
case '"': case '"':
if (startedQuote) { if (startedQuote) {
//This quote signifies the end of the argument //This quote signifies the end of the argument
if (isNotEmpty(currentToken)) { if (allowEmptyQuotes || isNotEmpty(currentToken)) {
tokens.add(currentToken.toString()); tokens.add(currentToken.toString());
currentToken = new StringBuilder(); currentToken = new StringBuilder();
} }