From 0cc0ee7f03938b5cb642b9a0b4469a2152eb8093 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 18 Aug 2018 01:37:35 +1000 Subject: [PATCH] Basic tab suggestions --- .../src/main/resources/plugin.yml | 2 +- .../com/boydti/fawe/command/MaskBinding.java | 4 ++-- .../boydti/fawe/command/PatternBinding.java | 3 ++- .../java/com/boydti/fawe/util/StringMan.java | 7 ++++++ .../util/commands/SuggestedRange.java | 4 ---- .../parametric/AParametricCallable.java | 12 +++++++++- .../command/parametric/BindingHelper.java | 22 +++++++++++++++++++ .../command/parametric/ParameterData.java | 8 +++++++ 8 files changed, 53 insertions(+), 9 deletions(-) diff --git a/worldedit-bukkit/src/main/resources/plugin.yml b/worldedit-bukkit/src/main/resources/plugin.yml index ac4873a5b..b62b370f3 100644 --- a/worldedit-bukkit/src/main/resources/plugin.yml +++ b/worldedit-bukkit/src/main/resources/plugin.yml @@ -10,7 +10,7 @@ database: false #softdepend: [WorldGuard, PlotSquared, MCore, Factions, GriefPrevention, Residence, Towny, PlotMe, PreciousStones] commands: fcancel: - description: (FAWE) Cancel your edit + description: "Cancel your edit" aliases: [fawecancel,/fcancel,/cancel,/fawecancel] permissions: fawe.plotsquared: diff --git a/worldedit-core/src/main/java/com/boydti/fawe/command/MaskBinding.java b/worldedit-core/src/main/java/com/boydti/fawe/command/MaskBinding.java index e231e7c57..09dd76af4 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/command/MaskBinding.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/command/MaskBinding.java @@ -34,6 +34,6 @@ public class MaskBinding extends FaweBinding { // List blocks = BundledBlockData.getInstance().getBlockNames(split2[0]); // return MainUtil.prepend(start, blocks); // } - return new ArrayList<>(); + return super.getSuggestions(parameter, prefix); } -} +} \ No newline at end of file diff --git a/worldedit-core/src/main/java/com/boydti/fawe/command/PatternBinding.java b/worldedit-core/src/main/java/com/boydti/fawe/command/PatternBinding.java index fa5d04614..9c9d90055 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/command/PatternBinding.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/command/PatternBinding.java @@ -3,6 +3,7 @@ package com.boydti.fawe.command; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.util.command.parametric.ParameterData; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class PatternBinding extends FaweBinding { @@ -15,7 +16,7 @@ public class PatternBinding extends FaweBinding { @Override public List getSuggestions(ParameterData parameter, String prefix) { - return new ArrayList<>(); + return super.getSuggestions(parameter, prefix); // int index = prefix.lastIndexOf(",|%"); // String start = index != -1 ? prefix.substring(0, index) : ""; // String current = index != -1 ? prefix.substring(index) : prefix; diff --git a/worldedit-core/src/main/java/com/boydti/fawe/util/StringMan.java b/worldedit-core/src/main/java/com/boydti/fawe/util/StringMan.java index 67eeca6f1..256b5f278 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/util/StringMan.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/util/StringMan.java @@ -52,6 +52,13 @@ public class StringMan { return -1; } + public static String prettyFormat(double d) { + if (d == Double.MIN_VALUE) return "-∞"; + if (d == Double.MAX_VALUE) return "∞"; + if(d == (long) d) return String.format("%d",(long)d); + else return String.format("%s",d); + } + public static boolean isBracketForwards(char c) { switch (c) { case '[': diff --git a/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SuggestedRange.java b/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SuggestedRange.java index 3eeb191ee..98aa1f63e 100644 --- a/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SuggestedRange.java +++ b/worldedit-core/src/main/java/com/sk89q/minecraft/util/commands/SuggestedRange.java @@ -5,10 +5,6 @@ import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface SuggestedRange { - Class clazz() default Link.class; - String value(); - - /** * The minimum value that the number can be at, inclusive. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/AParametricCallable.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/AParametricCallable.java index 73a734278..59e57d567 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/AParametricCallable.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/AParametricCallable.java @@ -223,11 +223,13 @@ public abstract class AParametricCallable implements CommandCallable { prefix = ""; } +// System.out.println("(0) Return get binding suggestions " + parameter + " | " + prefix); return parameter.getBinding().getSuggestions(parameter, prefix); } } // This should not happen +// System.out.println("(1) This should not happen"); return new ArrayList(); } @@ -240,7 +242,6 @@ public abstract class AParametricCallable implements CommandCallable { if (parameter.getFlag() != null) { continue; // We already handled flags } - try { scoped.mark(); parameter.getBinding().bind(parameter, scoped, true); @@ -253,26 +254,32 @@ public abstract class AParametricCallable implements CommandCallable { // For /command value1 |value2 // For /command |value1 value2 if (suggestable.forHangingValue()) { +// System.out.println("(2) Return get binding dangling " + parameter + " | " + ""); return parameter.getBinding().getSuggestions(parameter, ""); } else { // For /command value1| value2 if (lastConsumer != null) { +// System.out.println("(3) Return get consumed " + lastConsumer + " | " + lastConsumed); return lastConsumer.getBinding().getSuggestions(lastConsumer, lastConsumed); // For /command| value1 value2 // This should never occur } else { +// System.out.println("(4) Invalid suggestion context"); throw new RuntimeException("Invalid suggestion context"); } } } catch (ParameterException | InvocationTargetException e) { SuggestInputParseException suggestion = SuggestInputParseException.get(e); if (suggestion != null) { +// System.out.println("(5) Has suggestion " + suggestion.getSuggestions()); return suggestion.getSuggestions(); } if (suggestable.forHangingValue()) { String name = getDescription().getParameters().get(consumerIndex).getName(); +// System.out.println("(6) Has dangling invalid " + name + " | " + e.getMessage()); throw new InvalidUsageException("For parameter '" + name + "': " + e.getMessage(), this); } else { +// System.out.println("(7) HGet binding suggestions " + parameter + " | " + lastConsumed); return parameter.getBinding().getSuggestions(parameter, ""); } } @@ -281,13 +288,16 @@ public abstract class AParametricCallable implements CommandCallable { if (suggestable.forHangingValue()) { // There's nothing that we can suggest because there's no more parameters // to add on, and we can't change the previous parameter +// System.out.println("(7.1) No more parameters"); return new ArrayList(); } else { // For /command value1 value2| if (lastConsumer != null) { +// System.out.println("(8) Get binding suggestions " + lastConsumer + " | " + lastConsumed); return lastConsumer.getBinding().getSuggestions(lastConsumer, lastConsumed); // This should never occur } else { +// System.out.println("(9) Invalid suggestion context"); throw new RuntimeException("Invalid suggestion context"); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/BindingHelper.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/BindingHelper.java index 73b59f087..34b1efda2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/BindingHelper.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/BindingHelper.java @@ -19,13 +19,16 @@ package com.sk89q.worldedit.util.command.parametric; +import com.boydti.fawe.util.StringMan; import com.sk89q.minecraft.util.commands.CommandException; +import com.sk89q.worldedit.util.command.binding.Range; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -184,6 +187,25 @@ public class BindingHelper implements Binding { @Override public List getSuggestions(ParameterData parameter, String prefix) { + if (prefix.isEmpty()) { + char bracket = parameter.isOptional() ? '[' : '<'; + char endBracket = StringMan.getMatchingBracket(bracket); + StringBuilder result = new StringBuilder(); + result.append(bracket); + if (parameter.getFlag() != null) { + result.append('-').append(parameter.getFlag()).append(' '); + } + result.append(parameter.getName()); + if (parameter.getDefaultValue() != null) { + result.append('=').append(StringMan.join(parameter.getDefaultValue(), " ")); + } + Range range = parameter.getModifier(Range.class); + if (range != null) { + result.append('|').append(StringMan.prettyFormat(range.min())).append(",").append(StringMan.prettyFormat(range.max())); + } + result.append(endBracket); + return Collections.singletonList(result.toString()); + } return new ArrayList<>(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/ParameterData.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/ParameterData.java index 316b3ebcb..f902ae4bd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/ParameterData.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/command/parametric/ParameterData.java @@ -26,6 +26,7 @@ import com.sk89q.worldedit.util.command.binding.Text; import javax.xml.ws.Provider; import java.lang.annotation.Annotation; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.concurrent.Callable; @@ -119,6 +120,13 @@ public class ParameterData extends SimpleParameter { return modifiers; } + public T getModifier(Class annotatedType) { + for (Annotation annotation : getModifiers()) { + if (annotation.getClass() == annotatedType) return (T) annotation; + } + return null; + } + /** * Set the list of modifiers. *