PyUtil#removeQualifier handles possible whitespace before the dot
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 5 Oct 2015 11:57:21 +0000 (14:57 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Mon, 5 Oct 2015 13:00:35 +0000 (16:00 +0300)
python/src/com/jetbrains/python/inspections/quickfix/PyMakeFunctionFromMethodQuickFix.java
python/src/com/jetbrains/python/psi/PyUtil.java
python/testData/quickFixes/PyMakeFunctionFromMethodQuickFixTest/removeQualifiers.py [new file with mode: 0644]
python/testData/quickFixes/PyMakeFunctionFromMethodQuickFixTest/removeQualifiers_after.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/quickFixes/PyMakeFunctionFromMethodQuickFixTest.java

index 3df1e2826d686e410aac85cfb6fc7f077689670b..edbf4694251c69034761a5be28846c64458c7691 100644 (file)
@@ -106,9 +106,7 @@ public class PyMakeFunctionFromMethodQuickFix implements LocalQuickFix {
         updateAssignment(element, resolved);
       }
       else if (resolved instanceof PyClass) {     //call with first instance argument A.m(A())
         updateAssignment(element, resolved);
       }
       else if (resolved instanceof PyClass) {     //call with first instance argument A.m(A())
-        final PsiElement dot = qualifier.getNextSibling();
-        if (dot != null) dot.delete();
-        qualifier.delete();
+        PyUtil.removeQualifier(element);
         updateArgumentList(element);
       }
     }
         updateArgumentList(element);
       }
     }
index f04c0a08e142ed96592c33a9f91f0c76895dd1e5..1fbc96d6dd0fe9ed38bf985ee94c0a880ed64bc0 100644 (file)
@@ -545,23 +545,18 @@ public class PyUtil {
     if (qualifier == null) return;
 
     if (qualifier instanceof PyCallExpression) {
     if (qualifier == null) return;
 
     if (qualifier instanceof PyCallExpression) {
-      final StringBuilder newElement = new StringBuilder(element.getLastChild().getText());
       final PyExpression callee = ((PyCallExpression)qualifier).getCallee();
       if (callee instanceof PyReferenceExpression) {
         final PyExpression calleeQualifier = ((PyReferenceExpression)callee).getQualifier();
         if (calleeQualifier != null) {
       final PyExpression callee = ((PyCallExpression)qualifier).getCallee();
       if (callee instanceof PyReferenceExpression) {
         final PyExpression calleeQualifier = ((PyReferenceExpression)callee).getQualifier();
         if (calleeQualifier != null) {
-          newElement.insert(0, calleeQualifier.getText() + ".");
+          qualifier.replace(calleeQualifier);
+          return;
         }
       }
         }
       }
-      final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(element.getProject());
-      final PyExpression expression = elementGenerator.createExpressionFromText(LanguageLevel.forElement(element), newElement.toString());
-      element.replace(expression);
-    }
-    else {
-      final PsiElement dot = qualifier.getNextSibling();
-      if (dot != null) dot.delete();
-      qualifier.delete();
     }
     }
+    final PsiElement dot = PyPsiUtils.getNextNonWhitespaceSibling(qualifier);
+    if (dot != null) dot.delete();
+    qualifier.delete();
   }
 
   /**
   }
 
   /**
diff --git a/python/testData/quickFixes/PyMakeFunctionFromMethodQuickFixTest/removeQualifiers.py b/python/testData/quickFixes/PyMakeFunctionFromMethodQuickFixTest/removeQualifiers.py
new file mode 100644 (file)
index 0000000..317c0b1
--- /dev/null
@@ -0,0 +1,8 @@
+class C:
+    def me<caret>thod(self, x):
+        test = 1
+        
+
+inst = C()      
+inst . method(1)
+C . method(inst, 42)
diff --git a/python/testData/quickFixes/PyMakeFunctionFromMethodQuickFixTest/removeQualifiers_after.py b/python/testData/quickFixes/PyMakeFunctionFromMethodQuickFixTest/removeQualifiers_after.py
new file mode 100644 (file)
index 0000000..53e025d
--- /dev/null
@@ -0,0 +1,11 @@
+def method(x):
+    test = 1
+
+
+class C:
+    pass
+
+
+inst = C()
+method(1)
+method(42)
index f17fcc9256134024b77f934bfef4d66ff578ef84..347b9d2a6621ae62e9d3097d0e18fbaf117ef606 100644 (file)
@@ -79,4 +79,8 @@ public class PyMakeFunctionFromMethodQuickFixTest extends PyQuickFixTestCase {
   public void testLocalClass() {
     doQuickFixTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
   }
   public void testLocalClass() {
     doQuickFixTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
   }
+
+  public void testRemoveQualifiers() {
+    doQuickFixTest(PyMethodMayBeStaticInspection.class, PyBundle.message("QFIX.NAME.make.function"));
+  }
 }
 }