diff --git a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java index 1730580a21cd..2cebb3ec2720 100644 --- a/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java +++ b/python/src/com/jetbrains/python/refactoring/PyReplaceExpressionUtil.java @@ -52,6 +52,7 @@ public class PyReplaceExpressionUtil implements PyElementTypes { * It can happen in cases like (here {@code and } represent selection boundaries): * */ diff --git a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java index 9a9cbf4bc1a6..00448c8b69bd 100644 --- a/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java +++ b/python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java @@ -69,6 +69,11 @@ import static com.jetbrains.python.inspections.PyStringFormatParser.*; abstract public class IntroduceHandler implements RefactoringActionHandler { protected static PsiElement findAnchor(List occurrences) { PsiElement anchor = occurrences.get(0); + final Pair data = anchor.getUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE); + // Search anchor in the origin file, not in dummy.py, if selection breaks statement and thus element was generated + if (data != null && occurrences.size() == 1) { + return PsiTreeUtil.getParentOfType(data.getFirst(), PyStatement.class); + } next: do { final PyStatement statement = PsiTreeUtil.getParentOfType(anchor, PyStatement.class); diff --git a/python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.after.py b/python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.after.py new file mode 100644 index 000000000000..775927cedb39 --- /dev/null +++ b/python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.after.py @@ -0,0 +1,3 @@ +def foo(): + a = 2 + 3 + print 1 + a \ No newline at end of file diff --git a/python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.py b/python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.py new file mode 100644 index 000000000000..c5eee8c9a423 --- /dev/null +++ b/python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.py @@ -0,0 +1,2 @@ +def foo(): + print 1 + 2 + 3 \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java index f2117977cbf3..33a191f2b9a0 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java +++ b/python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java @@ -245,6 +245,8 @@ public class PyIntroduceVariableTest extends PyIntroduceTestCase { } } + public void testSelectionBreaksBinaryOperator() {doTest();} + private void doTestCannotPerform() { boolean thrownExpectedException = false; try {