private boolean isValid(List<Name> variables) {
    int assignCounter = 0;
    for (Name variable : variables) {
      if (variable.parent instanceof Assign) {
        Assign assign = (Assign) variable.parent;
        /* The given name has to be one of the targets of this assignment */
        for (exprType x : assign.targets) {
          if (x == variable) {
            assignCounter++;
            break;
          }
        }
      }

      if (variable.ctx == expr_contextType.Param || variable.ctx == expr_contextType.KwOnlyParam) {
        status.addFatalError(Messages.format(Messages.inlineLocalParameter, variable.getId()));
        return false;
      }
    }

    if (assignCounter > 1) {
      status.addFatalError(
          Messages.format(Messages.inlineLocalMultipleAssignments, variables.get(0).getId()));
      return false;
    }

    return true;
  }
  @Override
  public synchronized List<FunctionDefAdapter> getFunctionsInitFiltered() {
    if (cache == null) {
      cache = new ArrayList<FunctionDefAdapter>();
      for (IToken tok : this.tokens) {
        if (tok.getType() == IToken.TYPE_FUNCTION
            || tok.getType() == IToken.TYPE_BUILTIN
            || tok.getType() == IToken.TYPE_UNKNOWN) {
          String args = tok.getArgs();

          List<exprType> arguments = new ArrayList<exprType>();
          boolean useAnyArgs = false;
          if (args.length() > 0) {
            StringTokenizer strTok = new StringTokenizer(args, "( ,)");
            if (!strTok.hasMoreTokens()) {
              useAnyArgs = true;
            } else {
              while (strTok.hasMoreTokens()) {
                String nextArg = strTok.nextToken();
                arguments.add(new Name(nextArg, Name.Load, false));
              }
            }
          } else {
            useAnyArgs = true;
          }

          argumentsType functionArguments =
              new argumentsType(
                  arguments.toArray(new exprType[0]),
                  null,
                  null,
                  null,
                  null,
                  null,
                  null,
                  null,
                  null,
                  null);
          if (useAnyArgs) {
            Name name = new Name("self", Name.Store, false);
            name.addSpecial(new SpecialStr(",", -1, -1), true);
            functionArguments.args = new exprType[] {name};
            functionArguments.vararg = new NameTok("args", NameTok.VarArg);
            functionArguments.kwarg = new NameTok("kwargs", NameTok.KwArg);
          }
          //                System.out.println(tok.getRepresentation()+tok.getArgs());
          FunctionDef functionDef =
              new FunctionDef(
                  new NameTok(tok.getRepresentation(), NameTok.FunctionName),
                  functionArguments,
                  null,
                  null,
                  null);
          cache.add(new FunctionDefAdapter(this.getModule(), null, functionDef, adapterPrefs));
        }
      }
    }
    return cache;
  }
 protected final NameTok makeName(int ctx, Name name) {
   NameTok n = new NameTok(name.id, ctx);
   n.beginColumn = name.beginColumn;
   n.beginLine = name.beginLine;
   addSpecials(name, n);
   name.specialsBefore = n.getSpecialsBefore();
   name.specialsAfter = n.getSpecialsAfter();
   return n;
 }
Beispiel #4
-1
  /**
   * Called after the creation of any module. Used as a workaround for filling tokens that are in no
   * way available in the code-completion through the regular inspection.
   *
   * <p>The django objects class is the reason why this happens... It's structure for the creation
   * on a model class follows no real patterns for the creation of the 'objects' attribute in the
   * class, and thus, we have no real generic way of discovering it (actually, even by looking at
   * the class definition this is very obscure), so, the solution found is creating the objects by
   * decorating the module with that info.
   */
  private AbstractModule decorateModule(AbstractModule n, IPythonNature nature) {
    if (n instanceof SourceModule && "django.db.models.base".equals(n.getName())) {
      SourceModule sourceModule = (SourceModule) n;
      SimpleNode ast = sourceModule.getAst();
      for (SimpleNode node : ((Module) ast).body) {
        if (node instanceof ClassDef && "Model".equals(NodeUtils.getRepresentationString(node))) {
          Object[][] metaclassAttrs =
              new Object[][] {
                {"objects", NodeUtils.makeAttribute("django.db.models.manager.Manager()")},
                {"DoesNotExist", new Name("Exception", Name.Load, false)},
                {"MultipleObjectsReturned", new Name("Exception", Name.Load, false)},
              };

          ClassDef classDef = (ClassDef) node;
          stmtType[] newBody = new stmtType[classDef.body.length + metaclassAttrs.length];
          System.arraycopy(classDef.body, 0, newBody, metaclassAttrs.length, classDef.body.length);

          int i = 0;
          for (Object[] objAndType : metaclassAttrs) {
            // Note that the line/col is important so that we correctly acknowledge it inside the
            // "class Model" scope.
            Name name = new Name((String) objAndType[0], Name.Store, false);
            name.beginColumn = classDef.beginColumn + 4;
            name.beginLine = classDef.beginLine + 1;
            newBody[i] = new Assign(new exprType[] {name}, (exprType) objAndType[1]);
            newBody[i].beginColumn = classDef.beginColumn + 4;
            newBody[i].beginLine = classDef.beginLine + 1;

            i += 1;
          }

          classDef.body = newBody;
          break;
        }
      }
    }
    return n;
  }