Fix trying to insert declaration in dummy.py file for extract variable

This commit is contained in:
Mikhail Golubev
2014-08-09 01:54:05 +04:00
parent 69ad1191f3
commit 7a52f867d4
5 changed files with 13 additions and 0 deletions
@@ -52,6 +52,7 @@ public class PyReplaceExpressionUtil implements PyElementTypes {
* It can happen in cases like (here {@code <start> and <end>} represent selection boundaries):
* <ul>
* <li>Selection conflicts with operator precedence: {@code n = 1 * <start>2 + 3<end>}</li>
* <li>Selection conflicts with operator associativity: {@code n = 1 + <start>2 + 3<end>}</li>
* <li>Part of string literal is selected: {@code s = 'green <start>eggs<end> and ham'}</li>
* </ul>
*/
@@ -69,6 +69,11 @@ import static com.jetbrains.python.inspections.PyStringFormatParser.*;
abstract public class IntroduceHandler implements RefactoringActionHandler {
protected static PsiElement findAnchor(List<PsiElement> occurrences) {
PsiElement anchor = occurrences.get(0);
final Pair<PsiElement, TextRange> 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);
@@ -0,0 +1,3 @@
def foo():
a = 2 + 3
print 1 + a
@@ -0,0 +1,2 @@
def foo():
print 1 + <selection>2 + 3</selection>
@@ -245,6 +245,8 @@ public class PyIntroduceVariableTest extends PyIntroduceTestCase {
}
}
public void testSelectionBreaksBinaryOperator() {doTest();}
private void doTestCannotPerform() {
boolean thrownExpectedException = false;
try {