Remove Nullable Quick-Fix: Convert to Kotlin & refactor

This commit is contained in:
Alexey Sedunov
2016-01-06 12:39:47 +03:00
parent f1c38f5f16
commit 012a36d97c
3 changed files with 30 additions and 60 deletions
@@ -12,13 +12,10 @@ remove.parts.from.property.family=Remove parts from property
remove.psi.element.family=Remove element remove.psi.element.family=Remove element
remove.right.part.of.binary.expression=Remove right part of a binary expression remove.right.part.of.binary.expression=Remove right part of a binary expression
remove.elvis.operator=Remove elvis operator remove.elvis.operator=Remove elvis operator
remove.redundant.nullable=Remove redundant '?'
remove.supertype=Remove supertype ''{0}'' remove.supertype=Remove supertype ''{0}''
remove.supertype.family=Remove supertype remove.supertype.family=Remove supertype
remove.supertype.nullable=Remove '?'
remove.type.arguments=Remove type arguments remove.type.arguments=Remove type arguments
remove.useless.nullable=Remove useless '?' remove.useless.nullable=Remove useless '?'
remove.nullable.family=Remove '?'
remove.spread.sign=Remove '*' remove.spread.sign=Remove '*'
remove.conflicting.import=Remove conflicting import for ''{0}'' remove.conflicting.import=Remove conflicting import for ''{0}''
remove.conflicting.import.family=Remove Conflicting Import remove.conflicting.import.family=Remove Conflicting Import
@@ -150,9 +150,9 @@ class QuickFixRegistrar : QuickFixContributor {
INVISIBLE_MEMBER.registerFactory(ChangePrivateTopLevelToInternalFix) INVISIBLE_MEMBER.registerFactory(ChangePrivateTopLevelToInternalFix)
INVISIBLE_SETTER.registerFactory(ChangePrivateTopLevelToInternalFix) INVISIBLE_SETTER.registerFactory(ChangePrivateTopLevelToInternalFix)
REDUNDANT_NULLABLE.registerFactory(RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.REDUNDANT)) REDUNDANT_NULLABLE.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.REDUNDANT))
NULLABLE_SUPERTYPE.registerFactory(RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.SUPERTYPE)) NULLABLE_SUPERTYPE.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.SUPERTYPE))
USELESS_NULLABLE_CHECK.registerFactory(RemoveNullableFix.createFactory(RemoveNullableFix.NullableKind.USELESS)) USELESS_NULLABLE_CHECK.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.USELESS))
val implementMembersHandler = ImplementMembersHandler() val implementMembersHandler = ImplementMembersHandler()
@@ -14,64 +14,37 @@
* limitations under the License. * 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.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.KtFile
import org.jetbrains.kotlin.diagnostics.Diagnostic; import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.idea.KotlinBundle; import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
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;
public class RemoveNullableFix extends KotlinQuickFixAction<KtNullableType> { class RemoveNullableFix(element: KtNullableType,
public enum NullableKind { private val typeOfError: RemoveNullableFix.NullableKind) : KotlinQuickFixAction<KtNullableType>(element) {
REDUNDANT, SUPERTYPE, USELESS enum class NullableKind(val message: String) {
} REDUNDANT("Remove redundant '?'"),
private final NullableKind typeOfError; SUPERTYPE("Remove '?'"),
USELESS("Remove useless '?'")
public RemoveNullableFix(@NotNull KtNullableType element, @NotNull NullableKind type) {
super(element);
typeOfError = type;
} }
@NotNull override fun getFamilyName() = "Remove '?'"
@Override
public String getText() { override fun getText() = typeOfError.message
switch (typeOfError) {
case REDUNDANT: override fun invoke(project: Project, editor: Editor?, file: KtFile) {
return KotlinBundle.message("remove.redundant.nullable"); val type = element.innerType ?: error("No inner type " + element.text + ", should have been rejected in createFactory()")
case SUPERTYPE: element.replace(type)
return KotlinBundle.message("remove.supertype.nullable"); }
default:
return KotlinBundle.message("remove.useless.nullable"); class Factory(private val typeOfError: NullableKind) : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? {
val nullType = diagnostic.psiElement.getNonStrictParentOfType<KtNullableType>()
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<KtNullableType> createAction(@NotNull Diagnostic diagnostic) {
KtNullableType nullType = QuickFixUtil.getParentElementOfType(diagnostic, KtNullableType.class);
if (nullType == null || nullType.getInnerType() == null) return null;
return new RemoveNullableFix(nullType, typeOfError);
}
};
}
} }