Extract Function: Add backticks to non-identifiers and references originally surrounded in backticks
#KT-8192 Fixed
This commit is contained in:
+6
-2
@@ -23,6 +23,7 @@ import com.intellij.util.containers.MultiMap
|
|||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
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.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
|
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
|
||||||
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.ErrorMessage
|
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 {
|
override fun invoke(e: JetElement): JetElement {
|
||||||
var expressionToReplace = (e.getParent() as? JetThisExpression ?: e).getQualifiedExpressionForSelectorOrThis()
|
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 psiFactory = JetPsiFactory(e)
|
||||||
val replacement =
|
val replacement =
|
||||||
if (expressionToReplace is JetOperationReferenceExpression) {
|
if (expressionToReplace is JetOperationReferenceExpression) {
|
||||||
psiFactory.createOperationName(parameter.nameForRef)
|
psiFactory.createOperationName(replacingName)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
psiFactory.createSimpleName(parameter.nameForRef)
|
psiFactory.createSimpleName(replacingName)
|
||||||
}
|
}
|
||||||
return expressionToReplace.replaced(replacement)
|
return expressionToReplace.replaced(replacement)
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
|||||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import com.intellij.psi.PsiNameIdentifierOwner
|
||||||
import kotlin.properties.Delegates
|
import kotlin.properties.Delegates
|
||||||
import java.util.HashMap
|
import java.util.HashMap
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.JetFileReferencesResolver
|
import org.jetbrains.kotlin.idea.codeInsight.JetFileReferencesResolver
|
||||||
@@ -63,7 +64,7 @@ data class ExtractionOptions(
|
|||||||
|
|
||||||
data class ResolveResult(
|
data class ResolveResult(
|
||||||
val originalRefExpr: JetSimpleNameExpression,
|
val originalRefExpr: JetSimpleNameExpression,
|
||||||
val declaration: PsiNamedElement,
|
val declaration: PsiNameIdentifierOwner,
|
||||||
val descriptor: DeclarationDescriptor,
|
val descriptor: DeclarationDescriptor,
|
||||||
val resolvedCall: ResolvedCall<*>?
|
val resolvedCall: ResolvedCall<*>?
|
||||||
)
|
)
|
||||||
@@ -148,7 +149,7 @@ data class ExtractionData(
|
|||||||
|
|
||||||
val resolvedCall = ref.getResolvedCall(context)
|
val resolvedCall = ref.getResolvedCall(context)
|
||||||
val descriptor = context[BindingContext.REFERENCE_TARGET, ref] ?: return
|
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
|
?: if (isExtractableIt(descriptor, context)) itFakeDeclaration
|
||||||
else if (isSynthesizedInvoke(descriptor)) synthesizedInvokeDeclaration else return
|
else if (isSynthesizedInvoke(descriptor)) synthesizedInvokeDeclaration else return
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -709,7 +709,7 @@ private fun ExtractionData.inferParametersInfo(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!extractThis) {
|
if (!extractThis) {
|
||||||
parameter.currentName = originalDeclaration.getName()
|
parameter.currentName = originalDeclaration.getNameIdentifier()?.getText()
|
||||||
}
|
}
|
||||||
|
|
||||||
parameter.refCount++
|
parameter.refCount++
|
||||||
|
|||||||
+12
@@ -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>
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
@@ -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>
|
||||||
|
}
|
||||||
+10
@@ -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
|
||||||
|
}
|
||||||
+6
@@ -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>
|
||||||
|
}
|
||||||
+10
@@ -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
|
||||||
|
}
|
||||||
+6
@@ -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>
|
||||||
|
}
|
||||||
+10
@@ -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
|
||||||
|
}
|
||||||
+6
@@ -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>
|
||||||
|
}
|
||||||
+10
@@ -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`
|
||||||
|
}
|
||||||
+30
@@ -1873,6 +1873,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
public static class Misc extends AbstractJetExtractionTest {
|
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 {
|
public void testAllFilesPresentInMisc() throws Exception {
|
||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/refactoring/extractFunction/parameters/misc"), Pattern.compile("^(.+)\\.kt$"), true);
|
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);
|
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")
|
@TestMetadata("innerClassObject.kt")
|
||||||
public void testInnerClassObject() throws Exception {
|
public void testInnerClassObject() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/innerClassObject.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/innerClassObject.kt");
|
||||||
@@ -1979,6 +1997,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
doExtractFunctionTest(fileName);
|
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")
|
@TestMetadata("thisInObject.kt")
|
||||||
public void testThisInObject() throws Exception {
|
public void testThisInObject() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/thisInObject.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/thisInObject.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user