added convert static method to function intention

This commit is contained in:
Ekaterina Tuzova
2013-04-24 18:12:02 +04:00
parent ca3c43ced5
commit 64668ea702
6 changed files with 110 additions and 0 deletions
@@ -142,6 +142,11 @@
<category>Python</category>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.PyConvertStaticMethodToFunctionIntention</className>
<category>Python</category>
</intentionAction>
<intentionAction>
<className>com.jetbrains.python.codeInsight.intentions.SpecifyTypeInDocstringIntention</className>
<category>Python</category>
@@ -214,6 +214,9 @@ INTN.insert.assertion=Insert type assertion
#PyYieldFromIntention
INTN.yield.from=Transform explicit iteration with 'yield' into 'yield from' expression
#PyConvertStaticMethodToFunctionIntention
INTN.convert.static.method.to.function=Convert static method to function
# Conflict checker
CONFLICT.name.$0.obscured=Name ''{0}'' obscured by local definitions
CONFLICT.name.$0.obscured.cannot.convert=Name ''{0}'' obscured. Cannot convert.
@@ -0,0 +1,68 @@
package com.jetbrains.python.codeInsight.intentions;
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
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.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
/**
* User: ktisha
*/
public class PyConvertStaticMethodToFunctionIntention extends BaseIntentionAction {
@NotNull
public String getFamilyName() {
return PyBundle.message("INTN.convert.static.method.to.function");
}
@NotNull
public String getText() {
return PyBundle.message("INTN.convert.static.method.to.function");
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
final PsiElement element = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class);
if (function == null) return false;
final PyClass containingClass = function.getContainingClass();
if (containingClass == null) return false;
final PyDecoratorList decoratorList = function.getDecoratorList();
if (decoratorList != null) {
final PyDecorator staticMethod = decoratorList.findDecorator(PyNames.STATICMETHOD);
if (staticMethod != null) return true;
}
return false;
}
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final PsiElement element = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
PyFunction problemFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
if (problemFunction == null) return;
final PyClass containingClass = problemFunction.getContainingClass();
if (containingClass == null) return;
final PyDecoratorList decoratorList = problemFunction.getDecoratorList();
if (decoratorList != null) {
final PyDecorator decorator = decoratorList.findDecorator(PyNames.STATICMETHOD);
if (decorator != null) decorator.delete();
}
final PyElementGenerator generator = PyElementGenerator.getInstance(project);
final PsiElement copy = problemFunction.copy();
final PyStatementList classStatementList = containingClass.getStatementList();
classStatementList.deleteChildRange(problemFunction, problemFunction);
if (classStatementList.getStatements().length < 1) {
classStatementList.add(generator.createPassStatement());
}
file.addAfter(copy, containingClass);
}
}
@@ -0,0 +1,15 @@
class MyClass(object):
"""
My class to show intention.
"""
def __init__(self):
self.a = 1
def my_static_method():
import code
import time
time.sleep(100)
print code
@@ -0,0 +1,15 @@
class MyClass(object):
"""
My class to show intention.
"""
def __init__(self):
self.a = 1
@staticmethod
def my_<caret>static_method():
import code
import time
time.sleep(100)
print code
@@ -398,6 +398,10 @@ public class PyIntentionTest extends PyTestCase {
doTest(PyBundle.message("INTN.replace.plus.with.format.operator"), LanguageLevel.PYTHON25);
}
public void testConvertStaticMethodToFunction() {
doTest(PyBundle.message("INTN.convert.static.method.to.function"));
}
private void doDocStubTest(LanguageLevel languageLevel) {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);
try {