mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-15 20:26:04 +07:00
added Method may be static or a function (+ quickfix)
This commit is contained in:
@@ -321,6 +321,7 @@
|
||||
<localInspection language="Python" shortName="PyNoneFunctionAssignmentInspection" displayName="Assigning function call that doesn't return anything" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyNoneFunctionAssignmentInspection"/>
|
||||
<localInspection language="Python" shortName="PyGlobalUndefinedInspection" displayName="Global variable is undefined at the module level" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyGlobalUndefinedInspection"/>
|
||||
<localInspection language="Python" shortName="PyProtectedMemberInspection" displayName="Access to a protected member of a class" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyProtectedMemberInspection"/>
|
||||
<localInspection language="Python" shortName="PyMethodMayBeStaticInspection" displayName="Method may be static" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyMethodMayBeStaticInspection"/>
|
||||
<localInspection language="Python" shortName="PyDocstringTypesInspection" bundle="com.jetbrains.python.PyBundle" key="INSP.NAME.docstring.types" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WEAK WARNING" implementationClass="com.jetbrains.python.inspections.PyDocstringTypesInspection"/>
|
||||
<localInspection language="Python" shortName="PyShadowingBuiltinsInspection" displayName="Shadowing built-ins" groupKey="INSP.GROUP.python" enabledByDefault="true" level="WARNING" implementationClass="com.jetbrains.python.inspections.PyShadowingBuiltinsInspection"/>
|
||||
|
||||
|
||||
@@ -105,6 +105,12 @@ QFIX.rename.unresolved.reference=Rename reference
|
||||
#PyMoveAttributeToInitQuickFix
|
||||
QFIX.move.attribute=Move attribute to __init__ method
|
||||
|
||||
#PyMakeMethodStaticQuickFix
|
||||
QFIX.NAME.make.static=Make method static
|
||||
|
||||
#PyMakeFunctionFromMethodQuickFix
|
||||
QFIX.NAME.make.function=Make function from method
|
||||
|
||||
# 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'
|
||||
@@ -472,6 +478,10 @@ INSP.NAME.decorator.outside.class=Class specific decorator on method outside cla
|
||||
# PyPackageRequirementsInspection
|
||||
INSP.NAME.requirements=Package requirements
|
||||
|
||||
# PyMethodMayBeStaticInspection
|
||||
INSP.NAME.method.may.be.static=Method may be static
|
||||
INSP.method.may.be.static=Method <code>#ref</code> may be 'static'
|
||||
|
||||
# PyClassHasNoInitInspection
|
||||
INSP.NAME.class.has.no.init=Class has no __init__ method
|
||||
INSP.class.has.no.init=Class has no __init__ method
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.jetbrains.python.inspections;
|
||||
|
||||
import com.intellij.codeInspection.LocalInspectionToolSession;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PyNames;
|
||||
import com.jetbrains.python.inspections.quickfix.PyMakeFunctionFromMethodQuickFix;
|
||||
import com.jetbrains.python.inspections.quickfix.PyMakeMethodStaticQuickFix;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*
|
||||
*/
|
||||
public class PyMethodMayBeStaticInspection extends PyInspection {
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return PyBundle.message("INSP.NAME.method.may.be.static");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
|
||||
boolean isOnTheFly,
|
||||
@NotNull LocalInspectionToolSession session) {
|
||||
return new Visitor(holder, session);
|
||||
}
|
||||
|
||||
|
||||
private static class Visitor extends PyInspectionVisitor {
|
||||
public Visitor(@Nullable ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
|
||||
super(holder, session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPyFunction(PyFunction node) {
|
||||
if (PyNames.getBuiltinMethods(LanguageLevel.forElement(node)).containsKey(node.getName())) return;
|
||||
final PyClass containingClass = node.getContainingClass();
|
||||
if (containingClass == null) return;
|
||||
|
||||
final PyStatementList statementList = node.getStatementList();
|
||||
if (statementList == null) return;
|
||||
|
||||
final PyStatement[] statements = statementList.getStatements();
|
||||
|
||||
if (statements.length == 1 && statements[0] instanceof PyPassStatement) return;
|
||||
|
||||
final boolean[] mayBeStatic = {true};
|
||||
PyRecursiveElementVisitor visitor = new PyRecursiveElementVisitor() {
|
||||
@Override
|
||||
public void visitPyReferenceExpression(PyReferenceExpression node) {
|
||||
if (PyNames.CANONICAL_SELF.equals(node.getName())) {
|
||||
mayBeStatic[0] = false;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
node.accept(visitor);
|
||||
final PsiElement identifier = node.getNameIdentifier();
|
||||
if (mayBeStatic[0] && identifier != null) {
|
||||
registerProblem(identifier, PyBundle.message("INSP.method.may.be.static"), ProblemHighlightType.WEAK_WARNING,
|
||||
null, new PyMakeMethodStaticQuickFix(), new PyMakeFunctionFromMethodQuickFix());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.jetbrains.python.inspections.quickfix;
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
public class PyMakeFunctionFromMethodQuickFix implements LocalQuickFix {
|
||||
public PyMakeFunctionFromMethodQuickFix() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return PyBundle.message("QFIX.NAME.make.function");
|
||||
}
|
||||
|
||||
@NonNls
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final PyFunction problemFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
|
||||
if (problemFunction == null) return;
|
||||
final PyClass containingClass = problemFunction.getContainingClass();
|
||||
if (containingClass == null) return;
|
||||
|
||||
if (!PyUtil.deleteParameter(problemFunction, 0)) return;
|
||||
|
||||
final PsiElement copy = problemFunction.copy();
|
||||
final PyStatementList classStatementList = containingClass.getStatementList();
|
||||
classStatementList.deleteChildRange(problemFunction, problemFunction);
|
||||
final PsiFile file = containingClass.getContainingFile();
|
||||
file.addAfter(copy, containingClass);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.jetbrains.python.inspections.quickfix;
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
public class PyMakeMethodStaticQuickFix implements LocalQuickFix {
|
||||
public PyMakeMethodStaticQuickFix() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return PyBundle.message("QFIX.NAME.make.static");
|
||||
}
|
||||
|
||||
@NonNls
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final PyFunction problemFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
|
||||
if (problemFunction == null) return;
|
||||
if (!PyUtil.deleteParameter(problemFunction, 0)) return;
|
||||
|
||||
PyElementGenerator generator = PyElementGenerator.getInstance(project);
|
||||
final PyFunction function =
|
||||
generator.createFromText(LanguageLevel.forElement(problemFunction), PyFunction.class, "@staticmethod\ndef foo():\n\tpass");
|
||||
final PyDecoratorList decoratorList = function.getDecoratorList();
|
||||
assert decoratorList != null;
|
||||
final PyDecorator[] decorators = decoratorList.getDecorators();
|
||||
final PyDecorator decorator = decorators[0];
|
||||
problemFunction.addBefore(decorator, problemFunction.getFirstChild());
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -642,6 +642,19 @@ public class PyUtil {
|
||||
return AccessDirection.READ;
|
||||
}
|
||||
|
||||
public static boolean deleteParameter(@NotNull final PyFunction problemFunction, int index) {
|
||||
final PyParameterList parameterList = problemFunction.getParameterList();
|
||||
final PyParameter[] parameters = parameterList.getParameters();
|
||||
if (parameters.length <= 0) return false;
|
||||
|
||||
PsiElement first = parameters[index];
|
||||
PsiElement last = parameters.length > index + 1 ? parameters[index + 1] : parameterList.getLastChild();
|
||||
PsiElement prevSibling = last.getPrevSibling() != null ? last.getPrevSibling() : parameters[index];
|
||||
|
||||
parameterList.deleteChildRange(first, prevSibling);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static class KnownDecoratorProviderHolder {
|
||||
public static PyKnownDecoratorProvider[] KNOWN_DECORATOR_PROVIDERS = Extensions.getExtensions(PyKnownDecoratorProvider.EP_NAME);
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def f(self):
|
||||
pass
|
||||
@@ -0,0 +1,5 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
test = 1
|
||||
@@ -0,0 +1,6 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
|
||||
def f(self):
|
||||
self.test = 1
|
||||
@@ -0,0 +1,6 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
|
||||
def <weak_warning descr="Method 'f' may be 'static'">f</weak_warning>(self):
|
||||
test = 1
|
||||
@@ -0,0 +1,8 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
def <caret>f(self, ):
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
|
||||
def f():
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def <caret>f(self, x):
|
||||
test = 1
|
||||
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
|
||||
def f(x):
|
||||
test = 1
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
def <caret>f(self):
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
|
||||
def f():
|
||||
test = 1
|
||||
@@ -0,0 +1,8 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
def <caret>f(self, x):
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
|
||||
def f(x):
|
||||
test = 1
|
||||
@@ -0,0 +1,8 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
def <caret>f(self, ):
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
@staticmethod
|
||||
def f():
|
||||
test = 1
|
||||
@@ -0,0 +1,8 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
def <caret>f(self):
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
@staticmethod
|
||||
def f():
|
||||
test = 1
|
||||
@@ -0,0 +1,8 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
def <caret>f(self, x):
|
||||
test = 1
|
||||
@@ -0,0 +1,9 @@
|
||||
__author__ = 'ktisha'
|
||||
|
||||
class Child(Base):
|
||||
def __init__(self):
|
||||
super(Child, self).__init__()
|
||||
|
||||
@staticmethod
|
||||
def f(x):
|
||||
test = 1
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.jetbrains.python.inspections;
|
||||
|
||||
import com.jetbrains.python.fixtures.PyTestCase;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
public class PyMethodMayBeStaticInspectionTest extends PyTestCase {
|
||||
|
||||
public void testTruePositive() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testTrueNegative() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testEmpty() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInit() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile("inspections/PyMethodMayBeStaticInspection/" + getTestName(true) + ".py");
|
||||
myFixture.enableInspections(PyMethodMayBeStaticInspection.class);
|
||||
myFixture.checkHighlighting(false, false, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.jetbrains.python.quickFixes;
|
||||
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.inspections.PyMethodMayBeStaticInspection;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
public class PyMakeFunctionFromMethodQuickFixTest extends PyQuickFixTestCase {
|
||||
|
||||
public void testOneParam() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
|
||||
}
|
||||
|
||||
public void testTwoParams() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
|
||||
}
|
||||
|
||||
public void testEmptyParam() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
|
||||
}
|
||||
|
||||
public void testFirstMethod() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.jetbrains.python.quickFixes;
|
||||
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.inspections.PyMethodMayBeStaticInspection;
|
||||
|
||||
/**
|
||||
* User: ktisha
|
||||
*/
|
||||
public class PyMakeMethodStaticQuickFixTest extends PyQuickFixTestCase {
|
||||
|
||||
public void testOneParam() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.static"));
|
||||
}
|
||||
|
||||
public void testTwoParams() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.static"));
|
||||
}
|
||||
|
||||
public void testEmptyParam() {
|
||||
doInspectionTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.static"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user