Ejemplo n.º 1
0
  public static boolean doNeedImport(
      @NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) {
    if (QualifiedNamesUtil.getFirstSegment(importPath.fqnPart().getFqName())
        .equals(JavaDescriptorResolver.JAVA_ROOT.getName())) {
      FqName withoutJavaRoot = QualifiedNamesUtil.withoutFirstSegment(importPath.fqnPart());
      importPath = new ImportPath(withoutJavaRoot, importPath.isAllUnder());
    }

    if (isImportedByDefault(importPath, null, JetPsiUtil.getFQName(file))) {
      return false;
    }

    List<JetImportDirective> importDirectives = file.getImportDirectives();

    if (!importDirectives.isEmpty()) {
      // Check if import is already present
      for (JetImportDirective directive : importDirectives) {
        ImportPath existentImportPath = JetPsiUtil.getImportPath(directive);
        if (directive.getAliasName() == null && aliasName == null) {
          if (existentImportPath != null
              && QualifiedNamesUtil.isImported(existentImportPath, importPath)) {
            return false;
          }
        }
      }
    }

    return true;
  }
Ejemplo n.º 2
0
  @Nullable
  public static Name getAliasName(@NotNull JetImportDirective importDirective) {
    String aliasName = importDirective.getAliasName();
    JetExpression importedReference = importDirective.getImportedReference();
    if (importedReference == null) {
      return null;
    }
    JetSimpleNameExpression referenceExpression = getLastReference(importedReference);
    if (aliasName == null) {
      aliasName = referenceExpression != null ? referenceExpression.getReferencedName() : null;
    }

    return aliasName != null && !aliasName.isEmpty() ? Name.identifier(aliasName) : null;
  }
Ejemplo n.º 3
0
  public static void addImportDirective(
      @NotNull ImportPath importPath, @Nullable String aliasName, @NotNull JetFile file) {
    if (!doNeedImport(importPath, aliasName, file)) {
      return;
    }

    JetImportDirective newDirective =
        JetPsiFactory.createImportDirective(file.getProject(), importPath, aliasName);
    List<JetImportDirective> importDirectives = file.getImportDirectives();

    if (!importDirectives.isEmpty()) {
      JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1);
      lastDirective.getParent().addAfter(newDirective, lastDirective);
    } else {
      file.getNamespaceHeader().getParent().addAfter(newDirective, file.getNamespaceHeader());
    }
  }
Ejemplo n.º 4
0
  public static void writeImportToFile(ImportPath importPath, JetFile file) {
    JetImportDirective newDirective =
        JetPsiFactory.createImportDirective(file.getProject(), importPath);
    List<JetImportDirective> importDirectives = file.getImportDirectives();

    if (!importDirectives.isEmpty()) {
      JetImportDirective lastDirective = importDirectives.get(importDirectives.size() - 1);
      lastDirective.getParent().addAfter(newDirective, lastDirective);
    } else {
      JetNamespaceHeader header = file.getNamespaceHeader();
      if (header == null) {
        throw new IllegalStateException("Scripts are not supported: " + file.getName());
      }

      header.getParent().addAfter(newDirective, file.getNamespaceHeader());
    }
  }
Ejemplo n.º 5
0
  @Nullable
  @IfNotParsed
  public static ImportPath getImportPath(@NotNull JetImportDirective importDirective) {
    if (PsiTreeUtil.hasErrorElements(importDirective)) {
      return null;
    }

    FqName importFqn = getFQName(importDirective.getImportedReference());
    if (importFqn == null) {
      return null;
    }

    Name alias = null;
    String aliasName = importDirective.getAliasName();
    if (aliasName != null) {
      alias = Name.identifier(aliasName);
    }

    return new ImportPath(importFqn, importDirective.isAllUnder(), alias);
  }