From a0f81879db50fb355ec4be49e94b13ceb12da592 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 3 May 2024 14:58:55 +0200 Subject: [PATCH] Adds a new tab-completion helper function Adds a new tab-completion helper function which is able to tab-complete comma-separated inputs. --- .../knarlib/util/TabCompletionHelper.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java b/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java index 795910d..72f1c10 100644 --- a/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java +++ b/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java @@ -1,9 +1,11 @@ package net.knarcraft.knarlib.util; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.function.BiFunction; /** * Helper class for getting string lists required for auto-completion @@ -14,6 +16,51 @@ public final class TabCompletionHelper { private TabCompletionHelper() { } + /** + * Finds available tab-completions for a list where several comma-separated values are allowed + * + *

If, for example, the available values are [fish,potatoes,stew,strawberries], and the player has already typed + * "fish,potatoes,", then available tab-completions become fish,potatoes,stew and fish,potatoes,strawberries.

+ * + * @param values

The available tab-completion values

+ * @param typedText

The text the player has typed

+ * @param filterer

The filterer (filterMatching) to use to filter by typed text

+ * @return

Available tab-completions

+ */ + public static @NotNull List getStringList(@NotNull List values, @NotNull String typedText, + @Nullable BiFunction, String, List> filterer) { + if (typedText.isBlank() || !typedText.contains(",")) { + if (filterer != null) { + return filterer.apply(values, typedText); + } else { + return values; + } + } else { + // The argument base is everything the player has typed, until the last comma + List arguments = List.of(typedText.split(",")); + String base = getBase(arguments); + String lastArgument = arguments.get(arguments.size() - 1); + + // Filter available values by the text after the last comma only + List filtered; + if (filterer != null) { + filtered = filterer.apply(new ArrayList<>(values), lastArgument); + } else { + filtered = values; + } + + // Remove any already used values + List unused = new ArrayList<>(); + for (String string : filtered) { + if (!arguments.contains(string)) { + unused.add(base + string); + } + } + + return unused; + } + } + /** * Finds tab complete values that contain the typed text * @@ -48,4 +95,15 @@ public final class TabCompletionHelper { return configValues; } + /** + * Gets the "base" string from the given arguments, not including the incomplete last argument + * + * @param arguments

The string arguments to get the base of

+ * @return

The base string

+ */ + private static @NotNull String getBase(@NotNull List arguments) { + List baseArray = arguments.subList(0, arguments.size() - 1); + return String.join(",", baseArray) + ","; + } + }