Rename: Replace name identifiers via AST to prevent unwanted reformatting

#KT-8563 Fixed
This commit is contained in:
Alexey Sedunov
2018-01-30 14:43:45 +03:00
parent 58bf42c1dd
commit caeb594e09
6 changed files with 31 additions and 9 deletions
@@ -92,7 +92,7 @@ abstract class KtNamedDeclarationStub<T extends KotlinStubWithFqName<?>> extends
PsiElement newIdentifier = KtPsiFactory(this).createNameIdentifierIfPossible(name);
if (newIdentifier != null) {
identifier.replace(newIdentifier);
KtPsiUtilKt.astReplace(identifier, newIdentifier);
}
else {
identifier.delete();
@@ -568,4 +568,6 @@ fun KtExpression.getLabeledParent(labelName: String): KtLabeledExpression? {
}
}
return null
}
}
fun PsiElement.astReplace(newElement: PsiElement) = parent.node.replaceChild(node, newElement.node)
@@ -40,10 +40,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.isOneSegmentFQN
import org.jetbrains.kotlin.plugin.references.SimpleNameReferenceExtension
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementOrCallableRef
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DataClassDescriptorResolver
import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor
@@ -135,7 +132,12 @@ class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSimpleRefere
}
}
nameElement.replace(element)
if (element.node.elementType == KtTokens.IDENTIFIER) {
nameElement.astReplace(element)
}
else {
nameElement.replace(element)
}
return expression
}
@@ -0,0 +1,7 @@
public class OneLineKotlin {
var <caret>subject = 1
var aux = 1
public fun simple0() { subject }
public fun simple1() { subject = 2 }
public fun simple2() { aux = subject }
}
@@ -0,0 +1,7 @@
public class OneLineKotlin {
var subject2 = 1
var aux = 1
public fun simple0() { subject2 }
public fun simple1() { subject2 = 2 }
public fun simple2() { aux = subject2 }
}
@@ -11,6 +11,7 @@ import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.impl.SimpleDataContext
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.refactoring.rename.inplace.MemberInplaceRenameHandler
import com.intellij.refactoring.rename.inplace.VariableInplaceRenameHandler
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
import com.intellij.testFramework.fixtures.CodeInsightTestUtil
@@ -81,6 +82,10 @@ class InplaceRenameTest : LightPlatformCodeInsightTestCase() {
doTestInplaceRename("name1")
}
fun testNoReformat() {
doTestInplaceRename("subject2", MemberInplaceRenameHandler())
}
private fun doTestImplicitLambdaParameter(newName: String) {
configureByFile(getTestName(false) + ".kt")
@@ -131,7 +136,7 @@ class InplaceRenameTest : LightPlatformCodeInsightTestCase() {
checkResultByFile(getTestName(false) + ".kt.after")
}
private fun doTestInplaceRename(newName: String?) {
private fun doTestInplaceRename(newName: String?, handler: VariableInplaceRenameHandler = VariableInplaceRenameHandler()) {
configureByFile(getTestName(false) + ".kt")
val element = TargetElementUtil.findTargetElement(
LightPlatformCodeInsightTestCase.myEditor,
@@ -143,7 +148,6 @@ class InplaceRenameTest : LightPlatformCodeInsightTestCase() {
val dataContext = SimpleDataContext.getSimpleContext(CommonDataKeys.PSI_ELEMENT.name, element!!,
LightPlatformCodeInsightTestCase.getCurrentEditorDataContext())
val handler = VariableInplaceRenameHandler()
if (newName == null) {
assertFalse(handler.isRenaming(dataContext), "In-place rename is allowed for " + element)
}