[java] more generic 'delete element' intention/quick fix

This commit is contained in:
Roman Shevchenko
2017-02-17 13:22:37 +01:00
parent fa849e13f2
commit b66aab523b
9 changed files with 45 additions and 68 deletions
@@ -331,7 +331,7 @@ public class ModuleHighlightUtil {
if (!filter.add(refText)) {
String message = JavaErrorMessages.message("module.duplicate.impl", refText);
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(implRef).description(message).create();
QuickFixAction.registerQuickFixAction(info, new DeleteElementFix(implRef));
QuickFixAction.registerQuickFixAction(info, new DeleteElementFix(implRef, QuickFixBundle.message("delete.reference.fix.text")));
results.add(info);
continue;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,54 +15,59 @@
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.SmartPointerManager;
import com.intellij.psi.SmartPsiElementPointer;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class DeleteElementFix implements IntentionAction {
private final SmartPsiElementPointer<PsiElement> myPointer;
public class DeleteElementFix extends LocalQuickFixAndIntentionActionOnPsiElement {
private final String myText;
public DeleteElementFix(@NotNull PsiElement element) {
myPointer = SmartPointerManager.getInstance(element.getProject()).createSmartPsiElementPointer(element);
super(element);
myText = null;
}
public DeleteElementFix(@NotNull PsiElement element, @NotNull @Nls String text) {
super(element);
myText = text;
}
@Nls
@NotNull
@Override
public String getText() {
return QuickFixBundle.message("delete.element.fix.text");
return ObjectUtils.notNull(myText, getFamilyName());
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return getText();
return QuickFixBundle.message("delete.element.fix.text");
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return myPointer.getElement() != null;
}
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
PsiElement element = myPointer.getElement();
if (element != null) {
element.delete();
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
if (FileModificationService.getInstance().preparePsiElementForWrite(file)) {
WriteAction.run(() -> startElement.delete());
}
}
@Override
public boolean startInWriteAction() {
return true;
return false;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package com.intellij.codeInspection.compiler;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil;
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix;
import com.intellij.codeInspection.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
@@ -56,7 +57,7 @@ public class JavacQuirksInspectionVisitor extends JavaElementVisitor {
if (lastElement != null && PsiUtil.isJavaToken(lastElement, JavaTokenType.COMMA)) {
final String message = InspectionsBundle.message("inspection.compiler.javac.quirks.anno.array.comma.problem");
final String fixName = InspectionsBundle.message("inspection.compiler.javac.quirks.anno.array.comma.fix");
myHolder.registerProblem(lastElement, message, new RemoveElementQuickFix(fixName));
myHolder.registerProblem(lastElement, message, new DeleteElementFix(lastElement, fixName));
}
}
@@ -72,7 +73,7 @@ public class JavacQuirksInspectionVisitor extends JavaElementVisitor {
if (list.getFirstChild() != null && QUALIFIER_REFERENCE.accepts(list)) {
final String message = InspectionsBundle.message("inspection.compiler.javac.quirks.qualifier.type.args.problem");
final String fixName = InspectionsBundle.message("inspection.compiler.javac.quirks.qualifier.type.args.fix");
myHolder.registerProblem(list, message, new RemoveElementQuickFix(fixName));
myHolder.registerProblem(list, message, new DeleteElementFix(list, fixName));
}
}
});
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/** @deprecated use {@link com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix} (to be removed in IDEA 2018) */
public class RemoveElementQuickFix implements LocalQuickFix {
private final String myName;
@@ -42,4 +43,4 @@ public class RemoveElementQuickFix implements LocalQuickFix {
element.delete();
}
}
}
}
@@ -15,14 +15,14 @@
*/
package com.intellij.codeInspection.java19modules;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInspection.*;
import com.intellij.openapi.project.Project;
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix;
import com.intellij.codeInspection.BaseJavaLocalInspectionTool;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -31,7 +31,6 @@ import java.util.List;
* @author Pavel.Dolgov
*/
public class Java9ModuleExportsPackageToItselfInspection extends BaseJavaLocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
@@ -60,40 +59,11 @@ public class Java9ModuleExportsPackageToItselfInspection extends BaseJavaLocalIn
for (PsiJavaModuleReferenceElement referenceElement : referenceElements) {
if (moduleName.equals(referenceElement.getReferenceText())) {
String message = InspectionsBundle.message("inspection.module.exports.package.to.itself.message");
myHolder.registerProblem(referenceElement, message,
new DeleteExportsToModuleFix(referenceElement));
String fixText = InspectionsBundle.message("exports.to.itself.delete.module.fix.name", moduleName);
myHolder.registerProblem(referenceElement, message, new DeleteElementFix(referenceElement, fixText));
}
}
}
}
}
private static class DeleteExportsToModuleFix implements LocalQuickFix {
private final String myModuleName;
public DeleteExportsToModuleFix(PsiJavaModuleReferenceElement reference) {
myModuleName = reference.getReferenceText();
}
@Nls
@NotNull
@Override
public String getName() {
return InspectionsBundle.message("exports.to.itself.delete.module.fix.name", myModuleName);
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return InspectionsBundle.message("exports.to.itself.delete.module.fix.family.name");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement psiElement = descriptor.getPsiElement();
if (!FileModificationService.getInstance().preparePsiElementForWrite(psiElement)) return;
psiElement.delete();
}
}
}
}