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 Nov 18, 2023
1 parent c17d6f5 commit 2381de6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@

package io.papermc.codebook.lvt.suggestion;

import static io.papermc.codebook.lvt.LvtUtil.decapitalize;
import static io.papermc.codebook.lvt.LvtUtil.tryMatchPrefix;

import io.papermc.codebook.lvt.suggestion.context.ContainerContext;
import io.papermc.codebook.lvt.suggestion.context.method.MethodCallContext;
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;

import static io.papermc.codebook.lvt.LvtUtil.decapitalize;
import static io.papermc.codebook.lvt.LvtUtil.tryMatchPrefix;

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

return decapitalize(methodName, prefix.length());
@Nullable String newName = this.handleForLoopPrefix(methodName, insn.node(), "getMin", "getMax");
if (newName == null) {
newName = this.handleForLoopPrefix(methodName, insn.node(), "getMax", "getMin");
}
return newName != null ? newName : decapitalize(methodName, prefix.length());
}

private @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 decapitalize(methodName, first.length());
}
nextInsn = nextInsn.getNext();
}
}
return null;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group = io.papermc.codebook
version = 1.0.7
version = 1.0.8-SNAPSHOT

0 comments on commit 2381de6

Please sign in to comment.