mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-6620 Prohibit refactoring at class level, remove stale test cases and their data
This commit is contained in:
@@ -604,6 +604,7 @@ refactoring.extract.method.error.local.variable.modifications.and.returns=Cannot
|
||||
refactoring.extract.method.error.empty.fragment=Cannot perform refactoring from empty code fragment
|
||||
refactoring.extract.method.error.undetermined.execution.flow=Cannot determine execution flow for the code fragment
|
||||
refactoring.extract.method.error.yield=Cannot perform refactoring with 'yield' statement inside code block
|
||||
refactoring.extract.method.error.class.level=Cannot perform refactoring at class level
|
||||
|
||||
|
||||
# extract superclass
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.codeInsight.codeFragment.PyCodeFragment;
|
||||
import com.jetbrains.python.codeInsight.codeFragment.PyCodeFragmentUtil;
|
||||
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
|
||||
import com.jetbrains.python.psi.PyClass;
|
||||
import com.jetbrains.python.psi.PyElement;
|
||||
import com.jetbrains.python.psi.impl.PyPsiUtils;
|
||||
import com.jetbrains.python.refactoring.PyRefactoringUtil;
|
||||
@@ -82,6 +83,11 @@ public class PyExtractMethodHandler implements RefactoringActionHandler {
|
||||
RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
|
||||
return;
|
||||
}
|
||||
if (rangeBelongsToSameClassBody(element1, element2)) {
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.extract.method.error.class.level"),
|
||||
RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
|
||||
return;
|
||||
}
|
||||
|
||||
final Couple<PsiElement> statements = getStatementsRange(element1, element2);
|
||||
if (statements != null) {
|
||||
@@ -126,6 +132,12 @@ public class PyExtractMethodHandler implements RefactoringActionHandler {
|
||||
RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
|
||||
}
|
||||
|
||||
private static boolean rangeBelongsToSameClassBody(@NotNull PsiElement element1, @NotNull PsiElement element2) {
|
||||
final PyClass firstScopeOwner = PsiTreeUtil.getParentOfType(element1, PyClass.class, false, ScopeOwner.class);
|
||||
final PyClass secondScopeOwner = PsiTreeUtil.getParentOfType(element2, PyClass.class, false, ScopeOwner.class);
|
||||
return firstScopeOwner != null && firstScopeOwner == secondScopeOwner;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Couple<PsiElement> getStatementsRange(final PsiElement element1, final PsiElement element2) {
|
||||
final PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2);
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
class PyCharm:
|
||||
def bar():
|
||||
print("Hello Pycharm!")
|
||||
|
||||
bar()
|
||||
@@ -1,2 +0,0 @@
|
||||
class PyCharm:
|
||||
<selection>print("Hello Pycharm!")</selection>
|
||||
@@ -1,8 +0,0 @@
|
||||
class Foo():
|
||||
def baz():
|
||||
tmp = "!" # try to extract this assignment, either with or without this comment
|
||||
|
||||
baz()
|
||||
|
||||
def bar(self):
|
||||
pass
|
||||
@@ -1,5 +0,0 @@
|
||||
class Foo():
|
||||
<selection>tmp = "!" #try to extract this assignment, either with or without this comment</selection>
|
||||
|
||||
def bar(self):
|
||||
pass
|
||||
@@ -0,0 +1,8 @@
|
||||
def baz():
|
||||
tmp = "!" # try to extract this assignment, either with or without this comment
|
||||
|
||||
|
||||
baz()
|
||||
|
||||
def bar(self):
|
||||
pass
|
||||
@@ -0,0 +1,4 @@
|
||||
<selection>tmp = "!" #try to extract this assignment, either with or without this comment</selection>
|
||||
|
||||
def bar(self):
|
||||
pass
|
||||
@@ -1,5 +0,0 @@
|
||||
class A:
|
||||
def hello():
|
||||
pass
|
||||
|
||||
<selection>print("Hello")</selection>
|
||||
@@ -0,0 +1,2 @@
|
||||
class C():
|
||||
<selection>some_class_field = 1</selection>
|
||||
@@ -101,10 +101,6 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
|
||||
doTest("bar");
|
||||
}
|
||||
|
||||
public void testNameCollisionClass() {
|
||||
doFail("hello", "Method name clashes with already existing name");
|
||||
}
|
||||
|
||||
public void testNameCollisionFile() {
|
||||
doFail("hello", "Method name clashes with already existing name");
|
||||
}
|
||||
@@ -149,10 +145,6 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
|
||||
doTest("bar");
|
||||
}
|
||||
|
||||
public void testClassContext() {
|
||||
doTest("bar");
|
||||
}
|
||||
|
||||
public void testConditionalReturn() {
|
||||
doFail("bar", "Cannot perform refactoring when execution flow is interrupted");
|
||||
}
|
||||
@@ -161,7 +153,7 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
|
||||
doTest("bar");
|
||||
}
|
||||
|
||||
public void testComment2() {
|
||||
public void testCommentIncluded() {
|
||||
doTest("baz");
|
||||
}
|
||||
|
||||
@@ -281,4 +273,9 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
|
||||
public void testRedundantGlobalInTopLevelFunction() {
|
||||
doTest("foo");
|
||||
}
|
||||
|
||||
// PY-6620
|
||||
public void testProhibitedAtClassLevel() {
|
||||
doFail("foo", "Cannot perform refactoring at class level");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user