Java Code Examples for com.intellij.psi.PsiDirectory#getSubdirectories()

The following examples show how to use com.intellij.psi.PsiDirectory#getSubdirectories() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityBase.java    From CleanArchitecturePlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Create package in the directory specific with a name
 *
 * @param parent      package when create a subpackage
 * @param packageName package name
 * @return
 */
public static PsiDirectory createDirectory(PsiDirectory parent, String packageName) {
    directoryCreated = null;

    // Search subpackage with the same name
    for (PsiDirectory dir : parent.getSubdirectories()) {
        if (dir.getName().equalsIgnoreCase(packageName)) {
            directoryCreated = dir;
            break;
        }
    }

    // When the search not found a package with the same name, create a subdirectory
    if (directoryCreated == null) {
        runnable = () -> directoryCreated = parent.createSubdirectory(packageName);
        WriteCommandAction.runWriteCommandAction(project, runnable);
    }
    return directoryCreated;
}
 
Example 2
Source File: BlazePackage.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static boolean hasBlazePackageChild(
    Predicate<PsiDirectory> searchDirectory,
    BuildSystemProvider buildSystemProvider,
    PsiDirectory directory) {
  if (!searchDirectory.test(directory)) {
    return false;
  }
  if (buildSystemProvider.findBuildFileInDirectory(directory.getVirtualFile()) != null) {
    return true;
  }
  for (PsiDirectory child : directory.getSubdirectories()) {
    if (hasBlazePackageChild(searchDirectory, buildSystemProvider, child)) {
      return true;
    }
  }
  return false;
}
 
Example 3
Source File: ModulesUtil.java    From svgtoandroid with MIT License 6 votes vote down vote up
public Set<String> getModules() {
    Set<String> modules = new HashSet<String>();
    PsiDirectory baseDir = PsiDirectoryFactory.getInstance(project).createDirectory(project.getBaseDir());
    if (isAndroidProject(baseDir)) {
        Logger.debug(project.getName() + " is an Android project");
        PsiDirectory[] dirs = baseDir.getSubdirectories();
        for (PsiDirectory dir : dirs) {
            if (!dir.getName().equals("build") && !dir.getName().equals("gradle")) {
                if (isModule(dir)) {
                    Logger.debug(dir.getName() + " is a Module");
                    modules.add(dir.getName());
                }
            }
        }
    }
    Logger.debug(modules.toString());
    return modules;
}
 
Example 4
Source File: ModulesUtil.java    From svgtoandroid with MIT License 6 votes vote down vote up
private boolean isModule(PsiDirectory directory) {
    boolean hasGradle = false;
    boolean hasSrc = false;
    PsiFile[] files = directory.getFiles();
    PsiDirectory[] dirs = directory.getSubdirectories();
    for (PsiFile file : files) {
        if (file.getName().equals("build.gradle")) {
            hasGradle = true;
            break;
        }
    }

    for (PsiDirectory dir : dirs) {
        if (dir.getName().equals("src")) {
            hasSrc = true;
            break;
        }
    }
    return hasGradle && hasSrc;
}
 
Example 5
Source File: AbstractFileProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void findFiles(List<PsiFile> files, PsiDirectory directory, boolean subdirs) {
  final Project project = directory.getProject();
  PsiFile[] locals = directory.getFiles();
  for (PsiFile local : locals) {
    CopyrightProfile opts = CopyrightManager.getInstance(project).getCopyrightOptions(local);
    if (opts != null && CopyrightUpdaters.hasExtension(local)) {
      files.add(local);
    }

  }

  if (subdirs) {
    PsiDirectory[] dirs = directory.getSubdirectories();
    for (PsiDirectory dir : dirs) {
      findFiles(files, dir, subdirs);
    }

  }
}
 
Example 6
Source File: PackageNodeUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static PsiPackage[] getSubpackages(@Nonnull PsiPackage aPackage,
                                              @Nullable Module module,
                                              @Nonnull Project project,
                                              final boolean searchInLibraries) {
  final PsiDirectory[] dirs = getDirectories(aPackage, project, module, searchInLibraries);
  final Set<PsiPackage> subpackages = new HashSet<PsiPackage>();
  for (PsiDirectory dir : dirs) {
    final PsiDirectory[] subdirectories = dir.getSubdirectories();
    for (PsiDirectory subdirectory : subdirectories) {
      final PsiPackage psiPackage = PsiPackageManager.getInstance(project).findAnyPackage(subdirectory);
      if (psiPackage != null) {
        final String name = psiPackage.getName();
        // skip "default" subpackages as they should be attributed to other modules
        // this is the case when contents of one module is nested into contents of another
        if (name != null && !name.isEmpty()) {
          subpackages.add(psiPackage);
        }
      }
    }
  }
  return subpackages.toArray(new PsiPackage[subpackages.size()]);
}
 
Example 7
Source File: TypeStateCheckingTest.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
private void checkDirectoriesAreEqual(PsiDirectory result, PsiDirectory expected) {
    for (PsiDirectory directory : result.getSubdirectories()) {
        if (directory.getName().equals("expected")) {
            continue;
        }
        checkDirectoriesAreEqual(directory, expected.findSubdirectory(directory.getName()));
    }
    for (PsiFile file : result.getFiles()) {
        checkFilesAreEqual(file, expected.findFile(file.getName()));
    }
}
 
