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

The following examples show how to use com.intellij.psi.PsiDirectory#isValid() . 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: FieldReferenceProviderImpl.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiFile loadInMemoryDescriptorProto() {
    if (inm == null) {
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
            VirtualFile[] allSourceRoots = moduleRootManager.orderEntries().getAllSourceRoots();
            for (VirtualFile allSourceRoot : allSourceRoots) {
                PsiDirectory directory = PsiManager.getInstance(project).findDirectory(allSourceRoot);
                if (directory != null && directory.isValid()) {
                    String relPath = "google/protobuf/descriptor.proto";
                    VirtualFile file = directory.getVirtualFile().findFileByRelativePath(relPath);
                    if (file != null) {
                        PsiManager psiManager = PsiManager.getInstance(project);
                        PsiFile psiFile = psiManager.findFile(file);
                        if (psiFile instanceof ProtoPsiFileRoot) {
                            inm = psiFile;
                            return (ProtoPsiFileRoot) psiFile;
                        }
                    }
                }
            }
        }
    }
    return inm;
}
 
Example 2
Source File: BlazePyOutsideModuleImportResolver.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private static PsiElement resolveModuleAt(
    @Nullable PsiDirectory directory,
    QualifiedName qualifiedName,
    PyQualifiedNameResolveContext context) {
  if (directory == null || !directory.isValid()) {
    return null;
  }
  PsiElement seeker = directory;
  for (String name : qualifiedName.getComponents()) {
    if (name == null) {
      return null;
    }
    seeker =
        ResolveImportUtil.resolveChild(
            seeker, name, context.getFootholdFile(), false, true, false);
  }
  return seeker;
}
 
Example 3
Source File: BlazePsiDirectoryNode.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isValid() {
  PsiDirectory psi = getValue();
  if (psi == null || !psi.isValid()) {
    return false;
  }
  return super.isValid() || (getSettings().isHideEmptyMiddlePackages() && isSourceRoot(psi));
}
 
Example 4
Source File: HaxePackageModel.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
protected HaxeFile getFile(String fileName) {
  PsiDirectory directory = root.access(path);
  if (directory != null && directory.isValid()) {
    PsiFile file = directory.findFile(fileName + ".hx");
    if (file != null && file.isValid() && file instanceof HaxeFile) {
      return (HaxeFile)file;
    }
  }

  return null;
}
 
Example 5
Source File: DirectoryChooserUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiDirectory selectDirectory(Project project,
                                           PsiDirectory[] packageDirectories,
                                           PsiDirectory defaultDirectory,
                                           String postfixToShow) {
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();

  ArrayList<PsiDirectory> possibleDirs = new ArrayList<PsiDirectory>();
  for (PsiDirectory dir : packageDirectories) {
    if (!dir.isValid()) continue;
    if (!dir.isWritable()) continue;
    if (possibleDirs.contains(dir)) continue;
    if (!projectFileIndex.isInContent(dir.getVirtualFile())) continue;
    possibleDirs.add(dir);
  }

  if (possibleDirs.isEmpty()) return null;
  if (possibleDirs.size() == 1) return possibleDirs.get(0);

  if (ApplicationManager.getApplication().isUnitTestMode()) return possibleDirs.get(0);

  DirectoryChooser chooser = new DirectoryChooser(project);
  chooser.setTitle(IdeBundle.message("title.choose.destination.directory"));
  chooser.fillList(possibleDirs.toArray(new PsiDirectory[possibleDirs.size()]), defaultDirectory, project, postfixToShow);
  chooser.show();
  return chooser.isOK() ? chooser.getSelectedDirectory() : null;
}
 
Example 6
Source File: PlatformPackageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiDirectory getWritableModuleDirectory(@Nonnull Query<VirtualFile> vFiles,
                                                       GlobalSearchScope scope,
                                                       PsiManager manager) {
  for (VirtualFile vFile : vFiles) {
    if (!scope.contains(vFile)) continue;
    PsiDirectory directory = manager.findDirectory(vFile);
    if (directory != null && directory.isValid() && directory.isWritable()) {
      return directory;
    }
  }
  return null;
}