Skip to content

Commit 8e81698

Browse files
Improve naming for min/max for loops (#24)
1 parent a6f7366 commit 8e81698

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/FluentGetterSuggester.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.papermc.codebook.lvt.suggestion.context.method.MethodCallContext;
2828
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
2929
import java.io.IOException;
30+
import java.util.Objects;
3031
import java.util.Set;
3132
import java.util.function.IntPredicate;
3233
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -90,7 +91,8 @@ public class FluentGetterSuggester implements LvtSuggester {
9091
if (ignored.contains(name)) {
9192
return null;
9293
}
93-
return name;
94+
final @Nullable String forLoopAdjustedName = SingleVerbSuggester.handleForLoop(name, insn, "min", "max");
95+
return Objects.requireNonNullElse(forLoopAdjustedName, name);
9496
}
9597
return null;
9698
}

codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/SingleVerbSuggester.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
3131
import java.util.List;
3232
import org.checkerframework.checker.nullness.qual.Nullable;
33+
import org.objectweb.asm.tree.AbstractInsnNode;
34+
import org.objectweb.asm.tree.LineNumberNode;
35+
import org.objectweb.asm.tree.MethodInsnNode;
3336

3437
/*
3538
This matches against methods with a set prefix and trims that prefix off of the
@@ -49,6 +52,32 @@ public class SingleVerbSuggester implements LvtSuggester {
4952
return null;
5053
}
5154

52-
return parseSimpleTypeNameFromMethod(methodName, prefix.length());
55+
final @Nullable String newName = handleForLoop(methodName, insn, "getMin", "getMax");
56+
return newName != null ? newName : parseSimpleTypeNameFromMethod(methodName, prefix.length());
57+
}
58+
59+
public static @Nullable String handleForLoop(
60+
final String methodName, final MethodInsnContext insn, final String minPrefix, final String maxPrefix) {
61+
@Nullable String newName = handleForLoopPrefix(methodName, insn.node(), minPrefix, maxPrefix);
62+
if (newName == null) {
63+
newName = handleForLoopPrefix(methodName, insn.node(), maxPrefix, minPrefix);
64+
}
65+
return newName;
66+
}
67+
68+
private static @Nullable String handleForLoopPrefix(
69+
final String methodName, final MethodInsnNode methodInsnNode, final String first, final String second) {
70+
if (methodName.startsWith(first)) {
71+
@Nullable
72+
AbstractInsnNode nextInsn = methodInsnNode.getNext(); // look for getMin/MaxXXX call on the same line
73+
while (nextInsn != null && !(nextInsn instanceof LineNumberNode)) {
74+
if (nextInsn instanceof final MethodInsnNode afterMethodInvoke
75+
&& afterMethodInvoke.name.startsWith(second)) {
76+
return parseSimpleTypeNameFromMethod(methodName, first.length());
77+
}
78+
nextInsn = nextInsn.getNext();
79+
}
80+
}
81+
return null;
5382
}
5483
}

0 commit comments

Comments
 (0)