From 7a52f867d43f7716486fd85f3b9e18e6561ab9a4 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 8 Aug 2014 22:32:56 +0400 Subject: [PATCH] Fix trying to insert declaration in dummy.py file for extract variable --- .../python/refactoring/PyReplaceExpressionUtil.java | 1 + .../python/refactoring/introduce/IntroduceHandler.java | 5 +++++ .../introduceVariable/selectionBreaksBinaryOperator.after.py | 3 +++ .../introduceVariable/selectionBreaksBinaryOperator.py | 2 ++ .../python/refactoring/PyIntroduceVariableTest.java | 2 ++ 5 files changed, 13 insertions(+) create mode 100644 python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.after.py create mode 100644 python/testData/refactoring/introduceVariable/selectionBreaksBinaryOperator.py 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): *
    *
  • Selection conflicts with operator precedence: {@code n = 1 * 2 + 3}
  • + *
  • Selection conflicts with operator associativity: {@code n = 1 + 2 + 3}
  • *
  • Part of string literal is selected: {@code s = 'green eggs and ham'}
  • *
*/ 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 {