Rename: Respect naming conventions in automatic variable rename
#KT-7851 Fixed
This commit is contained in:
@@ -171,6 +171,9 @@
|
|||||||
|
|
||||||
#### Refactorings
|
#### Refactorings
|
||||||
|
|
||||||
|
###### New features
|
||||||
|
- [`KT-7851`](https://youtrack.jetbrains.com/issue/KT-7851) Respect naming conventions in automatic variable rename
|
||||||
|
|
||||||
###### Issues fixed
|
###### Issues fixed
|
||||||
- [`KT-9156`](https://youtrack.jetbrains.com/issue/KT-9156) Quote non-identifier names in Kotlin references
|
- [`KT-9156`](https://youtrack.jetbrains.com/issue/KT-9156) Quote non-identifier names in Kotlin references
|
||||||
- [`KT-9157`](https://youtrack.jetbrains.com/issue/KT-9157) Fixed in-place rename of Kotlin expression referring Java declaration
|
- [`KT-9157`](https://youtrack.jetbrains.com/issue/KT-9157) Fixed in-place rename of Kotlin expression referring Java declaration
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ import com.intellij.openapi.util.text.StringUtil
|
|||||||
import com.intellij.psi.PsiClass
|
import com.intellij.psi.PsiClass
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiNamedElement
|
import com.intellij.psi.PsiNamedElement
|
||||||
|
import com.intellij.psi.PsiVariable
|
||||||
|
import com.intellij.psi.codeStyle.JavaCodeStyleManager
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import com.intellij.refactoring.JavaRefactoringSettings
|
import com.intellij.refactoring.JavaRefactoringSettings
|
||||||
import com.intellij.refactoring.RefactoringBundle
|
import com.intellij.refactoring.RefactoringBundle
|
||||||
@@ -28,6 +30,7 @@ import com.intellij.refactoring.rename.naming.AutomaticRenamer
|
|||||||
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
|
import com.intellij.refactoring.rename.naming.AutomaticRenamerFactory
|
||||||
import com.intellij.usageView.UsageInfo
|
import com.intellij.usageView.UsageInfo
|
||||||
import org.jetbrains.kotlin.asJava.toLightClass
|
import org.jetbrains.kotlin.asJava.toLightClass
|
||||||
|
import org.jetbrains.kotlin.asJava.toLightElements
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||||
@@ -39,6 +42,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
|||||||
import org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException
|
import org.jetbrains.kotlin.resolve.lazy.NoDescriptorForDeclarationException
|
||||||
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class AutomaticVariableRenamer(
|
class AutomaticVariableRenamer(
|
||||||
@@ -85,19 +89,37 @@ class AutomaticVariableRenamer(
|
|||||||
override fun entityName() = RefactoringBundle.message("entity.name.variable")
|
override fun entityName() = RefactoringBundle.message("entity.name.variable")
|
||||||
|
|
||||||
override fun nameToCanonicalName(name: String, element: PsiNamedElement): String? {
|
override fun nameToCanonicalName(name: String, element: PsiNamedElement): String? {
|
||||||
|
if (element !is KtNamedDeclaration) return name
|
||||||
|
|
||||||
|
val psiVariable = element.toLightElements().firstIsInstanceOrNull<PsiVariable>()
|
||||||
|
val propertyName = if (psiVariable != null) {
|
||||||
|
val codeStyleManager = JavaCodeStyleManager.getInstance(psiVariable.project)
|
||||||
|
codeStyleManager.variableNameToPropertyName(name, codeStyleManager.getVariableKind(psiVariable))
|
||||||
|
}
|
||||||
|
else name
|
||||||
|
|
||||||
if (element in toUnpluralize) {
|
if (element in toUnpluralize) {
|
||||||
val singular = StringUtil.unpluralize(name)
|
val singular = StringUtil.unpluralize(propertyName)
|
||||||
if (singular != null) return singular
|
if (singular != null) return singular
|
||||||
toUnpluralize.remove(element)
|
toUnpluralize.remove(element)
|
||||||
}
|
}
|
||||||
return name
|
return propertyName
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun canonicalNameToName(canonicalName: String, element: PsiNamedElement): String? {
|
override fun canonicalNameToName(canonicalName: String, element: PsiNamedElement): String? {
|
||||||
|
if (element !is KtNamedDeclaration) return canonicalName
|
||||||
|
|
||||||
|
val psiVariable = element.toLightElements().firstIsInstanceOrNull<PsiVariable>()
|
||||||
|
val varName = if (psiVariable != null) {
|
||||||
|
val codeStyleManager = JavaCodeStyleManager.getInstance(psiVariable.project)
|
||||||
|
codeStyleManager.propertyNameToVariableName(canonicalName, codeStyleManager.getVariableKind(psiVariable))
|
||||||
|
}
|
||||||
|
else canonicalName
|
||||||
|
|
||||||
return if (element in toUnpluralize)
|
return if (element in toUnpluralize)
|
||||||
StringUtil.pluralize(canonicalName)
|
StringUtil.pluralize(varName)
|
||||||
else
|
else
|
||||||
canonicalName
|
varName
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
open class Bar : Throwable()
|
open class Bar : Throwable()
|
||||||
|
|
||||||
val bar: Bar = Bar()
|
val BAR: Bar = Bar()
|
||||||
val bar1: Bar = Bar()
|
val BAR_1: Bar = Bar()
|
||||||
|
|
||||||
val bars: List<Bar> = listOf()
|
val BARs: List<Bar> = listOf()
|
||||||
val foos1: Array<Bar> = array()
|
val FOOS_1: Array<Bar> = array()
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val bar: Bar = Bar()
|
val bar: Bar = Bar()
|
||||||
|
|||||||
+2
-2
@@ -2,5 +2,5 @@ class BarImpl : Bar()
|
|||||||
|
|
||||||
object BarObj : Bar()
|
object BarObj : Bar()
|
||||||
|
|
||||||
val bar: Bar = Bar()
|
val BAR: Bar = Bar()
|
||||||
val bars: Array<Bar> = throw Error()
|
val BARs: Array<Bar> = throw Error()
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
val SOME_BAR: Bar = Bar()
|
||||||
|
val BAR: Bar = Bar()
|
||||||
|
|
||||||
|
val SOME___BAR: Bar = Bar()
|
||||||
|
val BAR: Bar = Bar()
|
||||||
|
|
||||||
|
val SOME_BAR: Bar = Bar()
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
val SOME_FOO: Foo = Foo()
|
||||||
|
val FOO: Foo = Foo()
|
||||||
|
|
||||||
|
val some_foo: Foo = Foo()
|
||||||
|
val foo: Foo = Foo()
|
||||||
|
|
||||||
|
val SomeFoo: Foo = Foo()
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"type": "KOTLIN_CLASS",
|
||||||
|
"mainFile": "test.kt",
|
||||||
|
"classId": "test/Foo",
|
||||||
|
"newName": "Bar"
|
||||||
|
}
|
||||||
@@ -125,6 +125,12 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("renameClassWithAutoVarConventions/renameClassWithAutoVarConventions.test")
|
||||||
|
public void testRenameClassWithAutoVarConventions_RenameClassWithAutoVarConventions() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameClassWithAutoVarConventions/renameClassWithAutoVarConventions.test");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("renameCompareTo/compareTo.test")
|
@TestMetadata("renameCompareTo/compareTo.test")
|
||||||
public void testRenameCompareTo_CompareTo() throws Exception {
|
public void testRenameCompareTo_CompareTo() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameCompareTo/compareTo.test");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameCompareTo/compareTo.test");
|
||||||
|
|||||||
Reference in New Issue
Block a user