[extract method] IDEA-356707: simplify and speedup name suggestion

GitOrigin-RevId: 5a8baeec399127391f8e2069e6915f322c524458
This commit is contained in:
Alexandr Suhinin
2024-07-31 14:26:46 +00:00
committed by intellij-monorepo-bot
parent 7f81099d23
commit ed6d7fbc6d
6 changed files with 17 additions and 23 deletions
@@ -4,11 +4,11 @@ package com.intellij.refactoring.extractMethod.newImpl
import com.intellij.codeInsight.Nullability
import com.intellij.codeInsight.NullableNotNullManager
import com.intellij.codeInsight.PsiEquivalenceUtil
import com.intellij.codeInsight.generation.GenerateMembersUtil
import com.intellij.codeInsight.intention.AddAnnotationPsiFix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.Conditions
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.*
import com.intellij.psi.codeStyle.JavaCodeStyleManager
import com.intellij.psi.codeStyle.VariableKind
@@ -217,7 +217,7 @@ object ExtractMethodHelper {
fun guessMethodName(options: ExtractOptions): List<String> {
val project = options.project
val initialMethodNames: MutableSet<String> = LinkedHashSet()
val variableNames: MutableSet<String> = LinkedHashSet()
val codeStyleManager = JavaCodeStyleManager.getInstance(project) as JavaCodeStyleManagerImpl
val returnType = options.dataOutput.type
@@ -225,7 +225,7 @@ object ExtractMethodHelper {
if (expression != null || returnType !is PsiPrimitiveType) {
codeStyleManager.suggestVariableName(VariableKind.FIELD, null, expression, returnType).names
.forEach { name ->
initialMethodNames += codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD)
variableNames += codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD)
}
}
@@ -235,19 +235,13 @@ object ExtractMethodHelper {
val propertyName = codeStyleManager.variableNameToPropertyName(outVariable.name!!, outKind)
val names = codeStyleManager.suggestVariableName(VariableKind.FIELD, propertyName, null, outVariable.type).names
names.forEach { name ->
initialMethodNames += codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD)
variableNames += codeStyleManager.variableNameToPropertyName(name, VariableKind.FIELD)
}
}
val normalizedType = (returnType as? PsiEllipsisType)?.toArrayType() ?: returnType
val field = JavaPsiFacade.getElementFactory(project).createField("fieldNameToReplace", normalizedType)
fun suggestGetterName(name: String): String {
field.name = name
return GenerateMembersUtil.suggestGetterName(field)
}
return initialMethodNames.filter { PsiNameHelper.getInstance(project).isIdentifier(it) }
.map { propertyName -> suggestGetterName(propertyName) }
val prefix = if (returnType == PsiTypes.booleanType()) "is" else "get"
return variableNames.filter { PsiNameHelper.getInstance(project).isIdentifier(it) }
.map { variableName -> "$prefix${StringUtil.capitalize(variableName)}" }
}
fun replacePsiRange(source: List<PsiElement>, target: List<PsiElement>): List<PsiElement> {
@@ -1,10 +1,10 @@
class SomeClass {
public boolean test(String filePath) {
return isaBoolean(filePath);
return isABoolean(filePath);
}
private static boolean isaBoolean(String filePath) {
private static boolean isABoolean(String filePath) {
return FileUtilRt.extensionEquals(filePath, "jar") ||
FileUtilRt.extensionEquals(filePath, "zip");
}
@@ -15,11 +15,11 @@ class Child extends Parent {
public boolean test(Collection<File> files) {
return files.stream().allMatch(file -> {
String name = file.getName();
return isaBoolean(name);
return isABoolean(name);
});
}
private static boolean isaBoolean(String name) {
private static boolean isABoolean(String name) {
return name.endsWith("jar") || name.endsWith("tar");
}
}
@@ -1,9 +1,9 @@
public class Test<K> {
public Test<K> test() {
return getkTest(this);
return getKTest(this);
}
private static <K> Test<K> getkTest(Test<K> test) {
private static <K> Test<K> getKTest(Test<K> test) {
return test;
}
}
@@ -1,7 +1,7 @@
public class Test {
void test() {
if (isaBoolean()) {
if (isABoolean()) {
System.out.println();
}
if (getSize2() < getLimit2() && getCondition1()) {
@@ -9,7 +9,7 @@ public class Test {
}
}
private boolean isaBoolean() {
private boolean isABoolean() {
return getSize1() < getLimit1() && getCondition1();
}
@@ -1,7 +1,7 @@
public class Test {
void test() {
if (isaBoolean()) {
if (isABoolean()) {
System.out.println();
}
if (getSize2() < getLimit2()) {
@@ -9,7 +9,7 @@ public class Test {
}
}
private boolean isaBoolean() {
private boolean isABoolean() {
return getSize1() < getLimit1();
}