Rename test method when renaming production method

IDEA-154644 review refactor

GitOrigin-RevId: 013fdd5d1bfcdec2e8fb314cce3533918875787c
This commit is contained in:
Georgii Ustinov
2023-10-25 12:17:30 +03:00
committed by intellij-monorepo-bot
parent c4770b10a8
commit 025ca6debb
20 changed files with 151 additions and 109 deletions

View File

@@ -0,0 +1,49 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.rename.naming
import com.intellij.codeInsight.TestFrameworks
import com.intellij.java.refactoring.JavaRefactoringBundle
import com.intellij.openapi.module.Module
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.PsiShortNamesCache
import com.intellij.util.containers.ContainerUtil
import java.util.regex.Pattern
class AutomaticTestMethodRenamer(oldMethodName: String?, className: String?, module: Module?, newMethodName: String) : AutomaticRenamer() {
init {
findMethodsToReplace(oldMethodName, className, module)
suggestAllNames(oldMethodName, newMethodName)
}
private fun findMethodsToReplace(oldMethodName: String?, className: String?, module: Module?) {
if (oldMethodName == null || className == null || module == null) return
val moduleScope = GlobalSearchScope.moduleWithDependentsScope(module)
val cache = PsiShortNamesCache.getInstance(module.getProject())
val classPattern = Pattern.compile(".*$className.*")
val methodPattern = Pattern.compile(".*$oldMethodName.*", Pattern.CASE_INSENSITIVE)
for (eachName in ContainerUtil.newHashSet(*cache.getAllClassNames())) {
if (classPattern.matcher(eachName).matches()) {
for (eachClass in cache.getClassesByName(eachName, moduleScope)) {
if (TestFrameworks.detectFramework(eachClass) != null) {
eachClass.methods.forEach {
if (methodPattern.matcher(it.name).matches()) {
myElements.add(it)
}
}
}
}
}
}
}
override fun getDialogTitle(): String = JavaRefactoringBundle.message("rename.test.method.title")
override fun getDialogDescription(): String = JavaRefactoringBundle.message("rename.test.method.description")
override fun entityName(): String = JavaRefactoringBundle.message("rename.test.method.entity.name")
}

View File

@@ -0,0 +1,8 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.rename.naming
import com.intellij.java.refactoring.JavaRefactoringBundle
abstract class BaseAutomaticTestMethodRenamerFactory : AutomaticRenamerFactory {
override fun getOptionName(): String = JavaRefactoringBundle.message("rename.test.method")
}

View File

@@ -0,0 +1,35 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.refactoring.rename.naming
import com.intellij.codeInsight.TestFrameworks
import com.intellij.openapi.module.ModuleUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.intellij.refactoring.JavaRefactoringSettings
import com.intellij.usageView.UsageInfo
class JavaAutomaticTestMethodRenamerFactory : BaseAutomaticTestMethodRenamerFactory() {
override fun isApplicable(element: PsiElement): Boolean {
if (element !is PsiMethod) return false
val psiClass = element.containingClass ?: return false
return TestFrameworks.detectFramework(psiClass) == null
}
override fun isEnabled(): Boolean = JavaRefactoringSettings.getInstance().isRenameTestMethods
override fun setEnabled(enabled: Boolean) {
JavaRefactoringSettings.getInstance().isRenameTestMethods = enabled
}
override fun createRenamer(element: PsiElement, newName: String, usages: MutableCollection<UsageInfo>): AutomaticRenamer {
val psiMethod = element as? PsiMethod
val psiClass = psiMethod?.containingClass
val module = if (psiClass != null) ModuleUtil.findModuleForPsiElement(psiClass) else null
return AutomaticTestMethodRenamer(
psiMethod?.name,
psiClass?.name,
module,
newName)
}
}