Skip to content

Commit

Permalink
Improve naming for min/max for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Jan 14, 2024
1 parent a6f7366 commit 6d62b6a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.papermc.codebook.lvt.suggestion.context.method.MethodCallContext;
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;
import java.util.function.IntPredicate;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -90,7 +91,8 @@ public class FluentGetterSuggester implements LvtSuggester {
if (ignored.contains(name)) {
return null;
}
return name;
final @Nullable String forLoopAdjustedName = SingleVerbSuggester.handleForLoop(name, insn, "min", "max");
return Objects.requireNonNullElse(forLoopAdjustedName, name);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodInsnNode;

/*
This matches against methods with a set prefix and trims that prefix off of the
Expand All @@ -49,6 +52,28 @@ public class SingleVerbSuggester implements LvtSuggester {
return null;
}

return parseSimpleTypeNameFromMethod(methodName, prefix.length());
final @Nullable String newName = handleForLoop(methodName, insn, "getMin", "getMax");
return newName != null ? newName : parseSimpleTypeNameFromMethod(methodName, prefix.length());
}

public static @Nullable String handleForLoop(final String methodName, final MethodInsnContext insn, final String minPrefix, final String maxPrefix) {
@Nullable String newName = handleForLoopPrefix(methodName, insn.node(), minPrefix, maxPrefix);
if (newName == null) {
newName = handleForLoopPrefix(methodName, insn.node(), maxPrefix, minPrefix);
}
return newName;
}

private static @Nullable String handleForLoopPrefix(final String methodName, final MethodInsnNode methodInsnNode, final String first, final String second) {
if (methodName.startsWith(first)) {
@Nullable AbstractInsnNode nextInsn = methodInsnNode.getNext(); // look for getMin/MaxXXX call on the same line
while (nextInsn != null && !(nextInsn instanceof LineNumberNode)) {
if (nextInsn instanceof final MethodInsnNode afterMethodInvoke && afterMethodInvoke.name.startsWith(second)) {
return parseSimpleTypeNameFromMethod(methodName, first.length());
}
nextInsn = nextInsn.getNext();
}
}
return null;
}
}

0 comments on commit 6d62b6a

Please sign in to comment.