Skip to content

Commit

Permalink
move ex command tab completion to core
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 28, 2023
1 parent 73b9c8a commit b0412ac
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 5 deletions.
@@ -1,15 +1,21 @@
package com.denizenscript.denizencore.utilities;

import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.ObjectType;
import com.denizenscript.denizencore.scripts.ScriptBuilder;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.scripts.queues.core.TimedQueue;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagManager;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -74,4 +80,163 @@ public static boolean removeSustainedQueue(Object source) {
ScriptQueue queue = sustainedQueues.remove(source);
return queue != null && !queue.isStopped;
}

public static List<String> buildTabCompletions(String[] rawArgs, BiConsumer<AbstractCommand, AbstractCommand.TabCompletionsBuilder> commandExtras) {
String entry = String.join(" ", rawArgs);
if (entry.length() > 3 && entry.startsWith("-q ")) {
entry = entry.substring("-q ".length());
}
String[] args = ArgumentHelper.buildArgs(entry, true);
boolean isNewArg = rawArgs.length == 0 || rawArgs[rawArgs.length - 1].isEmpty();
boolean isCommandArg = args.length == 0 || (args.length == 1 && !isNewArg) || args[args.length - (isNewArg ? 1 : 2)].equals("-");
if (isCommandArg) {
if (isNewArg || args.length == 0) {
return new ArrayList<>(DenizenCore.commandRegistry.instances.keySet());
}
ArrayList<String> output = new ArrayList<>();
String startOfName = CoreUtilities.toLowerCase(args[args.length - 1]);
for (String command : DenizenCore.commandRegistry.instances.keySet()) {
if (command.startsWith(startOfName)) {
output.add(command);
}
}
return output;
}
if (!isNewArg) {
String lastArg = rawArgs[rawArgs.length - 1];
int argStart = 0;
for (int i = 0; i < lastArg.length(); i++) {
if (lastArg.charAt(i) == '"' || lastArg.charAt(i) == '\'') {
char quote = lastArg.charAt(i++);
while (i < lastArg.length() && lastArg.charAt(i) != quote) {
i++;
}
}
else if (lastArg.charAt(i) == ' ') {
argStart = i + 1;
}
}
String arg = lastArg.substring(argStart);
if (CoreUtilities.contains(arg, '<')) {
int tagBits = 0;
int relevantTagStart = -1;
for (int i = arg.length() - 1; i >= 0; i--) {
if (arg.charAt(i) == '>') {
tagBits++;
}
else if (arg.charAt(i) == '<') {
if (tagBits == 0) {
relevantTagStart = i + 1;
break;
}
tagBits--;
}
}
if (relevantTagStart != -1) {
String fullTag = CoreUtilities.toLowerCase(arg.substring(relevantTagStart));
int components = 0;
int subTags = 0;
int squareBrackets = 0;
int lastDot = 0;
int bracketStart = -1;
Collection<Class<? extends ObjectTag>> typesApplicable = null;
for (int i = 0; i < fullTag.length(); i++) {
char c = fullTag.charAt(i);
if (c == '<') {
subTags++;
}
else if (c == '>') {
subTags--;
}
else if (c == '[' && subTags == 0) {
squareBrackets++;
bracketStart = i;
}
else if (c == ']' && subTags == 0) {
squareBrackets--;
}
else if (c == '.' && subTags == 0 && squareBrackets == 0) {
Class<? extends ObjectTag> type = null;
String part = fullTag.substring(lastDot, bracketStart == -1 ? i : bracketStart);
if (components == 0) {
TagManager.TagBaseData baseType = TagManager.baseTags.get(part);
if (baseType != null) {
type = baseType.returnType;
}
}
else if (typesApplicable != null) {
for (Class<? extends ObjectTag> possibleType : typesApplicable) {
ObjectType<? extends ObjectTag> typeData = ObjectFetcher.getType(possibleType);
if (typeData != null && typeData.tagProcessor != null) {
ObjectTagProcessor.TagData data = typeData.tagProcessor.registeredObjectTags.get(part);
if (data != null && data.returnType != null) {
type = data.returnType;
break;
}
}
}
}
if (type != null) {
typesApplicable = ObjectFetcher.getAllApplicableSubTypesFor(type);
}
else {
typesApplicable = ObjectFetcher.objectsByClass.keySet();
}
components++;
lastDot = i + 1;
bracketStart = -1;
}
}
String beforeDot = arg.substring(0, relevantTagStart) + fullTag.substring(0, lastDot);
if (components == 0 && !CoreUtilities.contains(fullTag, '[')) {
ArrayList<String> output = new ArrayList<>();
for (String tagBase : TagManager.baseTags.keySet()) {
if (tagBase.startsWith(fullTag)) {
output.add(beforeDot + tagBase);
}
}
return output;
}
String subComponent = fullTag.substring(lastDot);
if (lastDot > 0 && !CoreUtilities.contains(subComponent, '[')) {
ArrayList<String> output = new ArrayList<>();
for (Class<? extends ObjectTag> possibleType : typesApplicable) {
ObjectType<? extends ObjectTag> typeData = ObjectFetcher.getType(possibleType);
if (typeData != null && typeData.tagProcessor != null) {
for (String tag : typeData.tagProcessor.registeredObjectTags.keySet()) {
if (tag.startsWith(subComponent)) {
output.add(beforeDot + tag);
}
}
}
}
return output;
}
}
}
}
AbstractCommand dcmd = DenizenCore.commandRegistry.get(args[0]);
for (int i = args.length - 2; i >= 0; i--) {
if (args[i].equals("-")) {
dcmd = DenizenCore.commandRegistry.get(args[i + 1]);
}
}
if (dcmd == null) {
return null;
}
String lowArg = CoreUtilities.toLowerCase(rawArgs[rawArgs.length - 1]);
AbstractCommand.TabCompletionsBuilder completionsBuilder = new AbstractCommand.TabCompletionsBuilder();
completionsBuilder.arg = lowArg;
for (String flat : dcmd.docFlagArgs) {
completionsBuilder.add(flat);
}
for (String prefix : dcmd.docPrefixes) {
completionsBuilder.add(prefix + ":");
}
dcmd.addCustomTabCompletions(completionsBuilder);
if (commandExtras != null) {
commandExtras.accept(dcmd, completionsBuilder);
}
return completionsBuilder.completions;
}
}
Expand Up @@ -2,9 +2,9 @@

public class OneTimeSchedulable extends Schedulable {

public OneTimeSchedulable(Runnable runnable, float fireTime) {
public OneTimeSchedulable(Runnable runnable, float fireAfterSeconds) {
run = runnable;
secondsLeft = fireTime;
secondsLeft = fireAfterSeconds;
}

@Override
Expand Down

0 comments on commit b0412ac

Please sign in to comment.