A quick fix for 'Raw type in is-expression'

This commit is contained in:
Andrey Breslav
2012-11-27 14:56:08 +04:00
parent af682d61f0
commit 6f4afd675f
15 changed files with 141 additions and 0 deletions
@@ -11,6 +11,7 @@ change.variable.mutability.family=Change variable mutability
remove.function.body=Remove function body
make.element.modifier=Make {0} {1}
add.modifier=Add ''{0}'' modifier
add.star.projections=Add ''{0}''
make.element.not.modifier=Make {0} not {1}
remove.modifier=Remove ''{0}'' modifier
remove.modifier.family=Remove modifier
@@ -0,0 +1,86 @@
/*
* Copyright 2010-2012 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.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters2;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.psi.JetTypeElement;
import org.jetbrains.jet.lang.psi.JetUserType;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.plugin.JetBundle;
public class AddStarProjectionsFix extends JetIntentionAction<JetUserType> {
private final int argumentCount;
private AddStarProjectionsFix(@NotNull JetUserType element, int count) {
super(element);
argumentCount = count;
}
@NotNull
@Override
public String getText() {
return JetBundle.message("add.star.projections", TypeUtils.getTypeNameAndStarProjectionsString("", argumentCount));
}
@NotNull
@Override
public String getFamilyName() {
return "Add star projections";
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
assert element.getTypeArguments().isEmpty();
String typeString = TypeUtils.getTypeNameAndStarProjectionsString(element.getText(), argumentCount);
JetTypeElement replacement = JetPsiFactory.createType(project, typeString).getTypeElement();
assert replacement != null : "No type element after parsing " + typeString;
element.replace(replacement);
}
@Override
public boolean startInWriteAction() {
return true;
}
public static JetIntentionActionFactory createFactory() {
return new JetIntentionActionFactory() {
@Override
public IntentionAction createAction(Diagnostic diagnostic) {
assert diagnostic.getFactory() == Errors.NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION;
@SuppressWarnings("unchecked")
DiagnosticWithParameters2<JetUserType, Integer, String> diagnosticWithParameters =
(DiagnosticWithParameters2<JetUserType, Integer, String>) diagnostic;
JetUserType userType = QuickFixUtil.getParentElementOfType(diagnostic, JetUserType.class);
if (userType == null) return null;
Integer size = diagnosticWithParameters.getA();
return new AddStarProjectionsFix(userType, size);
}
};
}
}
@@ -144,5 +144,7 @@ public class QuickFixes {
actions.put(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, new SpecifyTypeExplicitlyFix());
factories.put(NO_ELSE_IN_WHEN, AddWhenElseBranchFix.createFactory());
factories.put(NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION, AddStarProjectionsFix.createFactory());
}
}
@@ -0,0 +1,4 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
a is java.util.ArrayList<*>
}
@@ -0,0 +1,4 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
a is jet.List<*>
}
@@ -0,0 +1,4 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
a is jet.Map<*, *>
}
@@ -0,0 +1,4 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
a is List<*>
}
@@ -0,0 +1,4 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
a is Map<*, *>
}
@@ -0,0 +1,6 @@
// "Add '<*, *>'" "false"
// "Add '<*>'" "false"
// ERROR: 2 type arguments expected
public fun foo(a: Any) {
a is Map<Int>
}
@@ -0,0 +1,4 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
a is java.util.Array<caret>List
}
@@ -0,0 +1,4 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
a is jet.List<caret>
}
@@ -0,0 +1,4 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
a is jet.Map<caret>
}
@@ -0,0 +1,4 @@
// "Add '<*>'" "true"
public fun foo(a: Any) {
a is List<caret>
}
@@ -0,0 +1,4 @@
// "Add '<*, *>'" "true"
public fun foo(a: Any) {
a is Map<caret>
}
@@ -0,0 +1,6 @@
// "Add '<*, *>'" "false"
// "Add '<*>'" "false"
// ERROR: 2 type arguments expected
public fun foo(a: Any) {
a is <caret>Map<Int>
}