@Override
  @Nullable
  public GlobalSearchScope getSearchScope() {
    GlobalSearchScope scope = null;

    Module[] modules = getConvertContextModules();
    if (modules.length != 0) {

      PsiFile file = getFile();
      file = file.getOriginalFile();
      VirtualFile virtualFile = file.getVirtualFile();
      if (virtualFile != null) {
        ProjectFileIndex fileIndex =
            ProjectRootManager.getInstance(file.getProject()).getFileIndex();
        boolean tests = fileIndex.isInTestSourceContent(virtualFile);

        for (Module module : modules) {
          if (scope == null) {
            scope = module.getModuleRuntimeScope(tests);
          } else {
            scope = scope.union(module.getModuleRuntimeScope(tests));
          }
        }
      }
    }
    return scope; // ??? scope == null ? GlobalSearchScope.allScope(getProject()) : scope; ???
  }
  public AddModuleDependencyFix(
      Module currentModule, VirtualFile classVFile, PsiClass[] classes, PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    final Project project = psiElement.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      final Module classModule = fileIndex.getModuleForFile(virtualFile);
      if (classModule != null
          && classModule != currentModule
          && !ModuleRootManager.getInstance(currentModule).isDependsOn(classModule)) {
        myModules.add(classModule);
      }
    }
    myCurrentModule = currentModule;
    myClassVFile = classVFile;
    myClasses = classes;
    myReference = reference;
  }
  private static THashSet<String> collectFoldersToExclude(
      final Module module, final VirtualFile pubspecYamlFile) {
    final THashSet<String> newExcludedPackagesUrls = new THashSet<String>();
    final ProjectFileIndex fileIndex =
        ProjectRootManager.getInstance(module.getProject()).getFileIndex();
    final VirtualFile root = pubspecYamlFile.getParent();

    final VirtualFile binFolder = root.findChild("bin");
    if (binFolder != null && binFolder.isDirectory() && fileIndex.isInContent(binFolder)) {
      newExcludedPackagesUrls.add(binFolder.getUrl() + "/packages");
    }

    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("benchmark"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("example"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("test"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("tool"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("web"), fileIndex);

    // Folder packages/ThisProject (where ThisProject is the name specified in pubspec.yaml) is a
    // symlink to local 'lib' folder. Exclude it in order not to have duplicates. Resolve goes to
    // local 'lib' folder.
    // Empty 'ThisProject (link to 'lib' folder)' node is added to Project Structure by
    // DartTreeStructureProvider
    final VirtualFile libFolder = root.findChild("lib");
    if (libFolder != null && libFolder.isDirectory()) {
      final String pubspecName = PubspecYamlUtil.getPubspecName(pubspecYamlFile);
      if (pubspecName != null) {
        newExcludedPackagesUrls.add(root.getUrl() + "/packages/" + pubspecName);
      }
    }

    return newExcludedPackagesUrls;
  }
  @Nullable
  @Override
  public PsiPackage resolvePackage(
      @NotNull PsiPackageManager packageManager,
      @NotNull VirtualFile virtualFile,
      @NotNull Class<? extends ModuleExtension> extensionClass,
      String qualifiedName) {
    ProjectFileIndex fileIndexFacade =
        ProjectFileIndex.SERVICE.getInstance(packageManager.getProject());
    PsiManager psiManager = PsiManager.getInstance(packageManager.getProject());
    if (fileIndexFacade.isInLibraryClasses(virtualFile)) {

      List<OrderEntry> orderEntriesForFile = fileIndexFacade.getOrderEntriesForFile(virtualFile);
      for (OrderEntry orderEntry : orderEntriesForFile) {
        Module ownerModule = orderEntry.getOwnerModule();
        ModuleExtension extension = ModuleUtilCore.getExtension(ownerModule, extensionClass);
        if (extension != null) {
          for (PsiPackageSupportProvider p : PsiPackageSupportProvider.EP_NAME.getExtensions()) {
            if (p.isSupported(extension)) {
              return p.createPackage(psiManager, packageManager, extensionClass, qualifiedName);
            }
          }
        }
      }
    }
    return null;
  }
示例#5
0
 private boolean fileInRoots(VirtualFile file) {
   final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
   return file != null
       && (index.isInSourceContent(file)
           || index.isInLibraryClasses(file)
           || index.isInLibrarySource(file));
 }
  @Nullable
  public static PsiDirectory selectDirectory(
      Project project,
      PsiDirectory[] packageDirectories,
      PsiDirectory defaultDirectory,
      String postfixToShow) {
    ProjectFileIndex projectFileIndex = getInstance(project).getFileIndex();

    ArrayList<PsiDirectory> possibleDirs = new ArrayList<>();
    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 (getApplication().isUnitTestMode()) return possibleDirs.get(0);

    DirectoryChooser chooser = new DirectoryChooser(project);
    chooser.setTitle(message("title.choose.destination.directory"));
    chooser.fillList(
        possibleDirs.toArray(new PsiDirectory[possibleDirs.size()]),
        defaultDirectory,
        project,
        postfixToShow);
    return chooser.showAndGet() ? chooser.getSelectedDirectory() : null;
  }
  @NotNull
  private static DartLibInfo collectPackagesLibraryRoots(
      @NotNull final Project project, @NotNull final DartSdk sdk) {
    final DartLibInfo libInfo = new DartLibInfo(false);

    final Collection<VirtualFile> pubspecYamlFiles =
        FilenameIndex.getVirtualFilesByName(
            project, PUBSPEC_YAML, GlobalSearchScope.projectScope(project));
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (VirtualFile pubspecFile : pubspecYamlFiles) {
      final VirtualFile dotPackagesFile =
          pubspecFile.getParent().findChild(DotPackagesFileUtil.DOT_PACKAGES);
      final Module module =
          dotPackagesFile == null ? null : fileIndex.getModuleForFile(dotPackagesFile);

      if (dotPackagesFile != null
          && !dotPackagesFile.isDirectory()
          && module != null
          && DartSdkGlobalLibUtil.isDartSdkEnabled(module)) {
        final Map<String, String> packagesMap = DotPackagesFileUtil.getPackagesMap(dotPackagesFile);
        if (packagesMap != null) {
          for (Map.Entry<String, String> entry : packagesMap.entrySet()) {
            final String packageName = entry.getKey();
            final String packagePath = entry.getValue();
            if (isPathOutsideProjectContent(fileIndex, packagePath)) {
              libInfo.addPackage(packageName, packagePath);
            }
          }
        }
      }
    }

    return libInfo;
  }
示例#8
0
  @Nullable
  private PsiDirectory chooseDirectory(final Project project, final PsiFile file) {
    PsiDirectory preferredDirectory =
        myWritableDirectoryList.isEmpty() ? null : myWritableDirectoryList.get(0);
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final VirtualFile virtualFile = file.getVirtualFile();
    assert virtualFile != null;
    final Module moduleForFile = fileIndex.getModuleForFile(virtualFile);
    if (myWritableDirectoryList.size() > 1
        && !ApplicationManager.getApplication().isUnitTestMode()) {
      if (moduleForFile != null) {
        for (PsiDirectory directory : myWritableDirectoryList) {
          if (fileIndex.getModuleForFile(directory.getVirtualFile()) == moduleForFile) {
            preferredDirectory = directory;
            break;
          }
        }
      }

      return DirectoryChooserUtil.chooseDirectory(
          myWritableDirectoryList.toArray(new PsiDirectory[myWritableDirectoryList.size()]),
          preferredDirectory,
          project,
          new HashMap<PsiDirectory, String>());
    }
    return preferredDirectory;
  }
 @Override
 public PsiPackage getPackage(@NotNull PsiDirectory dir) {
   ProjectFileIndex projectFileIndex =
       ProjectRootManager.getInstance(dir.getProject()).getFileIndex();
   String packageName = projectFileIndex.getPackageNameByDirectory(dir.getVirtualFile());
   if (packageName == null) return null;
   return JavaPsiFacade.getInstance(dir.getProject()).findPackage(packageName);
 }
示例#10
0
 public static boolean isTest(VirtualFile virtualFile, Project project) {
   if (virtualFile == null) {
     return false;
   }
   final ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
   final ProjectFileIndex fileIndex = rootManager.getFileIndex();
   return fileIndex.isInTestSourceContent(virtualFile);
 }
示例#11
0
  @Override
  public boolean contains(@NotNull VirtualFile file) {
    final ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
    if (!index.isInLibrarySource(file) && !index.isInLibraryClasses(file)) {
      return false;
    }

    return someChildContainsFile(file, false);
  }
  @Override
  public PsiFileSystemItem findRoot(final Project project, @NotNull final VirtualFile file) {
    final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
    VirtualFile contentRootForFile = index.getContentRootForFile(file);

    return contentRootForFile != null
        ? PsiManager.getInstance(project).findDirectory(contentRootForFile)
        : null;
  }
 public static boolean isRoot(PsiFileSystemItem directory) {
   if (directory == null) return true;
   VirtualFile vFile = directory.getVirtualFile();
   if (vFile == null) return true;
   ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(directory.getProject());
   return Comparing.equal(fileIndex.getClassRootForFile(vFile), vFile)
       || Comparing.equal(fileIndex.getContentRootForFile(vFile), vFile)
       || Comparing.equal(fileIndex.getSourceRootForFile(vFile), vFile);
 }
  @Override
  public Module getModule() {
    if (!isValid()) return null;
    VirtualFile virtualFile = getFile();
    if (virtualFile == null) return null;

    ProjectRootManager projectRootManager = ProjectRootManager.getInstance(getProject());
    ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
    return fileIndex.getModuleForFile(virtualFile);
  }
  public void markModuleDirtyIfInSourceRoot(final VirtualFile file) {
    if (myCache.isEmpty()) return;

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    final Module module = fileIndex.getModuleForFile(file);
    if (module != null
        && fileIndex.getSourceRootForFile(file) != null
        && !fileIndex.isInTestSourceContent(file)) {
      markModuleDirty(module);
    }
  }
 public boolean isContentOrSourceRoot() {
   if (myVDirectory != null) {
     final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
     final VirtualFile contentRoot = fileIndex.getContentRootForFile(myVDirectory);
     final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(myVDirectory);
     if (myVDirectory.equals(contentRoot) || myVDirectory.equals(sourceRoot)) {
       return true;
     }
   }
   return false;
 }
  public static void removeDuplicatingClasses(
      final Module module,
      @NotNull final String packageName,
      @NotNull String className,
      @Nullable File classFile,
      String sourceRootPath) {
    if (sourceRootPath == null) {
      return;
    }
    VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByPath(sourceRootPath);
    if (sourceRoot == null) {
      return;
    }
    final Project project = module.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final String interfaceQualifiedName = packageName + '.' + className;
    PsiClass[] classes =
        facade.findClasses(interfaceQualifiedName, GlobalSearchScope.moduleScope(module));
    final ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiClass c : classes) {
      PsiFile psiFile = c.getContainingFile();
      if (className.equals(FileUtil.getNameWithoutExtension(psiFile.getName()))) {
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile != null
            && projectFileIndex.getSourceRootForFile(virtualFile) == sourceRoot) {
          final String path = virtualFile.getPath();
          File f = new File(path);

          try {
            f = f.getCanonicalFile();
            classFile = classFile != null ? classFile.getCanonicalFile() : null;
            if (f != null && !f.equals(classFile) && f.exists()) {
              if (f.delete()) {
                virtualFile.refresh(true, false);
              } else {
                ApplicationManager.getApplication()
                    .invokeLater(
                        new Runnable() {
                          public void run() {
                            Messages.showErrorDialog(
                                project, "Can't delete file " + path, CommonBundle.getErrorTitle());
                          }
                        },
                        project.getDisposed());
              }
            }
          } catch (IOException e) {
            LOG.info(e);
          }
        }
      }
    }
  }
示例#18
0
 @Override
 public boolean isInTestSourceContent(@NotNull final VirtualFile fileOrDir) {
   if (myProjectFileIndex.isInTestSourceContent(fileOrDir)
       || myProjectFileIndex.isInTestResource(fileOrDir)) {
     return true;
   }
   if (isUnderRoots(myGeneratedTestRoots, fileOrDir)) {
     return true;
   }
   return false;
 }
示例#19
0
 @Override
 public boolean isInSourceContent(@NotNull final VirtualFile fileOrDir) {
   if (myProjectFileIndex.isInSourceContent(fileOrDir)
       || myProjectFileIndex.isInResource(fileOrDir)) {
     return true;
   }
   if (isUnderRoots(myRootToModuleMap.keySet(), fileOrDir)) {
     return true;
   }
   return false;
 }
 public String getFQName() {
   final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
   VirtualFile directory = myVDirectory;
   VirtualFile contentRoot = index.getContentRootForFile(directory);
   if (Comparing.equal(directory, contentRoot)) {
     return "";
   }
   if (contentRoot == null) {
     return "";
   }
   return VfsUtilCore.getRelativePath(directory, contentRoot, '/');
 }
示例#21
0
  public static boolean isInsideProjectSources(AnActionEvent event) {
    ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(event.getProject()).getFileIndex();
    final IdeView view = event.getData(DataKeys.IDE_VIEW);

    for (PsiDirectory dir : view.getDirectories()) {
      if (projectFileIndex.isInSourceContent(dir.getVirtualFile())
          && JavaDirectoryService.getInstance().getPackage(dir) != null) {
        return true;
      }
    }
    return false;
  }
示例#22
0
 private boolean needTransformCopying(CompileScope compileScope) {
   final CompilerConfiguration configuration = CompilerConfiguration.getInstance(myProject);
   final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
   for (VirtualFile file :
       FilenameIndex.getVirtualFilesByName(
           myProject, AST_TRANSFORM_FILE_NAME, GlobalSearchScope.projectScope(myProject))) {
     if (compileScope.belongs(file.getUrl())
         && index.isInSource(file)
         && !configuration.isResourceFile(file)) {
       return true;
     }
   }
   return false;
 }
 private static PsiDirectory[] filterSourceDirectories(
     PsiDirectory baseDir, Project project, PsiDirectory[] moduleDirectories) {
   final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
   if (fileIndex.isInTestSourceContent(baseDir.getVirtualFile())) {
     List<PsiDirectory> result = new ArrayList<PsiDirectory>();
     for (PsiDirectory moduleDirectory : moduleDirectories) {
       if (fileIndex.isInTestSourceContent(moduleDirectory.getVirtualFile())) {
         result.add(moduleDirectory);
       }
     }
     moduleDirectories = result.toArray(new PsiDirectory[result.size()]);
   }
   return moduleDirectories;
 }
 protected boolean isTestScope(@Nullable final PsiFile context) {
   if ((context == null)) {
     return false;
   }
   PsiFile _originalFile = context.getOriginalFile();
   VirtualFile virtualFile = _originalFile.getVirtualFile();
   if ((virtualFile != null)) {
     Project _project = context.getProject();
     ProjectRootManager _instance = ProjectRootManager.getInstance(_project);
     ProjectFileIndex _fileIndex = _instance.getFileIndex();
     return _fileIndex.isInTestSourceContent(virtualFile);
   }
   return false;
 }
 private void collectFiles(final Set<VirtualFile> srcSet, final BitSet processed) {
   final ProjectFileIndex projectIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
   projectIndex.iterateContent(
       new ContentIterator() {
         @Override
         public boolean processFile(VirtualFile fileOrDir) {
           int fileId = ((VirtualFileWithId) fileOrDir).getId();
           if (!processed.get(fileId) && projectIndex.isInSourceContent(fileOrDir)) {
             srcSet.add(fileOrDir);
           }
           return true;
         }
       });
 }
示例#26
0
  public static String getRootDirectoryForPackage(PsiDirectory directory) {
    PsiManager manager = directory.getManager();
    final VirtualFile virtualFile = directory.getVirtualFile();
    final ProjectFileIndex fileIndex =
        ProjectRootManager.getInstance(manager.getProject()).getFileIndex();
    VirtualFile root = fileIndex.getSourceRootForFile(virtualFile);

    if (root == null) {
      root = fileIndex.getClassRootForFile(virtualFile);
    }
    if (root != null) {
      return root.getPresentableUrl();
    }
    return null;
  }
 int getRelevance() {
   final Project project = myImportable.getProject();
   final PsiFile psiFile = myImportable.getContainingFile();
   final VirtualFile vFile = psiFile == null ? null : psiFile.getVirtualFile();
   if (vFile == null) return 0;
   final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
   // files under project source are most relevant
   final Module module = fileIndex.getModuleForFile(vFile);
   if (module != null) return 3;
   // then come files directly under Lib
   if (vFile.getParent().getName().equals("Lib")) return 2;
   // tests we don't want
   if (vFile.getParent().getName().equals("test")) return 0;
   return 1;
 }
  public static boolean isInDartSdkOrDartPackagesFolder(
      final Project project, final VirtualFile file) {
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    if (fileIndex.isInLibraryClasses(file)) {
      return true; // file in SDK or in custom package root
    }

    if (fileIndex.isInContent(file) && isInDartPackagesFolder(fileIndex, file)) {
      return true; // symlinked child of 'packages' folder. Real location is in user cache folder
      // for Dart packages, not in project
    }

    return false;
  }
示例#29
0
  @Nullable
  public static Module findContainingMavenizedModule(@NotNull PsiFile psiFile) {
    VirtualFile file = psiFile.getVirtualFile();
    if (file == null) return null;

    Project project = psiFile.getProject();

    MavenProjectsManager manager = MavenProjectsManager.getInstance(project);
    if (!manager.isMavenizedProject()) return null;

    ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();

    Module module = index.getModuleForFile(file);
    if (module == null || !manager.isMavenizedModule(module)) return null;
    return module;
  }
示例#30
0
  @Nullable
  public VirtualFile findAppDirectory(@Nullable PsiElement element) {
    if (element == null) return null;

    PsiFile containingFile = element.getContainingFile().getOriginalFile();
    VirtualFile file = containingFile.getVirtualFile();
    if (file == null) return null;

    ProjectFileIndex index =
        ProjectRootManager.getInstance(containingFile.getProject()).getFileIndex();

    VirtualFile root = index.getContentRootForFile(file);
    if (root == null) return null;

    return root.findChild(getApplicationDirectoryName());
  }