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.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
@@ -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()
@@ -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<KtNullableType> {
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<KtNullableType>(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<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);
}
};
}
}