Skip to content

Commit

Permalink
reworked things
Browse files Browse the repository at this point in the history
will release tomorrow
  • Loading branch information
Prunoideae committed Mar 17, 2022
1 parent a3cb52a commit 54f457b
Show file tree
Hide file tree
Showing 30 changed files with 873 additions and 294 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ apply from: 'https://files.latmod.com/public/markdown-git-changelog.gradle'
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17

def ENV = System.getenv()
version = "${mod_version}-build.54"
version = "${mod_version}-build.57"
archivesBaseName = project.archives_base_name
group = project.maven_group

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false
loom.platform=forge
mod_id=probejs
archives_base_name=probejs
mod_version=2.1.0
mod_version=2.2.0
maven_group=com.prunoideae
mod_author=Prunoideae
minecraft_version=1.18.1
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/com/prunoideae/probejs/ProbeCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.prunoideae.probejs.document.parser.processor.DocumentProviderHandler;
import com.prunoideae.probejs.formatter.ClassResolver;
import com.prunoideae.probejs.formatter.NameResolver;
import com.prunoideae.probejs.info.ClassInfo;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.server.ServerSettings;
import net.minecraft.commands.CommandSourceStack;
Expand All @@ -27,20 +28,24 @@

public class ProbeCommands {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {

dispatcher.register(
Commands.literal("probejs")
.then(Commands.literal("dump")
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2))
.executes(context -> {
try {
export(context.getSource());
if (ProbeConfig.INSTANCE.autoExport)
export(context.getSource());
SnippetCompiler.compile();
DocumentProviderHandler.init();
CommentHandler.init();
Manager.init();
ClassResolver.init();
NameResolver.init();
TypingCompiler.compile();
if (ProbeConfig.INSTANCE.exportClassNames)
SnippetCompiler.compileClassNames();
} catch (Exception e) {
e.printStackTrace();
context.getSource().sendSuccess(new TextComponent("Uncaught exception happened in wrapper, please report to the Github issue with complete latest.log."), false);
Expand Down Expand Up @@ -77,6 +82,24 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
context.getSource().sendSuccess(new TextComponent("Changes will be applied next time you start the game."), false);
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("toggle_snippet_order").executes(context -> {
ProbeConfig.INSTANCE.vanillaOrder = !ProbeConfig.INSTANCE.vanillaOrder;
context.getSource().sendSuccess(new TextComponent("In snippets, which will appear first: %s".formatted(ProbeConfig.INSTANCE.vanillaOrder ? "mod_id" : "member_type")), false);
ProbeConfig.INSTANCE.save();
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("toggle_classname_snippets").executes(context -> {
ProbeConfig.INSTANCE.exportClassNames = !ProbeConfig.INSTANCE.exportClassNames;
context.getSource().sendSuccess(new TextComponent("Export class name as snippets set to: %s".formatted(ProbeConfig.INSTANCE.exportClassNames)), false);
ProbeConfig.INSTANCE.save();
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("toggle_autoexport").executes(context -> {
ProbeConfig.INSTANCE.autoExport = !ProbeConfig.INSTANCE.autoExport;
context.getSource().sendSuccess(new TextComponent("Auto-export for KubeJS set to: %s".formatted(ProbeConfig.INSTANCE.autoExport)), false);
ProbeConfig.INSTANCE.save();
return Command.SINGLE_SUCCESS;
}))
)
);
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/prunoideae/probejs/ProbeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
public class ProbeConfig {
public static ProbeConfig INSTANCE = new ProbeConfig();
private static final Path CONFIG = KubeJSPaths.CONFIG.resolve("probejs.json");
public boolean dumpMethod = false;
public boolean dumpMethod = true;
public boolean disabled = false;
public boolean vanillaOrder = true;
public boolean exportClassNames = false;
public boolean autoExport = true;


private static <E> E fetchPropertyOrDefault(Object key, Map<?, ?> value, E defaultValue) {
Object v = value.get(key);
Expand All @@ -26,8 +30,11 @@ private ProbeConfig() {
if (Files.exists(cfg)) {
try {
Map<?, ?> obj = new Gson().fromJson(Files.newBufferedReader(cfg), Map.class);
dumpMethod = fetchPropertyOrDefault("dumpMethod", obj, false);
dumpMethod = fetchPropertyOrDefault("dumpMethod", obj, true);
disabled = fetchPropertyOrDefault("disabled", obj, false);
vanillaOrder = fetchPropertyOrDefault("vanillaOrder", obj, true);
exportClassNames = fetchPropertyOrDefault("exportClassNames", obj, false);
autoExport = fetchPropertyOrDefault("autoExport", obj, true);
} catch (IOException e) {
ProbeJS.LOGGER.warn("Cannot read config properties, falling back to defaults.");
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/prunoideae/probejs/ProbePaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class ProbePaths {
public static Path DOCS = PROBE.resolve("docs");
public static Path GENERATED = PROBE.resolve("generated");
public static Path USER_DEFINED = PROBE.resolve("user");
public static Path SNIPPET = KubeJSPaths.DIRECTORY.resolve(".vscode");

public static void init() {
if (Files.notExists(PROBE, new LinkOption[0])) {
Expand All @@ -27,6 +28,9 @@ public static void init() {
if (Files.notExists(USER_DEFINED, new LinkOption[0])) {
UtilsJS.tryIO(() -> Files.createDirectories(USER_DEFINED));
}
if (Files.notExists(SNIPPET, new LinkOption[0])) {
UtilsJS.tryIO(() -> Files.createDirectories(SNIPPET));
}
}

static {
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/com/prunoideae/probejs/compiler/SnippetCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.prunoideae.probejs.ProbeConfig;
import com.prunoideae.probejs.ProbePaths;
import com.prunoideae.probejs.formatter.NameResolver;
import dev.latvian.mods.kubejs.KubeJSPaths;

import javax.json.Json;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
Expand Down Expand Up @@ -48,7 +52,10 @@ public JsonObject toSnippet() {
byModMembers.forEach((mod, modMembers) -> {
JsonObject modMembersJson = new JsonObject();
JsonArray prefixes = new JsonArray();
prefixes.add("@%s.%s".formatted(mod, type));
if (ProbeConfig.INSTANCE.vanillaOrder)
prefixes.add("@%s.%s".formatted(mod, type));
else
prefixes.add("@%s.%s".formatted(type, mod));
modMembersJson.add("prefix", prefixes);
modMembersJson.addProperty("body", "\"%s:${1|%s|}\"".formatted(mod, String.join(",", modMembers)));
resultJson.add("%s_%s".formatted(type, mod), modMembersJson);
Expand All @@ -68,7 +75,10 @@ public JsonObject toSnippet() {
byModMembers.forEach((mod, modMembers) -> {
JsonObject modMembersJson = new JsonObject();
JsonArray prefixes = new JsonArray();
prefixes.add("@%s.tags.%s".formatted(mod, type));
if (ProbeConfig.INSTANCE.vanillaOrder)
prefixes.add("@%s.tags.%s".formatted(mod, type));
else
prefixes.add("@%s.tags.%s".formatted(type, mod));
modMembersJson.add("prefix", prefixes);
modMembersJson.addProperty("body", "\"#%s:${1|%s|}\"".formatted(mod, String.join(",", modMembers)));
resultJson.add("%s_tag_%s".formatted(type, mod), modMembersJson);
Expand All @@ -83,12 +93,7 @@ public JsonObject toSnippet() {
public static void compile() throws IOException {
Path kubePath = KubeJSPaths.EXPORTED.resolve("kubejs-server-export.json");
if (kubePath.toFile().canRead()) {
Path codePath = KubeJSPaths.DIRECTORY.resolve(".vscode");
if (Files.notExists(codePath)) {
Files.createDirectories(codePath);
}
Path codeFile = codePath.resolve("probe.code-snippets");

Path codeFile = ProbePaths.SNIPPET.resolve("probe.code-snippets");
Gson gson = new Gson();
Reader reader = Files.newBufferedReader(kubePath);
KubeDump kubeDump = gson.fromJson(reader, KubeDump.class);
Expand All @@ -98,4 +103,24 @@ public static void compile() throws IOException {

}
}

public static void compileClassNames() throws IOException {
JsonObject resultJson = new JsonObject();
for (Map.Entry<String, NameResolver.ResolvedName> entry : NameResolver.resolvedNames.entrySet()) {
String className = entry.getKey();
NameResolver.ResolvedName resolvedName = entry.getValue();
JsonObject classJson = new JsonObject();
JsonArray prefix = new JsonArray();
prefix.add("!%s".formatted(resolvedName.getFullName()));
classJson.add("prefix", prefix);
classJson.addProperty("body", className);
resultJson.add(resolvedName.getFullName(), classJson);
}

Path codeFile = ProbePaths.SNIPPET.resolve("classNames.code-snippets");
Gson gson = new Gson();
BufferedWriter writer = Files.newBufferedWriter(codeFile);
gson.toJson(resultJson, writer);
writer.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static void compileGlobal(DummyBindingEvent bindingEvent, Set<Class<?>> g
Map<String, List<IFormatter>> namespaced = new HashMap<>();

for (Class<?> clazz : globalClasses) {
FormatterClass formatter = new FormatterClass(new ClassInfo(clazz));
FormatterClass formatter = new FormatterClass(ClassInfo.getOrCache(clazz));
Manager.classDocuments.getOrDefault(clazz.getName(), new ArrayList<>()).forEach(formatter::setDocument);

NameResolver.ResolvedName name = NameResolver.getResolvedName(clazz.getName());
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/prunoideae/probejs/document/type/Resolver.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.prunoideae.probejs.document.type;

import com.prunoideae.probejs.info.TypeInfo;
import com.prunoideae.probejs.info.type.*;
import com.prunoideae.probejs.util.Pair;
import com.prunoideae.probejs.util.StringUtil;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -36,23 +35,23 @@ public static IType resolveType(String type) {
return new TypeNamed(type);
}

public static boolean typeEquals(IType docType, TypeInfo param) {
public static boolean typeEquals(IType docType, ITypeInfo param) {
if (docType instanceof TypeUnion || docType instanceof TypeIntersection)
return false;
if (docType instanceof TypeArray && param.isArray())
return typeEquals(((TypeArray) docType).getComponent(), param.getComponent());
if (docType instanceof TypeParameterized && param.isParameterized()) {
List<TypeInfo> paramInfo = param.getParameterizedInfo();
if (docType instanceof TypeArray && param instanceof TypeInfoArray array)
return typeEquals(((TypeArray) docType).getComponent(), array.getBaseType());
if (docType instanceof TypeParameterized && param instanceof TypeInfoParameterized parameterized) {
List<ITypeInfo> paramInfo = parameterized.getParamTypes();
List<IType> paramDoc = ((TypeParameterized) docType).getParamTypes();
if (paramDoc.size() != paramInfo.size())
return false;
for (int i = 0; i < paramDoc.size(); i++) {
if (!typeEquals(paramDoc.get(i), paramInfo.get(i)))
return false;
}
return typeEquals(((TypeParameterized) docType).getRawType(), new TypeInfo(param.getRawType()));
return typeEquals(((TypeParameterized) docType).getRawType(), parameterized.getBaseType());
}
if (docType instanceof TypeNamed && (param.isVariable() || param.isClazz()))
if (docType instanceof TypeNamed && (param instanceof TypeInfoVariable || param instanceof TypeInfoClass))
return ((TypeNamed) docType).getRawTypeName().equals(param.getTypeName());

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public static void skipClass(Class<?>... clazz) {
}

public static boolean acceptMethod(String methodName) {
return !Pattern.matches("^[fm]_[\\d_]+$", methodName);
return !methodName.equals("constructor") && !Pattern.matches("^[fm]_[\\d_]+$", methodName);
}

public static boolean acceptField(String fieldName) {
return !Pattern.matches("^[fm]_[\\d_]+$", fieldName);
return !fieldName.equals("constructor") && !Pattern.matches("^[fm]_[\\d_]+$", fieldName);
}

public static void init() {
Expand All @@ -31,7 +31,6 @@ public static void init() {
skipClass(Byte.class, Byte.TYPE);
skipClass(Double.class, Double.TYPE, Float.class, Float.TYPE);
skipClass(Boolean.class, Boolean.TYPE);
skipClass(Map.class);
}


Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/prunoideae/probejs/formatter/NameResolver.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.prunoideae.probejs.formatter;

import com.google.gson.Gson;
import com.prunoideae.probejs.info.TypeInfo;
import com.prunoideae.probejs.info.type.ITypeInfo;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class NameResolver {
public static class ResolvedName {
public static final ResolvedName UNRESOLVED = new ResolvedName(List.of("Unknown"));
public static final ResolvedName UNRESOLVED = new ResolvedName(List.of("any"));
private final List<String> names;

private ResolvedName(List<String> names) {
Expand Down Expand Up @@ -50,7 +50,7 @@ public String toString() {
}

public static final HashMap<String, ResolvedName> resolvedNames = new HashMap<>();
public static final HashMap<Class<?>, Function<TypeInfo, String>> specialTypeFormatters = new HashMap<>();
public static final HashMap<Class<?>, Function<ITypeInfo, String>> specialTypeFormatters = new HashMap<>();
public static final HashMap<Class<?>, Function<Object, String>> specialValueFormatters = new HashMap<>();
public static final Set<String> keywords = new HashSet<>();

Expand All @@ -75,10 +75,20 @@ public static ResolvedName getResolvedName(String className) {
return resolvedNames.getOrDefault(className, ResolvedName.UNRESOLVED);
}

public static void putTypeFormatter(Class<?> className, Function<TypeInfo, String> formatter) {
public static void putTypeFormatter(Class<?> className, Function<ITypeInfo, String> formatter) {
specialTypeFormatters.put(className, formatter);
}

public static boolean isTypeSpecial(Class<?> clazz) {
if (specialTypeFormatters.containsKey(clazz))
return true;
for (Class<?> key : specialTypeFormatters.keySet()) {
if (key.isAssignableFrom(clazz))
return true;
}
return false;
}

public static void putValueFormatter(Function<Object, String> transformer, Class<?>... classes) {
for (Class<?> clazz : classes)
specialValueFormatters.put(clazz, transformer);
Expand Down Expand Up @@ -119,7 +129,7 @@ public static String getNameSafe(String kw) {
}

public static void init() {
putResolvedName(Object.class, "object");
putResolvedName(Object.class, "any");
putResolvedName(String.class, "string");
putResolvedName(Character.class, "string");
putResolvedName(Character.TYPE, "string");
Expand All @@ -143,7 +153,6 @@ public static void init() {

putResolvedName(Boolean.class, "boolean");
putResolvedName(Boolean.TYPE, "boolean");
putResolvedName(Map.class, "Map");

Gson gson = new Gson();

Expand Down
Loading

0 comments on commit 54f457b

Please sign in to comment.