Change to Star-Projection Quick-Fix: Convert to Kotlin & refactor

This commit is contained in:
Alexey Sedunov
2016-01-06 00:51:00 +03:00
parent 625967fc15
commit a7a5619523
3 changed files with 23 additions and 59 deletions
@@ -9,8 +9,6 @@ add.init.keyword.in.whole.project.family=Add 'init' keyword in whole project
insert.delegation.call=Insert ''{0}()'' call
make.class.annotation.class=Make ''{0}'' an annotation class
make.class.annotation.class.family=Make class an annotation class
change.to.star.projection=Change type arguments to {0}
change.to.star.projection.family=Change to star projection
remove.parts.from.property=Remove {0} from property
remove.parts.from.property.family=Remove parts from property
remove.psi.element.family=Remove element
@@ -14,64 +14,31 @@
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix;
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.diagnostics.Diagnostic;
import org.jetbrains.kotlin.idea.KotlinBundle;
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.types.expressions.TypeReconstructionUtil;
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
public class ChangeToStarProjectionFix extends KotlinQuickFixAction<KtTypeElement> {
public ChangeToStarProjectionFix(@NotNull KtTypeElement element) {
super(element);
class ChangeToStarProjectionFix(element: KtTypeElement) : KotlinQuickFixAction<KtTypeElement>(element) {
override fun getFamilyName() = "Change to star projection"
override fun getText() = "Change type arguments to <${element.typeArgumentsAsTypes.joinToString { "*" }}>"
public override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val star = KtPsiFactory(file).createStar()
element.typeArgumentsAsTypes.forEach { it?.replace(star) }
}
@NotNull
@Override
public String getText() {
String stars = TypeReconstructionUtil.getTypeNameAndStarProjectionsString("", getElement().getTypeArgumentsAsTypes().size());
return KotlinBundle.message("change.to.star.projection", stars);
}
@NotNull
@Override
public String getFamilyName() {
return KotlinBundle.message("change.to.star.projection.family");
}
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException {
for (KtTypeReference typeReference : getElement().getTypeArgumentsAsTypes()) {
if (typeReference != null) {
typeReference.replace(KtPsiFactoryKt.KtPsiFactory(file).createStar());
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val typeReference = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>()?.right
?: diagnostic.psiElement.getNonStrictParentOfType<KtTypeReference>()
val typeElement = typeReference?.typeElement ?: return null
return ChangeToStarProjectionFix(typeElement)
}
}
public static KotlinSingleIntentionActionFactory createFactory() {
return new KotlinSingleIntentionActionFactory() {
@Override
public IntentionAction createAction(@NotNull Diagnostic diagnostic) {
KtBinaryExpressionWithTypeRHS expression = QuickFixUtil
.getParentElementOfType(diagnostic, KtBinaryExpressionWithTypeRHS.class);
KtTypeReference typeReference;
if (expression == null) {
typeReference = QuickFixUtil.getParentElementOfType(diagnostic, KtTypeReference.class);
}
else {
typeReference = expression.getRight();
}
if (typeReference == null) return null;
KtTypeElement typeElement = typeReference.getTypeElement();
assert typeElement != null;
return new ChangeToStarProjectionFix(typeElement);
}
};
}
}
@@ -195,9 +195,8 @@ class QuickFixRegistrar : QuickFixContributor {
TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER.registerFactory(RemovePsiElementSimpleFix.createRemoveTypeArgumentsFactory())
val changeToStarProjectionFactory = ChangeToStarProjectionFix.createFactory()
UNCHECKED_CAST.registerFactory(changeToStarProjectionFactory)
CANNOT_CHECK_FOR_ERASED.registerFactory(changeToStarProjectionFactory)
UNCHECKED_CAST.registerFactory(ChangeToStarProjectionFix)
CANNOT_CHECK_FOR_ERASED.registerFactory(ChangeToStarProjectionFix)
INACCESSIBLE_OUTER_CLASS_EXPRESSION.registerFactory(AddModifierFix.createFactory(INNER_KEYWORD, KtClass::class.java))