From f2de21a346709fb92775088651ca815ad9c8093d Mon Sep 17 00:00:00 2001 From: buthed010203 Date: Mon, 13 Nov 2023 17:51:22 -0500 Subject: [PATCH] - Fix #146 - Cleanup unused autocomplete methods --- .../mindustry/client/utils/Autocomplete.java | 10 +---- .../mindustry/client/utils/Autocompleter.java | 4 -- .../src/mindustry/client/utils/BlockEmotes.kt | 38 +++++++++---------- .../client/utils/CommandCompletion.java | 16 +------- .../client/utils/PlayerCompletion.java | 19 ---------- 5 files changed, 21 insertions(+), 66 deletions(-) diff --git a/core/src/mindustry/client/utils/Autocomplete.java b/core/src/mindustry/client/utils/Autocomplete.java index 42d9360dbe..6b4d12b747 100644 --- a/core/src/mindustry/client/utils/Autocomplete.java +++ b/core/src/mindustry/client/utils/Autocomplete.java @@ -3,21 +3,13 @@ import arc.struct.*; import org.jetbrains.annotations.NotNull; -public class Autocomplete { +public class Autocomplete { // FINISHME: Rewrite the entire autocompletion system to not suck as much public static Seq autocompleters = new Seq<>(); public static void initialize() { autocompleters.forEach(Autocompleter::initialize); } - public static String getCompletion(String input) { - return closest(input).peek().getCompletion(input); - } - - public static String getHover(String input) { - return closest(input).peek().getHover(input); - } - public static boolean matches(String input) { return closest(input).peek().matches(input) > 0.5f; } diff --git a/core/src/mindustry/client/utils/Autocompleter.java b/core/src/mindustry/client/utils/Autocompleter.java index 2db4b5ad81..67e89a6280 100644 --- a/core/src/mindustry/client/utils/Autocompleter.java +++ b/core/src/mindustry/client/utils/Autocompleter.java @@ -5,9 +5,5 @@ public interface Autocompleter { default void initialize() { } - Autocompleteable getCompletion(String input); - - boolean matches(String input); - Seq closest(String input); } diff --git a/core/src/mindustry/client/utils/BlockEmotes.kt b/core/src/mindustry/client/utils/BlockEmotes.kt index c0fff42c0f..65c995bbf7 100644 --- a/core/src/mindustry/client/utils/BlockEmotes.kt +++ b/core/src/mindustry/client/utils/BlockEmotes.kt @@ -14,12 +14,6 @@ class BlockEmotes : Autocompleter { } } - override fun getCompletion(input: String): Autocompleteable = bestMatch(input) - - private fun bestMatch(input: String) = emotes.maxBy { it.matches(input) } - - override fun matches(input: String) = (bestMatch(input)?.matches(input) ?: 0f) > .5f - override fun closest(input: String): Seq { return emotes.sort { item: BlockEmote -> item.matches(input) }.`as`() } @@ -27,14 +21,16 @@ class BlockEmotes : Autocompleter { private class BlockEmote(private val unicode: String, private val name: String) : Autocompleteable { private var matchCache = 0f private var cacheName: String? = null + override fun matches(input: String): Float { if (input == cacheName) return matchCache // FINISHME: This system still sucks. cacheName = input - if (Strings.count(input, ':') % 2 == 0) { + val text = getLast(input) + if (text == null || Strings.count(text, ':') % 2 == 1) { matchCache = 0f return 0f } - var dst = biasedLevenshtein(input.substring(input.lastIndexOf(':') + 1), name) + var dst = biasedLevenshtein(text, name) dst *= -1f dst += name.length.toFloat() dst /= name.length.toFloat() @@ -42,20 +38,24 @@ class BlockEmotes : Autocompleter { return dst } - override fun getCompletion(input: String): String { // FINISHME: I don't know what foo was thinking when he wrote the autocomplete system but it's all horrible and needs a proper rewrite - val items = Seq(input.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) - if (items.isEmpty) return input - items.pop() - val start = items.toString("") - return start + unicode + override fun getCompletion(input: String): String { + val text = getLast(input) ?: return input + + return input.replaceLast(":$text", unicode) } override fun getHover(input: String): String { - if (!input.contains(":")) return input - val items = Seq(input.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) - if (items.size == 0) return input - val text = items.peek() - return input.replace(":$text", ":$name:") + val text = getLast(input) ?: return input + + return input.replaceLast(":$text", "$unicode :$name:") + } + + private fun getLast(input: String): String? { + val strings = Seq.with(input.split("\\s".toRegex())) + if (strings.isEmpty) return null + val text = strings.peek() + if (!text.startsWith(":")) return null + return text.replaceFirst(":", "") } } } \ No newline at end of file diff --git a/core/src/mindustry/client/utils/CommandCompletion.java b/core/src/mindustry/client/utils/CommandCompletion.java index 40bfe95db0..5ff4775f50 100644 --- a/core/src/mindustry/client/utils/CommandCompletion.java +++ b/core/src/mindustry/client/utils/CommandCompletion.java @@ -14,7 +14,7 @@ public void initialize() { reset(true); Vars.netClient.addPacketHandler("commandList", list -> { - Log.debug("Received Command List: " + list); + Log.debug("Received Command List: @", list); var json = Jval.read(list); var cmds = json.get("commands").asObject(); if (!cmds.isEmpty()) { @@ -37,20 +37,6 @@ private static void addCommands(CommandHandler handler) { commands.addAll(handler.getCommandList().map(c -> new CommandCompletable(c.text, c.text + " " + c.paramText, handler.getPrefix()))); } - @Override - public Autocompleteable getCompletion(String input) { - return bestMatch(input); - } - - private Autocompleteable bestMatch(String input) { - return commands.max(e -> e.matches(input)); - } - - @Override - public boolean matches(String input) { - return closest(input).any(); - } - @Override public Seq closest(String input) { return commands.sort(item -> item.matches(input)).as(); diff --git a/core/src/mindustry/client/utils/PlayerCompletion.java b/core/src/mindustry/client/utils/PlayerCompletion.java index ed7c821d6b..466a0edefe 100644 --- a/core/src/mindustry/client/utils/PlayerCompletion.java +++ b/core/src/mindustry/client/utils/PlayerCompletion.java @@ -6,25 +6,6 @@ public class PlayerCompletion implements Autocompleter { - public Autocompleteable getCompletion(String input) { - return bestMatch(input); - } - - @Override - public boolean matches(String input) { - Autocompleteable match = bestMatch(input); - if (match == null) { - return false; - } - return match.matches(input) > 0.5f; - } - - private Autocompleteable bestMatch(String input) { - Seq completions = closest(input); - if (completions.isEmpty()) return null; - return closest(input).first(); - } - public Seq closest(String input) { return Groups.player.array.map(PlayerMatcher::new).sort(p -> p.matches(input)).as(); }