common "invert boolean" quickfix

This commit is contained in:
Bas Leijdekkers
2013-11-07 20:45:45 +01:00
parent 9a5bfa9e7e
commit 9005acd75a
5 changed files with 96 additions and 96 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2013 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.
@@ -18,7 +18,6 @@ package com.intellij.refactoring.invertBoolean;
import com.intellij.ide.util.SuperMethodWarningUtil;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.project.Project;
@@ -70,8 +69,13 @@ public class InvertBooleanHandler implements RefactoringActionHandler {
}
public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, @NotNull DataContext dataContext) {
if (elements.length == 1 && elements[0] instanceof PsiMethod) {
invoke((PsiMethod)elements[0], project, null);
if (elements.length == 1) {
if (elements[0] instanceof PsiMethod) {
invoke((PsiMethod)elements[0], project, null);
}
else if (elements[0] instanceof PsiVariable) {
invoke((PsiVariable)elements[0], project, null);
}
}
}
@@ -2059,3 +2059,6 @@ problematic.whitespace.spaces.problem.descriptor=File ''{0}'' uses spaces for in
problematic.whitespace.show.whitespaces.quickfix=Toggle show whitespace in the editor
problematic.varargs.method.display.name=Non-varargs method overrides varargs method
problematic.varargs.method.override.problem.descriptor=Non-varargs method <code>#ref()</code> overrides varargs method #loc
invert.quickfix.family.name=Invert boolean
invert.method.quickfix=Invert method
invert.quickfix=Invert ''{0}''
@@ -15,60 +15,18 @@
*/
package com.intellij.codeInspection.booleanIsAlwaysInverted;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.QuickFix;
import com.intellij.ide.DataManager;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
import com.intellij.refactoring.RefactoringActionHandler;
import org.jetbrains.annotations.NotNull;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.fixes.InvertBooleanFix;
/**
* User: anna
* Date: 06-Jan-2006
*/
public class BooleanMethodIsAlwaysInvertedInspection extends BooleanMethodIsAlwaysInvertedInspectionBase {
@Override
public QuickFix getQuickFix(final String hint) {
return new InvertMethodFix();
return new InvertBooleanFix(InspectionGadgetsBundle.message("invert.method.quickfix"));
}
private static class InvertMethodFix implements LocalQuickFix {
@Override
@NotNull
public String getName() {
return "Invert method";
}
@Override
@NotNull
public String getFamilyName() {
return getName();
}
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiMethod psiMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
assert psiMethod != null;
final RefactoringActionHandler invertBooleanHandler = JavaRefactoringActionHandlerFactory.getInstance().createInvertBooleanHandler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
invertBooleanHandler.invoke(project, new PsiElement[]{psiMethod}, DataManager.getInstance().getDataContext());
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
runnable.run();
}
else {
ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed());
}
}
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Bas Leijdekkers
* Copyright 2011-2013 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,57 +15,16 @@
*/
package com.siyeh.ig.dataflow;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.invertBoolean.InvertBooleanDialog;
import com.intellij.util.IncorrectOperationException;
import com.intellij.psi.PsiVariable;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.NotNull;
import com.siyeh.ig.fixes.InvertBooleanFix;
public class BooleanVariableAlwaysNegatedInspection extends BooleanVariableAlwaysNegatedInspectionBase {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
final PsiVariable variable = (PsiVariable)infos[0];
return new BooleanVariableIsAlwaysNegatedFix(variable.getName());
}
private static class BooleanVariableIsAlwaysNegatedFix
extends InspectionGadgetsFix {
private final String name;
public BooleanVariableIsAlwaysNegatedFix(String name) {
this.name = name;
}
@NotNull
@Override
public String getName() {
return InspectionGadgetsBundle.message(
"boolean.variable.always.inverted.quickfix", name);
}
@NotNull
@Override
public String getFamilyName() {
return "Invert";
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final PsiVariable variable =
PsiTreeUtil.getParentOfType(element, PsiVariable.class);
if (variable == null) {
return;
}
final InvertBooleanDialog dialog = new InvertBooleanDialog(variable);
dialog.show();
}
return new InvertBooleanFix(InspectionGadgetsBundle.message("invert.quickfix", variable.getName()));
}
}
@@ -0,0 +1,76 @@
/*
* Copyright 2000-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.fixes;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.JavaRefactoringActionHandlerFactory;
import com.intellij.refactoring.RefactoringActionHandler;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class InvertBooleanFix extends InspectionGadgetsFix {
private final String myName;
public InvertBooleanFix(String name) {
myName = name;
}
@NotNull
@Override
public String getName() {
return myName;
}
@NotNull
@Override
public String getFamilyName() {
return InspectionGadgetsBundle.message("invert.quickfix.family.name");
}
@Override
protected void doFix(final Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiNameIdentifierOwner owner = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiVariable.class, PsiMethod.class);
if (owner == null) {
return;
}
final RefactoringActionHandler handler = JavaRefactoringActionHandlerFactory.getInstance().createInvertBooleanHandler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
handler.invoke(project, new PsiElement[]{owner}, null);
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
runnable.run();
}
else {
ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed());
}
}
}