mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
added Add Exception base class quick fix to the Exception doesn't inherit from standard ''Exception'' class inspection
This commit is contained in:
@@ -129,6 +129,8 @@ QFIX.NAME.remove.dict.key=Remove this key
|
||||
|
||||
QFIX.NAME.add.specifier=Add format specifier character
|
||||
|
||||
QFIX.NAME.add.exception.base=Add Exception base class
|
||||
|
||||
# Intentions: INTN
|
||||
INTN.Family.convert.import.unqualify=Convert 'import module' to 'from module import'
|
||||
INTN.Family.convert.import.qualify=Convert 'from module import' to 'import module'
|
||||
|
||||
@@ -19,7 +19,9 @@ import com.intellij.codeInspection.LocalInspectionToolSession;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.psi.PsiPolyVariantReference;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.inspections.quickfix.PyAddExceptionSuperClassQuickFix;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.psi.types.PyClassLikeType;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
@@ -60,7 +62,9 @@ public class PyExceptionInheritInspection extends PyInspection {
|
||||
if (expression instanceof PyCallExpression) {
|
||||
PyExpression callee = ((PyCallExpression)expression).getCallee();
|
||||
if (callee instanceof PyReferenceExpression) {
|
||||
PsiElement psiElement = ((PyReferenceExpression)callee).getReference(getResolveContext()).resolve();
|
||||
final PsiPolyVariantReference reference = ((PyReferenceExpression)callee).getReference(getResolveContext());
|
||||
if (reference == null) return;
|
||||
PsiElement psiElement = reference.resolve();
|
||||
if (psiElement instanceof PyClass) {
|
||||
PyClass aClass = (PyClass) psiElement;
|
||||
for (PyClassLikeType type : aClass.getAncestorTypes(myTypeEvalContext)) {
|
||||
@@ -72,7 +76,7 @@ public class PyExceptionInheritInspection extends PyInspection {
|
||||
return;
|
||||
}
|
||||
}
|
||||
registerProblem(expression, "Exception doesn't inherit from base \'Exception\' class");
|
||||
registerProblem(expression, "Exception doesn't inherit from base \'Exception\' class", new PyAddExceptionSuperClassQuickFix());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.jetbrains.python.inspections.quickfix;
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiPolyVariantReference;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PyAddExceptionSuperClassQuickFix implements LocalQuickFix {
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return PyBundle.message("QFIX.NAME.add.exception.base");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (element instanceof PyCallExpression) {
|
||||
PyExpression callee = ((PyCallExpression)element).getCallee();
|
||||
if (callee instanceof PyReferenceExpression) {
|
||||
final PsiPolyVariantReference reference = ((PyReferenceExpression)callee).getReference();
|
||||
PsiElement psiElement = reference.resolve();
|
||||
if (psiElement instanceof PyClass) {
|
||||
final PyElementGenerator generator = PyElementGenerator.getInstance(project);
|
||||
final PyArgumentList list = ((PyClass)psiElement).getSuperClassExpressionList();
|
||||
if (list != null) {
|
||||
final PyExpression exception =
|
||||
generator.createExpressionFromText(LanguageLevel.forElement(element), "Exception");
|
||||
list.addArgument(exception);
|
||||
}
|
||||
else {
|
||||
final PyArgumentList expressionList = generator.createFromText(
|
||||
LanguageLevel.forElement(element), PyClass.class, "class A(Exception): pass").getSuperClassExpressionList();
|
||||
assert expressionList != null;
|
||||
final ASTNode nameNode = ((PyClass)psiElement).getNameNode();
|
||||
assert nameNode != null;
|
||||
psiElement.addAfter(expressionList, nameNode.getPsi());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
class MyException:
|
||||
def __new__(cls, x):
|
||||
pass
|
||||
|
||||
|
||||
def foo():
|
||||
raise <warning descr="Exception doesn't inherit from base 'Exception' class">My<caret>Exception()</warning>
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
|
||||
class MyException(Exception):
|
||||
def __new__(cls, x):
|
||||
pass
|
||||
|
||||
|
||||
def foo():
|
||||
raise MyException()
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
class A:
|
||||
pass
|
||||
|
||||
class MyException(A):
|
||||
def __new__(cls, x):
|
||||
pass
|
||||
|
||||
|
||||
def foo():
|
||||
raise <warning descr="Exception doesn't inherit from base 'Exception' class">MyExcep<caret>tion()</warning>
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
|
||||
class A:
|
||||
pass
|
||||
|
||||
class MyException(A, Exception):
|
||||
def __new__(cls, x):
|
||||
pass
|
||||
|
||||
|
||||
def foo():
|
||||
raise MyException()
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.jetbrains.python.quickFixes;
|
||||
|
||||
import com.intellij.testFramework.TestDataPath;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PyQuickFixTestCase;
|
||||
import com.jetbrains.python.inspections.PyExceptionInheritInspection;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
@TestDataPath("$CONTENT_ROOT/../testData//quickFixes/PyAddExceptionSuperClassQuickFixTest/")
|
||||
public class PyAddExceptionSuperClassQuickFixTest extends PyQuickFixTestCase {
|
||||
|
||||
public void testEmptySuperList() {
|
||||
doQuickFixTest(PyExceptionInheritInspection.class, PyBundle.message("QFIX.NAME.add.exception.base"));
|
||||
}
|
||||
|
||||
public void testNonEmptySuperList() {
|
||||
doQuickFixTest(PyExceptionInheritInspection.class, PyBundle.message("QFIX.NAME.add.exception.base"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user