add field after super call
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Wed, 15 Jan 2014 14:32:25 +0000 (18:32 +0400)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Wed, 15 Jan 2014 14:32:25 +0000 (18:32 +0400)
14 files changed:
python/src/com/jetbrains/python/psi/PyUtil.java
python/testData/inspections/FieldFromUnusedParameterKeyword.py [deleted file]
python/testData/inspections/FieldFromUnusedParameterKeyword_after.py [deleted file]
python/testData/inspections/FieldFromUnusedParameter_after.py [deleted file]
python/testData/quickFixes/AddFieldQuickFixTest/addFieldAddConstructor_after.py
python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameter.py [moved from python/testData/inspections/FieldFromUnusedParameter.py with 100% similarity]
python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameterKeyword.py [new file with mode: 0644]
python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameterKeyword_after.py [new file with mode: 0644]
python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameter_after.py [new file with mode: 0644]
python/testData/quickFixes/PyMoveAttributeToInitQuickFixTest/addPass_after.py
python/testData/quickFixes/PyMoveAttributeToInitQuickFixTest/moveToInit_after.py
python/testData/refactoring/introduceField/py4437.after.py
python/testSrc/com/jetbrains/python/PyQuickFixTest.java
python/testSrc/com/jetbrains/python/quickFixes/AddFieldQuickFixTest.java

index e939857703d2a65c877c9bd9b30f5812ddd9756c..0cd299422694848b1060520b76243f9d8ac3300c 100644 (file)
@@ -44,6 +44,7 @@ import com.intellij.psi.*;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.psi.tree.TokenSet;
 import com.intellij.psi.util.PsiTreeUtil;
+import com.intellij.psi.util.QualifiedName;
 import com.intellij.ui.awt.RelativePoint;
 import com.intellij.util.ArrayUtil;
 import com.intellij.util.IncorrectOperationException;
@@ -60,7 +61,6 @@ import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
 import com.jetbrains.python.codeInsight.stdlib.PyNamedTupleType;
 import com.jetbrains.python.psi.impl.PyBuiltinCache;
 import com.jetbrains.python.psi.impl.PyPsiUtils;
-import com.intellij.psi.util.QualifiedName;
 import com.jetbrains.python.psi.resolve.PyResolveContext;
 import com.jetbrains.python.psi.resolve.QualifiedResolveResult;
 import com.jetbrains.python.psi.types.*;
