Create from Usage: Suggest "Create function/secondary constructor" quick fix on argument type mismatch

#KT-11864 Fixed
(cherry picked from commit 1912783)
This commit is contained in:
Alexey Sedunov
2016-06-15 17:55:13 +03:00
parent 5296b61ce9
commit 69704c017f
13 changed files with 57 additions and 4 deletions
+1
View File
@@ -153,6 +153,7 @@
- [`KT-11768`](https://youtrack.jetbrains.com/issue/KT-11768) "Introduce local variable" intention
- [`KT-11806`](https://youtrack.jetbrains.com/issue/KT-11806) Quick-fix to increase visibility for invisible member
- [`KT-11807`](https://youtrack.jetbrains.com/issue/KT-11807) Use function body template when generating overriding functions with default body
- [`KT-11864`](https://youtrack.jetbrains.com/issue/KT-11864) Suggest "Create function/secondary constructor" quick fix on argument type mismatch
- [`KT-11876`](https://youtrack.jetbrains.com/issue/KT-11876) Quickfix for "Extension function type is not allowed as supertype" error
- [`KT-11920`](https://youtrack.jetbrains.com/issue/KT-11920) "Increase visibility" and "Decrease visibility" quickfixes for exposed visibility errors
- [`KT-12089`](https://youtrack.jetbrains.com/issue/KT-12089) Quickfix "Make primary constructor parameter a property"
@@ -284,6 +284,8 @@ class QuickFixRegistrar : QuickFixContributor {
TOO_MANY_ARGUMENTS.registerFactory(*CreateCallableFromCallActionFactory.INSTANCES)
EXPRESSION_EXPECTED_PACKAGE_FOUND.registerFactory(*CreateCallableFromCallActionFactory.INSTANCES)
NONE_APPLICABLE.registerFactory(*CreateCallableFromCallActionFactory.INSTANCES)
TYPE_MISMATCH.registerFactory(CreateCallableFromCallActionFactory.Function)
TYPE_MISMATCH.registerFactory(CreateCallableFromCallActionFactory.Constructor)
NO_VALUE_FOR_PARAMETER.registerFactory(CreateConstructorFromDelegationCallActionFactory)
TOO_MANY_ARGUMENTS.registerFactory(CreateConstructorFromDelegationCallActionFactory)
@@ -31,10 +31,7 @@ import org.jetbrains.kotlin.idea.refactoring.getExtractionContainers
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getAssignmentByLHS
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypeAndBranch
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.getReferenceTargets
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
@@ -74,6 +71,10 @@ sealed class CreateCallableFromCallActionFactory<E : KtExpression>(
Errors.TOO_MANY_ARGUMENTS,
Errors.NONE_APPLICABLE -> diagElement.getNonStrictParentOfType<KtCallExpression>()
Errors.TYPE_MISMATCH -> diagElement
.getParentOfTypeAndBranch<KtValueArgument> { getArgumentExpression() }
?.getStrictParentOfType<KtCallExpression>()
else -> throw AssertionError("Unexpected diagnostic: ${diagnostic.factory}")
} as? KtExpression
}
@@ -0,0 +1,6 @@
// "Create function 'foo'" "true"
fun foo(n: Int) {}
fun test() {
foo("a<caret>bc")
}
@@ -0,0 +1,10 @@
// "Create function 'foo'" "true"
fun foo(n: Int) {}
fun test() {
foo("abc")
}
fun foo(n: String) {
throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
}
@@ -0,0 +1,7 @@
// "Create secondary constructor" "true"
// ERROR: None of the following functions can be called with the arguments supplied: <br>public constructor Foo(n: Int) defined in Foo<br>public constructor Foo(n: String) defined in Foo
class Foo(val n: Int)
fun test() {
val foo = Foo("a<caret>bc")
}
@@ -0,0 +1,9 @@
// "Create secondary constructor" "true"
// ERROR: None of the following functions can be called with the arguments supplied: <br>public constructor Foo(n: Int) defined in Foo<br>public constructor Foo(n: String) defined in Foo
class Foo(val n: Int) {
constructor(n: String) : this(<caret>)
}
fun test() {
val foo = Foo("abc")
}
@@ -1,5 +1,6 @@
// "Add non-null asserted (!!) call" "false"
// ACTION: Change parameter 's' type of function 'other' to 'String?'
// ACTION: Create function 'other'
// ERROR: Type mismatch: inferred type is String? but Int was expected
fun test() {
val s: String? = ""
@@ -1,6 +1,7 @@
// "Let 'B' implement interface 'A<Int>'" "false"
// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'B'
// ACTION: Convert to expression body
// ACTION: Create function 'foo'
// ERROR: Type mismatch: inferred type is B but A<Int> was expected
package let.implement
@@ -1,6 +1,7 @@
// "Let 'B' implement interface 'A<*>'" "false"
// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'B'
// ACTION: Convert to expression body
// ACTION: Create function 'foo'
// ERROR: Type mismatch: inferred type is B but A<*> was expected
package let.implement
@@ -1,6 +1,7 @@
// "Let 'B' implement interface 'A<Int>'" "false"
// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'B'
// ACTION: Convert to expression body
// ACTION: Create function 'foo'
// ERROR: Type mismatch: inferred type is B but A<Int> was expected
package let.implement
@@ -2,6 +2,7 @@
// ACTION: Change parameter 'a' type of function 'let.implement.foo' to 'String'
// ACTION: Convert to expression body
// ACTION: To raw string literal
// ACTION: Create function 'foo'
// ERROR: Type mismatch: inferred type is String but A was expected
package let.implement
@@ -1929,6 +1929,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/createFunction/call"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("argumentTypeMismatch.kt")
public void testArgumentTypeMismatch() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/argumentTypeMismatch.kt");
doTest(fileName);
}
@TestMetadata("callInAnnotationEntry.kt")
public void testCallInAnnotationEntry() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createFunction/call/callInAnnotationEntry.kt");
@@ -2680,6 +2686,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/createFromUsage/createSecondaryConstructor"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("argumentTypeMismatch.kt")
public void testArgumentTypeMismatch() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/argumentTypeMismatch.kt");
doTest(fileName);
}
@TestMetadata("callWithExpectedType.kt")
public void testCallWithExpectedType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/createFromUsage/createSecondaryConstructor/callWithExpectedType.kt");