Skip to content

Commit 54f457b

Browse files
committed
reworked things
will release tomorrow
1 parent a3cb52a commit 54f457b

30 files changed

+873
-294
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ apply from: 'https://files.latmod.com/public/markdown-git-changelog.gradle'
99
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_17
1010

1111
def ENV = System.getenv()
12-
version = "${mod_version}-build.54"
12+
version = "${mod_version}-build.57"
1313
archivesBaseName = project.archives_base_name
1414
group = project.maven_group
1515

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ org.gradle.daemon=false
33
loom.platform=forge
44
mod_id=probejs
55
archives_base_name=probejs
6-
mod_version=2.1.0
6+
mod_version=2.2.0
77
maven_group=com.prunoideae
88
mod_author=Prunoideae
99
minecraft_version=1.18.1

src/main/java/com/prunoideae/probejs/ProbeCommands.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.prunoideae.probejs.document.parser.processor.DocumentProviderHandler;
1212
import com.prunoideae.probejs.formatter.ClassResolver;
1313
import com.prunoideae.probejs.formatter.NameResolver;
14+
import com.prunoideae.probejs.info.ClassInfo;
1415
import dev.latvian.mods.kubejs.KubeJSPaths;
1516
import dev.latvian.mods.kubejs.server.ServerSettings;
1617
import net.minecraft.commands.CommandSourceStack;
@@ -27,20 +28,24 @@
2728

2829
public class ProbeCommands {
2930
public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
31+
3032
dispatcher.register(
3133
Commands.literal("probejs")
3234
.then(Commands.literal("dump")
3335
.requires(source -> source.getServer().isSingleplayer() || source.hasPermission(2))
3436
.executes(context -> {
3537
try {
36-
export(context.getSource());
38+
if (ProbeConfig.INSTANCE.autoExport)
39+
export(context.getSource());
3740
SnippetCompiler.compile();
3841
DocumentProviderHandler.init();
3942
CommentHandler.init();
4043
Manager.init();
4144
ClassResolver.init();
4245
NameResolver.init();
4346
TypingCompiler.compile();
47+
if (ProbeConfig.INSTANCE.exportClassNames)
48+
SnippetCompiler.compileClassNames();
4449
} catch (Exception e) {
4550
e.printStackTrace();
4651
context.getSource().sendSuccess(new TextComponent("Uncaught exception happened in wrapper, please report to the Github issue with complete latest.log."), false);
@@ -77,6 +82,24 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
7782
context.getSource().sendSuccess(new TextComponent("Changes will be applied next time you start the game."), false);
7883
return Command.SINGLE_SUCCESS;
7984
}))
85+
.then(Commands.literal("toggle_snippet_order").executes(context -> {
86+
ProbeConfig.INSTANCE.vanillaOrder = !ProbeConfig.INSTANCE.vanillaOrder;
87+
context.getSource().sendSuccess(new TextComponent("In snippets, which will appear first: %s".formatted(ProbeConfig.INSTANCE.vanillaOrder ? "mod_id" : "member_type")), false);
88+
ProbeConfig.INSTANCE.save();
89+
return Command.SINGLE_SUCCESS;
90+
}))
91+
.then(Commands.literal("toggle_classname_snippets").executes(context -> {
92+
ProbeConfig.INSTANCE.exportClassNames = !ProbeConfig.INSTANCE.exportClassNames;
93+
context.getSource().sendSuccess(new TextComponent("Export class name as snippets set to: %s".formatted(ProbeConfig.INSTANCE.exportClassNames)), false);
94+
ProbeConfig.INSTANCE.save();
95+
return Command.SINGLE_SUCCESS;
96+
}))
97+
.then(Commands.literal("toggle_autoexport").executes(context -> {
98+
ProbeConfig.INSTANCE.autoExport = !ProbeConfig.INSTANCE.autoExport;
99+
context.getSource().sendSuccess(new TextComponent("Auto-export for KubeJS set to: %s".formatted(ProbeConfig.INSTANCE.autoExport)), false);
100+
ProbeConfig.INSTANCE.save();
101+
return Command.SINGLE_SUCCESS;
102+
}))
80103
)
81104
);
82105
}

