AddFunctionParametersFix: use argument name as parameter name if argument is referenced variable
#KT-14021 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
8b8059acdd
commit
19d1532dc7
@@ -37,9 +37,7 @@ import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatu
|
|||||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinMethodDescriptor
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinMethodDescriptor
|
||||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.modify
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.modify
|
||||||
import org.jetbrains.kotlin.idea.refactoring.changeSignature.runChangeSignature
|
import org.jetbrains.kotlin.idea.refactoring.changeSignature.runChangeSignature
|
||||||
import org.jetbrains.kotlin.psi.KtCallElement
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
|
||||||
import org.jetbrains.kotlin.psi.ValueArgument
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
|
||||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
|
||||||
@@ -60,14 +58,16 @@ abstract class ChangeFunctionSignatureFix(
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected fun getNewArgumentName(argument: ValueArgument, validator: Function1<String, Boolean>): String {
|
protected fun getNewArgumentName(argument: ValueArgument, validator: Function1<String, Boolean>): String {
|
||||||
val argumentName = argument.getArgumentName()
|
val expression = KtPsiUtil.deparenthesize(argument.getArgumentExpression())
|
||||||
val expression = argument.getArgumentExpression()
|
val argumentName = argument.getArgumentName()?.asName?.asString()
|
||||||
|
?: (expression as? KtNameReferenceExpression)?.getReferencedName()?.takeIf { it != "it" && it != "field" }
|
||||||
|
|
||||||
return when {
|
return when {
|
||||||
argumentName != null -> KotlinNameSuggester.suggestNameByName(argumentName.asName.asString(), validator)
|
argumentName != null -> KotlinNameSuggester.suggestNameByName(argumentName, validator)
|
||||||
expression != null -> {
|
expression != null -> {
|
||||||
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
|
val bindingContext = expression.analyze(BodyResolveMode.PARTIAL)
|
||||||
if (expression.text == "it") {
|
val expressionText = expression.text
|
||||||
|
if (expressionText == "it" || expressionText == "field") {
|
||||||
val type = expression.getType(bindingContext)
|
val type = expression.getType(bindingContext)
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
return KotlinNameSuggester.suggestNamesByType(type, validator, "param").first()
|
return KotlinNameSuggester.suggestNamesByType(type, validator, "param").first()
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo() {}
|
||||||
|
|
||||||
|
fun test(isObject: Boolean) {
|
||||||
|
foo(isObject<caret>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo(isObject: Boolean) {}
|
||||||
|
|
||||||
|
fun test(isObject: Boolean) {
|
||||||
|
foo(isObject)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add parameter to function 'bar'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun bar(isObject: Boolean) {}
|
||||||
|
|
||||||
|
fun test(isObject: Boolean) {
|
||||||
|
bar(true, isObject<caret>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add parameter to function 'bar'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun bar(isObject: Boolean, isObject1: Boolean) {}
|
||||||
|
|
||||||
|
fun test(isObject: Boolean) {
|
||||||
|
bar(true, isObject)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo() {}
|
||||||
|
|
||||||
|
fun test(isObject: Boolean) {
|
||||||
|
foo((isObject)<caret>)
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo(isObject: Boolean) {}
|
||||||
|
|
||||||
|
fun test(isObject: Boolean) {
|
||||||
|
foo((isObject))
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo() {}
|
||||||
|
|
||||||
|
fun bar(f: (String) -> Unit) {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
bar {
|
||||||
|
foo(it<caret>)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo(s: String) {}
|
||||||
|
|
||||||
|
fun bar(f: (String) -> Unit) {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
bar {
|
||||||
|
foo(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo() {}
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
val x: String = ""
|
||||||
|
get() {
|
||||||
|
foo(field<caret>)
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// "Add parameter to function 'foo'" "true"
|
||||||
|
// DISABLE-ERRORS
|
||||||
|
fun foo(s: String) {}
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
val x: String = ""
|
||||||
|
get() {
|
||||||
|
foo(field)
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2253,6 +2253,31 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterAndChangeTypes.kt");
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterAndChangeTypes.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addFunctionParameterForReferencedArgument.kt")
|
||||||
|
public void testAddFunctionParameterForReferencedArgument() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForReferencedArgument.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addFunctionParameterForReferencedArgument2.kt")
|
||||||
|
public void testAddFunctionParameterForReferencedArgument2() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForReferencedArgument2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addFunctionParameterForReferencedArgument3.kt")
|
||||||
|
public void testAddFunctionParameterForReferencedArgument3() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForReferencedArgument3.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addFunctionParameterForReferencedArgument4.kt")
|
||||||
|
public void testAddFunctionParameterForReferencedArgument4() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForReferencedArgument4.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("addFunctionParameterForReferencedArgument5.kt")
|
||||||
|
public void testAddFunctionParameterForReferencedArgument5() throws Exception {
|
||||||
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForReferencedArgument5.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("addFunctionParameterForTypeMismatch1.kt")
|
@TestMetadata("addFunctionParameterForTypeMismatch1.kt")
|
||||||
public void testAddFunctionParameterForTypeMismatch1() throws Exception {
|
public void testAddFunctionParameterForTypeMismatch1() throws Exception {
|
||||||
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForTypeMismatch1.kt");
|
runTest("idea/testData/quickfix/changeSignature/addFunctionParameterForTypeMismatch1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user