Adds a new tab-completion helper function
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
Adds a new tab-completion helper function which is able to tab-complete comma-separated inputs.
This commit is contained in:
parent
75d4f84884
commit
a0f81879db
@ -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
|
||||
*
|
||||
* <p>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.</p>
|
||||
*
|
||||
* @param values <p>The available tab-completion values</p>
|
||||
* @param typedText <p>The text the player has typed</p>
|
||||
* @param filterer <p>The filterer (filterMatching) to use to filter by typed text</p>
|
||||
* @return <p>Available tab-completions</p>
|
||||
*/
|
||||
public static @NotNull List<String> getStringList(@NotNull List<String> values, @NotNull String typedText,
|
||||
@Nullable BiFunction<List<String>, String, List<String>> 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<String> 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<String> filtered;
|
||||
if (filterer != null) {
|
||||
filtered = filterer.apply(new ArrayList<>(values), lastArgument);
|
||||
} else {
|
||||
filtered = values;
|
||||
}
|
||||
|
||||
// Remove any already used values
|
||||
List<String> 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 <p>The string arguments to get the base of</p>
|
||||
* @return <p>The base string</p>
|
||||
*/
|
||||
private static @NotNull String getBase(@NotNull List<String> arguments) {
|
||||
List<String> baseArray = arguments.subList(0, arguments.size() - 1);
|
||||
return String.join(",", baseArray) + ",";
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user