diff --git a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java index 0cb8423bc81f..6366e2336a7f 100644 --- a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java +++ b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java @@ -16,18 +16,21 @@ public class PyReplaceExpressionUtil implements PyElementTypes { public static boolean isNeedParenthesis(@NotNull final PyElement oldExpr, @NotNull final PyElement newExpr) { final PyElement parentExpr = (PyElement)oldExpr.getParent(); + if (!(parentExpr instanceof PyExpression)) { + return false; + } int newPriority = getExpressionPriority(newExpr); int parentPriority = getExpressionPriority(parentExpr); if (parentPriority > newPriority) { - return true; - } else if (parentPriority == newPriority && parentPriority != 0) { - if (parentExpr instanceof PyBinaryExpression) { - PyBinaryExpression binaryExpression = (PyBinaryExpression) parentExpr; - if (isNotAssociative(binaryExpression) && oldExpr.equals(binaryExpression.getRightExpression())) { - return true; - } + return true; + } else if (parentPriority == newPriority && parentPriority != 0) { + if (parentExpr instanceof PyBinaryExpression) { + PyBinaryExpression binaryExpression = (PyBinaryExpression)parentExpr; + if (isNotAssociative(binaryExpression) && oldExpr.equals(binaryExpression.getRightExpression())) { + return true; } } + } return false; } diff --git a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java index 0a573d939b92..50120ac11604 100644 --- a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java +++ b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java @@ -175,7 +175,10 @@ abstract public class IntroduceHandler implements RefactoringActionHandler { return; } - final PyExpression expression = (PyExpression)element; + final PsiElement parent = element.getParent(); + final PyExpression expression = parent instanceof PyAssignmentStatement ? + ((PyAssignmentStatement)parent).getAssignedValue() : + (PyExpression)element; final List occurrences; if (expression.getUserData(PyPsiUtils.SELECTION_BREAKS_AST_NODE) == null && !(expression instanceof PyCallExpression)) { @@ -185,6 +188,7 @@ abstract public class IntroduceHandler implements RefactoringActionHandler { occurrences = Collections.emptyList(); } String[] possibleNames = getSuggestedNames(expression); + replaceAll &= occurrences.size() > 0; boolean initInConstructor = false; if (name == null) { PyIntroduceDialog dialog = new PyIntroduceDialog(project, expression, myDialogTitle, myValidator, occurrences.size(), possibleNames, getHelpId(), hasConstructor); diff --git a/python/testData/refactoring/inlinelocal/py994.after.py b/python/testData/refactoring/inlinelocal/py994.after.py new file mode 100644 index 000000000000..393bb3e15efa --- /dev/null +++ b/python/testData/refactoring/inlinelocal/py994.after.py @@ -0,0 +1,3 @@ +class C: + def foo(self): + return Conference() \ No newline at end of file diff --git a/python/testData/refactoring/inlinelocal/py994.before.py b/python/testData/refactoring/inlinelocal/py994.before.py new file mode 100644 index 000000000000..58b24ebe9d69 --- /dev/null +++ b/python/testData/refactoring/inlinelocal/py994.before.py @@ -0,0 +1,4 @@ +class C: + def foo(self): + conf = Conference() + return conf \ No newline at end of file diff --git a/python/testData/refactoring/introduceVariable/py995.after.py b/python/testData/refactoring/introduceVariable/py995.after.py new file mode 100644 index 000000000000..617b9f339f69 --- /dev/null +++ b/python/testData/refactoring/introduceVariable/py995.after.py @@ -0,0 +1,4 @@ +class ConferenceTest(TestCase): + def testSimple(self): + a = Conference() + c = a \ No newline at end of file diff --git a/python/testData/refactoring/introduceVariable/py995.py b/python/testData/refactoring/introduceVariable/py995.py new file mode 100644 index 000000000000..090081ec0b3e --- /dev/null +++ b/python/testData/refactoring/introduceVariable/py995.py @@ -0,0 +1,3 @@ +class ConferenceTest(TestCase): + def testSimple(self): + c = Conference() \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java index f18845fe59e9..18edd4372ff0 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyInlineLocalTest.java @@ -55,4 +55,8 @@ public class PyInlineLocalTest extends LightMarkedTestCase { public void testMultiple() throws Exception { doTest(); } + + public void testPy994() throws Exception { + doTest(); + } } diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java index 19d6cf72452d..f161ac20295f 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java @@ -13,9 +13,17 @@ public class PyIntroduceVariableTest extends PyLightFixtureTestCase { } public void testSimple() throws Exception { - myFixture.configureByFile("simple.py"); + doTest(); + } + + public void testPy995() throws Exception { + doTest(); + } + + private void doTest() throws Exception { + myFixture.configureByFile(getTestName(true) + ".py"); VariableIntroduceHandler handler = new VariableIntroduceHandler(); handler.performAction(myFixture.getProject(), myFixture.getEditor(), myFixture.getFile(), "a", true, false); - myFixture.checkResultByFile("simple.after.py"); + myFixture.checkResultByFile(getTestName(true) + ".after.py"); } }