Change to Star-Projection Quick-Fix: Convert to Kotlin & refactor
This commit is contained in:
@@ -9,8 +9,6 @@ add.init.keyword.in.whole.project.family=Add 'init' keyword in whole project
|
|||||||
insert.delegation.call=Insert ''{0}()'' call
|
insert.delegation.call=Insert ''{0}()'' call
|
||||||
make.class.annotation.class=Make ''{0}'' an annotation class
|
make.class.annotation.class=Make ''{0}'' an annotation class
|
||||||
make.class.annotation.class.family=Make class 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=Remove {0} from property
|
||||||
remove.parts.from.property.family=Remove parts from property
|
remove.parts.from.property.family=Remove parts from property
|
||||||
remove.psi.element.family=Remove element
|
remove.psi.element.family=Remove element
|
||||||
|
|||||||
@@ -14,64 +14,31 @@
|
|||||||
* limitations under the License.
|
* 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.codeInsight.intention.IntentionAction
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
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;
|
|
||||||
|
|
||||||
public class ChangeToStarProjectionFix extends KotlinQuickFixAction<KtTypeElement> {
|
class ChangeToStarProjectionFix(element: KtTypeElement) : KotlinQuickFixAction<KtTypeElement>(element) {
|
||||||
public ChangeToStarProjectionFix(@NotNull KtTypeElement element) {
|
override fun getFamilyName() = "Change to star projection"
|
||||||
super(element);
|
|
||||||
|
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
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
@Override
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
public String getText() {
|
val typeReference = diagnostic.psiElement.getNonStrictParentOfType<KtBinaryExpressionWithTypeRHS>()?.right
|
||||||
String stars = TypeReconstructionUtil.getTypeNameAndStarProjectionsString("", getElement().getTypeArgumentsAsTypes().size());
|
?: diagnostic.psiElement.getNonStrictParentOfType<KtTypeReference>()
|
||||||
return KotlinBundle.message("change.to.star.projection", stars);
|
val typeElement = typeReference?.typeElement ?: return null
|
||||||
}
|
return ChangeToStarProjectionFix(typeElement)
|
||||||
|
|
||||||
@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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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())
|
TYPE_ARGUMENTS_REDUNDANT_IN_SUPER_QUALIFIER.registerFactory(RemovePsiElementSimpleFix.createRemoveTypeArgumentsFactory())
|
||||||
|
|
||||||
val changeToStarProjectionFactory = ChangeToStarProjectionFix.createFactory()
|
UNCHECKED_CAST.registerFactory(ChangeToStarProjectionFix)
|
||||||
UNCHECKED_CAST.registerFactory(changeToStarProjectionFactory)
|
CANNOT_CHECK_FOR_ERASED.registerFactory(ChangeToStarProjectionFix)
|
||||||
CANNOT_CHECK_FOR_ERASED.registerFactory(changeToStarProjectionFactory)
|
|
||||||
|
|
||||||
INACCESSIBLE_OUTER_CLASS_EXPRESSION.registerFactory(AddModifierFix.createFactory(INNER_KEYWORD, KtClass::class.java))
|
INACCESSIBLE_OUTER_CLASS_EXPRESSION.registerFactory(AddModifierFix.createFactory(INNER_KEYWORD, KtClass::class.java))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user