Ejemplo n.º 1
0
 @Nullable
 private PyType getImplicitlyResolvedType(
     @NotNull List<Token<PyElementType>> tokens,
     @NotNull TypeEvalContext context,
     @NotNull Map<TextRange, PyType> types,
     @NotNull Map<PyType, TextRange> fullRanges,
     TextRange firstRange) {
   PyType type = null;
   QualifiedName qName = null;
   while (!tokens.isEmpty()) {
     final Token<PyElementType> token = tokens.get(0);
     final String name = token.getText().toString();
     qName = qName != null ? qName.append(name) : QualifiedName.fromComponents(name);
     PsiElement module =
         new QualifiedNameResolverImpl(qName).fromElement(myAnchor).firstResult();
     if (module == null) {
       break;
     }
     if (module instanceof PsiDirectory) {
       module = PyUtil.getPackageElement((PsiDirectory) module, myAnchor);
     }
     if (module instanceof PyTypedElement) {
       final PyType moduleType = context.getType((PyTypedElement) module);
       if (moduleType != null) {
         type = moduleType;
         types.put(token.getRange(), type);
         fullRanges.put(
             type,
             TextRange.create(firstRange.getStartOffset(), token.getRange().getEndOffset()));
       }
     }
     tokens.remove(0);
   }
   return type;
 }
 public static ResolveResultList rateResults(List<? extends PsiElement> targets) {
   ResolveResultList ret = new ResolveResultList();
   for (PsiElement target : targets) {
     if (target instanceof PsiDirectory) {
       target = PyUtil.getPackageElement((PsiDirectory) target, null);
     }
     if (target != null) {
       int rate = RatedResolveResult.RATE_HIGH;
       if (target instanceof PyFile) {
         for (PyResolveResultRater rater :
             Extensions.getExtensions(PyResolveResultRater.EP_NAME)) {
           rate += rater.getImportElementRate(target);
         }
       } else if (isDunderAll(target)) {
         rate = RatedResolveResult.RATE_NORMAL;
       }
       ret.poke(target, rate);
     }
   }
   return ret;
 }
  @NotNull
  public static List<RatedResolveResult> resolveNameInFromImport(
      PyFromImportStatement importStatement, @NotNull QualifiedName qName) {
    PsiFile file = importStatement.getContainingFile().getOriginalFile();
    String name = qName.getComponents().get(0);

    final List<PsiElement> candidates = importStatement.resolveImportSourceCandidates();
    List<PsiElement> resultList = new ArrayList<PsiElement>();
    for (PsiElement candidate : candidates) {
      if (!candidate.isValid()) {
        throw new PsiInvalidElementAccessException(
            candidate,
            "Got an invalid candidate from resolveImportSourceCandidates(): "
                + candidate.getClass());
      }
      if (candidate instanceof PsiDirectory) {
        candidate = PyUtil.getPackageElement((PsiDirectory) candidate, importStatement);
      }
      List<RatedResolveResult> results = resolveChildren(candidate, name, file, false, true);
      if (!results.isEmpty()) {
        for (RatedResolveResult result : results) {
          final PsiElement element = result.getElement();
          if (element != null) {
            if (!element.isValid()) {
              throw new PsiInvalidElementAccessException(
                  element, "Got an invalid candidate from resolveChild(): " + element.getClass());
            }
            resultList.add(element);
          }
        }
      }
    }
    if (!resultList.isEmpty()) {
      return rateResults(resultList);
    }
    return Collections.emptyList();
  }