Пример #1
0
  public ASTNode getDefaultAnchor(
      @NotNull PsiImportList list, @NotNull PsiImportStatementBase statement) {
    PsiJavaCodeReferenceElement ref = statement.getImportReference();
    if (ref == null) return null;

    int entryIndex = findEntryIndex(statement);
    PsiImportStatementBase[] allStatements = list.getAllImportStatements();
    int[] entries = ArrayUtil.newIntArray(allStatements.length);
    List<PsiImportStatementBase> statements = new ArrayList<PsiImportStatementBase>();
    for (int i = 0; i < allStatements.length; i++) {
      PsiImportStatementBase statement1 = allStatements[i];
      int entryIndex1 = findEntryIndex(statement1);
      entries[i] = entryIndex1;
      if (entryIndex1 == entryIndex) {
        statements.add(statement1);
      }
    }

    if (statements.isEmpty()) {
      int index;
      for (index = entries.length - 1; index >= 0; index--) {
        if (entries[index] < entryIndex) break;
      }
      index++;
      return index < entries.length
          ? SourceTreeToPsiMap.psiElementToTree(allStatements[index])
          : null;
    } else {
      String refText = ref.getCanonicalText();
      if (statement.isOnDemand()) {
        refText += ".";
      }

      PsiImportStatementBase insertBefore = null;
      PsiImportStatementBase insertAfter = null;
      for (PsiImportStatementBase statement1 : statements) {
        PsiJavaCodeReferenceElement ref1 = statement1.getImportReference();
        if (ref1 == null) {
          continue;
        }
        String refTextThis = ref1.getCanonicalText();
        if (statement1.isOnDemand()) {
          refTextThis += ".";
        }

        int comp = Comparing.compare(refText, refTextThis);
        if (comp < 0 && insertBefore == null) {
          insertBefore = statement1;
        }
        if (comp > 0) {
          insertAfter = statement1;
        }
      }

      if (insertBefore != null) return insertBefore.getNode();
      if (insertAfter != null) return insertAfter.getNode().getTreeNext();
      return null;
    }
  }
Пример #2
0
 @NotNull
 private static Import importToImport(@NotNull PsiImportStatementBase i) {
   PsiJavaCodeReferenceElement reference = i.getImportReference();
   if (reference != null) {
     return new Import(quoteKeywords(reference.getQualifiedName()) + (i.isOnDemand() ? ".*" : ""));
   }
   return new Import("");
 }
Пример #3
0
 public int findEntryIndex(@NotNull PsiImportStatementBase statement) {
   PsiJavaCodeReferenceElement ref = statement.getImportReference();
   if (ref == null) return -1;
   String packageName;
   if (statement.isOnDemand()) {
     packageName = ref.getCanonicalText();
   } else {
     String className = ref.getCanonicalText();
     packageName = getPackageOrClassName(className);
   }
   return findEntryIndex(packageName);
 }
Пример #4
0
 private static void addUnresolvedImportNames(
     @NotNull Set<String> set,
     @NotNull PsiJavaFile file,
     @NotNull Set<String> namesToImportStaticly) {
   PsiImportStatementBase[] imports = file.getImportList().getAllImportStatements();
   for (PsiImportStatementBase anImport : imports) {
     PsiJavaCodeReferenceElement ref = anImport.getImportReference();
     if (ref == null) continue;
     JavaResolveResult[] results = ref.multiResolve(false);
     if (results.length == 0) {
       String text = ref.getCanonicalText();
       if (anImport.isOnDemand()) {
         text += ".*";
       }
       if (anImport instanceof PsiImportStaticStatement) {
         namesToImportStaticly.add(text);
       }
       set.add(text);
     }
   }
 }