QuickFix for EXPECTED_PARAMETER/RETURN_TYPE_MISMATCH (ChangeTypeFix)

This commit is contained in:
Wojciech Lopata
2013-04-16 12:33:42 +02:00
parent 328e579852
commit d396c08454
8 changed files with 133 additions and 0 deletions
@@ -60,6 +60,7 @@ add.semicolon.family=Add Semicolon
change.function.return.type=Change ''{0}'' function return type to ''{1}''
remove.function.return.type=Remove explicitly specified return type in ''{0}'' function
change.element.type=Change ''{0}'' type to ''{1}''
change.type=Change type from ''{0}'' to ''{1}''
change.type.family=Change Type
add.kotlin.signature.action.family.name=Specify Custom Kotlin Signature
@@ -0,0 +1,89 @@
/*
* 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.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.JetParameter;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetTypeReference;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.JetBundle;
public class ChangeTypeFix extends JetIntentionAction<JetTypeReference> {
private final JetType type;
public ChangeTypeFix(@NotNull JetTypeReference element, JetType type) {
super(element);
this.type = type;
}
@NotNull
@Override
public String getText() {
return JetBundle.message("change.type", element.getText(), type);
}
@NotNull
@Override
public String getFamilyName() {
return JetBundle.message("change.type.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
element.replace(JetPsiFactory.createType(project, type.toString()));
}
@NotNull
public static JetIntentionActionFactory createFactoryForExpectedParameterTypeMismatch() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
assert diagnostic.getFactory() == Errors.EXPECTED_PARAMETER_TYPE_MISMATCH;
@SuppressWarnings("unchecked")
DiagnosticWithParameters1<JetParameter, JetType> diagnosticWithParameters = (DiagnosticWithParameters1<JetParameter, JetType>) diagnostic;
JetTypeReference typeReference = diagnosticWithParameters.getPsiElement().getTypeReference();
assert typeReference != null : "EXPECTED_PARAMETER_TYPE_MISMATCH reported on parameter without explicitly declared type";
return new ChangeTypeFix(typeReference, diagnosticWithParameters.getA());
}
};
}
@NotNull
public static JetIntentionActionFactory createFactoryForExpectedReturnTypeMismatch() {
return new JetIntentionActionFactory() {
@Nullable
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
assert diagnostic.getFactory() == Errors.EXPECTED_RETURN_TYPE_MISMATCH;
@SuppressWarnings("unchecked")
DiagnosticWithParameters1<JetTypeReference, JetType> diagnosticWithParameters = (DiagnosticWithParameters1<JetTypeReference, JetType>) diagnostic;
return new ChangeTypeFix(diagnosticWithParameters.getPsiElement(), diagnosticWithParameters.getA());
}
};
}
}
@@ -218,6 +218,9 @@ public class QuickFixes {
factories.put(HAS_NEXT_FUNCTION_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForHasNextFunctionTypeMismatch());
factories.put(COMPARE_TO_TYPE_MISMATCH, ChangeFunctionReturnTypeFix.createFactoryForCompareToTypeMismatch());
factories.put(EXPECTED_PARAMETER_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedParameterTypeMismatch());
factories.put(EXPECTED_RETURN_TYPE_MISMATCH, ChangeTypeFix.createFactoryForExpectedReturnTypeMismatch());
factories.put(PLATFORM_CLASS_MAPPED_TO_KOTLIN, MapPlatformClassToKotlinFix.createFactory());
factories.put(MANY_CLASSES_IN_SUPERTYPE_LIST, RemoveSupertypeFix.createFactory());
@@ -0,0 +1,6 @@
// "Change type from 'String' to 'Int'" "true"
fun foo(f: (Int) -> String) {
foo {
(x: Int) -> ""
}
}
@@ -0,0 +1,9 @@
// "Change type from 'A' to 'B'" "true"
trait A {}
trait B : A {}
fun foo(f: (B) -> B) {
foo {
(b: B): B -> b
}
}
@@ -0,0 +1,6 @@
// "Change type from 'String' to 'Int'" "true"
fun foo(f: (Int) -> String) {
foo {
(x: String<caret>) -> ""
}
}
@@ -0,0 +1,9 @@
// "Change type from 'A' to 'B'" "true"
trait A {}
trait B : A {}
fun foo(f: (B) -> B) {
foo {
(b: B): A<caret> -> b
}
}
@@ -1028,6 +1028,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest("idea/testData/quickfix/typeMismatch/beforeComponentFunctionReturnTypeMismatch5.kt");
}
@TestMetadata("beforeExpectedParameterTypeMismatch.kt")
public void testExpectedParameterTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeExpectedParameterTypeMismatch.kt");
}
@TestMetadata("beforeExpectedReturnTypeMismatch.kt")
public void testExpectedReturnTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeExpectedReturnTypeMismatch.kt");
}
@TestMetadata("beforeHasNextFunctionReturnTypeMismatch.kt")
public void testHasNextFunctionReturnTypeMismatch() throws Exception {
doTest("idea/testData/quickfix/typeMismatch/beforeHasNextFunctionReturnTypeMismatch.kt");