Skip to content

Commit

Permalink
- Fix #146
Browse files Browse the repository at this point in the history
- Cleanup unused autocomplete methods
  • Loading branch information
buthed010203 committed Nov 13, 2023
1 parent c16cc1b commit f2de21a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 66 deletions.
10 changes: 1 addition & 9 deletions core/src/mindustry/client/utils/Autocomplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Autocompleter> 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;
}
Expand Down
4 changes: 0 additions & 4 deletions core/src/mindustry/client/utils/Autocompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@
public interface Autocompleter {
default void initialize() { }

Autocompleteable getCompletion(String input);

boolean matches(String input);

Seq<Autocompleteable> closest(String input);
}
38 changes: 19 additions & 19 deletions core/src/mindustry/client/utils/BlockEmotes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,48 @@ 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<Autocompleteable> {
return emotes.sort { item: BlockEmote -> item.matches(input) }.`as`()
}

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()
matchCache = dst
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(":", "")
}
}
}
16 changes: 1 addition & 15 deletions core/src/mindustry/client/utils/CommandCompletion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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<Autocompleteable> closest(String input) {
return commands.sort(item -> item.matches(input)).as();
Expand Down
19 changes: 0 additions & 19 deletions core/src/mindustry/client/utils/PlayerCompletion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Autocompleteable> completions = closest(input);
if (completions.isEmpty()) return null;
return closest(input).first();
}

public Seq<Autocompleteable> closest(String input) {
return Groups.player.array.map(PlayerMatcher::new).sort(p -> p.matches(input)).as();
}
Expand Down

0 comments on commit f2de21a

Please sign in to comment.