src/main/java/com/prunoideae/probejs/ProbeConfig.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
public class ProbeConfig {
1414
public static ProbeConfig INSTANCE = new ProbeConfig();
1515
private static final Path CONFIG = KubeJSPaths.CONFIG.resolve("probejs.json");
16-
public boolean dumpMethod = false;
16+
public boolean dumpMethod = true;
1717
public boolean disabled = false;
18+
public boolean vanillaOrder = true;
19+
public boolean exportClassNames = false;
20+
public boolean autoExport = true;
21+
1822

1923
private static <E> E fetchPropertyOrDefault(Object key, Map<?, ?> value, E defaultValue) {
2024
Object v = value.get(key);
@@ -26,8 +30,11 @@ private ProbeConfig() {
2630
if (Files.exists(cfg)) {
2731
try {
2832
Map<?, ?> obj = new Gson().fromJson(Files.newBufferedReader(cfg), Map.class);
29-
dumpMethod = fetchPropertyOrDefault("dumpMethod", obj, false);
33+
dumpMethod = fetchPropertyOrDefault("dumpMethod", obj, true);
3034
disabled = fetchPropertyOrDefault("disabled", obj, false);
35+
vanillaOrder = fetchPropertyOrDefault("vanillaOrder", obj, true);
36+
exportClassNames = fetchPropertyOrDefault("exportClassNames", obj, false);
37+
autoExport = fetchPropertyOrDefault("autoExport", obj, true);
3138
} catch (IOException e) {
3239
ProbeJS.LOGGER.warn("Cannot read config properties, falling back to defaults.");
3340
}

src/main/java/com/prunoideae/probejs/ProbePaths.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class ProbePaths {
1313
public static Path DOCS = PROBE.resolve("docs");
1414
public static Path GENERATED = PROBE.resolve("generated");
1515
public static Path USER_DEFINED = PROBE.resolve("user");
16+
public static Path SNIPPET = KubeJSPaths.DIRECTORY.resolve(".vscode");
1617

1718
public static void init() {
1819
if (Files.notExists(PROBE, new LinkOption[0])) {
@@ -27,6 +28,9 @@ public static void init() {
2728
if (Files.notExists(USER_DEFINED, new LinkOption[0])) {
2829
UtilsJS.tryIO(() -> Files.createDirectories(USER_DEFINED));
2930
}
31+
if (Files.notExists(SNIPPET, new LinkOption[0])) {
32+
UtilsJS.tryIO(() -> Files.createDirectories(SNIPPET));
33+
}
3034
}
3135

3236
static {

src/main/java/com/prunoideae/probejs/compiler/SnippetCompiler.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
import com.google.gson.Gson;
44
import com.google.gson.JsonArray;
55
import com.google.gson.JsonObject;
6+
import com.prunoideae.probejs.ProbeConfig;
7+
import com.prunoideae.probejs.ProbePaths;
8+
import com.prunoideae.probejs.formatter.NameResolver;
69
import dev.latvian.mods.kubejs.KubeJSPaths;
710

11+
import javax.json.Json;
812
import java.io.BufferedWriter;
913
import java.io.IOException;
1014
import java.io.Reader;
@@ -48,7 +52,10 @@ public JsonObject toSnippet() {
4852
byModMembers.forEach((mod, modMembers) -> {
4953
JsonObject modMembersJson = new JsonObject();
5054
JsonArray prefixes = new JsonArray();
51-
prefixes.add("@%s.%s".formatted(mod, type));
55+
if (ProbeConfig.INSTANCE.vanillaOrder)
56+
prefixes.add("@%s.%s".formatted(mod, type));
57+
else
58+
prefixes.add("@%s.%s".formatted(type, mod));
5259
modMembersJson.add("prefix", prefixes);
5360
modMembersJson.addProperty("body", "\"%s:${1|%s|}\"".formatted(mod, String.join(",", modMembers)));
5461
resultJson.add("%s_%s".formatted(type, mod), modMembersJson);
@@ -68,7 +75,10 @@ public JsonObject toSnippet() {
6875
byModMembers.forEach((mod, modMembers) -> {
6976
JsonObject modMembersJson = new JsonObject();
7077
JsonArray prefixes = new JsonArray();
71-
prefixes.add("@%s.tags.%s".formatted(mod, type));
78+
if (ProbeConfig.INSTANCE.vanillaOrder)
79+
prefixes.add("@%s.tags.%s".formatted(mod, type));
80+
else
81+
prefixes.add("@%s.tags.%s".formatted(type, mod));
7282
modMembersJson.add("prefix", prefixes);
7383
modMembersJson.addProperty("body", "\"#%s:${1|%s|}\"".formatted(mod, String.join(",", modMembers)));
7484
resultJson.add("%s_tag_%s".formatted(type, mod), modMembersJson);
@@ -83,12 +93,7 @@ public JsonObject toSnippet() {
8393
public static void compile() throws IOException {
8494
Path kubePath = KubeJSPaths.EXPORTED.resolve("kubejs-server-export.json");
8595
if (kubePath.toFile().canRead()) {
86-
Path codePath = KubeJSPaths.DIRECTORY.resolve(".vscode");
87-
if (Files.notExists(codePath)) {
88-
Files.createDirectories(codePath);
89-
}
90-
Path codeFile = codePath.resolve("probe.code-snippets");
91-
96+
Path codeFile = ProbePaths.SNIPPET.resolve("probe.code-snippets");
9297
Gson gson = new Gson();
9398
Reader reader = Files.newBufferedReader(kubePath);
9499
KubeDump kubeDump = gson.fromJson(reader, KubeDump.class);
@@ -98,4 +103,24 @@ public static void compile() throws IOException {
98103

99104
}
100105
}
106+
107+
public static void compileClassNames() throws IOException {
108+
JsonObject resultJson = new JsonObject();
109+
for (Map.Entry<String, NameResolver.ResolvedName> entry : NameResolver.resolvedNames.entrySet()) {
110+
String className = entry.getKey();
111+
NameResolver.ResolvedName resolvedName = entry.getValue();
112+
JsonObject classJson = new JsonObject();
113+
JsonArray prefix = new JsonArray();
114+
prefix.add("!%s".formatted(resolvedName.getFullName()));
115+
classJson.add("prefix", prefix);
116+
classJson.addProperty("body", className);
117+
resultJson.add(resolvedName.getFullName(), classJson);
118+
}
119+
120+
Path codeFile = ProbePaths.SNIPPET.resolve("classNames.code-snippets");
121+
Gson gson = new Gson();
122+
BufferedWriter writer = Files.newBufferedWriter(codeFile);
123+
gson.toJson(resultJson, writer);
124+
writer.flush();
125+
}
101126
}

src/main/java/com/prunoideae/probejs/compiler/TypingCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static void compileGlobal(DummyBindingEvent bindingEvent, Set<Class<?>> g
9090
Map<String, List<IFormatter>> namespaced = new HashMap<>();
9191

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

9696
NameResolver.ResolvedName name = NameResolver.getResolvedName(clazz.getName());

src/main/java/com/prunoideae/probejs/document/type/Resolver.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.prunoideae.probejs.document.type;
22

3-
import com.prunoideae.probejs.info.TypeInfo;
3+
import com.prunoideae.probejs.info.type.*;
44
import com.prunoideae.probejs.util.Pair;
55
import com.prunoideae.probejs.util.StringUtil;
66

7-
import java.lang.reflect.ParameterizedType;
87
import java.util.List;
98
import java.util.stream.Collectors;
109

@@ -36,23 +35,23 @@ public static IType resolveType(String type) {
3635
return new TypeNamed(type);
3736
}
3837

39-
public static boolean typeEquals(IType docType, TypeInfo param) {
38+
public static boolean typeEquals(IType docType, ITypeInfo param) {
4039
if (docType instanceof TypeUnion || docType instanceof TypeIntersection)
4140
return false;
42-
if (docType instanceof TypeArray && param.isArray())
43-
return typeEquals(((TypeArray) docType).getComponent(), param.getComponent());
44-
if (docType instanceof TypeParameterized && param.isParameterized()) {
45-
List<TypeInfo> paramInfo = param.getParameterizedInfo();
41+
if (docType instanceof TypeArray && param instanceof TypeInfoArray array)
42+
return typeEquals(((TypeArray) docType).getComponent(), array.getBaseType());
43+
if (docType instanceof TypeParameterized && param instanceof TypeInfoParameterized parameterized) {
44+
List<ITypeInfo> paramInfo = parameterized.getParamTypes();
4645
List<IType> paramDoc = ((TypeParameterized) docType).getParamTypes();
4746
if (paramDoc.size() != paramInfo.size())
4847
return false;
4948
for (int i = 0; i < paramDoc.size(); i++) {
5049
if (!typeEquals(paramDoc.get(i), paramInfo.get(i)))
5150
return false;
5251
}
53-
return typeEquals(((TypeParameterized) docType).getRawType(), new TypeInfo(param.getRawType()));
52+
return typeEquals(((TypeParameterized) docType).getRawType(), parameterized.getBaseType());
5453
}
55-
if (docType instanceof TypeNamed && (param.isVariable() || param.isClazz()))
54+
if (docType instanceof TypeNamed && (param instanceof TypeInfoVariable || param instanceof TypeInfoClass))
5655
return ((TypeNamed) docType).getRawTypeName().equals(param.getTypeName());
5756

5857
return false;

src/main/java/com/prunoideae/probejs/formatter/ClassResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public static void skipClass(Class<?>... clazz) {
1414
}
1515

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

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

2424
public static void init() {
@@ -31,7 +31,6 @@ public static void init() {
3131
skipClass(Byte.class, Byte.TYPE);
3232
skipClass(Double.class, Double.TYPE, Float.class, Float.TYPE);
3333
skipClass(Boolean.class, Boolean.TYPE);
34-
skipClass(Map.class);
3534
}
3635

3736

src/main/java/com/prunoideae/probejs/formatter/NameResolver.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.prunoideae.probejs.formatter;
22

33
import com.google.gson.Gson;
4-
import com.prunoideae.probejs.info.TypeInfo;
4+
import com.prunoideae.probejs.info.type.ITypeInfo;
55

66
import java.util.*;
77
import java.util.function.Function;
88
import java.util.stream.Collectors;
99

1010
public class NameResolver {
1111
public static class ResolvedName {
12-
public static final ResolvedName UNRESOLVED = new ResolvedName(List.of("Unknown"));
12+
public static final ResolvedName UNRESOLVED = new ResolvedName(List.of("any"));
1313
private final List<String> names;
1414

1515
private ResolvedName(List<String> names) {
@@ -50,7 +50,7 @@ public String toString() {
5050
}
5151

5252
public static final HashMap<String, ResolvedName> resolvedNames = new HashMap<>();
53-
public static final HashMap<Class<?>, Function<TypeInfo, String>> specialTypeFormatters = new HashMap<>();
53+
public static final HashMap<Class<?>, Function<ITypeInfo, String>> specialTypeFormatters = new HashMap<>();
5454
public static final HashMap<Class<?>, Function<Object, String>> specialValueFormatters = new HashMap<>();
5555
public static final Set<String> keywords = new HashSet<>();
5656

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

78-
public static void putTypeFormatter(Class<?> className, Function<TypeInfo, String> formatter) {
78+
public static void putTypeFormatter(Class<?> className, Function<ITypeInfo, String> formatter) {
7979
specialTypeFormatters.put(className, formatter);
8080
}
8181

82+
public static boolean isTypeSpecial(Class<?> clazz) {
83+
if (specialTypeFormatters.containsKey(clazz))
84+
return true;
85+
for (Class<?> key : specialTypeFormatters.keySet()) {
86+
if (key.isAssignableFrom(clazz))
87+
return true;
88+
}
89+
return false;
90+
}
91+
8292
public static void putValueFormatter(Function<Object, String> transformer, Class<?>... classes) {
8393
for (Class<?> clazz : classes)
8494
specialValueFormatters.put(clazz, transformer);
@@ -119,7 +129,7 @@ public static String getNameSafe(String kw) {
119129
}
120130

121131
public static void init() {
122-
putResolvedName(Object.class, "object");
132+
putResolvedName(Object.class, "any");
123133
putResolvedName(String.class, "string");
124134
putResolvedName(Character.class, "string");
125135
putResolvedName(Character.TYPE, "string");
@@ -143,7 +153,6 @@ public static void init() {
143153

144154
putResolvedName(Boolean.class, "boolean");
145155
putResolvedName(Boolean.TYPE, "boolean");
146-
putResolvedName(Map.class, "Map");
147156

148157
Gson gson = new Gson();
149158

0 commit comments

Comments
 (0)