From 35fa071e1b31b3c0c6f89fc07d20490c5a63a62e Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Tue, 30 Apr 2013 19:32:21 +0200 Subject: [PATCH 01/19] Better behaviour of "Change Type" quickfixes in case of function types --- .../plugin/quickfix/CastExpressionFix.java | 9 ++++-- .../quickfix/ChangeAccessorTypeFix.java | 30 +++++++++---------- .../quickfix/ChangeFunctionReturnTypeFix.java | 9 ++++-- .../jet/plugin/quickfix/ChangeTypeFix.java | 9 +++--- .../quickfix/ChangeVariableTypeFix.java | 9 +++--- ...ngeOverridingPropertyTypeToFunctionType.kt | 7 +++++ ...ngeOverridingPropertyTypeToFunctionType.kt | 7 +++++ .../afterChangeAccessorTypeToFunctionType.kt | 5 ++++ .../beforeChangeAccessorTypeToFunctionType.kt | 5 ++++ ...ctionLiteralParameterTypeToFunctionType.kt | 6 ++++ ...rChangeFunctionReturnTypeToFunctionType.kt | 4 +++ ...ctionLiteralParameterTypeToFunctionType.kt | 6 ++++ ...eChangeFunctionReturnTypeToFunctionType.kt | 4 +++ .../casts/afterCastToFunctionType.kt | 4 +++ .../casts/beforeCastToFunctionType.kt | 4 +++ .../quickfix/QuickFixTestGenerated.java | 25 ++++++++++++++++ 16 files changed, 113 insertions(+), 30 deletions(-) create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverridingPropertyTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeAddition/afterChangeAccessorTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeAddition/beforeChangeAccessorTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeMismatch/afterChangeFunctionLiteralParameterTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeChangeFunctionLiteralParameterTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt create mode 100644 idea/testData/quickfix/typeMismatch/casts/afterCastToFunctionType.kt create mode 100644 idea/testData/quickfix/typeMismatch/casts/beforeCastToFunctionType.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java index 1b4cc20a134..9d96a551817 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java @@ -33,19 +33,22 @@ import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; +import org.jetbrains.jet.renderer.DescriptorRenderer; public class CastExpressionFix extends JetIntentionAction { private final JetType type; + private final String renderedType; public CastExpressionFix(@NotNull JetExpression element, @NotNull JetType type) { super(element); this.type = type; + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); } @NotNull @Override public String getText() { - return JetBundle.message("cast.expression.to.type", element.getText(), type.toString()); + return JetBundle.message("cast.expression.to.type", element.getText(), renderedType); } @NotNull @@ -64,9 +67,9 @@ public class CastExpressionFix extends JetIntentionAction { @Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { JetBinaryExpressionWithTypeRHS castedExpression = - (JetBinaryExpressionWithTypeRHS) JetPsiFactory.createExpression(project, "(" + element.getText() + ") as " + type.toString()); + (JetBinaryExpressionWithTypeRHS) JetPsiFactory.createExpression(project, "(" + element.getText() + ") as " + renderedType); if (JetPsiUtil.areParenthesesUseless((JetParenthesizedExpression) castedExpression.getLeft())) { - castedExpression = (JetBinaryExpressionWithTypeRHS) JetPsiFactory.createExpression(project, element.getText() + " as " + type.toString()); + castedExpression = (JetBinaryExpressionWithTypeRHS) JetPsiFactory.createExpression(project, element.getText() + " as " + renderedType); } JetParenthesizedExpression castedExpressionInParentheses = diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java index 102874d7f83..6eef4a97ffe 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java @@ -23,38 +23,38 @@ import com.intellij.psi.impl.source.codeStyle.CodeEditUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.JetBundle; +import org.jetbrains.jet.renderer.DescriptorRenderer; public class ChangeAccessorTypeFix extends JetIntentionAction { + private String renderedType; + public ChangeAccessorTypeFix(@NotNull JetPropertyAccessor element) { super(element); } - @Nullable - private JetType getPropertyType() { - JetProperty property = PsiTreeUtil.getParentOfType(element, JetProperty.class); - if (property == null) return null; - return QuickFixUtil.getDeclarationReturnType(property); - } - @Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - JetType type = getPropertyType(); - return super.isAvailable(project, editor, file) && type != null && !ErrorUtils.isErrorType(type); + JetProperty property = PsiTreeUtil.getParentOfType(element, JetProperty.class); + if (property == null) return false; + JetType type = QuickFixUtil.getDeclarationReturnType(property); + if (super.isAvailable(project, editor, file) && type != null && !ErrorUtils.isErrorType(type)) { + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); + return true; + } + return false; } @NotNull @Override public String getText() { - JetType type = getPropertyType(); return element.isGetter() - ? JetBundle.message("change.getter.type", type) - : JetBundle.message("change.setter.type", type); + ? JetBundle.message("change.getter.type", renderedType) + : JetBundle.message("change.setter.type", renderedType); } @NotNull @@ -65,10 +65,8 @@ public class ChangeAccessorTypeFix extends JetIntentionAction { private final JetType type; + private final String renderedType; public ChangeFunctionReturnTypeFix(@NotNull JetNamedFunction element, @NotNull JetType type) { super(element); this.type = type; + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); } @NotNull @@ -65,8 +68,8 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction { - private final JetType type; + private final String renderedType; public ChangeTypeFix(@NotNull JetTypeReference element, JetType type) { super(element); - this.type = type; + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); } @NotNull @Override public String getText() { - return JetBundle.message("change.type", element.getText(), type); + return JetBundle.message("change.type", element.getText(), renderedType); } @NotNull @@ -54,7 +55,7 @@ public class ChangeTypeFix extends JetIntentionAction { @Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { - element.replace(JetPsiFactory.createType(project, type.toString())); + element.replace(JetPsiFactory.createType(project, renderedType)); } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index c3423fa6b28..92e7d23ea82 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -36,19 +36,20 @@ import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil; import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; +import org.jetbrains.jet.renderer.DescriptorRenderer; public class ChangeVariableTypeFix extends JetIntentionAction { - private final JetType type; + private final String renderedType; public ChangeVariableTypeFix(@NotNull JetVariableDeclaration element, @NotNull JetType type) { super(element); - this.type = type; + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); } @NotNull @Override public String getText() { - return JetBundle.message("change.element.type", element.getName(), type); + return JetBundle.message("change.element.type", element.getName(), renderedType); } @NotNull @@ -62,7 +63,7 @@ public class ChangeVariableTypeFix extends JetIntentionAction typeWhiteSpaceAndColon = JetPsiFactory.createTypeWhiteSpaceAndColon(project, type.toString()); + Pair typeWhiteSpaceAndColon = JetPsiFactory.createTypeWhiteSpaceAndColon(project, renderedType); element.addRangeAfter(typeWhiteSpaceAndColon.first, typeWhiteSpaceAndColon.second, nameIdentifier); } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverridingPropertyTypeToFunctionType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverridingPropertyTypeToFunctionType.kt new file mode 100644 index 00000000000..dea0db239e9 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverridingPropertyTypeToFunctionType.kt @@ -0,0 +1,7 @@ +// "Change 'x' type to '(String) -> Int'" "true" +trait A { + var x: (String) -> Int +} +trait B : A { + override var x: (String) -> Int +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt new file mode 100644 index 00000000000..86cf8bd43da --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt @@ -0,0 +1,7 @@ +// "Change 'x' type to '(String) -> Int'" "true" +trait A { + var x: (String) -> Int +} +trait B : A { + override var x: (Int) -> String +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeAddition/afterChangeAccessorTypeToFunctionType.kt b/idea/testData/quickfix/typeAddition/afterChangeAccessorTypeToFunctionType.kt new file mode 100644 index 00000000000..642223733a3 --- /dev/null +++ b/idea/testData/quickfix/typeAddition/afterChangeAccessorTypeToFunctionType.kt @@ -0,0 +1,5 @@ +// "Change getter type to (String) -> Int" "true" +class A { + val x: (String) -> Int + get(): (String) -> Int = {42} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeAddition/beforeChangeAccessorTypeToFunctionType.kt b/idea/testData/quickfix/typeAddition/beforeChangeAccessorTypeToFunctionType.kt new file mode 100644 index 00000000000..2a070579945 --- /dev/null +++ b/idea/testData/quickfix/typeAddition/beforeChangeAccessorTypeToFunctionType.kt @@ -0,0 +1,5 @@ +// "Change getter type to (String) -> Int" "true" +class A { + val x: (String) -> Int + get(): Int = {42} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/afterChangeFunctionLiteralParameterTypeToFunctionType.kt b/idea/testData/quickfix/typeMismatch/afterChangeFunctionLiteralParameterTypeToFunctionType.kt new file mode 100644 index 00000000000..89a07f91281 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterChangeFunctionLiteralParameterTypeToFunctionType.kt @@ -0,0 +1,6 @@ +// "Change type from 'String' to '(Int) -> String'" "true" +fun foo(f: ((Int) -> String) -> String) { + foo { + (f: (Int) -> String) -> f(42) + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToFunctionType.kt b/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToFunctionType.kt new file mode 100644 index 00000000000..4d6e718b946 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToFunctionType.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to '(Long) -> Int'" "true" +fun foo(x: Any): (Long) -> Int { + return {(x: Long) -> 42} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeChangeFunctionLiteralParameterTypeToFunctionType.kt b/idea/testData/quickfix/typeMismatch/beforeChangeFunctionLiteralParameterTypeToFunctionType.kt new file mode 100644 index 00000000000..c8897d3a65e --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeChangeFunctionLiteralParameterTypeToFunctionType.kt @@ -0,0 +1,6 @@ +// "Change type from 'String' to '(Int) -> String'" "true" +fun foo(f: ((Int) -> String) -> String) { + foo { + (f: String) -> f(42) + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt b/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt new file mode 100644 index 00000000000..2f90f8ee5e5 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to '(Long) -> Int'" "true" +fun foo(x: Any): Int { + return {(x: Long) -> 42} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/casts/afterCastToFunctionType.kt b/idea/testData/quickfix/typeMismatch/casts/afterCastToFunctionType.kt new file mode 100644 index 00000000000..989e55304cf --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/casts/afterCastToFunctionType.kt @@ -0,0 +1,4 @@ +// "Cast expression 'x' to '() -> Int'" "true" +fun foo(x: Any): () -> Int { + return x as () -> Int +} diff --git a/idea/testData/quickfix/typeMismatch/casts/beforeCastToFunctionType.kt b/idea/testData/quickfix/typeMismatch/casts/beforeCastToFunctionType.kt new file mode 100644 index 00000000000..eb0f35b50c3 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/casts/beforeCastToFunctionType.kt @@ -0,0 +1,4 @@ +// "Cast expression 'x' to '() -> Int'" "true" +fun foo(x: Any): () -> Int { + return x +} diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 350eaa4bd87..0d9ee817a72 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1018,6 +1018,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/override/typeMismatchOnOverride"), Pattern.compile("^before(\\w+)\\.kt$"), true); } + @TestMetadata("beforeChangeOverridingPropertyTypeToFunctionType.kt") + public void testChangeOverridingPropertyTypeToFunctionType() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt"); + } + @TestMetadata("beforePropertyReturnTypeMismatchOnOverride.kt") public void testPropertyReturnTypeMismatchOnOverride() throws Exception { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyReturnTypeMismatchOnOverride.kt"); @@ -1149,6 +1154,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeAddition/beforeAmbiguousPropertyReturnType.kt"); } + @TestMetadata("beforeChangeAccessorTypeToFunctionType.kt") + public void testChangeAccessorTypeToFunctionType() throws Exception { + doTest("idea/testData/quickfix/typeAddition/beforeChangeAccessorTypeToFunctionType.kt"); + } + @TestMetadata("beforeNoAddErrorType.kt") public void testNoAddErrorType() throws Exception { doTest("idea/testData/quickfix/typeAddition/beforeNoAddErrorType.kt"); @@ -1236,6 +1246,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); } + @TestMetadata("beforeChangeFunctionReturnTypeToFunctionType.kt") + public void testChangeFunctionReturnTypeToFunctionType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt"); + } + + @TestMetadata("beforeChangeParameterTypeToFunctionType.kt") + public void testChangeParameterTypeToFunctionType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeChangeParameterTypeToFunctionType.kt"); + } + @TestMetadata("beforeChangeReturnTypeWhenFunctionNameIsMissing.kt") public void testChangeReturnTypeWhenFunctionNameIsMissing() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt"); @@ -1332,6 +1352,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/casts/beforeAutocastImpossible3.kt"); } + @TestMetadata("beforeCastToFunctionType.kt") + public void testCastToFunctionType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/casts/beforeCastToFunctionType.kt"); + } + @TestMetadata("beforeTypeMismatch1.kt") public void testTypeMismatch1() throws Exception { doTest("idea/testData/quickfix/typeMismatch/casts/beforeTypeMismatch1.kt"); From 07ea3183612042d741162a05499f7ae573935461 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 1 May 2013 18:22:40 +0200 Subject: [PATCH 02/19] Get rid of warnings in "Change Type" quickfixes --- .../jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java | 4 ++-- idea/src/org/jetbrains/jet/plugin/quickfix/ChangeTypeFix.java | 4 ++-- .../jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index 0c4177e7911..3ae4dff778e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -22,7 +22,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiErrorElement; -import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -79,7 +78,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction) diagnostic).getA().asString(); int componentIndex = Integer.valueOf(componentName.substring(DescriptorResolver.COMPONENT_FUNCTION_NAME_PREFIX.length())); JetMultiDeclaration multiDeclaration = QuickFixUtil.getParentElementOfType(diagnostic, JetMultiDeclaration.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeTypeFix.java index 717179f49fd..318b74b0015 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeTypeFix.java @@ -19,13 +19,13 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters1; import org.jetbrains.jet.lang.diagnostics.Errors; +import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetParameter; import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.psi.JetTypeReference; @@ -54,7 +54,7 @@ public class ChangeTypeFix extends JetIntentionAction { } @Override - public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException { element.replace(JetPsiFactory.createType(project, renderedType)); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index 92e7d23ea82..3236e846711 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -21,7 +21,6 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -59,7 +58,7 @@ public class ChangeVariableTypeFix extends JetIntentionAction Date: Wed, 1 May 2013 19:30:43 +0200 Subject: [PATCH 03/19] Cleanup in "type mismatch" tests --- ...terComponentFunctionReturnTypeMismatch1.kt | 0 ...terComponentFunctionReturnTypeMismatch2.kt | 0 ...terComponentFunctionReturnTypeMismatch3.kt | 0 ...terComponentFunctionReturnTypeMismatch4.kt | 0 ...terComponentFunctionReturnTypeMismatch5.kt | 0 ...oreComponentFunctionReturnTypeMismatch1.kt | 0 ...oreComponentFunctionReturnTypeMismatch2.kt | 0 ...oreComponentFunctionReturnTypeMismatch3.kt | 0 ...oreComponentFunctionReturnTypeMismatch4.kt | 0 ...oreComponentFunctionReturnTypeMismatch5.kt | 0 ...rChangeFunctionReturnTypeToFunctionType.kt | 0 .../afterTypeMismatchInInitializer.kt | 0 .../afterTypeMismatchInReturnStatement.kt | 0 ...eChangeFunctionReturnTypeToFunctionType.kt | 0 .../beforeTypeMismatchInInitializer.kt | 0 .../beforeTypeMismatchInReturnStatement.kt | 0 .../quickfix/QuickFixTestGenerated.java | 106 ++++++++++-------- 17 files changed, 62 insertions(+), 44 deletions(-) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/afterComponentFunctionReturnTypeMismatch1.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/afterComponentFunctionReturnTypeMismatch2.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/afterComponentFunctionReturnTypeMismatch3.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/afterComponentFunctionReturnTypeMismatch4.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/afterComponentFunctionReturnTypeMismatch5.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/beforeComponentFunctionReturnTypeMismatch1.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/beforeComponentFunctionReturnTypeMismatch2.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/beforeComponentFunctionReturnTypeMismatch3.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/beforeComponentFunctionReturnTypeMismatch4.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => componentFunctionReturnTypeMismatch}/beforeComponentFunctionReturnTypeMismatch5.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => typeMismatchOnReturnedExpression}/afterChangeFunctionReturnTypeToFunctionType.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => typeMismatchOnReturnedExpression}/afterTypeMismatchInInitializer.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => typeMismatchOnReturnedExpression}/afterTypeMismatchInReturnStatement.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => typeMismatchOnReturnedExpression}/beforeChangeFunctionReturnTypeToFunctionType.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => typeMismatchOnReturnedExpression}/beforeTypeMismatchInInitializer.kt (100%) rename idea/testData/quickfix/typeMismatch/{ => typeMismatchOnReturnedExpression}/beforeTypeMismatchInReturnStatement.kt (100%) diff --git a/idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch1.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch1.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch1.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch1.kt diff --git a/idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch2.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch2.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch2.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch2.kt diff --git a/idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch3.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch3.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch3.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch3.kt diff --git a/idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch4.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch4.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch4.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch4.kt diff --git a/idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch5.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch5.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterComponentFunctionReturnTypeMismatch5.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/afterComponentFunctionReturnTypeMismatch5.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch1.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch1.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch1.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch1.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch2.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch2.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch2.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch2.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch3.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch3.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch3.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch3.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch4.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch4.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch4.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch4.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt b/idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt rename to idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt diff --git a/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToFunctionType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToFunctionType.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToFunctionType.kt rename to idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToFunctionType.kt diff --git a/idea/testData/quickfix/typeMismatch/afterTypeMismatchInInitializer.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInInitializer.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterTypeMismatchInInitializer.kt rename to idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInInitializer.kt diff --git a/idea/testData/quickfix/typeMismatch/afterTypeMismatchInReturnStatement.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInReturnStatement.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/afterTypeMismatchInReturnStatement.kt rename to idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInReturnStatement.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToFunctionType.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt rename to idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToFunctionType.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInInitializer.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt rename to idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInInitializer.kt diff --git a/idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInReturnStatement.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt rename to idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInReturnStatement.kt diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 0d9ee817a72..fb646cabc37 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1240,20 +1240,15 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } @TestMetadata("idea/testData/quickfix/typeMismatch") - @InnerTestClasses({TypeMismatch.Casts.class}) + @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) public static class TypeMismatch extends AbstractQuickFixTest { public void testAllFilesPresentInTypeMismatch() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); } - @TestMetadata("beforeChangeFunctionReturnTypeToFunctionType.kt") - public void testChangeFunctionReturnTypeToFunctionType() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToFunctionType.kt"); - } - - @TestMetadata("beforeChangeParameterTypeToFunctionType.kt") - public void testChangeParameterTypeToFunctionType() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeChangeParameterTypeToFunctionType.kt"); + @TestMetadata("beforeChangeFunctionLiteralParameterTypeToFunctionType.kt") + public void testChangeFunctionLiteralParameterTypeToFunctionType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeChangeFunctionLiteralParameterTypeToFunctionType.kt"); } @TestMetadata("beforeChangeReturnTypeWhenFunctionNameIsMissing.kt") @@ -1271,31 +1266,6 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt"); } - @TestMetadata("beforeComponentFunctionReturnTypeMismatch1.kt") - public void testComponentFunctionReturnTypeMismatch1() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch1.kt"); - } - - @TestMetadata("beforeComponentFunctionReturnTypeMismatch2.kt") - public void testComponentFunctionReturnTypeMismatch2() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch2.kt"); - } - - @TestMetadata("beforeComponentFunctionReturnTypeMismatch3.kt") - public void testComponentFunctionReturnTypeMismatch3() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch3.kt"); - } - - @TestMetadata("beforeComponentFunctionReturnTypeMismatch4.kt") - public void testComponentFunctionReturnTypeMismatch4() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch4.kt"); - } - - @TestMetadata("beforeComponentFunctionReturnTypeMismatch5.kt") - public void testComponentFunctionReturnTypeMismatch5() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt"); - } - @TestMetadata("beforeExpectedParameterTypeMismatch.kt") public void testExpectedParameterTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeExpectedParameterTypeMismatch.kt"); @@ -1321,16 +1291,6 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt"); } - @TestMetadata("beforeTypeMismatchInInitializer.kt") - public void testTypeMismatchInInitializer() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeTypeMismatchInInitializer.kt"); - } - - @TestMetadata("beforeTypeMismatchInReturnStatement.kt") - public void testTypeMismatchInReturnStatement() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/beforeTypeMismatchInReturnStatement.kt"); - } - @TestMetadata("idea/testData/quickfix/typeMismatch/casts") public static class Casts extends AbstractQuickFixTest { public void testAllFilesPresentInCasts() throws Exception { @@ -1384,10 +1344,68 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } + @TestMetadata("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch") + public static class ComponentFunctionReturnTypeMismatch extends AbstractQuickFixTest { + public void testAllFilesPresentInComponentFunctionReturnTypeMismatch() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); + } + + @TestMetadata("beforeComponentFunctionReturnTypeMismatch1.kt") + public void testComponentFunctionReturnTypeMismatch1() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch1.kt"); + } + + @TestMetadata("beforeComponentFunctionReturnTypeMismatch2.kt") + public void testComponentFunctionReturnTypeMismatch2() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch2.kt"); + } + + @TestMetadata("beforeComponentFunctionReturnTypeMismatch3.kt") + public void testComponentFunctionReturnTypeMismatch3() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch3.kt"); + } + + @TestMetadata("beforeComponentFunctionReturnTypeMismatch4.kt") + public void testComponentFunctionReturnTypeMismatch4() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch4.kt"); + } + + @TestMetadata("beforeComponentFunctionReturnTypeMismatch5.kt") + public void testComponentFunctionReturnTypeMismatch5() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/componentFunctionReturnTypeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt"); + } + + } + + @TestMetadata("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression") + public static class TypeMismatchOnReturnedExpression extends AbstractQuickFixTest { + public void testAllFilesPresentInTypeMismatchOnReturnedExpression() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression"), Pattern.compile("^before(\\w+)\\.kt$"), true); + } + + @TestMetadata("beforeChangeFunctionReturnTypeToFunctionType.kt") + public void testChangeFunctionReturnTypeToFunctionType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToFunctionType.kt"); + } + + @TestMetadata("beforeTypeMismatchInInitializer.kt") + public void testTypeMismatchInInitializer() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInInitializer.kt"); + } + + @TestMetadata("beforeTypeMismatchInReturnStatement.kt") + public void testTypeMismatchInReturnStatement() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInReturnStatement.kt"); + } + + } + public static Test innerSuite() { TestSuite suite = new TestSuite("TypeMismatch"); suite.addTestSuite(TypeMismatch.class); suite.addTestSuite(Casts.class); + suite.addTestSuite(ComponentFunctionReturnTypeMismatch.class); + suite.addTestSuite(TypeMismatchOnReturnedExpression.class); return suite; } } From 44fa32c617374627c6674f87322b2db88c887dd3 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 8 May 2013 18:34:13 +0200 Subject: [PATCH 04/19] ChangeFunctionParameterTypeFix for TYPE_MISMATCH error --- .../jetbrains/jet/plugin/JetBundle.properties | 1 + .../ChangeFunctionParameterTypeFix.java | 110 ++++++++++++++++++ .../jet/plugin/quickfix/QuickFixUtil.java | 62 +++++++++- .../jet/plugin/quickfix/QuickFixes.java | 4 +- .../afterChangeFunctionParameterType1.kt | 5 + .../afterChangeFunctionParameterType2.kt | 4 + .../afterChangeFunctionParameterType3.kt | 4 + .../beforeChangeFunctionParameterType1.kt | 5 + .../beforeChangeFunctionParameterType2.kt | 4 + .../beforeChangeFunctionParameterType3.kt | 4 + .../beforeChangeFunctionParameterType4.kt | 8 ++ .../beforeChangeFunctionParameterType5.kt | 5 + .../quickfix/QuickFixTestGenerated.java | 36 +++++- 13 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType1.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType2.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType3.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt create mode 100644 idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index c640470d94b..eb1f9fda628 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -62,6 +62,7 @@ change.no.name.function.return.type=Change function return type to ''{0}'' remove.function.return.type=Remove explicitly specified return type in ''{0}'' function remove.no.name.function.return.type=Remove explicitly specified function return type change.element.type=Change ''{0}'' type to ''{1}'' +change.function.parameter.type=Change parameter ''{0}'' type of function ''{1}'' to ''{2}'' change.type=Change type from ''{0}'' to ''{1}'' change.type.family=Change Type add.parameters.to.function=Add parameter{0} to function ''{1}'' diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java new file mode 100644 index 00000000000..0c7e797928f --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java @@ -0,0 +1,110 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.descriptors.CallableDescriptor; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingContextUtils; +import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; +import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.resolve.name.NameUtils; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.plugin.JetBundle; +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; +import org.jetbrains.jet.renderer.DescriptorRenderer; + +public class ChangeFunctionParameterTypeFix extends JetIntentionAction { + private final String renderedType; + private final String containingFunctionName; + + public ChangeFunctionParameterTypeFix(@NotNull JetParameter element, @NotNull JetType type) { + super(element); + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); + JetFunction function = PsiTreeUtil.getParentOfType(element, JetFunction.class); + FqName functionFQName = function == null ? null : JetPsiUtil.getFQName(function); + containingFunctionName = functionFQName == null ? null : functionFQName.getFqName(); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return super.isAvailable(project, editor, file) && containingFunctionName != null; + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("change.function.parameter.type", element.getName(), containingFunctionName, renderedType); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("change.type.family"); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException { + JetTypeReference typeReference = element.getTypeReference(); + assert typeReference != null : "Parameter without type annotation cannot cause type mismatch"; + typeReference.replace(JetPsiFactory.createType(project, renderedType)); + } + + @NotNull + public static JetIntentionActionFactory createFactory() { + return new JetIntentionActionFactory() { + @Nullable + @Override + public IntentionAction createAction(Diagnostic diagnostic) { + // Change type of function parameter in case TYPE_MISMATCH is reported on expression passed as value argument of call + JetParameter correspondingParameter = null; + JetType type = null; + + BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) diagnostic.getPsiFile()).getBindingContext(); + JetFunctionLiteralExpression functionLiteralExpression = + QuickFixUtil.getParentElementOfType(diagnostic, JetFunctionLiteralExpression.class); + + if (functionLiteralExpression != null && diagnostic.getPsiElement() == functionLiteralExpression.getBodyExpression()) { + correspondingParameter = + QuickFixUtil.getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(functionLiteralExpression); + type = context.get(BindingContext.EXPRESSION_TYPE, functionLiteralExpression); + } + else { + JetValueArgument valueArgument = QuickFixUtil.getParentElementOfType(diagnostic, JetValueArgument.class); + if (valueArgument != null && valueArgument.getArgumentExpression() == diagnostic.getPsiElement()) { + correspondingParameter = QuickFixUtil.getFunctionParameterCorrespondingToValueArgumentPassedInCall(valueArgument); + type = context.get(BindingContext.EXPRESSION_TYPE, valueArgument.getArgumentExpression()); + } + } + if (correspondingParameter != null && type != null) { + return new ChangeFunctionParameterTypeFix(correspondingParameter, type); + } + return null; + } + }; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index a1d9aa40425..7c3f2971576 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -26,10 +26,10 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; -import org.jetbrains.jet.lang.psi.JetDeclaration; -import org.jetbrains.jet.lang.psi.JetFile; -import org.jetbrains.jet.lang.psi.JetNamedDeclaration; +import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingContextUtils; +import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.types.DeferredType; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; @@ -93,4 +93,60 @@ public class QuickFixUtil { public static boolean canModifyElement(@NotNull PsiElement element) { return element.isWritable() && !BuiltInsReferenceResolver.isFromBuiltIns(element); } + + @Nullable + public static JetParameterList getParameterListOfCalledFunction(@NotNull JetCallExpression callExpression) { + BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) callExpression.getContainingFile()).getBindingContext(); + ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, callExpression.getCalleeExpression()); + if (resolvedCall == null) return null; + PsiElement functionDeclaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + if (functionDeclaration instanceof JetFunction) { + return ((JetFunction) functionDeclaration).getValueParameterList(); + } + return null; + } + + @Nullable + public static JetParameter getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(@NotNull JetFunctionLiteralExpression functionLiteralExpression) { + if (!(functionLiteralExpression.getParent() instanceof JetCallExpression)) { + return null; + } + JetCallExpression callExpression = (JetCallExpression) functionLiteralExpression.getParent(); + JetParameterList parameterList = getParameterListOfCalledFunction(callExpression); + if (parameterList == null) return null; + return parameterList.getParameters().get(parameterList.getParameters().size() - 1); + } + + @Nullable + public static JetParameter getFunctionParameterCorrespondingToValueArgumentPassedInCall(@NotNull JetValueArgument valueArgument) { + if (!(valueArgument.getParent() instanceof JetValueArgumentList)) { + return null; + } + JetValueArgumentList valueArgumentList = (JetValueArgumentList) valueArgument.getParent(); + if (!(valueArgumentList.getParent() instanceof JetCallExpression)) { + return null; + } + JetCallExpression callExpression = (JetCallExpression) valueArgumentList.getParent(); + JetParameterList parameterList = getParameterListOfCalledFunction(callExpression); + if (parameterList == null) return null; + int position = valueArgumentList.getArguments().indexOf(valueArgument); + if (position == -1) return null; + + if (valueArgument.isNamed()) { + JetValueArgumentName valueArgumentName = valueArgument.getArgumentName(); + JetSimpleNameExpression referenceExpression = valueArgumentName == null ? null : valueArgumentName.getReferenceExpression(); + String valueArgumentNameAsString = referenceExpression == null ? null : referenceExpression.getReferencedName(); + if (valueArgumentNameAsString == null) return null; + + for (JetParameter parameter: parameterList.getParameters()) { + if (valueArgumentNameAsString.equals(parameter.getName())) { + return parameter; + } + } + return null; + } + else { + return parameterList.getParameters().get(position); + } + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index be8f7fb5b30..c1fd41dab2e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -235,7 +235,9 @@ public class QuickFixes { factories.put(EXPECTED_PARAMETER_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch()); factories.put(EXPECTED_RETURN_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedReturnTypeMismatch()); - + + factories.put(TYPE_MISMATCH, ChangeFunctionParameterTypeFix.createFactory()); + factories.put(AUTOCAST_IMPOSSIBLE, CastExpressionFix.createFactoryForAutoCastImpossible()); factories.put(TYPE_MISMATCH, CastExpressionFix.createFactoryForTypeMismatch()); diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType1.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType1.kt new file mode 100644 index 00000000000..b253e8b1782 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType1.kt @@ -0,0 +1,5 @@ +// "Change parameter 'x' type of function 'bar.foo' to '(String) -> Int'" "true" +package bar +fun foo(w: Int = 0, x: (String) -> Int, y: Int = 0, z: (Int) -> Int = {42}) { + foo(1, {(a: String) -> 42}, 1) +} diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType2.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType2.kt new file mode 100644 index 00000000000..c1b3343642c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType2.kt @@ -0,0 +1,4 @@ +// "Change parameter 'y' type of function 'foo' to 'String'" "true" +fun foo(v: Int, w: Int = 0, x: Int = 0, y: String, z: (Int) -> Int = {42}) { + foo(0, 1, y = "") +} diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType3.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType3.kt new file mode 100644 index 00000000000..30b490ba5ed --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType3.kt @@ -0,0 +1,4 @@ +// "Change parameter 'z' type of function 'foo' to '(Int) -> Unit'" "true" +fun foo(w: Int = 0, x: Int, y: Int = 0, z: (Int) -> Unit) { + foo(0, 1) {} +} diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt new file mode 100644 index 00000000000..deb5313f041 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt @@ -0,0 +1,5 @@ +// "Change parameter 'x' type of function 'bar.foo' to '(String) -> Int'" "true" +package bar +fun foo(w: Int = 0, x: Int, y: Int = 0, z: (Int) -> Int = {42}) { + foo(1, {(a: String) -> 42}, 1) +} diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt new file mode 100644 index 00000000000..c5e45b514be --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt @@ -0,0 +1,4 @@ +// "Change parameter 'y' type of function 'foo' to 'String'" "true" +fun foo(v: Int, w: Int = 0, x: Int = 0, y: Int, z: (Int) -> Int = {42}) { + foo(0, 1, y = "") +} diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt new file mode 100644 index 00000000000..9d5f48d961d --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt @@ -0,0 +1,4 @@ +// "Change parameter 'z' type of function 'foo' to '(Int) -> Unit'" "true" +fun foo(w: Int = 0, x: Int, y: Int = 0, z: (Int) -> String) { + foo(0, 1) {} +} diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt new file mode 100644 index 00000000000..a4c9f02baaf --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt @@ -0,0 +1,8 @@ +// "Change parameter 'z' type of function 'foo' to '(Int) -> String'" "false" +// ERROR: Type mismatch.
Required:jet.Int
Found:jet.String
+fun foo(y: Int = 0, z: (Int) -> String = {""}) { + foo { + "": Int + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt new file mode 100644 index 00000000000..80f37c15a85 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt @@ -0,0 +1,5 @@ +// "Change parameter 'y' type of function 'foo' to 'Int'" "false" +// ERROR: Type mismatch.
Required:jet.Int
Found:jet.String
+fun foo(y: Int = 0, z: (Int) -> String = {""}) { + foo("": Int) +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index fb646cabc37..20ca2288240 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1240,7 +1240,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } @TestMetadata("idea/testData/quickfix/typeMismatch") - @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) + @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FunctionParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) public static class TypeMismatch extends AbstractQuickFixTest { public void testAllFilesPresentInTypeMismatch() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); @@ -1377,6 +1377,39 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } + @TestMetadata("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch") + public static class FunctionParameterTypeMismatch extends AbstractQuickFixTest { + public void testAllFilesPresentInFunctionParameterTypeMismatch() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); + } + + @TestMetadata("beforeChangeFunctionParameterType1.kt") + public void testChangeFunctionParameterType1() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt"); + } + + @TestMetadata("beforeChangeFunctionParameterType2.kt") + public void testChangeFunctionParameterType2() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt"); + } + + @TestMetadata("beforeChangeFunctionParameterType3.kt") + public void testChangeFunctionParameterType3() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt"); + } + + @TestMetadata("beforeChangeFunctionParameterType4.kt") + public void testChangeFunctionParameterType4() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt"); + } + + @TestMetadata("beforeChangeFunctionParameterType5.kt") + public void testChangeFunctionParameterType5() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt"); + } + + } + @TestMetadata("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression") public static class TypeMismatchOnReturnedExpression extends AbstractQuickFixTest { public void testAllFilesPresentInTypeMismatchOnReturnedExpression() throws Exception { @@ -1405,6 +1438,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { suite.addTestSuite(TypeMismatch.class); suite.addTestSuite(Casts.class); suite.addTestSuite(ComponentFunctionReturnTypeMismatch.class); + suite.addTestSuite(FunctionParameterTypeMismatch.class); suite.addTestSuite(TypeMismatchOnReturnedExpression.class); return suite; } From 9b90ee2e8006ab376b296fa6bdb315ba3744ed72 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 8 May 2013 18:55:31 +0200 Subject: [PATCH 05/19] ChangeFunctionLiteralReturnTypeFix for EXPECTED/ASSIGNMENT_TYPE_MISMATCH errors --- .../jetbrains/jet/plugin/JetBundle.properties | 1 + .../ChangeFunctionLiteralReturnTypeFix.java | 140 ++++++++++++++++++ .../jet/plugin/quickfix/QuickFixUtil.java | 25 ++++ .../jet/plugin/quickfix/QuickFixes.java | 4 + .../afterAssignmentTypeMismatch.kt | 7 + .../afterExpectedTypeMismatch.kt | 7 + .../beforeAssignmentTypeMismatch.kt | 7 + .../beforeExpectedTypeMismatch.kt | 7 + .../quickfix/QuickFixTestGenerated.java | 10 ++ 9 files changed, 208 insertions(+) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterAssignmentTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterExpectedTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index eb1f9fda628..6f7ebde6deb 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -59,6 +59,7 @@ add.semicolon.after.invocation=Add semicolon after invocation of ''{0}'' add.semicolon.family=Add Semicolon change.function.return.type=Change ''{0}'' function return type to ''{1}'' change.no.name.function.return.type=Change function return type to ''{0}'' +change.function.literal.return.type=Change function literal return type to ''{0}'' remove.function.return.type=Remove explicitly specified return type in ''{0}'' function remove.no.name.function.return.type=Remove explicitly specified function return type change.element.type=Change ''{0}'' type to ''{1}'' diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java new file mode 100644 index 00000000000..c93d084532e --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java @@ -0,0 +1,140 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.descriptors.ClassDescriptor; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.TypeProjection; +import org.jetbrains.jet.lang.types.TypeUtils; +import org.jetbrains.jet.lang.types.checker.JetTypeChecker; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; +import org.jetbrains.jet.plugin.JetBundle; +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; +import org.jetbrains.jet.renderer.DescriptorRenderer; + +import java.util.LinkedList; +import java.util.List; + +public class ChangeFunctionLiteralReturnTypeFix extends JetIntentionAction { + private final String renderedType; + private final JetTypeReference functionLiteralReturnTypeRef; + private IntentionAction appropriateQuickFix = null; + + public ChangeFunctionLiteralReturnTypeFix(@NotNull JetFunctionLiteralExpression element, @NotNull JetType type) { + super(element); + renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); + functionLiteralReturnTypeRef = element.getFunctionLiteral().getReturnTypeRef(); + + BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) element.getContainingFile()).getBindingContext(); + JetType functionLiteralType = context.get(BindingContext.EXPRESSION_TYPE, element); + assert functionLiteralType != null : "Type of function literal not available in binding context"; + + ClassDescriptor functionClass = KotlinBuiltIns.getInstance().getFunction(functionLiteralType.getArguments().size() - 1); + List functionClassTypeParameters = new LinkedList(); + for (TypeProjection typeProjection: functionLiteralType.getArguments()) { + functionClassTypeParameters.add(typeProjection.getType()); + } + // Replacing return type: + functionClassTypeParameters.remove(functionClassTypeParameters.size() - 1); + functionClassTypeParameters.add(type); + JetType eventualFunctionLiteralType = TypeUtils.substituteParameters(functionClass, functionClassTypeParameters); + + JetProperty correspondingProperty = PsiTreeUtil.getParentOfType(element, JetProperty.class); + if (correspondingProperty != null && QuickFixUtil.canEvaluateTo(correspondingProperty.getInitializer(), element)) { + JetTypeReference correspondingPropertyTypeRef = correspondingProperty.getTypeRef(); + JetType propertyType = context.get(BindingContext.TYPE, correspondingPropertyTypeRef); + if (propertyType != null && !JetTypeChecker.INSTANCE.isSubtypeOf(eventualFunctionLiteralType, propertyType)) { + appropriateQuickFix = new ChangeVariableTypeFix(correspondingProperty, eventualFunctionLiteralType); + } + return; + } + + JetParameter correspondingParameter = QuickFixUtil.getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(element); + if (correspondingParameter != null) { + JetTypeReference correspondingParameterTypeRef = correspondingParameter.getTypeReference(); + JetType parameterType = context.get(BindingContext.TYPE, correspondingParameterTypeRef); + if (parameterType != null && !JetTypeChecker.INSTANCE.isSubtypeOf(eventualFunctionLiteralType, parameterType)) { + appropriateQuickFix = new ChangeFunctionParameterTypeFix(correspondingParameter, eventualFunctionLiteralType); + } + return; + } + + JetFunction parentFunction = PsiTreeUtil.getParentOfType(element, JetFunction.class, true); + if (parentFunction != null && QuickFixUtil.canFunctionReturnExpression(parentFunction, element)) { + JetTypeReference parentFunctionReturnTypeRef = parentFunction.getReturnTypeRef(); + JetType parentFunctionReturnType = context.get(BindingContext.TYPE, parentFunctionReturnTypeRef); + if (parentFunctionReturnType != null && !JetTypeChecker.INSTANCE.isSubtypeOf(eventualFunctionLiteralType, parentFunctionReturnType)) { + appropriateQuickFix = new ChangeFunctionReturnTypeFix(parentFunction, eventualFunctionLiteralType); + } + } + } + + @NotNull + @Override + public String getText() { + if (appropriateQuickFix != null) { + return appropriateQuickFix.getText(); + } + return JetBundle.message("change.function.literal.return.type", renderedType); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("change.type.family"); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return super.isAvailable(project, editor, file) && + (functionLiteralReturnTypeRef != null || (appropriateQuickFix != null && appropriateQuickFix.isAvailable(project, editor, file))); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException { + if (functionLiteralReturnTypeRef != null) { + functionLiteralReturnTypeRef.replace(JetPsiFactory.createType(project, renderedType)); + } + if (appropriateQuickFix != null && appropriateQuickFix.isAvailable(project, editor, file)) { + appropriateQuickFix.invoke(project, editor, file); + } + } + + @NotNull + public static JetIntentionActionFactory createFactoryForExpectedOrAssignmentTypeMismatch() { + return new JetIntentionActionFactory() { + @Nullable + @Override + public IntentionAction createAction(Diagnostic diagnostic) { + JetFunctionLiteralExpression functionLiteralExpression = QuickFixUtil.getParentElementOfType(diagnostic, JetFunctionLiteralExpression.class); + assert functionLiteralExpression != null : "ASSIGNMENT/EXPECTED_TYPE_MISMATCH reported outside any function literal"; + return new ChangeFunctionLiteralReturnTypeFix(functionLiteralExpression, KotlinBuiltIns.getInstance().getUnitType()); + } + }; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index 7c3f2971576..beb270a8724 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -149,4 +149,29 @@ public class QuickFixUtil { return parameterList.getParameters().get(position); } } + + private static boolean equalOrLastInThenOrElse(JetExpression thenOrElse, JetExpression expression) { + if (thenOrElse == expression) return true; + return thenOrElse instanceof JetBlockExpression && expression.getParent() == thenOrElse && + PsiTreeUtil.getNextSiblingOfType(expression, JetExpression.class) == null; + } + + public static boolean canEvaluateTo(JetExpression parent, JetExpression child) { + if (parent == null || child == null) { + return false; + } + while (parent != child) { + if (child.getParent() instanceof JetParenthesizedExpression) { + child = (JetExpression) child.getParent(); + continue; + } + JetIfExpression jetIfExpression = PsiTreeUtil.getParentOfType(child, JetIfExpression.class, true); + if (jetIfExpression == null) return false; + if (!equalOrLastInThenOrElse(jetIfExpression.getThen(), child) && !equalOrLastInThenOrElse(jetIfExpression.getElse(), child)) { + return false; + } + child = jetIfExpression; + } + return true; + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index c1fd41dab2e..a6b9c110a51 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -236,6 +236,10 @@ public class QuickFixes { factories.put(EXPECTED_PARAMETER_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch()); factories.put(EXPECTED_RETURN_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedReturnTypeMismatch()); + JetIntentionActionFactory changeFunctionLiteralReturnTypeFix = ChangeFunctionLiteralReturnTypeFix.createFactoryForExpectedOrAssignmentTypeMismatch(); + factories.put(EXPECTED_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); + factories.put(ASSIGNMENT_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); + factories.put(TYPE_MISMATCH, ChangeFunctionParameterTypeFix.createFactory()); factories.put(AUTOCAST_IMPOSSIBLE, CastExpressionFix.createFactoryForAutoCastImpossible()); diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterAssignmentTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterAssignmentTypeMismatch.kt new file mode 100644 index 00000000000..ac73a6453e9 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterAssignmentTypeMismatch.kt @@ -0,0 +1,7 @@ +// "Change 'f' type to '() -> Unit'" "true" +fun foo() { + val f: () -> Unit = { + var x = 1 + x += 21 + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterExpectedTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterExpectedTypeMismatch.kt new file mode 100644 index 00000000000..d51e2648772 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterExpectedTypeMismatch.kt @@ -0,0 +1,7 @@ +// "Change 'bar' type to '(() -> Long) -> Unit'" "true" +fun foo() { + val bar: (() -> Long) -> Unit = { + (f: () -> Long): Unit -> + var x = 5 + } +} diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt new file mode 100644 index 00000000000..ad45d53e8c7 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt @@ -0,0 +1,7 @@ +// "Change 'f' type to '() -> Unit'" "true" +fun foo() { + val f: () -> Int = { + var x = 1 + x += 21 + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt new file mode 100644 index 00000000000..379a397ab85 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt @@ -0,0 +1,7 @@ +// "Change 'bar' type to '(() -> Long) -> Unit'" "true" +fun foo() { + val bar: () -> Double = { + (f: () -> Long): String -> + var x = 5 + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 20ca2288240..810c38bc41d 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1416,11 +1416,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression"), Pattern.compile("^before(\\w+)\\.kt$"), true); } + @TestMetadata("beforeAssignmentTypeMismatch.kt") + public void testAssignmentTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt"); + } + @TestMetadata("beforeChangeFunctionReturnTypeToFunctionType.kt") public void testChangeFunctionReturnTypeToFunctionType() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToFunctionType.kt"); } + @TestMetadata("beforeExpectedTypeMismatch.kt") + public void testExpectedTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt"); + } + @TestMetadata("beforeTypeMismatchInInitializer.kt") public void testTypeMismatchInInitializer() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInInitializer.kt"); From 0144ad06706530c6d972ed15d3dfcff5d25cab65 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 8 May 2013 23:18:36 +0200 Subject: [PATCH 06/19] Enable intention actions factories to return multiple quickfixes --- .../jet/plugin/highlighter/JetPsiChecker.java | 17 +++---- .../plugin/quickfix/AddFunctionBodyFix.java | 4 +- .../quickfix/AddFunctionToSupertypeFix.java | 4 +- .../jet/plugin/quickfix/AddModifierFix.java | 6 +-- .../plugin/quickfix/AddNameToArgumentFix.java | 4 +- .../AddOpenModifierToClassDeclarationFix.java | 4 +- .../AddSemicolonAfterFunctionCallFix.java | 4 +- .../quickfix/AddStarProjectionsFix.java | 8 ++-- .../plugin/quickfix/AddWhenElseBranchFix.java | 4 +- .../jet/plugin/quickfix/AutoImportFix.java | 4 +- .../plugin/quickfix/CastExpressionFix.java | 8 ++-- .../quickfix/ChangeAccessorTypeFix.java | 4 +- .../ChangeFunctionLiteralReturnTypeFix.java | 4 +- .../ChangeFunctionParameterTypeFix.java | 9 +--- .../quickfix/ChangeFunctionReturnTypeFix.java | 24 +++++----- .../quickfix/ChangeFunctionSignatureFix.java | 12 ++--- .../ChangeMemberFunctionSignatureFix.java | 4 +- .../quickfix/ChangeToBackingFieldFix.java | 4 +- .../ChangeToConstructorInvocationFix.java | 4 +- .../ChangeToFunctionInvocationFix.java | 4 +- .../quickfix/ChangeToPropertyNameFix.java | 4 +- .../quickfix/ChangeToStarProjectionFix.java | 4 +- .../jet/plugin/quickfix/ChangeTypeFix.java | 8 ++-- .../quickfix/ChangeVariableTypeFix.java | 8 ++-- .../quickfix/ChangeVisibilityModifierFix.java | 4 +- ...y.java => JetIntentionActionsFactory.java} | 9 ++-- .../JetSingleIntentionActionFactory.java | 42 +++++++++++++++++ .../MakeClassAnAnnotationClassFix.java | 4 +- .../quickfix/MakeOverriddenMemberOpenFix.java | 4 +- .../quickfix/MapPlatformClassToKotlinFix.java | 4 +- .../quickfix/MigrateSureInProjectFix.java | 4 +- .../quickfix/MoveWhenElseBranchFix.java | 4 +- .../jet/plugin/quickfix/QuickFixes.java | 46 +++++++++---------- .../quickfix/RemoveFunctionBodyFix.java | 4 +- .../plugin/quickfix/RemoveModifierFix.java | 20 ++++---- .../plugin/quickfix/RemoveNullableFix.java | 4 +- .../quickfix/RemovePartsFromPropertyFix.java | 4 +- .../quickfix/RemovePsiElementSimpleFix.java | 16 +++---- .../RemoveRightPartOfBinaryExpressionFix.java | 8 ++-- .../plugin/quickfix/RemoveSupertypeFix.java | 4 +- ...meParameterToMatchOverriddenMethodFix.java | 4 +- .../plugin/quickfix/ReplaceInfixCallFix.java | 4 +- ...ReplaceOperationInBinaryExpressionFix.java | 5 +- 43 files changed, 194 insertions(+), 160 deletions(-) rename idea/src/org/jetbrains/jet/plugin/quickfix/{JetIntentionActionFactory.java => JetIntentionActionsFactory.java} (80%) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/JetSingleIntentionActionFactory.java diff --git a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java index 21d32d41fa7..21b4a2a6729 100644 --- a/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java +++ b/idea/src/org/jetbrains/jet/plugin/highlighter/JetPsiChecker.java @@ -43,9 +43,8 @@ import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetReferenceExpression; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.plugin.project.WholeProjectAnalyzerFacade; -import org.jetbrains.jet.plugin.quickfix.JetIntentionActionFactory; +import org.jetbrains.jet.plugin.quickfix.JetIntentionActionsFactory; import org.jetbrains.jet.plugin.quickfix.QuickFixes; -import org.jetbrains.jet.utils.ExceptionUtils; import java.util.Collection; import java.util.List; @@ -217,14 +216,12 @@ public class JetPsiChecker implements Annotator { return null; } - Collection intentionActionFactories = QuickFixes.getActionFactories(diagnostic.getFactory()); - for (JetIntentionActionFactory intentionActionFactory : intentionActionFactories) { - IntentionAction action = null; - if (intentionActionFactory != null) { - action = intentionActionFactory.createAction(diagnostic); - } - if (action != null) { - annotation.registerFix(action); + Collection intentionActionsFactories = QuickFixes.getActionsFactories(diagnostic.getFactory()); + for (JetIntentionActionsFactory intentionActionsFactory : intentionActionsFactories) { + if (intentionActionsFactory != null) { + for (IntentionAction action: intentionActionsFactory.createActions(diagnostic)) { + annotation.registerFix(action); + } } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionBodyFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionBodyFix.java index 2654f228e78..b0819791d25 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionBodyFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionBodyFix.java @@ -67,8 +67,8 @@ public class AddFunctionBodyFix extends JetIntentionAction { element.replace(newElement); } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionToSupertypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionToSupertypeFix.java index 05844b5f937..f565679cb6d 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionToSupertypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddFunctionToSupertypeFix.java @@ -158,8 +158,8 @@ public class AddFunctionToSupertypeFix extends JetHintAction { return new JetAddFunctionToClassifierAction(project, editor, bindingContext, functionsToAdd); } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetIntentionActionsFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddModifierFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddModifierFix.java index 162dc6b7aa3..d3bdd9b985e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddModifierFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddModifierFix.java @@ -164,8 +164,8 @@ public class AddModifierFix extends JetIntentionAction { return true; } - public static JetIntentionActionFactory createFactory(final JetKeywordToken modifier, final Class modifierOwnerClass) { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory(final JetKeywordToken modifier, final Class modifierOwnerClass) { + return new JetSingleIntentionActionFactory() { @Override public IntentionAction createAction(Diagnostic diagnostic) { JetModifierListOwner modifierListOwner = QuickFixUtil.getParentElementOfType(diagnostic, modifierOwnerClass); @@ -175,7 +175,7 @@ public class AddModifierFix extends JetIntentionAction { }; } - public static JetIntentionActionFactory createFactory(JetKeywordToken modifier) { + public static JetSingleIntentionActionFactory createFactory(JetKeywordToken modifier) { return createFactory(modifier, JetModifierListOwner.class); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddNameToArgumentFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddNameToArgumentFix.java index cc53c6ad0e3..93c03343de2 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddNameToArgumentFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddNameToArgumentFix.java @@ -183,8 +183,8 @@ public class AddNameToArgumentFix extends JetIntentionAction { return JetBundle.message("add.name.to.argument.family"); } @NotNull - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetIntentionActionsFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddOpenModifierToClassDeclarationFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddOpenModifierToClassDeclarationFix.java index 8fa3b39a8c6..13509245a17 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddOpenModifierToClassDeclarationFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddOpenModifierToClassDeclarationFix.java @@ -81,8 +81,8 @@ public class AddOpenModifierToClassDeclarationFix extends JetIntentionAction editor.getCaretModel().moveToOffset(textRange.getStartOffset() + indexOfOpenBrace + 1); } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.java index c4cdd8d9533..68b66bfa623 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AutoImportFix.java @@ -289,8 +289,8 @@ public class AutoImportFix extends JetHintAction implem } @Nullable - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(@NotNull Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java index 9d96a551817..6cbf68bf8a9 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java @@ -81,8 +81,8 @@ public class CastExpressionFix extends JetIntentionAction { } @NotNull - public static JetIntentionActionFactory createFactoryForAutoCastImpossible() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactoryForAutoCastImpossible() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { @@ -96,8 +96,8 @@ public class CastExpressionFix extends JetIntentionAction { } @NotNull - public static JetIntentionActionFactory createFactoryForTypeMismatch() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactoryForTypeMismatch() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java index 6eef4a97ffe..59dee5775e8 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeAccessorTypeFix.java @@ -83,8 +83,8 @@ public class ChangeAccessorTypeFix extends JetIntentionAction createAction(Diagnostic diagnostic) { JetPropertyAccessor accessor = QuickFixUtil.getParentElementOfType(diagnostic, JetPropertyAccessor.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java index c93d084532e..b257b99cfdc 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java @@ -126,8 +126,8 @@ public class ChangeFunctionLiteralReturnTypeFix extends JetIntentionAction createAction(Diagnostic diagnostic) { JetSimpleNameExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetSimpleNameExpression.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java index 7890d0a49da..3cb0532bf48 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToConstructorInvocationFix.java @@ -84,8 +84,8 @@ public class ChangeToConstructorInvocationFix extends JetIntentionAction createAction(Diagnostic diagnostic) { if (diagnostic.getPsiElement() instanceof JetDelegatorToSuperClass) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java index acbdc338f17..b96a01b3155 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToFunctionInvocationFix.java @@ -50,8 +50,8 @@ public class ChangeToFunctionInvocationFix extends JetIntentionAction createAction(Diagnostic diagnostic) { if (diagnostic.getPsiElement() instanceof JetExpression) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToPropertyNameFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToPropertyNameFix.java index 96ecda49d7e..9d832492d35 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToPropertyNameFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToPropertyNameFix.java @@ -58,8 +58,8 @@ public class ChangeToPropertyNameFix extends JetIntentionAction createAction(Diagnostic diagnostic) { JetSimpleNameExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetSimpleNameExpression.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToStarProjectionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToStarProjectionFix.java index 18c050c42d3..ac083e375bd 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToStarProjectionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeToStarProjectionFix.java @@ -54,8 +54,8 @@ public class ChangeToStarProjectionFix extends JetIntentionAction { } @NotNull - public static JetIntentionActionFactory createFactoryForExpectedParameterTypeMismatch() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactoryForExpectedParameterTypeMismatch() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { @@ -75,8 +75,8 @@ public class ChangeTypeFix extends JetIntentionAction { } @NotNull - public static JetIntentionActionFactory createFactoryForExpectedReturnTypeMismatch() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactoryForExpectedReturnTypeMismatch() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index 3236e846711..c70d024f7a6 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -67,8 +67,8 @@ public class ChangeVariableTypeFix extends JetIntentionAction createAction(Diagnostic diagnostic) { PsiElement element = diagnostic.getPsiElement(); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/JetIntentionActionFactory.java b/idea/src/org/jetbrains/jet/plugin/quickfix/JetIntentionActionsFactory.java similarity index 80% rename from idea/src/org/jetbrains/jet/plugin/quickfix/JetIntentionActionFactory.java rename to idea/src/org/jetbrains/jet/plugin/quickfix/JetIntentionActionsFactory.java index 3301507e6ac..953acf3469b 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/JetIntentionActionFactory.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/JetIntentionActionsFactory.java @@ -17,12 +17,13 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.codeInsight.intention.IntentionAction; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.diagnostics.Diagnostic; -public interface JetIntentionActionFactory { +import java.util.List; - @Nullable - IntentionAction createAction(Diagnostic diagnostic); +public interface JetIntentionActionsFactory { + @NotNull + List createActions(Diagnostic diagnostic); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/JetSingleIntentionActionFactory.java b/idea/src/org/jetbrains/jet/plugin/quickfix/JetSingleIntentionActionFactory.java new file mode 100644 index 00000000000..1ecbd890a63 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/JetSingleIntentionActionFactory.java @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.intention.IntentionAction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; + +import java.util.LinkedList; +import java.util.List; + +public abstract class JetSingleIntentionActionFactory implements JetIntentionActionsFactory { + + @Nullable + public abstract IntentionAction createAction(Diagnostic diagnostic); + + @NotNull + @Override + public final List createActions(Diagnostic diagnostic) { + List intentionActionList = new LinkedList(); + IntentionAction intentionAction = createAction(diagnostic); + if (intentionAction != null) { + intentionActionList.add(intentionAction); + } + return intentionActionList; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/MakeClassAnAnnotationClassFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/MakeClassAnAnnotationClassFix.java index 4f5191179a3..6f2712cc75d 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/MakeClassAnAnnotationClassFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/MakeClassAnAnnotationClassFix.java @@ -90,8 +90,8 @@ public class MakeClassAnAnnotationClassFix extends JetIntentionAction { } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java index 132d8989c9d..95e503a6eab 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java @@ -78,8 +78,8 @@ public class MoveWhenElseBranchFix extends JetIntentionAction editor.getCaretModel().moveToOffset(insertedWhenEntry.getTextOffset() + cursorOffset); } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index a6b9c110a51..7a4ce23f253 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -30,10 +30,10 @@ import static org.jetbrains.jet.lexer.JetTokens.*; public class QuickFixes { - private static final Multimap factories = HashMultimap.create(); + private static final Multimap factories = HashMultimap.create(); private static final Multimap actions = HashMultimap.create(); - public static Collection getActionFactories(AbstractDiagnosticFactory diagnosticFactory) { + public static Collection getActionsFactories(AbstractDiagnosticFactory diagnosticFactory) { return factories.get(diagnosticFactory); } @@ -44,12 +44,12 @@ public class QuickFixes { private QuickFixes() {} static { - JetIntentionActionFactory removeAbstractModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(ABSTRACT_KEYWORD); - JetIntentionActionFactory addAbstractModifierFactory = AddModifierFix.createFactory(ABSTRACT_KEYWORD); + JetSingleIntentionActionFactory removeAbstractModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(ABSTRACT_KEYWORD); + JetSingleIntentionActionFactory addAbstractModifierFactory = AddModifierFix.createFactory(ABSTRACT_KEYWORD); factories.put(ABSTRACT_PROPERTY_IN_PRIMARY_CONSTRUCTOR_PARAMETERS, removeAbstractModifierFactory); - JetIntentionActionFactory removePartsFromPropertyFactory = RemovePartsFromPropertyFix.createFactory(); + JetSingleIntentionActionFactory removePartsFromPropertyFactory = RemovePartsFromPropertyFix.createFactory(); factories.put(ABSTRACT_PROPERTY_WITH_INITIALIZER, removeAbstractModifierFactory); factories.put(ABSTRACT_PROPERTY_WITH_INITIALIZER, removePartsFromPropertyFactory); @@ -63,13 +63,13 @@ public class QuickFixes { factories.put(MUST_BE_INITIALIZED_OR_BE_ABSTRACT, addAbstractModifierFactory); - JetIntentionActionFactory removeFinalModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(FINAL_KEYWORD); + JetSingleIntentionActionFactory removeFinalModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(FINAL_KEYWORD); - JetIntentionActionFactory addAbstractToClassFactory = AddModifierFix.createFactory(ABSTRACT_KEYWORD, JetClass.class); + JetSingleIntentionActionFactory addAbstractToClassFactory = AddModifierFix.createFactory(ABSTRACT_KEYWORD, JetClass.class); factories.put(ABSTRACT_PROPERTY_IN_NON_ABSTRACT_CLASS, removeAbstractModifierFactory); factories.put(ABSTRACT_PROPERTY_IN_NON_ABSTRACT_CLASS, addAbstractToClassFactory); - JetIntentionActionFactory removeFunctionBodyFactory = RemoveFunctionBodyFix.createFactory(); + JetSingleIntentionActionFactory removeFunctionBodyFactory = RemoveFunctionBodyFix.createFactory(); factories.put(ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS, removeAbstractModifierFactory); factories.put(ABSTRACT_FUNCTION_IN_NON_ABSTRACT_CLASS, addAbstractToClassFactory); @@ -79,7 +79,7 @@ public class QuickFixes { factories.put(FINAL_PROPERTY_IN_TRAIT, removeFinalModifierFactory); factories.put(FINAL_FUNCTION_WITH_NO_BODY, removeFinalModifierFactory); - JetIntentionActionFactory addFunctionBodyFactory = AddFunctionBodyFix.createFactory(); + JetSingleIntentionActionFactory addFunctionBodyFactory = AddFunctionBodyFix.createFactory(); factories.put(NON_ABSTRACT_FUNCTION_WITH_NO_BODY, addAbstractModifierFactory); factories.put(NON_ABSTRACT_FUNCTION_WITH_NO_BODY, addFunctionBodyFactory); @@ -97,13 +97,13 @@ public class QuickFixes { factories.put(USELESS_CAST_STATIC_ASSERT_IS_FINE, ReplaceOperationInBinaryExpressionFix.createChangeCastToStaticAssertFactory()); factories.put(USELESS_CAST, RemoveRightPartOfBinaryExpressionFix.createRemoveCastFactory()); - JetIntentionActionFactory changeAccessorTypeFactory = ChangeAccessorTypeFix.createFactory(); + JetSingleIntentionActionFactory changeAccessorTypeFactory = ChangeAccessorTypeFix.createFactory(); factories.put(WRONG_SETTER_PARAMETER_TYPE, changeAccessorTypeFactory); factories.put(WRONG_GETTER_RETURN_TYPE, changeAccessorTypeFactory); factories.put(USELESS_ELVIS, RemoveRightPartOfBinaryExpressionFix.createRemoveElvisOperatorFactory()); - JetIntentionActionFactory removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFactory(true); + JetSingleIntentionActionFactory removeRedundantModifierFactory = RemoveModifierFix.createRemoveModifierFactory(true); factories.put(REDUNDANT_MODIFIER, removeRedundantModifierFactory); factories.put(ABSTRACT_MODIFIER_IN_TRAIT, RemoveModifierFix.createRemoveModifierFromListOwnerFactory(ABSTRACT_KEYWORD, true)); factories.put(OPEN_MODIFIER_IN_TRAIT, RemoveModifierFix.createRemoveModifierFromListOwnerFactory(OPEN_KEYWORD, true)); @@ -112,28 +112,28 @@ public class QuickFixes { factories.put(INCOMPATIBLE_MODIFIERS, RemoveModifierFix.createRemoveModifierFactory(false)); factories.put(VARIANCE_ON_TYPE_PARAMETER_OF_FUNCTION_OR_PROPERTY, RemoveModifierFix.createRemoveVarianceFactory()); - JetIntentionActionFactory removeOpenModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(OPEN_KEYWORD); + JetSingleIntentionActionFactory removeOpenModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(OPEN_KEYWORD); factories.put(NON_FINAL_MEMBER_IN_FINAL_CLASS, AddModifierFix.createFactory(OPEN_KEYWORD, JetClass.class)); factories.put(NON_FINAL_MEMBER_IN_FINAL_CLASS, removeOpenModifierFactory); - JetIntentionActionFactory removeModifierFactory = RemoveModifierFix.createRemoveModifierFactory(); + JetSingleIntentionActionFactory removeModifierFactory = RemoveModifierFix.createRemoveModifierFactory(); factories.put(GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY, removeModifierFactory); factories.put(REDUNDANT_MODIFIER_IN_GETTER, removeRedundantModifierFactory); factories.put(ILLEGAL_MODIFIER, removeModifierFactory); - JetIntentionActionFactory changeToBackingFieldFactory = ChangeToBackingFieldFix.createFactory(); + JetSingleIntentionActionFactory changeToBackingFieldFactory = ChangeToBackingFieldFix.createFactory(); factories.put(INITIALIZATION_USING_BACKING_FIELD_CUSTOM_SETTER, changeToBackingFieldFactory); factories.put(INITIALIZATION_USING_BACKING_FIELD_OPEN_SETTER, changeToBackingFieldFactory); - JetIntentionActionFactory changeToPropertyNameFactory = ChangeToPropertyNameFix.createFactory(); + JetSingleIntentionActionFactory changeToPropertyNameFactory = ChangeToPropertyNameFix.createFactory(); factories.put(NO_BACKING_FIELD_ABSTRACT_PROPERTY, changeToPropertyNameFactory); factories.put(NO_BACKING_FIELD_CUSTOM_ACCESSORS, changeToPropertyNameFactory); factories.put(INACCESSIBLE_BACKING_FIELD, changeToPropertyNameFactory); - JetIntentionActionFactory unresolvedReferenceFactory = AutoImportFix.createFactory(); + JetSingleIntentionActionFactory unresolvedReferenceFactory = AutoImportFix.createFactory(); factories.put(UNRESOLVED_REFERENCE, unresolvedReferenceFactory); - JetIntentionActionFactory removeImportFixFactory = RemovePsiElementSimpleFix.createRemoveImportFactory(); + JetSingleIntentionActionFactory removeImportFixFactory = RemovePsiElementSimpleFix.createRemoveImportFactory(); factories.put(USELESS_SIMPLE_IMPORT, removeImportFixFactory); factories.put(USELESS_HIDDEN_IMPORT, removeImportFixFactory); @@ -173,7 +173,7 @@ public class QuickFixes { actions.put(UNNECESSARY_NOT_NULL_ASSERTION, ExclExclCallFix.removeExclExclCall()); factories.put(UNSAFE_INFIX_CALL, ReplaceInfixCallFix.createFactory()); - JetIntentionActionFactory removeProtectedModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(PROTECTED_KEYWORD); + JetSingleIntentionActionFactory removeProtectedModifierFactory = RemoveModifierFix.createRemoveModifierFromListOwnerFactory(PROTECTED_KEYWORD); factories.put(PACKAGE_MEMBER_CANNOT_BE_PROTECTED, removeProtectedModifierFactory); actions.put(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, new SpecifyTypeExplicitlyFix()); @@ -187,13 +187,13 @@ public class QuickFixes { factories.put(TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER, RemovePsiElementSimpleFix.createRemoveTypeArgumentsFactory()); - JetIntentionActionFactory changeToStarProjectionFactory = ChangeToStarProjectionFix.createFactory(); + JetSingleIntentionActionFactory changeToStarProjectionFactory = ChangeToStarProjectionFix.createFactory(); factories.put(UNCHECKED_CAST, changeToStarProjectionFactory); factories.put(CANNOT_CHECK_FOR_ERASED, changeToStarProjectionFactory); factories.put(INACCESSIBLE_OUTER_CLASS_EXPRESSION, AddModifierFix.createFactory(INNER_KEYWORD, JetClass.class)); - JetIntentionActionFactory addOpenModifierToClassDeclarationFix = AddOpenModifierToClassDeclarationFix.createFactory(); + JetSingleIntentionActionFactory addOpenModifierToClassDeclarationFix = AddOpenModifierToClassDeclarationFix.createFactory(); factories.put(FINAL_SUPERTYPE, addOpenModifierToClassDeclarationFix); factories.put(FINAL_UPPER_BOUND, addOpenModifierToClassDeclarationFix); @@ -214,12 +214,12 @@ public class QuickFixes { factories.put(DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED, AddSemicolonAfterFunctionCallFix.createFactory()); - JetIntentionActionFactory changeVariableTypeFix = ChangeVariableTypeFix.createFactoryForPropertyOrReturnTypeMismatchOnOverride(); + JetSingleIntentionActionFactory changeVariableTypeFix = ChangeVariableTypeFix.createFactoryForPropertyOrReturnTypeMismatchOnOverride(); factories.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, changeVariableTypeFix); factories.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, changeVariableTypeFix); factories.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, ChangeVariableTypeFix.createFactoryForComponentFunctionReturnTypeMismatch()); - JetIntentionActionFactory changeFunctionReturnTypeFix = ChangeFunctionReturnTypeFix.createFactoryForChangingReturnTypeToUnit(); + JetSingleIntentionActionFactory changeFunctionReturnTypeFix = ChangeFunctionReturnTypeFix.createFactoryForChangingReturnTypeToUnit(); factories.put(RETURN_TYPE_MISMATCH, changeFunctionReturnTypeFix); factories.put(NO_RETURN_IN_FUNCTION_WITH_BLOCK_BODY, changeFunctionReturnTypeFix); factories.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, ChangeFunctionReturnTypeFix.createFactoryForReturnTypeMismatchOnOverride()); @@ -236,7 +236,7 @@ public class QuickFixes { factories.put(EXPECTED_PARAMETER_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch()); factories.put(EXPECTED_RETURN_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedReturnTypeMismatch()); - JetIntentionActionFactory changeFunctionLiteralReturnTypeFix = ChangeFunctionLiteralReturnTypeFix.createFactoryForExpectedOrAssignmentTypeMismatch(); + JetSingleIntentionActionFactory changeFunctionLiteralReturnTypeFix = ChangeFunctionLiteralReturnTypeFix.createFactoryForExpectedOrAssignmentTypeMismatch(); factories.put(EXPECTED_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); factories.put(ASSIGNMENT_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveFunctionBodyFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveFunctionBodyFix.java index f93954651f4..19e77848cd5 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveFunctionBodyFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveFunctionBodyFix.java @@ -87,8 +87,8 @@ public class RemoveFunctionBodyFix extends JetIntentionAction { return false; } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveModifierFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveModifierFix.java index 6dfe9a14c17..03d61b2998a 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveModifierFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveModifierFix.java @@ -103,12 +103,12 @@ public class RemoveModifierFix extends JetIntentionAction } - public static JetIntentionActionFactory createRemoveModifierFromListOwnerFactory(JetKeywordToken modifier) { + public static JetSingleIntentionActionFactory createRemoveModifierFromListOwnerFactory(JetKeywordToken modifier) { return createRemoveModifierFromListOwnerFactory(modifier, false); } - public static JetIntentionActionFactory createRemoveModifierFromListOwnerFactory(final JetKeywordToken modifier, final boolean isRedundant) { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveModifierFromListOwnerFactory(final JetKeywordToken modifier, final boolean isRedundant) { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { @@ -119,12 +119,12 @@ public class RemoveModifierFix extends JetIntentionAction }; } - public static JetIntentionActionFactory createRemoveModifierFactory() { + public static JetSingleIntentionActionFactory createRemoveModifierFactory() { return createRemoveModifierFactory(false); } - public static JetIntentionActionFactory createRemoveModifierFactory(final boolean isRedundant) { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveModifierFactory(final boolean isRedundant) { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { @@ -139,8 +139,8 @@ public class RemoveModifierFix extends JetIntentionAction }; } - public static JetIntentionActionFactory createRemoveProjectionFactory(final boolean isRedundant) { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveProjectionFactory(final boolean isRedundant) { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { @@ -156,8 +156,8 @@ public class RemoveModifierFix extends JetIntentionAction }; } - public static JetIntentionActionFactory createRemoveVarianceFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveVarianceFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public JetIntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveNullableFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveNullableFix.java index fdabcc18e73..a1f0e08fd8b 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveNullableFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveNullableFix.java @@ -62,8 +62,8 @@ public class RemoveNullableFix extends JetIntentionAction { super.element.replace(type); } - public static JetIntentionActionFactory createFactory(final NullableKind typeOfError) { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory(final NullableKind typeOfError) { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { JetNullableType nullType = QuickFixUtil.getParentElementOfType(diagnostic, JetNullableType.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java index 6854ba50f3c..6778eec9646 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePartsFromPropertyFix.java @@ -121,8 +121,8 @@ public class RemovePartsFromPropertyFix extends JetIntentionAction } } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { PsiElement element = diagnostic.getPsiElement(); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePsiElementSimpleFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePsiElementSimpleFix.java index 43388a24bb5..40f46346cf6 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePsiElementSimpleFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemovePsiElementSimpleFix.java @@ -60,8 +60,8 @@ public class RemovePsiElementSimpleFix extends JetIntentionAction { element.delete(); } - public static JetIntentionActionFactory createRemoveImportFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveImportFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { JetImportDirective directive = QuickFixUtil.getParentElementOfType(diagnostic, JetImportDirective.class); @@ -79,8 +79,8 @@ public class RemovePsiElementSimpleFix extends JetIntentionAction { }; } - public static JetIntentionActionFactory createRemoveSpreadFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveSpreadFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { PsiElement element = diagnostic.getPsiElement(); @@ -92,8 +92,8 @@ public class RemovePsiElementSimpleFix extends JetIntentionAction { }; } - public static JetIntentionActionFactory createRemoveTypeArgumentsFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveTypeArgumentsFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { JetTypeArgumentList element = QuickFixUtil.getParentElementOfType(diagnostic, JetTypeArgumentList.class); @@ -104,8 +104,8 @@ public class RemovePsiElementSimpleFix extends JetIntentionAction { }; } - public static JetIntentionActionFactory createRemoveVariableFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveVariableFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { final JetProperty expression = QuickFixUtil.getParentElementOfType(diagnostic, JetProperty.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveRightPartOfBinaryExpressionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveRightPartOfBinaryExpressionFix.java index 3ad449a3bc4..a2918726a83 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveRightPartOfBinaryExpressionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveRightPartOfBinaryExpressionFix.java @@ -57,8 +57,8 @@ public class RemoveRightPartOfBinaryExpressionFix exten } } - public static JetIntentionActionFactory createRemoveCastFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveCastFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { JetBinaryExpressionWithTypeRHS expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpressionWithTypeRHS.class); @@ -68,8 +68,8 @@ public class RemoveRightPartOfBinaryExpressionFix exten }; } - public static JetIntentionActionFactory createRemoveElvisOperatorFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createRemoveElvisOperatorFactory() { + return new JetSingleIntentionActionFactory() { @Override public JetIntentionAction createAction(Diagnostic diagnostic) { JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java index e6374d65aa4..fd7983caa1e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RemoveSupertypeFix.java @@ -63,8 +63,8 @@ public class RemoveSupertypeFix extends JetIntentionAction createAction(Diagnostic diagnostic) { JetDelegationSpecifier superClass = QuickFixUtil.getParentElementOfType(diagnostic, JetDelegationSpecifier.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/RenameParameterToMatchOverriddenMethodFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/RenameParameterToMatchOverriddenMethodFix.java index 59c7b6fa088..2c41571125c 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/RenameParameterToMatchOverriddenMethodFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/RenameParameterToMatchOverriddenMethodFix.java @@ -82,8 +82,8 @@ public class RenameParameterToMatchOverriddenMethodFix extends JetIntentionActio } @NotNull - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Nullable @Override public IntentionAction createAction(Diagnostic diagnostic) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceInfixCallFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceInfixCallFix.java index b4c93eb5b5c..7f31feeac9e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceInfixCallFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceInfixCallFix.java @@ -59,8 +59,8 @@ public class ReplaceInfixCallFix extends JetIntentionAction return true; } - public static JetIntentionActionFactory createFactory() { - return new JetIntentionActionFactory() { + public static JetSingleIntentionActionFactory createFactory() { + return new JetSingleIntentionActionFactory() { @Override public IntentionAction createAction(Diagnostic diagnostic) { JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class); diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceOperationInBinaryExpressionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceOperationInBinaryExpressionFix.java index 317a2c3cd9d..2bb7c65bfdb 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceOperationInBinaryExpressionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ReplaceOperationInBinaryExpressionFix.java @@ -18,7 +18,6 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -55,8 +54,8 @@ public abstract class ReplaceOperationInBinaryExpressionFix createAction(Diagnostic diagnostic) { JetBinaryExpressionWithTypeRHS expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpressionWithTypeRHS.class); From 6c8096138a2e762316918ca7e067ff02ba177350 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Thu, 9 May 2013 11:05:38 +0200 Subject: [PATCH 07/19] Make ChangeFunctionReturnTypeFix handle function literals --- .../quickfix/ChangeFunctionReturnTypeFix.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index ad4e875ed06..599ef46e755 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -22,6 +22,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiErrorElement; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,19 +45,32 @@ import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; -public class ChangeFunctionReturnTypeFix extends JetIntentionAction { +public class ChangeFunctionReturnTypeFix extends JetIntentionAction { private final JetType type; private final String renderedType; + private final ChangeFunctionLiteralReturnTypeFix changeFunctionLiteralReturnTypeFix; - public ChangeFunctionReturnTypeFix(@NotNull JetNamedFunction element, @NotNull JetType type) { + public ChangeFunctionReturnTypeFix(@NotNull JetFunction element, @NotNull JetType type) { super(element); this.type = type; renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); + if (element instanceof JetFunctionLiteral) { + JetFunctionLiteralExpression functionLiteralExpression = PsiTreeUtil.getParentOfType(element, JetFunctionLiteralExpression.class); + assert functionLiteralExpression != null : "FunctionLiteral outside any FunctionLiteralExpression"; + changeFunctionLiteralReturnTypeFix = new ChangeFunctionLiteralReturnTypeFix(functionLiteralExpression, type); + } + else { + changeFunctionLiteralReturnTypeFix = null; + } } @NotNull @Override public String getText() { + if (changeFunctionLiteralReturnTypeFix != null) { + return changeFunctionLiteralReturnTypeFix.getText(); + } + String functionName = element.getName(); FqName fqName = JetPsiUtil.getFQName(element); if (fqName != null) functionName = fqName.asString(); @@ -79,9 +93,14 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction resolvedCall = context.get(BindingContext.COMPONENT_RESOLVED_CALL, entry); if (resolvedCall == null) return null; - JetNamedFunction componentFunction = (JetNamedFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + JetFunction componentFunction = (JetFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); JetType expectedType = context.get(BindingContext.TYPE, entry.getTypeRef()); if (componentFunction != null && expectedType != null) { return new ChangeFunctionReturnTypeFix(componentFunction, expectedType); @@ -138,7 +157,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction resolvedCall = context.get(BindingContext.LOOP_RANGE_HAS_NEXT_RESOLVED_CALL, expression); if (resolvedCall == null) return null; - JetNamedFunction hasNextFunction = (JetNamedFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + JetFunction hasNextFunction = (JetFunction) BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); if (hasNextFunction != null) { return new ChangeFunctionReturnTypeFix(hasNextFunction, KotlinBuiltIns.getInstance().getBooleanType()); } @@ -159,8 +178,8 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); if (resolvedCall == null) return null; PsiElement compareTo = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); - if (!(compareTo instanceof JetNamedFunction)) return null; - return new ChangeFunctionReturnTypeFix((JetNamedFunction) compareTo, KotlinBuiltIns.getInstance().getIntType()); + if (!(compareTo instanceof JetFunction)) return null; + return new ChangeFunctionReturnTypeFix((JetFunction) compareTo, KotlinBuiltIns.getInstance().getIntType()); } }; } @@ -171,7 +190,7 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction Date: Thu, 9 May 2013 22:07:31 +0200 Subject: [PATCH 08/19] Single factory for TYPE_MISMATCH error --- .../plugin/quickfix/CastExpressionFix.java | 29 ------- .../ChangeFunctionParameterTypeFix.java | 39 --------- .../quickfix/ChangeFunctionReturnTypeFix.java | 20 ----- .../QuickFixFactoryForTypeMismatchError.java | 84 +++++++++++++++++++ .../jet/plugin/quickfix/QuickFixUtil.java | 15 ++++ .../jet/plugin/quickfix/QuickFixes.java | 4 +- ...ypeWithoutChangingFunctionParameterType.kt | 7 ++ ...nLiteralTypeWithoutChangingPropertyType.kt | 7 ++ ...nTypeToMatchReturnTypeOfReturnedLiteral.kt | 4 + ...essionTypeMismatchFunctionParameterType.kt | 6 ++ ...MismatchInIfStatementReturnedByFunction.kt | 7 ++ ...eMismatchInIfStatementReturnedByLiteral.kt | 12 +++ ...ypeWithoutChangingFunctionParameterType.kt | 7 ++ ...nLiteralTypeWithoutChangingPropertyType.kt | 7 ++ ...nTypeToMatchReturnTypeOfReturnedLiteral.kt | 4 + ...CantEvaluateToExpresionThatTypeMismatch.kt | 6 ++ ...essionTypeMismatchFunctionParameterType.kt | 6 ++ ...MismatchInIfStatementReturnedByFunction.kt | 7 ++ ...eMismatchInIfStatementReturnedByLiteral.kt | 12 +++ .../quickfix/QuickFixTestGenerated.java | 35 ++++++++ 20 files changed, 227 insertions(+), 91 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java index 6cbf68bf8a9..d58f92e6ebd 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/CastExpressionFix.java @@ -20,7 +20,6 @@ import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; -import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -94,32 +93,4 @@ public class CastExpressionFix extends JetIntentionAction { } }; } - - @NotNull - public static JetSingleIntentionActionFactory createFactoryForTypeMismatch() { - return new JetSingleIntentionActionFactory() { - @Nullable - @Override - public IntentionAction createAction(Diagnostic diagnostic) { - assert diagnostic.getFactory() == Errors.TYPE_MISMATCH; - @SuppressWarnings("unchecked") - DiagnosticWithParameters2 diagnosticWithParameters = - (DiagnosticWithParameters2) diagnostic; - JetExpression expression = diagnosticWithParameters.getPsiElement(); - - // we don't want to cast a cast: - if (expression instanceof JetBinaryExpressionWithTypeRHS) { - return null; - } - - // 'x: Int' - TYPE_MISMATCH might be reported on 'x', and we don't want this quickfix to be available: - JetBinaryExpressionWithTypeRHS parentExpressionWithTypeRHS = - PsiTreeUtil.getParentOfType(expression, JetBinaryExpressionWithTypeRHS.class, true); - if (parentExpressionWithTypeRHS != null && parentExpressionWithTypeRHS.getLeft() == expression) { - return null; - } - return new CastExpressionFix(expression, diagnosticWithParameters.getA()); - } - }; - } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java index 3c7d2b2ba67..3375089e6f7 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionParameterTypeFix.java @@ -16,21 +16,16 @@ package org.jetbrains.jet.plugin.quickfix; -import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.psi.*; -import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.JetBundle; -import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; public class ChangeFunctionParameterTypeFix extends JetIntentionAction { @@ -68,38 +63,4 @@ public class ChangeFunctionParameterTypeFix extends JetIntentionAction } }; } - - @NotNull - public static JetSingleIntentionActionFactory createFactoryForTypeMismatch() { - return new JetSingleIntentionActionFactory() { - @Nullable - @Override - public IntentionAction createAction(Diagnostic diagnostic) { - JetExpression expression = (JetExpression) diagnostic.getPsiElement(); - JetNamedFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetNamedFunction.class); - - if (function != null && (function.getInitializer() == expression || expression.getParent() instanceof JetReturnExpression)) { - BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) diagnostic.getPsiFile()).getBindingContext(); - JetType type = context.get(BindingContext.EXPRESSION_TYPE, expression); - assert type != null : "Expression type mismatch, but expression has no type"; - return new ChangeFunctionReturnTypeFix(function, type); - } - return null; - } - }; - } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java new file mode 100644 index 00000000000..1f0ae66d09b --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -0,0 +1,84 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2; +import org.jetbrains.jet.lang.diagnostics.Errors; +import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; + +import java.util.LinkedList; +import java.util.List; + +public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsFactory { + @NotNull + @Override + public List createActions(Diagnostic diagnostic) { + List actions = new LinkedList(); + + assert diagnostic.getFactory() == Errors.TYPE_MISMATCH; + @SuppressWarnings("unchecked") + DiagnosticWithParameters2 diagnosticWithParameters = + (DiagnosticWithParameters2) diagnostic; + JetExpression expression = diagnosticWithParameters.getPsiElement(); + JetType expectedType = diagnosticWithParameters.getA(); + JetType expressionType = diagnosticWithParameters.getB(); + BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) diagnostic.getPsiFile()).getBindingContext(); + + // We don't want to cast a cast or type-asserted expression: + if (!(expression instanceof JetBinaryExpressionWithTypeRHS) && !(expression.getParent() instanceof JetBinaryExpressionWithTypeRHS)) { + actions.add(new CastExpressionFix(expression, expectedType)); + } + + // Mismatch in returned expression: + JetFunction function = PsiTreeUtil.getParentOfType(expression, JetFunction.class, true); + if (function != null && QuickFixUtil.canFunctionReturnExpression(function, expression)) { + actions.add(new ChangeFunctionReturnTypeFix(function, expressionType)); + } + + // Change type of a function parameter in case TYPE_MISMATCH is reported on expression passed as value argument of call. + // 1) When an argument is a dangling function literal: + JetFunctionLiteralExpression functionLiteralExpression = + QuickFixUtil.getParentElementOfType(diagnostic, JetFunctionLiteralExpression.class); + if (functionLiteralExpression != null && functionLiteralExpression.getBodyExpression() == expression) { + JetParameter correspondingParameter = + QuickFixUtil.getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(functionLiteralExpression); + JetType functionLiteralExpressionType = context.get(BindingContext.EXPRESSION_TYPE, functionLiteralExpression); + if (correspondingParameter != null && functionLiteralExpressionType != null) { + actions.add(new ChangeFunctionParameterTypeFix(correspondingParameter, functionLiteralExpressionType)); + } + } + // 2) When an argument is passed inside value argument list: + else { + JetValueArgument valueArgument = QuickFixUtil.getParentElementOfType(diagnostic, JetValueArgument.class); + if (valueArgument != null && valueArgument.getArgumentExpression() == expression) { + JetParameter correspondingParameter = QuickFixUtil.getFunctionParameterCorrespondingToValueArgumentPassedInCall(valueArgument); + JetType valueArgumentType = context.get(BindingContext.EXPRESSION_TYPE, valueArgument.getArgumentExpression()); + if (correspondingParameter != null && valueArgumentType != null) { + actions.add(new ChangeFunctionParameterTypeFix(correspondingParameter, valueArgumentType)); + } + } + } + return actions; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index beb270a8724..a7217ba7006 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -174,4 +174,19 @@ public class QuickFixUtil { } return true; } + + public static boolean canFunctionReturnExpression(@NotNull JetFunction function, @NotNull JetExpression expression) { + if (function instanceof JetFunctionLiteral) { + JetBlockExpression functionLiteralBody = ((JetFunctionLiteral) function).getBodyExpression(); + PsiElement returnedElement = functionLiteralBody == null ? null : functionLiteralBody.getLastChild(); + return returnedElement instanceof JetExpression && canEvaluateTo((JetExpression) returnedElement, expression); + } + else { + if (function instanceof JetWithExpressionInitializer && canEvaluateTo(((JetWithExpressionInitializer) function).getInitializer(), expression)) { + return true; + } + JetReturnExpression returnExpression = PsiTreeUtil.getParentOfType(expression, JetReturnExpression.class); + return returnExpression != null && canEvaluateTo(returnExpression.getReturnedExpression(), expression); + } + } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 7a4ce23f253..ad2eb561f5a 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -226,7 +226,6 @@ public class QuickFixes { factories.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForComponentFunctionReturnTypeMismatch()); factories.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForHasNextFunctionTypeMismatch()); factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch()); - factories.put(TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForTypeMismatch()); factories.put(TOO_MANY_ARGUMENTS, ChangeFunctionSignatureFix.createFactory()); factories.put(NO_VALUE_FOR_PARAMETER, ChangeFunctionSignatureFix.createFactory()); @@ -240,10 +239,9 @@ public class QuickFixes { factories.put(EXPECTED_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); factories.put(ASSIGNMENT_TYPE_MISMATCH, changeFunctionLiteralReturnTypeFix); - factories.put(TYPE_MISMATCH, ChangeFunctionParameterTypeFix.createFactory()); + factories.put(TYPE_MISMATCH, new QuickFixFactoryForTypeMismatchError()); factories.put(AUTOCAST_IMPOSSIBLE, CastExpressionFix.createFactoryForAutoCastImpossible()); - factories.put(TYPE_MISMATCH, CastExpressionFix.createFactoryForTypeMismatch()); factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory()); diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt new file mode 100644 index 00000000000..b2b6552068c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo(f: (String) -> Any) { + foo { + (s: String): String -> + s + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt new file mode 100644 index 00000000000..35be90c1bea --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionLiteralTypeWithoutChangingPropertyType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo() { + val f: (String) -> String = { + (s: Any): String -> + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt new file mode 100644 index 00000000000..5c7182093c5 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to '() -> Any'" "true" +fun foo(x: Any): () -> Any { + return {x} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt new file mode 100644 index 00000000000..16a4b270648 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterReturnedExpressionTypeMismatchFunctionParameterType.kt @@ -0,0 +1,6 @@ +// "Change parameter 'f' type of function 'foo' to '() -> String'" "true" +fun foo(f: () -> String) { + foo { + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt new file mode 100644 index 00000000000..031dded208b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByFunction.kt @@ -0,0 +1,7 @@ +// "Change 'boo' function return type to 'String'" "true" +fun boo(): String { + return ((if (true) { + val a = "" + a + } else "")) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt new file mode 100644 index 00000000000..19f21d70c71 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterTypeMismatchInIfStatementReturnedByLiteral.kt @@ -0,0 +1,12 @@ +// "Change 'f' type to '(Int, Int) -> (String) -> Int'" "true" +fun foo() { + val f: (Int, Int) -> (String) -> Int = { + (a: Int, b: Int): (String) -> Int -> + val x = {(s: String) -> 42} + if (true) x + else if (true) x else { + var y = 42 + if (true) x else x + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt new file mode 100644 index 00000000000..0a04b9f473b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo(f: (String) -> Any) { + foo { + (s: String): Int -> + s + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt new file mode 100644 index 00000000000..dbec7994ac1 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt @@ -0,0 +1,7 @@ +// "Change function literal return type to 'String'" "true" +fun foo() { + val f: (String) -> String = { + (s: Any): Int -> + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt new file mode 100644 index 00000000000..44c4c565685 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt @@ -0,0 +1,4 @@ +// "Change 'foo' function return type to '() -> Any'" "true" +fun foo(x: Any): () -> Int { + return {x} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt new file mode 100644 index 00000000000..8161c29c287 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt @@ -0,0 +1,6 @@ +// "Change function 'foo' return type to 'String'" "false" +// ERROR: Type mismatch.
Required:jet.Int
Found:jet.String
+// ACTION: Disable 'Replace 'if' with 'when'' +// ACTION: Edit intention settings +// ACTION: Replace 'if' with 'when' +fun foo(): Int = if (true) "": Int else 4 \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt new file mode 100644 index 00000000000..cb39787a072 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt @@ -0,0 +1,6 @@ +// "Change parameter 'f' type of function 'foo' to '() -> String'" "true" +fun foo(f: () -> Int) { + foo { + "" + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt new file mode 100644 index 00000000000..8a4869a7439 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt @@ -0,0 +1,7 @@ +// "Change 'boo' function return type to 'String'" "true" +fun boo(): Int { + return ((if (true) { + val a = "" + a + } else "")) +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt new file mode 100644 index 00000000000..ae1c5c6a142 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt @@ -0,0 +1,12 @@ +// "Change 'f' type to '(Int, Int) -> (String) -> Int'" "true" +fun foo() { + val f: () -> Long = { + (a: Int, b: Int): Long -> + val x = {(s: String) -> 42} + if (true) x + else if (true) x else { + var y = 42 + if (true) x else x + } + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 810c38bc41d..7dbabee2862 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1421,16 +1421,51 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeAssignmentTypeMismatch.kt"); } + @TestMetadata("beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt") + public void testChangeFunctionLiteralTypeWithoutChangingFunctionParameterType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingFunctionParameterType.kt"); + } + + @TestMetadata("beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt") + public void testChangeFunctionLiteralTypeWithoutChangingPropertyType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionLiteralTypeWithoutChangingPropertyType.kt"); + } + @TestMetadata("beforeChangeFunctionReturnTypeToFunctionType.kt") public void testChangeFunctionReturnTypeToFunctionType() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToFunctionType.kt"); } + @TestMetadata("beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt") + public void testChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt"); + } + @TestMetadata("beforeExpectedTypeMismatch.kt") public void testExpectedTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt"); } + @TestMetadata("beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt") + public void testReturnedExpresionCantEvaluateToExpresionThatTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt"); + } + + @TestMetadata("beforeReturnedExpressionTypeMismatchFunctionParameterType.kt") + public void testReturnedExpressionTypeMismatchFunctionParameterType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpressionTypeMismatchFunctionParameterType.kt"); + } + + @TestMetadata("beforeTypeMismatchInIfStatementReturnedByFunction.kt") + public void testTypeMismatchInIfStatementReturnedByFunction() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByFunction.kt"); + } + + @TestMetadata("beforeTypeMismatchInIfStatementReturnedByLiteral.kt") + public void testTypeMismatchInIfStatementReturnedByLiteral() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInIfStatementReturnedByLiteral.kt"); + } + @TestMetadata("beforeTypeMismatchInInitializer.kt") public void testTypeMismatchInInitializer() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeTypeMismatchInInitializer.kt"); From d3492d8e6f708d861641aeea0b2fce33f67cbc91 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Thu, 9 May 2013 22:24:15 +0200 Subject: [PATCH 09/19] QuickFix: change property type to match it's initializer type --- .../quickfix/QuickFixFactoryForTypeMismatchError.java | 6 ++++++ .../quickfix/typeMismatch/afterPropertyTypeMismatch.kt | 4 ++++ .../quickfix/typeMismatch/beforePropertyTypeMismatch.kt | 4 ++++ .../jet/plugin/quickfix/QuickFixTestGenerated.java | 5 +++++ 4 files changed, 19 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/afterPropertyTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforePropertyTypeMismatch.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java index 1f0ae66d09b..7ba9769d05e 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -50,6 +50,12 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF actions.add(new CastExpressionFix(expression, expectedType)); } + // Property initializer type mismatch property type: + JetProperty property = PsiTreeUtil.getParentOfType(expression, JetProperty.class); + if (property != null && QuickFixUtil.canEvaluateTo(property.getInitializer(), expression)) { + actions.add(new ChangeVariableTypeFix(property, expressionType)); + } + // Mismatch in returned expression: JetFunction function = PsiTreeUtil.getParentOfType(expression, JetFunction.class, true); if (function != null && QuickFixUtil.canFunctionReturnExpression(function, expression)) { diff --git a/idea/testData/quickfix/typeMismatch/afterPropertyTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/afterPropertyTypeMismatch.kt new file mode 100644 index 00000000000..4302b75770c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterPropertyTypeMismatch.kt @@ -0,0 +1,4 @@ +// "Change 'f' type to '(Long) -> Unit'" "true" +fun foo() { + var f: (Long) -> Unit = if (true) { (x: Long) -> } else { (x: Long) -> } +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforePropertyTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/beforePropertyTypeMismatch.kt new file mode 100644 index 00000000000..a1ffc07759c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforePropertyTypeMismatch.kt @@ -0,0 +1,4 @@ +// "Change 'f' type to '(Long) -> Unit'" "true" +fun foo() { + var f: Int = if (true) { (x: Long) -> } else { (x: Long) -> } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 7dbabee2862..034c2451009 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1286,6 +1286,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeNoReturnInFunctionWithBlockBody.kt"); } + @TestMetadata("beforePropertyTypeMismatch.kt") + public void testPropertyTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforePropertyTypeMismatch.kt"); + } + @TestMetadata("beforeReturnTypeMismatch.kt") public void testReturnTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeReturnTypeMismatch.kt"); From 76e648ded30e392d39be09065fcb2de6fea3ed66 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Fri, 10 May 2013 00:42:40 +0200 Subject: [PATCH 10/19] QuickFix: change overloaded operator parameter type or return type --- .../QuickFixFactoryForTypeMismatchError.java | 29 +++++++++++++++++++ .../afterChangeNotFunctionReturnType.kt | 7 +++++ .../afterChangePlusFunctionReturnType.kt | 8 +++++ .../afterChangeTimesFunctionParameterType.kt | 6 ++++ .../beforeChangeNotFunctionReturnType.kt | 7 +++++ .../beforeChangePlusFunctionReturnType.kt | 8 +++++ .../beforeChangeTimesFunctionParameterType.kt | 6 ++++ .../quickfix/QuickFixTestGenerated.java | 26 ++++++++++++++++- 8 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeNotFunctionReturnType.kt create mode 100644 idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangePlusFunctionReturnType.kt create mode 100644 idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeTimesFunctionParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeNotFunctionReturnType.kt create mode 100644 idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangePlusFunctionReturnType.kt create mode 100644 idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeTimesFunctionParameterType.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java index 7ba9769d05e..613136a1bd3 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -17,13 +17,17 @@ package org.jetbrains.jet.plugin.quickfix; import com.intellij.codeInsight.intention.IntentionAction; +import com.intellij.psi.PsiElement; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2; import org.jetbrains.jet.lang.diagnostics.Errors; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingContextUtils; +import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; @@ -62,6 +66,31 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF actions.add(new ChangeFunctionReturnTypeFix(function, expressionType)); } + // Fixing overloaded operators: + if (expression instanceof JetOperationExpression) { + ResolvedCall resolvedCall = + context.get(BindingContext.RESOLVED_CALL, ((JetOperationExpression) expression).getOperationReference()); + if (resolvedCall != null) { + PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getResultingDescriptor()); + if (declaration instanceof JetFunction) { + actions.add(new ChangeFunctionReturnTypeFix((JetFunction) declaration, expectedType)); + } + } + } + if (expression.getParent() instanceof JetBinaryExpression) { + JetBinaryExpression parentBinary = (JetBinaryExpression) expression.getParent(); + if (parentBinary.getRight() == expression) { + ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, parentBinary.getOperationReference()); + if (resolvedCall != null) { + PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getResultingDescriptor()); + if (declaration instanceof JetFunction) { + JetParameter binaryOperatorParameter = ((JetFunction) declaration).getValueParameterList().getParameters().get(0); + actions.add(new ChangeFunctionParameterTypeFix(binaryOperatorParameter, expressionType)); + } + } + } + } + // Change type of a function parameter in case TYPE_MISMATCH is reported on expression passed as value argument of call. // 1) When an argument is a dangling function literal: JetFunctionLiteralExpression functionLiteralExpression = diff --git a/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeNotFunctionReturnType.kt b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeNotFunctionReturnType.kt new file mode 100644 index 00000000000..2e02e549264 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeNotFunctionReturnType.kt @@ -0,0 +1,7 @@ +// "Change 'A.not' function return type to 'A'" "true" +trait A { + fun not(): A + fun times(a: A): A +} + +fun foo(a: A): A = a * !(if (true) a else a) \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangePlusFunctionReturnType.kt b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangePlusFunctionReturnType.kt new file mode 100644 index 00000000000..5f841fe38b4 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangePlusFunctionReturnType.kt @@ -0,0 +1,8 @@ +// "Change 'A.plus' function return type to '() -> Int'" "true" +trait A { + fun plus(a: A): () -> Int +} + +fun foo(a: A): () -> Int { + return a + a +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeTimesFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeTimesFunctionParameterType.kt new file mode 100644 index 00000000000..a0178c297d5 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/afterChangeTimesFunctionParameterType.kt @@ -0,0 +1,6 @@ +// "Change parameter 'a' type of function 'A.times' to 'String'" "true" +trait A { + fun times(a: String): A +} + +fun foo(a: A): A = a * "" \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeNotFunctionReturnType.kt b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeNotFunctionReturnType.kt new file mode 100644 index 00000000000..3fcbddff06c --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeNotFunctionReturnType.kt @@ -0,0 +1,7 @@ +// "Change 'A.not' function return type to 'A'" "true" +trait A { + fun not(): String + fun times(a: A): A +} + +fun foo(a: A): A = a * !(if (true) a else a) \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangePlusFunctionReturnType.kt b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangePlusFunctionReturnType.kt new file mode 100644 index 00000000000..c83d83f0c41 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangePlusFunctionReturnType.kt @@ -0,0 +1,8 @@ +// "Change 'A.plus' function return type to '() -> Int'" "true" +trait A { + fun plus(a: A): String +} + +fun foo(a: A): () -> Int { + return a + a +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeTimesFunctionParameterType.kt b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeTimesFunctionParameterType.kt new file mode 100644 index 00000000000..837fc7af446 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeTimesFunctionParameterType.kt @@ -0,0 +1,6 @@ +// "Change parameter 'a' type of function 'A.times' to 'String'" "true" +trait A { + fun times(a: A): A +} + +fun foo(a: A): A = a * "" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 034c2451009..a4eb2f2f42a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1240,7 +1240,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } @TestMetadata("idea/testData/quickfix/typeMismatch") - @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FunctionParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) + @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FixOverloadedOperator.class, TypeMismatch.FunctionParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) public static class TypeMismatch extends AbstractQuickFixTest { public void testAllFilesPresentInTypeMismatch() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); @@ -1382,6 +1382,29 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } + @TestMetadata("idea/testData/quickfix/typeMismatch/fixOverloadedOperator") + public static class FixOverloadedOperator extends AbstractQuickFixTest { + public void testAllFilesPresentInFixOverloadedOperator() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/fixOverloadedOperator"), Pattern.compile("^before(\\w+)\\.kt$"), true); + } + + @TestMetadata("beforeChangeNotFunctionReturnType.kt") + public void testChangeNotFunctionReturnType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeNotFunctionReturnType.kt"); + } + + @TestMetadata("beforeChangePlusFunctionReturnType.kt") + public void testChangePlusFunctionReturnType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangePlusFunctionReturnType.kt"); + } + + @TestMetadata("beforeChangeTimesFunctionParameterType.kt") + public void testChangeTimesFunctionParameterType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/fixOverloadedOperator/beforeChangeTimesFunctionParameterType.kt"); + } + + } + @TestMetadata("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch") public static class FunctionParameterTypeMismatch extends AbstractQuickFixTest { public void testAllFilesPresentInFunctionParameterTypeMismatch() throws Exception { @@ -1488,6 +1511,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { suite.addTestSuite(TypeMismatch.class); suite.addTestSuite(Casts.class); suite.addTestSuite(ComponentFunctionReturnTypeMismatch.class); + suite.addTestSuite(FixOverloadedOperator.class); suite.addTestSuite(FunctionParameterTypeMismatch.class); suite.addTestSuite(TypeMismatchOnReturnedExpression.class); return suite; From 1a5d5159727ec0c6f33331787f8e10582b45681b Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Fri, 10 May 2013 00:57:45 +0200 Subject: [PATCH 11/19] QuickFix: change function return type to match expexted type of call --- .../QuickFixFactoryForTypeMismatchError.java | 12 ++++++++++++ ...ngeFunctionReturnTypeToMatchExpectedTypeOfCall.kt | 3 +++ ...ngeFunctionReturnTypeToMatchExpectedTypeOfCall.kt | 3 +++ .../jet/plugin/quickfix/QuickFixTestGenerated.java | 5 +++++ 4 files changed, 23 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt create mode 100644 idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java index 613136a1bd3..70c1c6632b0 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -91,6 +91,18 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF } } + // Change function return type when TYPE_MISMATCH is reported on call expression: + if (expression instanceof JetCallExpression) { + ResolvedCall resolvedCall = + context.get(BindingContext.RESOLVED_CALL, ((JetCallExpression) expression).getCalleeExpression()); + if (resolvedCall != null) { + PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getResultingDescriptor()); + if (declaration instanceof JetFunction) { + actions.add(new ChangeFunctionReturnTypeFix((JetFunction) declaration, expectedType)); + } + } + } + // Change type of a function parameter in case TYPE_MISMATCH is reported on expression passed as value argument of call. // 1) When an argument is a dangling function literal: JetFunctionLiteralExpression functionLiteralExpression = diff --git a/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt b/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt new file mode 100644 index 00000000000..e043c28dd7b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/afterChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt @@ -0,0 +1,3 @@ +// "Change 'bar' function return type to 'String'" "true" +fun bar(): String = "" +fun foo(): String = bar() \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt b/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt new file mode 100644 index 00000000000..dcd7a090c5b --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt @@ -0,0 +1,3 @@ +// "Change 'bar' function return type to 'String'" "true" +fun bar(): Any = "" +fun foo(): String = bar() \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index a4eb2f2f42a..a7a0898cffe 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1251,6 +1251,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeChangeFunctionLiteralParameterTypeToFunctionType.kt"); } + @TestMetadata("beforeChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt") + public void testChangeFunctionReturnTypeToMatchExpectedTypeOfCall() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeChangeFunctionReturnTypeToMatchExpectedTypeOfCall.kt"); + } + @TestMetadata("beforeChangeReturnTypeWhenFunctionNameIsMissing.kt") public void testChangeReturnTypeWhenFunctionNameIsMissing() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeChangeReturnTypeWhenFunctionNameIsMissing.kt"); From 0d65a1b7895ed6bdc68f47cf4f6e5c90de251bc2 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 15 May 2013 20:25:34 +0200 Subject: [PATCH 12/19] Better text for ChangeVariableTypeFix --- .../jet/plugin/quickfix/ChangeVariableTypeFix.java | 7 ++++++- .../afterChangeOverridingPropertyTypeToFunctionType.kt | 2 +- .../afterPropertyReturnTypeMismatchOnOverride.kt | 2 +- .../afterPropertyTypeMismatchOnOverrideIntLong.kt | 2 +- .../afterPropertyTypeMismatchOnOverrideIntUnit.kt | 2 +- .../beforeChangeOverridingPropertyTypeToFunctionType.kt | 2 +- .../beforePropertyReturnTypeMismatchOnOverride.kt | 2 +- .../beforePropertyTypeMismatchOnOverrideIntLong.kt | 2 +- .../beforePropertyTypeMismatchOnOverrideIntUnit.kt | 2 +- 9 files changed, 14 insertions(+), 9 deletions(-) diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index c70d024f7a6..4df5e245e88 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -30,6 +30,7 @@ import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; +import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil; @@ -48,7 +49,11 @@ public class ChangeVariableTypeFix extends JetIntentionAction Int'" "true" +// "Change 'B.x' type to '(String) -> Int'" "true" trait A { var x: (String) -> Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyReturnTypeMismatchOnOverride.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyReturnTypeMismatchOnOverride.kt index 9dbb3cbe563..0f79cd40f9d 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyReturnTypeMismatchOnOverride.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyReturnTypeMismatchOnOverride.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to 'Int'" "true" +// "Change 'A.x' type to 'Int'" "true" trait X { val x: Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntLong.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntLong.kt index e810c22a1ee..80d3b2e6a05 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntLong.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntLong.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to 'Int'" "true" +// "Change 'B.x' type to 'Int'" "true" abstract class A { abstract var x : Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntUnit.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntUnit.kt index 8296b837504..f37ab4f9ff0 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntUnit.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterPropertyTypeMismatchOnOverrideIntUnit.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to 'Int'" "true" +// "Change 'B.x' type to 'Int'" "true" abstract class A { abstract var x : Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt index 86cf8bd43da..6a6b1155d01 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to '(String) -> Int'" "true" +// "Change 'B.x' type to '(String) -> Int'" "true" trait A { var x: (String) -> Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyReturnTypeMismatchOnOverride.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyReturnTypeMismatchOnOverride.kt index afd321e5cfa..4378d809f4a 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyReturnTypeMismatchOnOverride.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyReturnTypeMismatchOnOverride.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to 'Int'" "true" +// "Change 'A.x' type to 'Int'" "true" trait X { val x: Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntLong.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntLong.kt index db0eabb5856..a70d8d74a05 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntLong.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntLong.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to 'Int'" "true" +// "Change 'B.x' type to 'Int'" "true" abstract class A { abstract var x : Int } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntUnit.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntUnit.kt index 39289180da2..469a62099b0 100644 --- a/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntUnit.kt +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyTypeMismatchOnOverrideIntUnit.kt @@ -1,4 +1,4 @@ -// "Change 'x' type to 'Int'" "true" +// "Change 'B.x' type to 'Int'" "true" abstract class A { abstract var x : Int } From 9fdbd01660c21f21091c53cdf2a3eca2565bd4e4 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 15 May 2013 21:16:41 +0200 Subject: [PATCH 13/19] QuickFix: change type of overridden property to type of overridding property --- .../quickfix/ChangeVariableTypeFix.java | 48 +++++++++++++++---- .../jet/plugin/quickfix/QuickFixes.java | 2 +- .../afterChangeOverriddenPropertyType.kt | 12 +++++ ...ChangeMultipleOverriddenPropertiesTypes.kt | 14 ++++++ .../beforeChangeOverriddenPropertyType.kt | 12 +++++ .../quickfix/QuickFixTestGenerated.java | 10 ++++ 6 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index 4df5e245e88..e30cd47a574 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -25,6 +25,7 @@ import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.PropertyDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; @@ -32,12 +33,16 @@ import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil; import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; +import java.util.LinkedList; +import java.util.List; + public class ChangeVariableTypeFix extends JetIntentionAction { private final String renderedType; @@ -90,16 +95,43 @@ public class ChangeVariableTypeFix extends JetIntentionAction createActions(Diagnostic diagnostic) { + List actions = new LinkedList(); + JetProperty property = QuickFixUtil.getParentElementOfType(diagnostic, JetProperty.class); - if (property == null) return null; - BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(property); - JetType type = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, property); - return type == null ? null : new ChangeVariableTypeFix(property, type); + if (property != null) { + BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(property); + JetType lowerBoundOfOverriddenPropertiesTypes = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, property); + if (lowerBoundOfOverriddenPropertiesTypes != null) { + actions.add(new ChangeVariableTypeFix(property, lowerBoundOfOverriddenPropertiesTypes)); + } + + PropertyDescriptor descriptor = (PropertyDescriptor) context.get(BindingContext.DECLARATION_TO_DESCRIPTOR, property); + assert descriptor != null : "Descriptor of property not available in binding context"; + JetType propertyType = descriptor.getReturnType(); + assert propertyType != null : "Property type cannot be null if it mismatch something"; + + List overriddenMismatchingProperties = new LinkedList(); + for (PropertyDescriptor overriddenProperty: descriptor.getOverriddenDescriptors()) { + JetType overriddenPropertyType = overriddenProperty.getReturnType(); + if (overriddenPropertyType != null && !JetTypeChecker.INSTANCE.isSubtypeOf(propertyType, overriddenPropertyType)) { + overriddenMismatchingProperties.add(overriddenProperty); + } + } + + if (overriddenMismatchingProperties.size() == 1) { + JetProperty overriddenProperty = + (JetProperty) BindingContextUtils.descriptorToDeclaration(context, overriddenMismatchingProperties.get(0)); + if (overriddenProperty != null) { + actions.add(new ChangeVariableTypeFix(overriddenProperty, propertyType)); + } + } + } + return actions; } }; } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index ad2eb561f5a..63f87c80c17 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -214,7 +214,7 @@ public class QuickFixes { factories.put(DANGLING_FUNCTION_LITERAL_ARGUMENT_SUSPECTED, AddSemicolonAfterFunctionCallFix.createFactory()); - JetSingleIntentionActionFactory changeVariableTypeFix = ChangeVariableTypeFix.createFactoryForPropertyOrReturnTypeMismatchOnOverride(); + JetIntentionActionsFactory changeVariableTypeFix = ChangeVariableTypeFix.createFactoryForPropertyOrReturnTypeMismatchOnOverride(); factories.put(RETURN_TYPE_MISMATCH_ON_OVERRIDE, changeVariableTypeFix); factories.put(PROPERTY_TYPE_MISMATCH_ON_OVERRIDE, changeVariableTypeFix); factories.put(COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH, ChangeVariableTypeFix.createFactoryForComponentFunctionReturnTypeMismatch()); diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType.kt new file mode 100644 index 00000000000..6a3586ed492 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType.kt @@ -0,0 +1,12 @@ +// "Change 'B.x' type to '(Int) -> Int'" "true" +trait A { + val x: (Int) -> Int +} + +trait B { + val x: (Int) -> Int +} + +trait C : A, B { + override val x: (Int) -> Int +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt new file mode 100644 index 00000000000..9fe0b5a6264 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt @@ -0,0 +1,14 @@ +// "Change 'A.x' type to '(Int) -> Int'" "false" +// ACTION: Change 'C.x' type to '(String) -> Int' +// ERROR: Return type is '(jet.Int) → jet.Int', which is not a subtype of overridden
internal abstract val x: (jet.String) → jet.Int defined in A +trait A { + val x: (String) -> Int +} + +trait B { + val x: (String) -> Any +} + +trait C : A, B { + override val x: (Int) -> Int +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt new file mode 100644 index 00000000000..9216f429193 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt @@ -0,0 +1,12 @@ +// "Change 'B.x' type to '(Int) -> Int'" "true" +trait A { + val x: (Int) -> Int +} + +trait B { + val x: (String) -> Any +} + +trait C : A, B { + override val x: (Int) -> Int +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index a7a0898cffe..19755e455eb 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1018,6 +1018,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/override/typeMismatchOnOverride"), Pattern.compile("^before(\\w+)\\.kt$"), true); } + @TestMetadata("beforeCantChangeMultipleOverriddenPropertiesTypes.kt") + public void testCantChangeMultipleOverriddenPropertiesTypes() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt"); + } + + @TestMetadata("beforeChangeOverriddenPropertyType.kt") + public void testChangeOverriddenPropertyType() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt"); + } + @TestMetadata("beforeChangeOverridingPropertyTypeToFunctionType.kt") public void testChangeOverridingPropertyTypeToFunctionType() throws Exception { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt"); From e08d9ee8da49df82f6f6e5d3f92700745a1a8363 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 15 May 2013 21:43:37 +0200 Subject: [PATCH 14/19] Fix quickFix for PROPERTY_TYPE_MISMATCH_ON_OVERRIDE --- .../quickfix/ChangeVariableTypeFix.java | 30 ++++++++++++------- ... => afterChangeOverriddenPropertyType1.kt} | 0 .../afterChangeOverriddenPropertyType2.kt | 12 ++++++++ ...enPropertyTypeToMatchOverridingProperty.kt | 13 ++++++++ ...ePropertyTypeToMatchOverridenProperties.kt | 13 ++++++++ ...=> beforeChangeOverriddenPropertyType1.kt} | 0 .../beforeChangeOverriddenPropertyType2.kt | 12 ++++++++ .../quickfix/QuickFixTestGenerated.java | 21 +++++++++++-- 8 files changed, 88 insertions(+), 13 deletions(-) rename idea/testData/quickfix/override/typeMismatchOnOverride/{afterChangeOverriddenPropertyType.kt => afterChangeOverriddenPropertyType1.kt} (100%) create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType2.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt rename idea/testData/quickfix/override/typeMismatchOnOverride/{beforeChangeOverriddenPropertyType.kt => beforeChangeOverriddenPropertyType1.kt} (100%) create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType2.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index e30cd47a574..9d9f5cdfd9f 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -106,9 +106,6 @@ public class ChangeVariableTypeFix extends JetIntentionAction overriddenMismatchingProperties = new LinkedList(); + boolean canChangeOverriddenPropertyType = true; for (PropertyDescriptor overriddenProperty: descriptor.getOverriddenDescriptors()) { JetType overriddenPropertyType = overriddenProperty.getReturnType(); - if (overriddenPropertyType != null && !JetTypeChecker.INSTANCE.isSubtypeOf(propertyType, overriddenPropertyType)) { - overriddenMismatchingProperties.add(overriddenProperty); + if (overriddenPropertyType != null) { + if (!JetTypeChecker.INSTANCE.isSubtypeOf(propertyType, overriddenPropertyType)) { + overriddenMismatchingProperties.add(overriddenProperty); + } + else if (overriddenProperty.isVar() && !JetTypeChecker.INSTANCE.equalTypes(overriddenPropertyType, propertyType)) { + canChangeOverriddenPropertyType = false; + } + if (overriddenProperty.isVar() && lowerBoundOfOverriddenPropertiesTypes != null && + !JetTypeChecker.INSTANCE.equalTypes(lowerBoundOfOverriddenPropertiesTypes, overriddenPropertyType)) { + lowerBoundOfOverriddenPropertiesTypes = null; + } } } - if (overriddenMismatchingProperties.size() == 1) { - JetProperty overriddenProperty = - (JetProperty) BindingContextUtils.descriptorToDeclaration(context, overriddenMismatchingProperties.get(0)); - if (overriddenProperty != null) { - actions.add(new ChangeVariableTypeFix(overriddenProperty, propertyType)); + if (lowerBoundOfOverriddenPropertiesTypes != null) { + actions.add(new ChangeVariableTypeFix(property, lowerBoundOfOverriddenPropertiesTypes)); + } + + if (overriddenMismatchingProperties.size() == 1 && canChangeOverriddenPropertyType) { + PsiElement overriddenProperty = BindingContextUtils.descriptorToDeclaration(context, overriddenMismatchingProperties.get(0)); + if (overriddenProperty instanceof JetProperty) { + actions.add(new ChangeVariableTypeFix((JetProperty) overriddenProperty, propertyType)); } } } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType1.kt similarity index 100% rename from idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType.kt rename to idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType1.kt diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType2.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType2.kt new file mode 100644 index 00000000000..fd1e09a06ce --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeOverriddenPropertyType2.kt @@ -0,0 +1,12 @@ +// "Change 'A.x' type to 'String'" "true" +trait A { + var x: String +} + +trait B { + var x: String +} + +trait C : A, B { + override var x: String +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt new file mode 100644 index 00000000000..1a7149f1ca8 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt @@ -0,0 +1,13 @@ +// "Change 'A.x' type to 'String'" "false" +// ERROR: Var-property type is 'jet.String', which is not a type of overridden
internal abstract var x: jet.Int defined in A +trait A { + var x: Int +} + +trait B { + var x: Any +} + +trait C : A, B { + override var x: String +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt new file mode 100644 index 00000000000..c47babf6e69 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt @@ -0,0 +1,13 @@ +// "Change 'C.x' type to 'String'" "false" +// ERROR: Var-property type is 'jet.Int', which is not a type of overridden
internal abstract var x: jet.String defined in A +trait A { + var x: String +} + +trait B { + var x: Any +} + +trait C : A, B { + override var x: Int +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType1.kt similarity index 100% rename from idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt rename to idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType1.kt diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType2.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType2.kt new file mode 100644 index 00000000000..df144be7ce6 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType2.kt @@ -0,0 +1,12 @@ +// "Change 'A.x' type to 'String'" "true" +trait A { + var x: Int +} + +trait B { + var x: String +} + +trait C : A, B { + override var x: String +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 19755e455eb..9b22cc5d85a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1023,9 +1023,24 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeMultipleOverriddenPropertiesTypes.kt"); } - @TestMetadata("beforeChangeOverriddenPropertyType.kt") - public void testChangeOverriddenPropertyType() throws Exception { - doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType.kt"); + @TestMetadata("beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt") + public void testCantChangeOverriddenPropertyTypeToMatchOverridingProperty() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeOverriddenPropertyTypeToMatchOverridingProperty.kt"); + } + + @TestMetadata("beforeCantChangePropertyTypeToMatchOverridenProperties.kt") + public void testCantChangePropertyTypeToMatchOverridenProperties() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt"); + } + + @TestMetadata("beforeChangeOverriddenPropertyType1.kt") + public void testChangeOverriddenPropertyType1() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType1.kt"); + } + + @TestMetadata("beforeChangeOverriddenPropertyType2.kt") + public void testChangeOverriddenPropertyType2() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType2.kt"); } @TestMetadata("beforeChangeOverridingPropertyTypeToFunctionType.kt") From 16d9abf5466979d7f0a5c5824ac560e80e95e2c0 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 15 May 2013 23:02:36 +0200 Subject: [PATCH 15/19] QuickFix: change type of overridden function to type of overridding function --- .../quickfix/ChangeFunctionReturnTypeFix.java | 51 +++++++++++++++---- ...terChangeReturnTypeOfOverriddenFunction.kt | 12 +++++ ...antChangeReturnTypeOfOverriddenFunction.kt | 13 +++++ ...oreChangeReturnTypeOfOverriddenFunction.kt | 12 +++++ .../quickfix/QuickFixTestGenerated.java | 10 ++++ 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeReturnTypeOfOverriddenFunction.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeReturnTypeOfOverriddenFunction.kt create mode 100644 idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeReturnTypeOfOverriddenFunction.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index b9483a3e818..c9a974790aa 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -28,6 +28,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.CallableDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; +import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor; import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters3; import org.jetbrains.jet.lang.psi.*; @@ -38,6 +39,7 @@ import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.plugin.JetBundle; import org.jetbrains.jet.plugin.caches.resolve.KotlinCacheManagerUtil; @@ -45,6 +47,9 @@ import org.jetbrains.jet.plugin.intentions.SpecifyTypeExplicitlyAction; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; +import java.util.LinkedList; +import java.util.List; + public class ChangeFunctionReturnTypeFix extends JetIntentionAction { private final JetType type; private final String renderedType; @@ -175,7 +180,8 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction JetBinaryExpression expression = QuickFixUtil.getParentElementOfType(diagnostic, JetBinaryExpression.class); assert expression != null : "COMPARE_TO_TYPE_MISMATCH reported on element that is not within any expression"; BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) expression.getContainingFile()).getBindingContext(); - ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression.getOperationReference()); + ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, + expression.getOperationReference()); if (resolvedCall == null) return null; PsiElement compareTo = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); if (!(compareTo instanceof JetFunction)) return null; @@ -185,16 +191,43 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction } @NotNull - public static JetSingleIntentionActionFactory createFactoryForReturnTypeMismatchOnOverride() { - return new JetSingleIntentionActionFactory() { - @Nullable + public static JetIntentionActionsFactory createFactoryForReturnTypeMismatchOnOverride() { + return new JetIntentionActionsFactory() { + @NotNull @Override - public IntentionAction createAction(Diagnostic diagnostic) { + public List createActions(Diagnostic diagnostic) { + List actions = new LinkedList(); + JetFunction function = QuickFixUtil.getParentElementOfType(diagnostic, JetFunction.class); - if (function == null) return null; - BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(function); - JetType matchingReturnType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, function); - return matchingReturnType == null ? null : new ChangeFunctionReturnTypeFix(function, matchingReturnType); + if (function != null) { + BindingContext context = KotlinCacheManagerUtil.getDeclarationsBindingContext(function); + JetType matchingReturnType = QuickFixUtil.findLowerBoundOfOverriddenCallablesReturnTypes(context, function); + if (matchingReturnType != null) { + actions.add(new ChangeFunctionReturnTypeFix(function, matchingReturnType)); + } + + SimpleFunctionDescriptor descriptor = context.get(BindingContext.FUNCTION, function); + if (descriptor == null) return actions; + JetType functionType = descriptor.getReturnType(); + if (functionType == null) return actions; + + List overriddenMismatchingFunctions = new LinkedList(); + for (FunctionDescriptor overriddenFunction: descriptor.getOverriddenDescriptors()) { + JetType overriddenFunctionType = overriddenFunction.getReturnType(); + if (overriddenFunctionType == null) continue; + if (!JetTypeChecker.INSTANCE.isSubtypeOf(functionType, overriddenFunctionType)) { + overriddenMismatchingFunctions.add(overriddenFunction); + } + } + + if (overriddenMismatchingFunctions.size() == 1) { + PsiElement overriddenFunction = BindingContextUtils.descriptorToDeclaration(context, overriddenMismatchingFunctions.get(0)); + if (overriddenFunction instanceof JetFunction) { + actions.add(new ChangeFunctionReturnTypeFix((JetFunction) overriddenFunction, functionType)); + } + } + } + return actions; } }; } diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeReturnTypeOfOverriddenFunction.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeReturnTypeOfOverriddenFunction.kt new file mode 100644 index 00000000000..0efefb16139 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/afterChangeReturnTypeOfOverriddenFunction.kt @@ -0,0 +1,12 @@ +// "Change 'A.foo' function return type to 'Long'" "true" +trait A { + fun foo(): Long +} + +trait B { + fun foo(): Number +} + +trait C : A, B { + override fun foo(): Long +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeReturnTypeOfOverriddenFunction.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeReturnTypeOfOverriddenFunction.kt new file mode 100644 index 00000000000..302ed8d5be4 --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeReturnTypeOfOverriddenFunction.kt @@ -0,0 +1,13 @@ +// "Change 'A.foo' function return type to 'Long'" "false" +// ERROR: Return type is 'jet.Long', which is not a subtype of overridden
internal abstract fun foo(): jet.Int defined in A +trait A { + fun foo(): Int +} + +trait B { + fun foo(): String +} + +trait C : A, B { + override fun foo(): Long +} \ No newline at end of file diff --git a/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeReturnTypeOfOverriddenFunction.kt b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeReturnTypeOfOverriddenFunction.kt new file mode 100644 index 00000000000..a8ccbc2551c --- /dev/null +++ b/idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeReturnTypeOfOverriddenFunction.kt @@ -0,0 +1,12 @@ +// "Change 'A.foo' function return type to 'Long'" "true" +trait A { + fun foo(): Int +} + +trait B { + fun foo(): Number +} + +trait C : A, B { + override fun foo(): Long +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 9b22cc5d85a..8e766d00b3a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1033,6 +1033,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangePropertyTypeToMatchOverridenProperties.kt"); } + @TestMetadata("beforeCantChangeReturnTypeOfOverriddenFunction.kt") + public void testCantChangeReturnTypeOfOverriddenFunction() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeCantChangeReturnTypeOfOverriddenFunction.kt"); + } + @TestMetadata("beforeChangeOverriddenPropertyType1.kt") public void testChangeOverriddenPropertyType1() throws Exception { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverriddenPropertyType1.kt"); @@ -1048,6 +1053,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeOverridingPropertyTypeToFunctionType.kt"); } + @TestMetadata("beforeChangeReturnTypeOfOverriddenFunction.kt") + public void testChangeReturnTypeOfOverriddenFunction() throws Exception { + doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforeChangeReturnTypeOfOverriddenFunction.kt"); + } + @TestMetadata("beforePropertyReturnTypeMismatchOnOverride.kt") public void testPropertyReturnTypeMismatchOnOverride() throws Exception { doTest("idea/testData/quickfix/override/typeMismatchOnOverride/beforePropertyReturnTypeMismatchOnOverride.kt"); From 0a541373a504da242f484aebe0ccc5369d2c00b9 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 15 May 2013 23:48:49 +0200 Subject: [PATCH 16/19] QuickFix: change property type to match expression returned by getter --- .../ChangeFunctionLiteralReturnTypeFix.java | 2 +- .../plugin/quickfix/ChangeVariableTypeFix.java | 15 +++++++++++++++ .../QuickFixFactoryForTypeMismatchError.java | 10 +++++++--- .../jet/plugin/quickfix/QuickFixUtil.java | 8 ++++---- .../afterPropertyGetterInitializerTypeMismatch.kt | 6 ++++++ ...beforePropertyGetterInitializerTypeMismatch.kt | 6 ++++++ .../plugin/quickfix/QuickFixTestGenerated.java | 5 +++++ 7 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java index b257b99cfdc..c7ac17cd164 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java @@ -85,7 +85,7 @@ public class ChangeFunctionLiteralReturnTypeFix extends JetIntentionAction typeWhiteSpaceAndColon = JetPsiFactory.createTypeWhiteSpaceAndColon(project, renderedType); element.addRangeAfter(typeWhiteSpaceAndColon.first, typeWhiteSpaceAndColon.second, nameIdentifier); + + if (element instanceof JetProperty) { + JetPropertyAccessor getter = ((JetProperty) element).getGetter(); + JetTypeReference getterReturnTypeRef = getter == null ? null : getter.getReturnTypeReference(); + if (getterReturnTypeRef != null) { + getterReturnTypeRef.replace(JetPsiFactory.createType(project, renderedType)); + } + + JetPropertyAccessor setter = ((JetProperty) element).getSetter(); + JetParameter setterParameter = setter == null ? null : setter.getParameter(); + JetTypeReference setterParameterTypeRef = setterParameter == null ? null : setterParameter.getTypeReference(); + if (setterParameterTypeRef != null) { + setterParameterTypeRef.replace(JetPsiFactory.createType(project, renderedType)); + } + } } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java index 70c1c6632b0..ec304583cb9 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -56,13 +56,17 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF // Property initializer type mismatch property type: JetProperty property = PsiTreeUtil.getParentOfType(expression, JetProperty.class); - if (property != null && QuickFixUtil.canEvaluateTo(property.getInitializer(), expression)) { - actions.add(new ChangeVariableTypeFix(property, expressionType)); + if (property != null) { + JetPropertyAccessor getter = property.getGetter(); + if (QuickFixUtil.canEvaluateTo(property.getInitializer(), expression) || + (getter != null && QuickFixUtil.canFunctionOrGetterReturnExpression(property.getGetter(), expression))) { + actions.add(new ChangeVariableTypeFix(property, expressionType)); + } } // Mismatch in returned expression: JetFunction function = PsiTreeUtil.getParentOfType(expression, JetFunction.class, true); - if (function != null && QuickFixUtil.canFunctionReturnExpression(function, expression)) { + if (function != null && QuickFixUtil.canFunctionOrGetterReturnExpression(function, expression)) { actions.add(new ChangeFunctionReturnTypeFix(function, expressionType)); } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index a7217ba7006..c7d003be348 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -175,14 +175,14 @@ public class QuickFixUtil { return true; } - public static boolean canFunctionReturnExpression(@NotNull JetFunction function, @NotNull JetExpression expression) { - if (function instanceof JetFunctionLiteral) { - JetBlockExpression functionLiteralBody = ((JetFunctionLiteral) function).getBodyExpression(); + public static boolean canFunctionOrGetterReturnExpression(@NotNull JetDeclaration functionOrGetter, @NotNull JetExpression expression) { + if (functionOrGetter instanceof JetFunctionLiteral) { + JetBlockExpression functionLiteralBody = ((JetFunctionLiteral) functionOrGetter).getBodyExpression(); PsiElement returnedElement = functionLiteralBody == null ? null : functionLiteralBody.getLastChild(); return returnedElement instanceof JetExpression && canEvaluateTo((JetExpression) returnedElement, expression); } else { - if (function instanceof JetWithExpressionInitializer && canEvaluateTo(((JetWithExpressionInitializer) function).getInitializer(), expression)) { + if (functionOrGetter instanceof JetWithExpressionInitializer && canEvaluateTo(((JetWithExpressionInitializer) functionOrGetter).getInitializer(), expression)) { return true; } JetReturnExpression returnExpression = PsiTreeUtil.getParentOfType(expression, JetReturnExpression.class); diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt new file mode 100644 index 00000000000..e22fc7027ef --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/afterPropertyGetterInitializerTypeMismatch.kt @@ -0,0 +1,6 @@ +// "Change 'A.x' type to '() -> Int'" "true" +class A { + var x: () -> Int + get(): () -> Int = if (true) { {42} } else { {24} } + set(i: () -> Int) {} +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt new file mode 100644 index 00000000000..c9196877276 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt @@ -0,0 +1,6 @@ +// "Change 'A.x' type to '() -> Int'" "true" +class A { + var x: Int + get(): Int = if (true) { {42} } else { {24} } + set(i: Int) {} +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 8e766d00b3a..66801905864 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1514,6 +1514,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt"); } + @TestMetadata("beforePropertyGetterInitializerTypeMismatch.kt") + public void testPropertyGetterInitializerTypeMismatch() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforePropertyGetterInitializerTypeMismatch.kt"); + } + @TestMetadata("beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt") public void testReturnedExpresionCantEvaluateToExpresionThatTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeReturnedExpresionCantEvaluateToExpresionThatTypeMismatch.kt"); From c6a378f4fbfd5ee3a878ea091eb8fc9f58581fbf Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Thu, 16 May 2013 00:44:23 +0200 Subject: [PATCH 17/19] Rename directory with tests --- .../afterChangeFunctionParameterType1.kt | 0 .../afterChangeFunctionParameterType2.kt | 0 .../afterChangeFunctionParameterType3.kt | 0 .../beforeChangeFunctionParameterType1.kt | 0 .../beforeChangeFunctionParameterType2.kt | 0 .../beforeChangeFunctionParameterType3.kt | 0 .../beforeChangeFunctionParameterType4.kt | 0 .../beforeChangeFunctionParameterType5.kt | 0 .../quickfix/QuickFixTestGenerated.java | 22 +++++++++---------- 9 files changed, 11 insertions(+), 11 deletions(-) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/afterChangeFunctionParameterType1.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/afterChangeFunctionParameterType2.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/afterChangeFunctionParameterType3.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/beforeChangeFunctionParameterType1.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/beforeChangeFunctionParameterType2.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/beforeChangeFunctionParameterType3.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/beforeChangeFunctionParameterType4.kt (100%) rename idea/testData/quickfix/typeMismatch/{functionParameterTypeMismatch => parameterTypeMismatch}/beforeChangeFunctionParameterType5.kt (100%) diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType1.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangeFunctionParameterType1.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType1.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangeFunctionParameterType1.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType2.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangeFunctionParameterType2.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType2.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangeFunctionParameterType2.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType3.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangeFunctionParameterType3.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/afterChangeFunctionParameterType3.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangeFunctionParameterType3.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType1.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType1.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType2.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType2.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType3.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType3.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType4.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType4.kt diff --git a/idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType5.kt similarity index 100% rename from idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt rename to idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType5.kt diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 66801905864..5ad88c7a44e 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1275,7 +1275,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } @TestMetadata("idea/testData/quickfix/typeMismatch") - @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FixOverloadedOperator.class, TypeMismatch.FunctionParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) + @InnerTestClasses({TypeMismatch.Casts.class, TypeMismatch.ComponentFunctionReturnTypeMismatch.class, TypeMismatch.FixOverloadedOperator.class, TypeMismatch.ParameterTypeMismatch.class, TypeMismatch.TypeMismatchOnReturnedExpression.class}) public static class TypeMismatch extends AbstractQuickFixTest { public void testAllFilesPresentInTypeMismatch() throws Exception { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); @@ -1445,35 +1445,35 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } - @TestMetadata("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch") - public static class FunctionParameterTypeMismatch extends AbstractQuickFixTest { - public void testAllFilesPresentInFunctionParameterTypeMismatch() throws Exception { - JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); + @TestMetadata("idea/testData/quickfix/typeMismatch/parameterTypeMismatch") + public static class ParameterTypeMismatch extends AbstractQuickFixTest { + public void testAllFilesPresentInParameterTypeMismatch() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/quickfix/typeMismatch/parameterTypeMismatch"), Pattern.compile("^before(\\w+)\\.kt$"), true); } @TestMetadata("beforeChangeFunctionParameterType1.kt") public void testChangeFunctionParameterType1() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType1.kt"); + doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType1.kt"); } @TestMetadata("beforeChangeFunctionParameterType2.kt") public void testChangeFunctionParameterType2() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType2.kt"); + doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType2.kt"); } @TestMetadata("beforeChangeFunctionParameterType3.kt") public void testChangeFunctionParameterType3() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType3.kt"); + doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType3.kt"); } @TestMetadata("beforeChangeFunctionParameterType4.kt") public void testChangeFunctionParameterType4() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType4.kt"); + doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType4.kt"); } @TestMetadata("beforeChangeFunctionParameterType5.kt") public void testChangeFunctionParameterType5() throws Exception { - doTest("idea/testData/quickfix/typeMismatch/functionParameterTypeMismatch/beforeChangeFunctionParameterType5.kt"); + doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType5.kt"); } } @@ -1557,7 +1557,7 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { suite.addTestSuite(Casts.class); suite.addTestSuite(ComponentFunctionReturnTypeMismatch.class); suite.addTestSuite(FixOverloadedOperator.class); - suite.addTestSuite(FunctionParameterTypeMismatch.class); + suite.addTestSuite(ParameterTypeMismatch.class); suite.addTestSuite(TypeMismatchOnReturnedExpression.class); return suite; } From 2a97617880e0b164017f64ce887bfa3faa1a210e Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Thu, 16 May 2013 00:43:26 +0200 Subject: [PATCH 18/19] QuickFix: change type of constructor parameter to match value argument in invocation --- .../jetbrains/jet/plugin/JetBundle.properties | 1 + .../ChangeFunctionLiteralReturnTypeFix.java | 4 ++-- ...peFix.java => ChangeParameterTypeFix.java} | 20 +++++++++++-------- .../QuickFixFactoryForTypeMismatchError.java | 12 +++++------ .../jet/plugin/quickfix/QuickFixUtil.java | 19 ++++++++++-------- ...erChangePrimaryConstructorParameterType.kt | 5 +++++ ...reChangePrimaryConstructorParameterType.kt | 5 +++++ .../quickfix/QuickFixTestGenerated.java | 5 +++++ 8 files changed, 47 insertions(+), 24 deletions(-) rename idea/src/org/jetbrains/jet/plugin/quickfix/{ChangeFunctionParameterTypeFix.java => ChangeParameterTypeFix.java} (67%) create mode 100644 idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangePrimaryConstructorParameterType.kt create mode 100644 idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangePrimaryConstructorParameterType.kt diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 6f7ebde6deb..a68d3edbb91 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -64,6 +64,7 @@ remove.function.return.type=Remove explicitly specified return type in ''{0}'' f remove.no.name.function.return.type=Remove explicitly specified function return type change.element.type=Change ''{0}'' type to ''{1}'' change.function.parameter.type=Change parameter ''{0}'' type of function ''{1}'' to ''{2}'' +change.primary.constructor.parameter.type=Change parameter ''{0}'' type of primary constructor of class ''{1}'' to ''{2}'' change.type=Change type from ''{0}'' to ''{1}'' change.type.family=Change Type add.parameters.to.function=Add parameter{0} to function ''{1}'' diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java index c7ac17cd164..3027cba79c4 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionLiteralReturnTypeFix.java @@ -74,12 +74,12 @@ public class ChangeFunctionLiteralReturnTypeFix extends JetIntentionAction { +public class ChangeParameterTypeFix extends JetIntentionAction { private final String renderedType; - private final String containingFunctionName; + private final String containingDeclarationName; + private final boolean isPrimaryConstructorParameter; - public ChangeFunctionParameterTypeFix(@NotNull JetParameter element, @NotNull JetType type) { + public ChangeParameterTypeFix(@NotNull JetParameter element, @NotNull JetType type) { super(element); renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); - JetFunction function = PsiTreeUtil.getParentOfType(element, JetFunction.class); - FqName functionFQName = function == null ? null : JetPsiUtil.getFQName(function); - containingFunctionName = functionFQName == null ? null : functionFQName.getFqName(); + JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class); + isPrimaryConstructorParameter = declaration instanceof JetClass; + FqName declarationFQName = declaration == null ? null : JetPsiUtil.getFQName(declaration); + containingDeclarationName = declarationFQName == null ? null : declarationFQName.asString(); } @Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { - return super.isAvailable(project, editor, file) && containingFunctionName != null; + return super.isAvailable(project, editor, file) && containingDeclarationName != null; } @NotNull @Override public String getText() { - return JetBundle.message("change.function.parameter.type", element.getName(), containingFunctionName, renderedType); + return isPrimaryConstructorParameter ? + JetBundle.message("change.primary.constructor.parameter.type", element.getName(), containingDeclarationName, renderedType) : + JetBundle.message("change.function.parameter.type", element.getName(), containingDeclarationName, renderedType); } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java index ec304583cb9..9eb2dd5314b 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixFactoryForTypeMismatchError.java @@ -89,7 +89,7 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getResultingDescriptor()); if (declaration instanceof JetFunction) { JetParameter binaryOperatorParameter = ((JetFunction) declaration).getValueParameterList().getParameters().get(0); - actions.add(new ChangeFunctionParameterTypeFix(binaryOperatorParameter, expressionType)); + actions.add(new ChangeParameterTypeFix(binaryOperatorParameter, expressionType)); } } } @@ -113,20 +113,20 @@ public class QuickFixFactoryForTypeMismatchError implements JetIntentionActionsF QuickFixUtil.getParentElementOfType(diagnostic, JetFunctionLiteralExpression.class); if (functionLiteralExpression != null && functionLiteralExpression.getBodyExpression() == expression) { JetParameter correspondingParameter = - QuickFixUtil.getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(functionLiteralExpression); + QuickFixUtil.getParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(functionLiteralExpression); JetType functionLiteralExpressionType = context.get(BindingContext.EXPRESSION_TYPE, functionLiteralExpression); if (correspondingParameter != null && functionLiteralExpressionType != null) { - actions.add(new ChangeFunctionParameterTypeFix(correspondingParameter, functionLiteralExpressionType)); + actions.add(new ChangeParameterTypeFix(correspondingParameter, functionLiteralExpressionType)); } } // 2) When an argument is passed inside value argument list: else { JetValueArgument valueArgument = QuickFixUtil.getParentElementOfType(diagnostic, JetValueArgument.class); - if (valueArgument != null && valueArgument.getArgumentExpression() == expression) { - JetParameter correspondingParameter = QuickFixUtil.getFunctionParameterCorrespondingToValueArgumentPassedInCall(valueArgument); + if (valueArgument != null && QuickFixUtil.canEvaluateTo(valueArgument.getArgumentExpression(), expression)) { + JetParameter correspondingParameter = QuickFixUtil.getParameterCorrespondingToValueArgumentPassedInCall(valueArgument); JetType valueArgumentType = context.get(BindingContext.EXPRESSION_TYPE, valueArgument.getArgumentExpression()); if (correspondingParameter != null && valueArgumentType != null) { - actions.add(new ChangeFunctionParameterTypeFix(correspondingParameter, valueArgumentType)); + actions.add(new ChangeParameterTypeFix(correspondingParameter, valueArgumentType)); } } } diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java index c7d003be348..11380a56cc4 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixUtil.java @@ -95,30 +95,33 @@ public class QuickFixUtil { } @Nullable - public static JetParameterList getParameterListOfCalledFunction(@NotNull JetCallExpression callExpression) { + public static JetParameterList getParameterListOfCallee(@NotNull JetCallExpression callExpression) { BindingContext context = AnalyzerFacadeWithCache.analyzeFileWithCache((JetFile) callExpression.getContainingFile()).getBindingContext(); ResolvedCall resolvedCall = context.get(BindingContext.RESOLVED_CALL, callExpression.getCalleeExpression()); if (resolvedCall == null) return null; - PsiElement functionDeclaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); - if (functionDeclaration instanceof JetFunction) { - return ((JetFunction) functionDeclaration).getValueParameterList(); + PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, resolvedCall.getCandidateDescriptor()); + if (declaration instanceof JetFunction) { + return ((JetFunction) declaration).getValueParameterList(); + } + if (declaration instanceof JetClass) { + return ((JetClass) declaration).getPrimaryConstructorParameterList(); } return null; } @Nullable - public static JetParameter getFunctionParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(@NotNull JetFunctionLiteralExpression functionLiteralExpression) { + public static JetParameter getParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList(@NotNull JetFunctionLiteralExpression functionLiteralExpression) { if (!(functionLiteralExpression.getParent() instanceof JetCallExpression)) { return null; } JetCallExpression callExpression = (JetCallExpression) functionLiteralExpression.getParent(); - JetParameterList parameterList = getParameterListOfCalledFunction(callExpression); + JetParameterList parameterList = getParameterListOfCallee(callExpression); if (parameterList == null) return null; return parameterList.getParameters().get(parameterList.getParameters().size() - 1); } @Nullable - public static JetParameter getFunctionParameterCorrespondingToValueArgumentPassedInCall(@NotNull JetValueArgument valueArgument) { + public static JetParameter getParameterCorrespondingToValueArgumentPassedInCall(@NotNull JetValueArgument valueArgument) { if (!(valueArgument.getParent() instanceof JetValueArgumentList)) { return null; } @@ -127,7 +130,7 @@ public class QuickFixUtil { return null; } JetCallExpression callExpression = (JetCallExpression) valueArgumentList.getParent(); - JetParameterList parameterList = getParameterListOfCalledFunction(callExpression); + JetParameterList parameterList = getParameterListOfCallee(callExpression); if (parameterList == null) return null; int position = valueArgumentList.getArguments().indexOf(valueArgument); if (position == -1) return null; diff --git a/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangePrimaryConstructorParameterType.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangePrimaryConstructorParameterType.kt new file mode 100644 index 00000000000..9acd1ad0389 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/afterChangePrimaryConstructorParameterType.kt @@ -0,0 +1,5 @@ +// "Change parameter 'a' type of primary constructor of class 'B' to 'String'" "true" +class B(val a: String) +fun foo() { + B(if (true) "" else "") +} diff --git a/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangePrimaryConstructorParameterType.kt b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangePrimaryConstructorParameterType.kt new file mode 100644 index 00000000000..7e39c54a4a1 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangePrimaryConstructorParameterType.kt @@ -0,0 +1,5 @@ +// "Change parameter 'a' type of primary constructor of class 'B' to 'String'" "true" +class B(val a: Int) +fun foo() { + B(if (true) "" else "") +} diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index 5ad88c7a44e..fe3b3c67a02 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1476,6 +1476,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangeFunctionParameterType5.kt"); } + @TestMetadata("beforeChangePrimaryConstructorParameterType.kt") + public void testChangePrimaryConstructorParameterType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/parameterTypeMismatch/beforeChangePrimaryConstructorParameterType.kt"); + } + } @TestMetadata("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression") From 4bf7151e473cf3cf78c89379943b895b936703e9 Mon Sep 17 00:00:00 2001 From: Wojciech Lopata Date: Wed, 29 May 2013 13:52:27 +0200 Subject: [PATCH 19/19] Make "type mismatch" quickfixes not available in case of error types --- .../plugin/quickfix/ChangeFunctionReturnTypeFix.java | 7 +++++++ .../jet/plugin/quickfix/ChangeVariableTypeFix.java | 9 +++++++++ ...eforeDontChangeOverriddenPropertyTypeToErrorType.kt | 10 ++++++++++ .../beforeDontChangeFunctionReturnTypeToErrorType.kt | 6 ++++++ .../jet/plugin/quickfix/QuickFixTestGenerated.java | 10 ++++++++++ 5 files changed, 42 insertions(+) create mode 100644 idea/testData/quickfix/typeMismatch/beforeDontChangeOverriddenPropertyTypeToErrorType.kt create mode 100644 idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java index c9a974790aa..cf22d69bf9c 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeFunctionReturnTypeFix.java @@ -22,6 +22,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiErrorElement; +import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -38,6 +39,7 @@ import org.jetbrains.jet.lang.resolve.DescriptorResolver; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.Name; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; @@ -96,6 +98,11 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction return JetBundle.message("change.type.family"); } + @Override + public boolean isAvailable(@NotNull Project project, @Nullable Editor editor, @Nullable PsiFile file) { + return super.isAvailable(project, editor, file) && !ErrorUtils.containsErrorType(type); + } + @Override public void invoke(@NotNull Project project, @Nullable Editor editor, @Nullable JetFile file) throws IncorrectOperationException { if (changeFunctionLiteralReturnTypeFix != null) { diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java index c80f8d356dd..bcb408900ba 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/ChangeVariableTypeFix.java @@ -21,6 +21,7 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,6 +33,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall; import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.plugin.JetBundle; @@ -45,10 +47,12 @@ import java.util.List; public class ChangeVariableTypeFix extends JetIntentionAction { private final String renderedType; + private final JetType type; public ChangeVariableTypeFix(@NotNull JetVariableDeclaration element, @NotNull JetType type) { super(element); renderedType = DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(type); + this.type = type; } @NotNull @@ -67,6 +71,11 @@ public class ChangeVariableTypeFix extends JetIntentionAction [ERROR : Ay]'" "false" +// ACTION: Change 'A.x' type to '(Int) -> Int' +// ERROR: Return type is '(jet.Int) → jet.Int', which is not a subtype of overridden
internal abstract val x: (jet.String) → [ERROR : Ay] defined in A +// ERROR: Unresolved reference: Ay +trait A { + val x: (String) -> Ay +} +trait B : A { + override val x: (Int) -> Int +} \ No newline at end of file diff --git a/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt new file mode 100644 index 00000000000..ff1d6c75cf7 --- /dev/null +++ b/idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt @@ -0,0 +1,6 @@ +// "Change 'foo' function return type to '([ERROR : NoSuchType]) -> Int'" "false" +// ERROR: Type mismatch.
Required:jet.Int
Found:([ERROR : NoSuchType]) → jet.Int
+// ERROR: Unresolved reference: NoSuchType +fun foo(): Int { + return { (x: NoSuchType) -> 42 } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java index fe3b3c67a02..f4226698736 100644 --- a/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/jet/plugin/quickfix/QuickFixTestGenerated.java @@ -1306,6 +1306,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/beforeCompareToTypeMismatch.kt"); } + @TestMetadata("beforeDontChangeOverriddenPropertyTypeToErrorType.kt") + public void testDontChangeOverriddenPropertyTypeToErrorType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/beforeDontChangeOverriddenPropertyTypeToErrorType.kt"); + } + @TestMetadata("beforeExpectedParameterTypeMismatch.kt") public void testExpectedParameterTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/beforeExpectedParameterTypeMismatch.kt"); @@ -1514,6 +1519,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeChangeFunctionReturnTypeToMatchReturnTypeOfReturnedLiteral.kt"); } + @TestMetadata("beforeDontChangeFunctionReturnTypeToErrorType.kt") + public void testDontChangeFunctionReturnTypeToErrorType() throws Exception { + doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeDontChangeFunctionReturnTypeToErrorType.kt"); + } + @TestMetadata("beforeExpectedTypeMismatch.kt") public void testExpectedTypeMismatch() throws Exception { doTest("idea/testData/quickfix/typeMismatch/typeMismatchOnReturnedExpression/beforeExpectedTypeMismatch.kt");