Example 8
Source File: GodClassTest.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
private void checkDirectoriesAreEqual(PsiDirectory result, PsiDirectory expected) {
    for (PsiDirectory directory : result.getSubdirectories()) {
        if (directory.getName().equals("expected")) {
            continue;
        }
        checkDirectoriesAreEqual(directory, expected.findSubdirectory(directory.getName()));
    }

    for (PsiFile file : result.getFiles()) {
        checkFilesAreEqual(file, expected.findFile(file.getName()));
    }
}
 
Example 9
Source File: ExtraModulesUtil.java    From weex-language-support with MIT License 5 votes vote down vote up
private static PsiDirectory[] getNodeModules(PsiDirectory root) {
    PsiDirectory node_modules = root.findSubdirectory("node_modules");
    if (node_modules != null) {
        return node_modules.getSubdirectories();
    }
    return new PsiDirectory[0];
}
 
Example 10
Source File: EntityBase.java    From CleanArchitecturePlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Find the directory from user click to create new entity
 *
 * @param sourceDirectory source directory to search
 * @param namePackage     name about package that you search
 * @return
 */
public static PsiDirectory containsPackage(PsiDirectory sourceDirectory, String namePackage) {
    PsiDirectory mPackage = null;

    // Find from project directory to subdirectories
    for (PsiDirectory iterator : sourceDirectory.getSubdirectories()) {
        if (iterator.getName().equals(namePackage))
            mPackage = iterator;
    }

    return mPackage;
}
 
Example 11
Source File: BlazePackage.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void processPackageFiles(Processor<PsiFile> processor, PsiDirectory directory) {
  processDirectory(processor, directory);
  for (PsiDirectory child : directory.getSubdirectories()) {
    if (!isBlazePackage(child)) {
      processPackageFiles(processor, directory);
    }
  }
}
 
Example 12
Source File: MultipleJavaClassesTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void addClassesInDirectory(
    WorkspaceFileFinder finder, PsiDirectory dir, Set<PsiClass> set, int currentDepth) {
  if (currentDepth > MAX_DEPTH_TO_SEARCH || !relevantDirectory(finder, dir)) {
    return;
  }
  PsiClass[] classes = JavaDirectoryService.getInstance().getClasses(dir);
  set.addAll(
      Arrays.stream(classes).filter(ProducerUtils::isTestClass).collect(toImmutableList()));
  for (PsiDirectory child : dir.getSubdirectories()) {
    addClassesInDirectory(finder, child, set, currentDepth + 1);
  }
}
 
Example 13
Source File: ModulesUtil.java    From svgtoandroid with MIT License 5 votes vote down vote up
public Set<String> getDrawableDirs(PsiDirectory resDir) {
    Set<String> dirs = new HashSet<String>();
    if (resDir != null) {
        PsiDirectory[] subdirs = resDir.getSubdirectories();
        for (PsiDirectory dir : subdirs) {
            if (dir.getName().contains("drawable")) {
                dirs.add(dir.getName());
            }
        }
    }
    return dirs;
}
 
Example 14
Source File: PrintManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void addToPsiFileList(PsiDirectory psiDirectory, List<Pair<PsiFile, Editor>> filesList, boolean isRecursive) {
  PsiFile[] files = psiDirectory.getFiles();
  for (PsiFile file : files) {
    filesList.add(Pair.create(file, PsiUtilBase.findEditor(file)));
  }
  if (isRecursive) {
    for (PsiDirectory directory : psiDirectory.getSubdirectories()) {
      if (!Project.DIRECTORY_STORE_FOLDER.equals(directory.getName())) {
        addToPsiFileList(directory, filesList, isRecursive);
      }
    }
  }
}
 
Example 15
Source File: PackageNodeUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Collection<AbstractTreeNode> createPackageViewChildrenOnFiles(@Nonnull List<VirtualFile> sourceRoots,
                                                                            @Nonnull Project project,
                                                                            @Nonnull ViewSettings settings,
                                                                            @Nullable Module module,
                                                                            final boolean inLibrary) {
  final PsiManager psiManager = PsiManager.getInstance(project);

  final List<AbstractTreeNode> children = new ArrayList<AbstractTreeNode>();
  final Set<PsiPackage> topLevelPackages = new HashSet<PsiPackage>();

  for (final VirtualFile root : sourceRoots) {
    final PsiDirectory directory = psiManager.findDirectory(root);
    if (directory == null) {
      continue;
    }
    final PsiPackage directoryPackage = PsiPackageManager.getInstance(project).findAnyPackage(directory);
    if (directoryPackage == null || isPackageDefault(directoryPackage)) {
      // add subpackages
      final PsiDirectory[] subdirectories = directory.getSubdirectories();
      for (PsiDirectory subdirectory : subdirectories) {
        final PsiPackage aPackage = PsiPackageManager.getInstance(project).findAnyPackage(subdirectory);
        if (aPackage != null && !isPackageDefault(aPackage)) {
          topLevelPackages.add(aPackage);
        }
      }
      // add non-dir items
      children.addAll(BaseProjectViewDirectoryHelper.getDirectoryChildren(directory, settings, false));
    }
    else {
      topLevelPackages.add(directoryPackage);
    }
  }

  for (final PsiPackage topLevelPackage : topLevelPackages) {
    addPackageAsChild(children, topLevelPackage, module, settings, inLibrary);
  }

  return children;
}