@@ -1313,13 +1313,31 @@ public class PyUtil {
       final PyStatement[] statements = statementList.getStatements();
       if (toTheBeginning && statements.length > 0) {
         final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(statementList, PyDocStringOwner.class);
-        final PyStatement firstStatement = statements[0];
-        if (docStringOwner != null && firstStatement instanceof PyExpressionStatement &&
-            ((PyExpressionStatement)firstStatement).getExpression() == docStringOwner.getDocStringExpression()) {
-          element = statementList.addAfter(element, firstStatement);
+        PyStatement anchor = statements[0];
+        if (docStringOwner != null && anchor instanceof PyExpressionStatement &&
+            ((PyExpressionStatement)anchor).getExpression() == docStringOwner.getDocStringExpression()) {
+          final PyStatement next = PsiTreeUtil.getNextSiblingOfType(anchor, PyStatement.class);
+          if (next == null) {
+            return statementList.addAfter(element, anchor);
+          }
+          anchor = next;
+        }
+        while (anchor instanceof PyExpressionStatement) {
+          final PyExpression expression = ((PyExpressionStatement)anchor).getExpression();
+          if (expression instanceof PyCallExpression) {
+            final PyExpression callee = ((PyCallExpression)expression).getCallee();
+            if ((isSuperCall((PyCallExpression)expression) || (callee != null && PyNames.INIT.equals(callee.getName())))) {
+              final PyStatement next = PsiTreeUtil.getNextSiblingOfType(anchor, PyStatement.class);
+              if (next == null) {
+                return statementList.addAfter(element, anchor);
+              }
+              anchor = next;
+            }
+            else break;
+          }
+          else break;
         }
-        else
-          element = statementList.addBefore(element, firstStatement);
+        element = statementList.addBefore(element, anchor);
       }
       else {
         element = statementList.add(element);
diff --git a/python/testData/inspections/FieldFromUnusedParameterKeyword.py b/python/testData/inspections/FieldFromUnusedParameterKeyword.py
deleted file mode 100644 (file)
index 35851f0..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-class A:
-    def __init__(self, <weak_warning descr="Parameter 'foo' value is not used">f<caret>oo=True</weak_warning>):
-        print('hello')
\ No newline at end of file
diff --git a/python/testData/inspections/FieldFromUnusedParameterKeyword_after.py b/python/testData/inspections/FieldFromUnusedParameterKeyword_after.py
deleted file mode 100644 (file)
index 7e0d662..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-class A:
-    def __init__(self, foo=True):
-        print('hello')
-        self.foo = foo
\ No newline at end of file
diff --git a/python/testData/inspections/FieldFromUnusedParameter_after.py b/python/testData/inspections/FieldFromUnusedParameter_after.py
deleted file mode 100644 (file)
index 7405ba3..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-class A:
-    def __init__(self, foo):
-        print('hello')
-        self.foo = foo
\ No newline at end of file
index 881dd8adfc8e5bb1e7089a2ba1a8a705a2085aa8..4125368868db7af7ac1fb8c63e947c7285fee7cf 100644 (file)
@@ -5,8 +5,8 @@ class A:
 
 class B(A):
     def __init__(self, a, b):
-        self.x = None
         A.__init__(self, a, b)
+        self.x = None
 
     def foo(self):
         return self.x
diff --git a/python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameterKeyword.py b/python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameterKeyword.py
new file mode 100644 (file)
index 0000000..27f1600
--- /dev/null
@@ -0,0 +1,3 @@
+class A:
+    def __init__(self, fo<caret>o=True):
+        print('hello')
\ No newline at end of file
diff --git a/python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameterKeyword_after.py b/python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameterKeyword_after.py
new file mode 100644 (file)
index 0000000..3f86fa4
--- /dev/null
@@ -0,0 +1,4 @@
+class A:
+    def __init__(self, foo=True):
+        self.foo = foo
+        print('hello')
\ No newline at end of file
diff --git a/python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameter_after.py b/python/testData/quickFixes/AddFieldQuickFixTest/fromUnusedParameter_after.py
new file mode 100644 (file)
index 0000000..58785ed
--- /dev/null
@@ -0,0 +1,4 @@
+class A:
+    def __init__(self, foo):
+        self.foo = foo
+        print('hello')
\ No newline at end of file
index da65a030ea4d9300fa861bb8e44272be2c7e4524..040de7386118f6d5b0ce81a50ecb68679406f4e7 100644 (file)
@@ -3,8 +3,8 @@ __author__ = 'ktisha'
 
 class A:
     def __init__(self):
-        self._a = 1
         self.b = 1
+        self._a = 1
 
     def foo(self):
         pass
index 2dc80743d949eba0e816e9b72abf8b80242ea4d9..9a83f676bfdc733e119b26b25b89a2192e6d5363 100644 (file)
@@ -3,8 +3,8 @@ __author__ = 'ktisha'
 
 class A:
     def __init__(self):
-        self._a = 1
         self.b = 1
+        self._a = 1
 
     def foo(self):
         c = 1
\ No newline at end of file
index e9c7d7535699f612a96e46226257482a5fbb6cf6..07a1331b604a88e6c66f356048bc833f136211df 100644 (file)
@@ -1,7 +1,7 @@
 class SomeClass():
     def __init__(self):
-        self.x = 1
         self.a = ''
+        self.x = 1
 
     def foo(self):
         self.a
index e0f82b0718c4ea44e6f8bd511fa3c1ae89b7e293..4083775e8a41fb284aa0eb6f4ad952f2d6be4647 100644 (file)
@@ -173,13 +173,6 @@ public class PyQuickFixTest extends PyTestCase {
     doInspectionTest("AddClass.py", PyUnresolvedReferencesInspection.class, "Create class 'Xyzzy'", true, true);
   }
 
-  public void testFieldFromUnusedParameter() {  // PY-1398
-    doInspectionTest("FieldFromUnusedParameter.py", PyUnusedLocalInspection.class, "Add field 'foo' to class A", true, true);
-  }
-
-  public void testFieldFromUnusedParameterKeyword() {  // PY-1602
-    doInspectionTest("FieldFromUnusedParameterKeyword.py", PyUnusedLocalInspection.class, "Add field 'foo' to class A", true, true);
-  }
 
   public void testAddFunctionToModule() {  // PY-1602
     doInspectionTest(
index 5ffe1a651f2badb336530b5ab51a9bd597946ebb..c93553d2c3a4c76584dd803a3f43399f99597f6f 100644 (file)
  */
 package com.jetbrains.python.quickFixes;
 
+import com.intellij.testFramework.TestDataPath;
 import com.jetbrains.python.PyBundle;
 import com.jetbrains.python.PyQuickFixTestCase;
 import com.jetbrains.python.inspections.PyUnresolvedReferencesInspection;
+import com.jetbrains.python.inspections.PyUnusedLocalInspection;
 
 /**
  * User: ktisha
  */
+@TestDataPath("$CONTENT_ROOT/../testData//quickFixes/AddFieldQuickFixTest/")
 public class AddFieldQuickFixTest extends PyQuickFixTestCase {
 
   public void testAddClassField() {
@@ -44,4 +47,12 @@ public class AddFieldQuickFixTest extends PyQuickFixTestCase {
     doQuickFixTest(PyUnresolvedReferencesInspection.class, PyBundle.message("QFIX.NAME.add.field.$0.to.class.$1", "x", "B"));
   }
 
+  public void testFromUnusedParameter() {  // PY-1398
+    doQuickFixTest(PyUnusedLocalInspection.class, "Add field 'foo' to class A");
+  }
+
+  public void testFromUnusedParameterKeyword() {  // PY-1602
+    doQuickFixTest(PyUnusedLocalInspection.class, "Add field 'foo' to class A");
+  }
+
 }