예제 #1
0
  /**
   * @param s the scope we're checking for
   * @return if the scope passed as a parameter starts with the same scope we have here. It should
   *     not be called if the size of the scope we're checking is bigger than the size of 'this'
   *     scope.
   */
  @SuppressWarnings("unchecked")
  private boolean checkIfScopesMatch(ILocalScope s) {
    Iterator<SimpleNode> otIt = s.getScopeStack().iterator();

    for (Iterator<SimpleNode> iter = this.scope.iterator(); iter.hasNext(); ) {
      SimpleNode element = iter.next();
      SimpleNode otElement = otIt.next();

      if (element.beginColumn != otElement.beginColumn) {
        return false;
      }

      if (element.beginLine != otElement.beginLine) {
        return false;
      }

      if (!element.getClass().equals(otElement.getClass())) {
        return false;
      }

      String rep1 = NodeUtils.getFullRepresentationString(element);
      String rep2 = NodeUtils.getFullRepresentationString(otElement);
      if (rep1 == null || rep2 == null) {
        if (rep1 != rep2) {
          return false;
        }

      } else if (!rep1.equals(rep2)) {
        return false;
      }
    }
    return true;
  }
예제 #2
0
  /**
   * @see
   *     org.python.pydev.core.ILocalScope#isOuterOrSameScope(org.python.pydev.editor.codecompletion.revisited.visitors.LocalScope)
   */
  public boolean isOuterOrSameScope(ILocalScope s) {
    if (this.scope.size() > s.getScopeStack().size()) {
      return false;
    }

    return checkIfScopesMatch(s);
  }
예제 #3
0
  /**
   * Get self completions when you already have a scope
   *
   * @throws MisconfigurationException
   */
  @SuppressWarnings("unchecked")
  public static void getSelfOrClsCompletions(
      ILocalScope scope,
      CompletionRequest request,
      List theList,
      ICompletionState state,
      boolean getOnlySupers)
      throws BadLocationException, MisconfigurationException {
    for (Iterator<SimpleNode> it = scope.iterator(); it.hasNext(); ) {
      SimpleNode node = it.next();
      if (node instanceof ClassDef) {
        ClassDef d = (ClassDef) node;

        if (getOnlySupers) {
          List gottenComps = new ArrayList();
          for (int i = 0; i < d.bases.length; i++) {
            if (d.bases[i] instanceof Name) {
              Name n = (Name) d.bases[i];
              state.setActivationToken(n.id);
              IToken[] completions;
              try {
                completions =
                    request
                        .nature
                        .getAstManager()
                        .getCompletionsForToken(request.editorFile, request.doc, state);
                gottenComps.addAll(Arrays.asList(completions));
              } catch (CompletionRecursionException e) {
                // ok...
              }
            }
          }
          theList.addAll(gottenComps);
        } else {
          // ok, get the completions for the class, only thing we have to take care now is that we
          // may
          // not have only 'self' for completion, but something like self.foo.
          // so, let's analyze our activation token to see what should we do.

          String trimmed = request.activationToken.replace('.', ' ').trim();
          String[] actTokStrs = trimmed.split(" ");
          if (actTokStrs.length == 0
              || (!actTokStrs[0].equals("self") && !actTokStrs[0].equals("cls"))) {
            throw new AssertionError(
                "We need to have at least one token (self or cls) for doing completions in the class.");
          }

          if (actTokStrs.length == 1) {
            // ok, it's just really self, let's get on to get the completions
            state.setActivationToken(NodeUtils.getNameFromNameTok((NameTok) d.name));
            try {
              theList.addAll(
                  Arrays.asList(
                      request
                          .nature
                          .getAstManager()
                          .getCompletionsForToken(request.editorFile, request.doc, state)));
            } catch (CompletionRecursionException e) {
              // ok
            }

          } else {
            // it's not only self, so, first we have to get the definition of the token
            // the first one is self, so, just discard it, and go on, token by token to know what is
            // the last
            // one we are completing (e.g.: self.foo.bar)
            int line = request.doc.getLineOfOffset(request.documentOffset);
            IRegion region = request.doc.getLineInformationOfOffset(request.documentOffset);
            int col = request.documentOffset - region.getOffset();

            // ok, try our best shot at getting the module name of the current buffer used in the
            // request.
            String modName = "";
            File requestFile = request.editorFile;
            if (request.editorFile != null) {
              String resolveModule = request.nature.resolveModule(requestFile);
              if (resolveModule != null) {
                modName = resolveModule;
              }
            }

            IModule module =
                AbstractModule.createModuleFromDoc(
                    modName, requestFile, request.doc, request.nature, line);

            AbstractASTManager astMan = ((AbstractASTManager) request.nature.getAstManager());
            theList.addAll(
                new AssignAnalysis()
                    .getAssignCompletions(
                        astMan,
                        module,
                        new CompletionState(
                            line, col, request.activationToken, request.nature, request.qualifier))
                    .completions);
          }
        }
      }
    }
  }
  /**
   * @see
   *     org.python.pydev.parser.jython.ast.VisitorBase#visitAssign(org.python.pydev.parser.jython.ast.Assign)
   */
  public Object visitAssign(Assign node) throws Exception {
    ILocalScope scope = new LocalScope(this.defsStack);
    if (foundAsDefinition
        && !scope.equals(
            definitionFound
                .scope)) { // if it is found as a definition it is an 'exact' match, so, we do not
                           // keep checking it
      return null;
    }

    for (int i = 0; i < node.targets.length; i++) {
      exprType target = node.targets[i];
      if (target instanceof Subscript) {
        continue; // assigning to an element and not the variable itself. E.g.: mydict[1] = 10
                  // (instead of mydict = 10)
      }

      if (target instanceof Tuple) {
        // if assign is xxx, yyy = 1, 2
        // let's separate those as different assigns and analyze one by one
        Tuple targetTuple = (Tuple) target;
        if (node.value instanceof Tuple) {
          Tuple valueTuple = (Tuple) node.value;
          checkTupleAssignTarget(targetTuple, valueTuple.elts);

        } else if (node.value instanceof org.python.pydev.parser.jython.ast.List) {
          org.python.pydev.parser.jython.ast.List valueList =
              (org.python.pydev.parser.jython.ast.List) node.value;
          checkTupleAssignTarget(targetTuple, valueList.elts);

        } else {
          checkTupleAssignTarget(targetTuple, new exprType[] {node.value});
        }

      } else {
        String rep = NodeUtils.getFullRepresentationString(target);

        if (tokenToFind.equals(
            rep)) { // note, order of equals is important (because one side may be null).
          exprType nodeValue = node.value;
          String value = NodeUtils.getFullRepresentationString(nodeValue);
          if (value == null) {
            value = "";
          }

          // get the line and column correspondent to the target
          int line = NodeUtils.getLineDefinition(target);
          int col = NodeUtils.getColDefinition(target);

          AssignDefinition definition =
              new AssignDefinition(value, rep, i, node, line, col, scope, module.get());

          // mark it as global (if it was found as global in some of the previous contexts).
          for (Set<String> globals : globalDeclarationsStack) {
            if (globals.contains(rep)) {
              definition.foundAsGlobal = true;
            }
          }

          definitions.add(definition);
        }
      }
    }

    return null;
  }