From 2381de656c427501af0c8b3bb157139e795cbba0 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Fri, 17 Nov 2023 17:09:33 -0800 Subject: [PATCH] Improve naming for min/max for loops --- .../lvt/suggestion/SingleVerbSuggester.java | 28 ++++++++++++++++--- gradle.properties | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/SingleVerbSuggester.java b/codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/SingleVerbSuggester.java index 6d77c2d..3fbddcc 100644 --- a/codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/SingleVerbSuggester.java +++ b/codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/SingleVerbSuggester.java @@ -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 @@ -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; } } diff --git a/gradle.properties b/gradle.properties index c140d1c..bbeb37c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ group = io.papermc.codebook -version = 1.0.7 +version = 1.0.8-SNAPSHOT