Extract Function: Add backticks to non-identifiers and references originally surrounded in backticks

#KT-8192 Fixed
This commit is contained in:
Alexey Sedunov
2015-06-23 19:13:58 +03:00
parent c6f6637b25
commit 4e7200d8e8
14 changed files with 132 additions and 5 deletions
@@ -23,6 +23,7 @@ import com.intellij.util.containers.MultiMap
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.idea.core.refactoring.JetNameSuggester
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.ErrorMessage
@@ -82,13 +83,16 @@ class RenameReplacement(override val parameter: Parameter): ParameterReplacement
override fun invoke(e: JetElement): JetElement {
var expressionToReplace = (e.getParent() as? JetThisExpression ?: e).getQualifiedExpressionForSelectorOrThis()
val parameterName = JetPsiUtil.unquoteIdentifier(parameter.nameForRef)
val replacingName =
if (e.getText().startsWith('`') || !JetNameSuggester.isIdentifier(parameterName)) "`$parameterName`" else parameterName
val psiFactory = JetPsiFactory(e)
val replacement =
if (expressionToReplace is JetOperationReferenceExpression) {
psiFactory.createOperationName(parameter.nameForRef)
psiFactory.createOperationName(replacingName)
}
else {
psiFactory.createSimpleName(parameter.nameForRef)
psiFactory.createSimpleName(replacingName)
}
return expressionToReplace.replaced(replacement)
}
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiNameIdentifierOwner
import kotlin.properties.Delegates
import java.util.HashMap
import org.jetbrains.kotlin.idea.codeInsight.JetFileReferencesResolver
@@ -63,7 +64,7 @@ data class ExtractionOptions(
data class ResolveResult(
val originalRefExpr: JetSimpleNameExpression,
val declaration: PsiNamedElement,
val declaration: PsiNameIdentifierOwner,
val descriptor: DeclarationDescriptor,
val resolvedCall: ResolvedCall<*>?
)
@@ -148,7 +149,7 @@ data class ExtractionData(
val resolvedCall = ref.getResolvedCall(context)
val descriptor = context[BindingContext.REFERENCE_TARGET, ref] ?: return
val declaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor) as? PsiNamedElement
val declaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor) as? PsiNameIdentifierOwner
?: if (isExtractableIt(descriptor, context)) itFakeDeclaration
else if (isSynthesizedInvoke(descriptor)) synthesizedInvokeDeclaration else return
@@ -709,7 +709,7 @@ private fun ExtractionData.inferParametersInfo(
}
if (!extractThis) {
parameter.currentName = originalDeclaration.getName()
parameter.currentName = originalDeclaration.getNameIdentifier()?.getText()
}
parameter.refCount++
@@ -0,0 +1,12 @@
// PARAM_TYPES: X
// PARAM_TYPES: Y
// PARAM_DESCRIPTOR: internal final fun X.test(): kotlin.Unit defined in Y
// PARAM_DESCRIPTOR: internal final class Y defined in root package
class X(val x: Int)
// SIBLING:
class Y(val y: Int) {
fun X.test() {
<selection>`x`plus y</selection>
}
}
@@ -0,0 +1,16 @@
// PARAM_TYPES: X
// PARAM_TYPES: Y
// PARAM_DESCRIPTOR: internal final fun X.test(): kotlin.Unit defined in Y
// PARAM_DESCRIPTOR: internal final class Y defined in root package
class X(val x: Int)
// SIBLING:
class Y(val y: Int) {
fun X.test() {
__dummyTestFun__(this, this@Y)
}
}
private fun __dummyTestFun__(x: X, y1: Y) {
x.`x`plus y1.y
}
@@ -0,0 +1,6 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val i: kotlin.Int defined in test
fun test() {
val `i` = 0
<selection>`i`plus i</selection>
}
@@ -0,0 +1,10 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val i: kotlin.Int defined in test
fun test() {
val `i` = 0
__dummyTestFun__(`i`)
}
private fun __dummyTestFun__(`i`: Int) {
`i`plus i
}
@@ -0,0 +1,6 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val 0: kotlin.Int defined in test
fun test() {
val `0` = 0
<selection>`0`plus 1</selection>
}
@@ -0,0 +1,10 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val 0: kotlin.Int defined in test
fun test() {
val `0` = 0
__dummyTestFun__(`0`)
}
private fun __dummyTestFun__(`0`: Int) {
`0`plus 1
}
@@ -0,0 +1,6 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val i: kotlin.Int defined in test
fun test() {
val i = 0
<selection>`i`plus i</selection>
}
@@ -0,0 +1,10 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val i: kotlin.Int defined in test
fun test() {
val i = 0
__dummyTestFun__(`i`)
}
private fun __dummyTestFun__(i: Int) {
`i`plus i
}
@@ -0,0 +1,6 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val i: kotlin.Int defined in test
fun test() {
val i = 0
<selection>i plus `i`</selection>
}
@@ -0,0 +1,10 @@
// PARAM_TYPES: kotlin.Int
// PARAM_DESCRIPTOR: val i: kotlin.Int defined in test
fun test() {
val i = 0
__dummyTestFun__(i)
}
private fun __dummyTestFun__(i: Int) {
i plus `i`
}
@@ -1873,6 +1873,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Misc extends AbstractJetExtractionTest {
@TestMetadata("addPrefixToBackticks.kt")
public void testAddPrefixToBackticks() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/addPrefixToBackticks.kt");
doExtractFunctionTest(fileName);
}
public void testAllFilesPresentInMisc() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/refactoring/extractFunction/parameters/misc"), Pattern.compile("^(.+)\\.kt$"), true);
}
@@ -1895,6 +1901,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest(fileName);
}
@TestMetadata("idWithBackticks.kt")
public void testIdWithBackticks() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/idWithBackticks.kt");
doExtractFunctionTest(fileName);
}
@TestMetadata("idWithRequiredBackticks.kt")
public void testIdWithRequiredBackticks() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/idWithRequiredBackticks.kt");
doExtractFunctionTest(fileName);
}
@TestMetadata("innerClassObject.kt")
public void testInnerClassObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/innerClassObject.kt");
@@ -1979,6 +1997,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest(fileName);
}
@TestMetadata("replaceWithBackticks.kt")
public void testReplaceWithBackticks() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/replaceWithBackticks.kt");
doExtractFunctionTest(fileName);
}
@TestMetadata("replaceWithBackticks2.kt")
public void testReplaceWithBackticks2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/replaceWithBackticks2.kt");
doExtractFunctionTest(fileName);
}
@TestMetadata("thisInObject.kt")
public void testThisInObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/thisInObject.kt");