diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties index 37517a060af..55e3c0a03cd 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties @@ -12,13 +12,10 @@ remove.parts.from.property.family=Remove parts from property remove.psi.element.family=Remove element remove.right.part.of.binary.expression=Remove right part of a binary expression remove.elvis.operator=Remove elvis operator -remove.redundant.nullable=Remove redundant '?' remove.supertype=Remove supertype ''{0}'' remove.supertype.family=Remove supertype -remove.supertype.nullable=Remove '?' remove.type.arguments=Remove type arguments remove.useless.nullable=Remove useless '?' -remove.nullable.family=Remove '?' remove.spread.sign=Remove '*' remove.conflicting.import=Remove conflicting import for ''{0}'' remove.conflicting.import.family=Remove Conflicting Import diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index eae1737527b..ad2e09b7a8a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -150,9 +150,9 @@ class QuickFixRegistrar : QuickFixContributor { INVISIBLE_MEMBER.registerFactory(ChangePrivateTopLevelToInternalFix) INVISIBLE_SETTER.registerFactory(ChangePrivateTopLevelToInternalFix) - REDUNDANT_NULLABLE.registerFactory(RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.REDUNDANT)) - NULLABLE_SUPERTYPE.registerFactory(RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.SUPERTYPE)) - USELESS_NULLABLE_CHECK.registerFactory(RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.USELESS)) + REDUNDANT_NULLABLE.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.REDUNDANT)) + NULLABLE_SUPERTYPE.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.SUPERTYPE)) + USELESS_NULLABLE_CHECK.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.USELESS)) val implementMembersHandler = ImplementMembersHandler() diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt index 288832793af..7dabc48141b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt @@ -14,64 +14,37 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.quickfix; +package org.jetbrains.kotlin.idea.quickfix -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.KtFile; -import org.jetbrains.kotlin.psi.KtNullableType; -import org.jetbrains.kotlin.psi.KtTypeElement; +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtNullableType +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType -public class RemoveNullableFix extends KotlinQuickFixAction { - public enum NullableKind { - REDUNDANT, SUPERTYPE, USELESS - } - private final NullableKind typeOfError; - - public RemoveNullableFix(@NotNull KtNullableType element, @NotNull NullableKind type) { - super(element); - typeOfError = type; +class RemoveNullableFix(element: KtNullableType, + private val typeOfError: RemoveNullableFix.NullableKind) : KotlinQuickFixAction(element) { + enum class NullableKind(val message: String) { + REDUNDANT("Remove redundant '?'"), + SUPERTYPE("Remove '?'"), + USELESS("Remove useless '?'") } - @NotNull - @Override - public String getText() { - switch (typeOfError) { - case REDUNDANT: - return KotlinBundle.message("remove.redundant.nullable"); - case SUPERTYPE: - return KotlinBundle.message("remove.supertype.nullable"); - default: - return KotlinBundle.message("remove.useless.nullable"); + override fun getFamilyName() = "Remove '?'" + + override fun getText() = typeOfError.message + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val type = element.innerType ?: error("No inner type " + element.text + ", should have been rejected in createFactory()") + element.replace(type) + } + + class Factory(private val typeOfError: NullableKind) : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { + val nullType = diagnostic.psiElement.getNonStrictParentOfType() + if (nullType == null || nullType.innerType == null) return null + return RemoveNullableFix(nullType, typeOfError) } } - - @NotNull - @Override - public String getFamilyName() { - return KotlinBundle.message("remove.nullable.family"); - } - - @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException { - KtTypeElement type = super.getElement().getInnerType(); - assert type != null : "No inner type " + getElement().getText() + ", should have been rejected in createFactory()"; - super.getElement().replace(type); - } - - public static KotlinSingleIntentionActionFactory createFactory(final NullableKind typeOfError) { - return new KotlinSingleIntentionActionFactory() { - @Override - public KotlinQuickFixAction createAction(@NotNull Diagnostic diagnostic) { - KtNullableType nullType = QuickFixUtil.getParentElementOfType(diagnostic, KtNullableType.class); - if (nullType == null || nullType.getInnerType() == null) return null; - return new RemoveNullableFix(nullType, typeOfError); - } - }; - } }