PY-17360 Allow to extract qualifier of a call expression
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 26 Oct 2015 11:47:03 +0000 (14:47 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 26 Oct 2015 12:19:40 +0000 (15:19 +0300)
python/src/com/jetbrains/python/refactoring/introduce/IntroduceHandler.java
python/testData/refactoring/introduceVariable/callExpressionQualifier.after.py [new file with mode: 0644]
python/testData/refactoring/introduceVariable/callExpressionQualifier.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/refactoring/PyIntroduceVariableTest.java

index 1d165c30faf2532d68e623ea7e823506bc272265..d1d9bdf1349206126483ab3153fb828c79e9f4af 100644 (file)
@@ -60,6 +60,7 @@ import org.jetbrains.annotations.Nullable;
 import java.util.*;
 
 import static com.jetbrains.python.inspections.PyStringFormatParser.*;
+import static com.jetbrains.python.psi.PyUtil.as;
 
 /**
  * @author Alexey.Ivanov
@@ -417,8 +418,8 @@ abstract public class IntroduceHandler implements RefactoringActionHandler {
   }
 
   private static boolean isValidIntroduceVariant(PsiElement element) {
-    final PyCallExpression call = PsiTreeUtil.getParentOfType(element, PyCallExpression.class);
-    if (call != null && PsiTreeUtil.isAncestor(call.getCallee(), element, false)) {
+    final PyCallExpression call = as(element.getParent(), PyCallExpression.class);
+    if (call != null && call.getCallee() == element) {
       return false;
     }
     final PyComprehensionElement comprehension = PsiTreeUtil.getParentOfType(element, PyComprehensionElement.class, true);
diff --git a/python/testData/refactoring/introduceVariable/callExpressionQualifier.after.py b/python/testData/refactoring/introduceVariable/callExpressionQualifier.after.py
new file mode 100644 (file)
index 0000000..b119a19
--- /dev/null
@@ -0,0 +1,7 @@
+class MyClass:
+    def method(self):
+        return 42
+
+
+a = MyClass()
+x = a.method()
\ No newline at end of file
diff --git a/python/testData/refactoring/introduceVariable/callExpressionQualifier.py b/python/testData/refactoring/introduceVariable/callExpressionQualifier.py
new file mode 100644 (file)
index 0000000..6ad140f
--- /dev/null
@@ -0,0 +1,6 @@
+class MyClass:
+    def method(self):
+        return 42
+
+
+x = MyCla<caret>ss().method()
\ No newline at end of file
index 0b16601c3eab367d83dd0fabe723216eb7ec2bd3..6cd1ac009b9942a8e888c1f6a63fd90d40b31cea 100644 (file)
@@ -309,6 +309,11 @@ public class PyIntroduceVariableTest extends PyIntroduceTestCase {
     doTest();
   }
 
+  // PY-17360
+  public void testCallExpressionQualifier() {
+    doTest();
+  }
+
   @Override
   protected String getTestDataPath() {
     return super.getTestDataPath() + "/refactoring/introduceVariable";