mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-04 00:21:30 +00:00
Rename: Respect naming conventions in automatic variable rename
#KT-7851 Fixed
This commit is contained in:
@@ -171,6 +171,9 @@
|
||||
|
||||
#### Refactorings
|
||||
|
||||
###### New features
|
||||
- [`KT-7851`](https://youtrack.jetbrains.com/issue/KT-7851) Respect naming conventions in automatic variable rename
|
||||
|
||||
###### Issues fixed
|
||||
- [`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
|
||||
|
||||
@@ -21,6 +21,8 @@ import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.PsiElement
|
||||
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.refactoring.JavaRefactoringSettings
|
||||
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.usageView.UsageInfo
|
||||
import org.jetbrains.kotlin.asJava.toLightClass
|
||||
import org.jetbrains.kotlin.asJava.toLightElements
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
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.source.PsiSourceElement
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
import java.util.*
|
||||
|
||||
class AutomaticVariableRenamer(
|
||||
@@ -85,19 +89,37 @@ class AutomaticVariableRenamer(
|
||||
override fun entityName() = RefactoringBundle.message("entity.name.variable")
|
||||
|
||||
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) {
|
||||
val singular = StringUtil.unpluralize(name)
|
||||
val singular = StringUtil.unpluralize(propertyName)
|
||||
if (singular != null) return singular
|
||||
toUnpluralize.remove(element)
|
||||
}
|
||||
return name
|
||||
return propertyName
|
||||
}
|
||||
|
||||
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)
|
||||
StringUtil.pluralize(canonicalName)
|
||||
StringUtil.pluralize(varName)
|
||||
else
|
||||
canonicalName
|
||||
varName
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
open class Bar : Throwable()
|
||||
|
||||
val bar: Bar = Bar()
|
||||
val bar1: Bar = Bar()
|
||||
val BAR: Bar = Bar()
|
||||
val BAR_1: Bar = Bar()
|
||||
|
||||
val bars: List<Bar> = listOf()
|
||||
val foos1: Array<Bar> = array()
|
||||
val BARs: List<Bar> = listOf()
|
||||
val FOOS_1: Array<Bar> = array()
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val bar: Bar = Bar()
|
||||
|
||||
@@ -2,5 +2,5 @@ class BarImpl : Bar()
|
||||
|
||||
object BarObj : Bar()
|
||||
|
||||
val bar: Bar = Bar()
|
||||
val bars: Array<Bar> = throw Error()
|
||||
val BAR: Bar = Bar()
|
||||
val BARs: Array<Bar> = throw Error()
|
||||
13
idea/testData/refactoring/rename/renameClassWithAutoVarConventions/after/test.kt
vendored
Normal file
13
idea/testData/refactoring/rename/renameClassWithAutoVarConventions/after/test.kt
vendored
Normal file
@@ -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
idea/testData/refactoring/rename/renameClassWithAutoVarConventions/before/test.kt
vendored
Normal file
13
idea/testData/refactoring/rename/renameClassWithAutoVarConventions/before/test.kt
vendored
Normal file
@@ -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()
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@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")
|
||||
public void testRenameCompareTo_CompareTo() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/renameCompareTo/compareTo.test");
|
||||
|
||||
Reference in New Issue
Block a user