Example #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;
  }
Example #2
0
  /** Check that import is useless. */
  private static boolean isImportedByDefault(
      @NotNull ImportPath importPath, @Nullable String aliasName, @NotNull FqName filePackageFqn) {
    if (importPath.fqnPart().isRoot()) {
      return true;
    }

    if (aliasName != null) {
      return false;
    }

    // Single element import without .* and alias is useless
    if (!importPath.isAllUnder() && QualifiedNamesUtil.isOneSegmentFQN(importPath.fqnPart())) {
      return true;
    }

    // There's no need to import a declaration from the package of current file
    if (!importPath.isAllUnder() && filePackageFqn.equals(importPath.fqnPart().parent())) {
      return true;
    }

    if (isImportedWithKotlinDefault(importPath)) return true;

    if (isImportedWithJavaDefault(importPath)) return true;

    return false;
  }
Example #3
0
  public static boolean isImported(@NotNull ImportPath alreadyImported, @NotNull FqName fqName) {
    if (alreadyImported.isAllUnder() && !fqName.isRoot()) {
      return alreadyImported.fqnPart().equals(fqName.parent());
    }

    return alreadyImported.fqnPart().equals(fqName);
  }
Example #4
0
  /** Check that import is useless. */
  private static boolean isImportedByDefault(
      @NotNull ImportPath importPath, @NotNull JetFile jetFile) {
    if (importPath.fqnPart().isRoot()) {
      return true;
    }

    if (!importPath.isAllUnder() && !importPath.hasAlias()) {
      // Single element import without .* and alias is useless
      if (QualifiedNamesUtil.isOneSegmentFQN(importPath.fqnPart())) {
        return true;
      }

      // There's no need to import a declaration from the package of current file
      if (JetPsiUtil.getFQName(jetFile).equals(importPath.fqnPart().parent())) {
        return true;
      }
    }

    if (isImportedWithKotlinDefault(importPath)) return true;

    if (KotlinFrameworkDetector.isJsKotlinModule(jetFile)) {
      return isImportedWithJsDefault(importPath);
    } else {
      return isImportedWithJavaDefault(importPath);
    }
  }
Example #5
0
  public static boolean isImported(
      @NotNull ImportPath alreadyImported, @NotNull ImportPath newImport) {
    if (newImport.isAllUnder()) {
      return alreadyImported.equals(newImport);
    }

    return isImported(alreadyImported, newImport.fqnPart());
  }