Rename: Forbid rename of synthetic declaration references

#KT-20241 Fixed
This commit is contained in:
Alexey Sedunov
2018-03-02 14:56:32 +03:00
parent 9761e33110
commit e145a64d50
12 changed files with 102 additions and 0 deletions
+1
View File
@@ -2781,6 +2781,7 @@
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameClassByCompanionObjectShortReferenceHandler"/>
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameByLabeledReferenceInLambdaArgumentHandler"/>
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameImportAliasByReferenceHandler"/>
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.RenameSyntheticDeclarationByReferenceHandler"/>
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.KotlinMemberInplaceRenameHandler"/>
<renameHandler implementation="org.jetbrains.kotlin.idea.refactoring.rename.KotlinVariableInplaceRenameHandler"/>
@@ -0,0 +1,38 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.refactoring.rename
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.refactoring.rename.RenameHandler
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
import org.jetbrains.kotlin.resolve.calls.tower.isSynthesized
class RenameSyntheticDeclarationByReferenceHandler : RenameHandler {
override fun isAvailableOnDataContext(dataContext: DataContext?): Boolean {
if (dataContext == null) return false
val file = CommonDataKeys.PSI_FILE.getData(dataContext) ?: return false
val editor = CommonDataKeys.EDITOR.getData(dataContext) ?: return false
val refExpression = file.findElementForRename<KtSimpleNameExpression>(editor.caretModel.offset) ?: return false
return (refExpression.resolveToCall()?.resultingDescriptor)?.isSynthesized ?: false
}
override fun isRenaming(dataContext: DataContext?) = isAvailableOnDataContext(dataContext)
override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) {
CodeInsightUtils.showErrorHint(project, editor, "Rename is not applicable to synthetic declaration", "Rename", null)
}
override fun invoke(project: Project, elements: Array<out PsiElement>, dataContext: DataContext?) {
// Do nothing: this method is not called from editor
}
}
@@ -0,0 +1,5 @@
data class XYZ(val x: Int, val y: Int, val z: Int)
fun test() {
val y = XYZ(1, 2, 3)./*rename*/component2()
}
@@ -0,0 +1,5 @@
data class XYZ(val x: Int, val y: Int, val z: Int)
fun test() {
val y = XYZ(1, 2, 3)./*rename*/component2()
}
@@ -0,0 +1,7 @@
{
"type": "AUTO_DETECT",
"mainFile": "test.kt",
"newName": "component123",
"withRuntime": "true",
"hint": "Rename is not applicable to synthetic declaration"
}
@@ -0,0 +1,5 @@
data class XYZ(val x: Int, val y: Int, val z: Int)
fun test() {
XYZ(1, 2, 3)./*rename*/copy(x = 10)
}
@@ -0,0 +1,5 @@
data class XYZ(val x: Int, val y: Int, val z: Int)
fun test() {
XYZ(1, 2, 3)./*rename*/copy(x = 10)
}
@@ -0,0 +1,7 @@
{
"type": "AUTO_DETECT",
"mainFile": "test.kt",
"newName": "copyNew",
"withRuntime": "true",
"hint": "Rename is not applicable to synthetic declaration"
}
@@ -0,0 +1,2 @@
enum class Foo{BAR, BAZ}
val bar = Foo./*rename*/valueOf("BAR")
@@ -0,0 +1,2 @@
enum class Foo{BAR, BAZ}
val bar = Foo./*rename*/valueOf("BAR")
@@ -0,0 +1,7 @@
{
"type": "AUTO_DETECT",
"mainFile": "test.kt",
"newName": "valueOfNew",
"withRuntime": "true",
"hint": "Rename is not applicable to synthetic declaration"
}
@@ -205,6 +205,24 @@ public class RenameTestGenerated extends AbstractRenameTest {
doTest(fileName);
}
@TestMetadata("dataClassComponentN/dataClassComponentN.test")
public void testDataClassComponentN_DataClassComponentN() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/dataClassComponentN/dataClassComponentN.test");
doTest(fileName);
}
@TestMetadata("dataClassCopy/dataClassCopy.test")
public void testDataClassCopy_DataClassCopy() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/dataClassCopy/dataClassCopy.test");
doTest(fileName);
}
@TestMetadata("enumValueOf/enumValueOf.test")
public void testEnumValueOf_EnumValueOf() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/enumValueOf/enumValueOf.test");
doTest(fileName);
}
@TestMetadata("funTextOccurrences/funTextOccurrences.test")
public void testFunTextOccurrences_FunTextOccurrences() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/funTextOccurrences/funTextOccurrences.test");