Merge branch 'master' into vlan/pyi
[idea/community.git] / python / src / com / jetbrains / python / psi / impl / PyCallExpressionHelper.java
index 9588a385d2fd4a53d60661f09221499c96e80acb..f87e0bd99e7bbdc854362f7013de8bfed228ad6a 100644 (file)
@@ -43,20 +43,22 @@ import java.util.*;
  */
 public class PyCallExpressionHelper {
   private PyCallExpressionHelper() {
-    // none
   }
 
   /**
-   * TODO: Copy/Paste with {@link com.jetbrains.python.psi.PyArgumentList#addArgument(com.jetbrains.python.psi.PyExpression)}
+   * TODO: Copy/Paste with {@link PyArgumentList#addArgument(PyExpression)}
    * Adds an argument to the end of argument list.
    *
    * @param us         the arg list
    * @param expression what to add
    */
   public static void addArgument(PyCallExpression us, PyExpression expression) {
-    PyExpression[] arguments = us.getArgumentList().getArguments();
-    final PyExpression last_arg = arguments.length == 0 ? null : arguments[arguments.length - 1];
-    PyElementGenerator.getInstance(us.getProject()).insertItemIntoList(us, last_arg, expression);
+    final PyArgumentList argumentList = us.getArgumentList();
+    if (argumentList != null) {
+      final PyExpression[] arguments = argumentList.getArguments();
+      final PyExpression last_arg = arguments.length == 0 ? null : arguments[arguments.length - 1];
+      PyElementGenerator.getInstance(us.getProject()).insertItemIntoList(us, last_arg, expression);
+    }
   }
 
   /**
@@ -70,25 +72,27 @@ public class PyCallExpressionHelper {
   public static Pair<String, PyFunction> interpretAsModifierWrappingCall(PyCallExpression redefiningCall, PsiElement us) {
     PyExpression redefining_callee = redefiningCall.getCallee();
     if (redefiningCall.isCalleeText(PyNames.CLASSMETHOD, PyNames.STATICMETHOD)) {
-      final PyReferenceExpression refex = (PyReferenceExpression)redefining_callee;
-      final String refname = refex.getReferencedName();
-      if ((PyNames.CLASSMETHOD.equals(refname) || PyNames.STATICMETHOD.equals(refname))) {
-        PsiElement redefining_func = refex.getReference().resolve();
-        if (redefining_func != null) {
-          PsiElement true_func = PyBuiltinCache.getInstance(us).getByName(refname);
-          if (true_func instanceof PyClass) true_func = ((PyClass)true_func).findInitOrNew(true, null);
-          if (true_func == redefining_func) {
-            // yes, really a case of "foo = classmethod(foo)"
-            PyArgumentList arglist = redefiningCall.getArgumentList();
-            if (arglist != null) { // really can't be any other way
-              PyExpression[] args = arglist.getArguments();
-              if (args.length == 1) {
-                PyExpression possible_original_ref = args[0];
-                if (possible_original_ref instanceof PyReferenceExpression) {
-                  PsiElement original = ((PyReferenceExpression)possible_original_ref).getReference().resolve();
-                  if (original instanceof PyFunction) {
-                    // pinned down the original; replace our resolved callee with it and add flags.
-                    return Pair.create(refname, (PyFunction)original);
+      final PyReferenceExpression referenceExpr = (PyReferenceExpression)redefining_callee;
+      if (referenceExpr != null) {
+        final String refName = referenceExpr.getReferencedName();
+        if ((PyNames.CLASSMETHOD.equals(refName) || PyNames.STATICMETHOD.equals(refName))) {
+          PsiElement redefining_func = referenceExpr.getReference().resolve();
+          if (redefining_func != null) {
+            PsiElement true_func = PyBuiltinCache.getInstance(us).getByName(refName);
+            if (true_func instanceof PyClass) true_func = ((PyClass)true_func).findInitOrNew(true, null);
+            if (true_func == redefining_func) {
+              // yes, really a case of "foo = classmethod(foo)"
+              PyArgumentList argumentList = redefiningCall.getArgumentList();
+              if (argumentList != null) { // really can't be any other way
+                PyExpression[] args = argumentList.getArguments();
+                if (args.length == 1) {
+                  PyExpression possible_original_ref = args[0];
+                  if (possible_original_ref instanceof PyReferenceExpression) {
+                    PsiElement original = ((PyReferenceExpression)possible_original_ref).getReference().resolve();
+                    if (original instanceof PyFunction) {
+                      // pinned down the original; replace our resolved callee with it and add flags.
+                      return Pair.create(refName, (PyFunction)original);
+                    }
                   }
                 }
               }
@@ -105,7 +109,7 @@ public class PyCallExpressionHelper {
     PyExpression callee = us.getCallee();
 
     PsiElement resolved;
-    QualifiedResolveResult resolveResult = null;
+    QualifiedResolveResult resolveResult;
     if (callee instanceof PyReferenceExpression) {
       // dereference
       PyReferenceExpression ref = (PyReferenceExpression)callee;
@@ -525,10 +529,6 @@ public class PyCallExpressionHelper {
       final PyCallable callable = (PyCallable)target;
       return Ref.create(callable.getCallType(context, call));
     }
-    /*PyCallExpression.PyMarkedCallee markedCallee = call.resolveCallee(PyResolveContext.defaultContext().withTypeEvalContext(context));
-    if (markedCallee != null) {
-      return Ref.create(markedCallee.getCallable().getCallType(context, call));
-    }*/
     return null;
   }
 
@@ -540,10 +540,10 @@ public class PyCallExpressionHelper {
       if (must_be_super_init instanceof PyFunction) {
         PyClass must_be_super = ((PyFunction)must_be_super_init).getContainingClass();
         if (must_be_super == PyBuiltinCache.getInstance(call).getClass(PyNames.SUPER)) {
-          PyArgumentList arglist = call.getArgumentList();
-          if (arglist != null) {
+          final PyArgumentList argumentList = call.getArgumentList();
+          if (argumentList != null) {
             final PyClass containingClass = PsiTreeUtil.getParentOfType(call, PyClass.class);
-            PyExpression[] args = arglist.getArguments();
+            PyExpression[] args = argumentList.getArguments();
             if (args.length > 1) {
               PyExpression first_arg = args[0];
               if (first_arg instanceof PyReferenceExpression) {