Search in sources :

Example 96 with ProjectFileIndex

use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.

the class PsiJavaDirectoryFactory method isPackage.

@Override
public boolean isPackage(@NotNull PsiDirectory directory) {
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myManager.getProject()).getFileIndex();
    VirtualFile virtualFile = directory.getVirtualFile();
    return fileIndex.isUnderSourceRootOfType(virtualFile, JavaModuleSourceRootTypes.SOURCES) && fileIndex.getPackageNameByDirectory(virtualFile) != null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex)

Example 97 with ProjectFileIndex

use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.

the class CreateTemplateInPackageAction method isAvailable.

@Override
protected boolean isAvailable(final DataContext dataContext) {
    final Project project = CommonDataKeys.PROJECT.getData(dataContext);
    final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (project == null || view == null || view.getDirectories().length == 0) {
        return false;
    }
    if (mySourceRootTypes == null) {
        return true;
    }
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiDirectory dir : view.getDirectories()) {
        if (projectFileIndex.isUnderSourceRootOfType(dir.getVirtualFile(), mySourceRootTypes) && checkPackageExists(dir)) {
            return true;
        }
    }
    return false;
}
Also used : Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiDirectory(com.intellij.psi.PsiDirectory) IdeView(com.intellij.ide.IdeView)

Example 98 with ProjectFileIndex

use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.

the class CopyReferenceAction method getVirtualFileFqn.

private static String getVirtualFileFqn(@NotNull VirtualFile virtualFile, @NotNull Project project) {
    final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(virtualFile);
    VirtualFile logicalRootFile = logicalRoot != null ? logicalRoot.getVirtualFile() : null;
    if (logicalRootFile != null && !virtualFile.equals(logicalRootFile)) {
        return ObjectUtils.assertNotNull(VfsUtilCore.getRelativePath(virtualFile, logicalRootFile, '/'));
    }
    VirtualFile outerMostRoot = null;
    VirtualFile each = virtualFile;
    ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
    while (each != null && (each = index.getContentRootForFile(each, false)) != null) {
        outerMostRoot = each;
        each = each.getParent();
    }
    if (outerMostRoot != null && !outerMostRoot.equals(virtualFile)) {
        String relative = VfsUtilCore.getRelativePath(virtualFile, outerMostRoot, '/');
        if (relative != null) {
            return relative;
        }
    }
    return virtualFile.getPath();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LogicalRoot(com.intellij.util.LogicalRoot) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex)

Example 99 with ProjectFileIndex

use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.

the class JavaAnalysisScope method getNarrowedComplementaryScope.

@Override
@NotNull
public AnalysisScope getNarrowedComplementaryScope(@NotNull Project defaultProject) {
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(defaultProject).getFileIndex();
    if (myType == FILE) {
        if (myElement instanceof PsiJavaFile && !FileTypeUtils.isInServerPageFile(myElement)) {
            PsiJavaFile psiJavaFile = (PsiJavaFile) myElement;
            final PsiClass[] classes = psiJavaFile.getClasses();
            boolean onlyPackLocalClasses = true;
            for (final PsiClass aClass : classes) {
                if (aClass.hasModifierProperty(PsiModifier.PUBLIC)) {
                    onlyPackLocalClasses = false;
                }
            }
            if (onlyPackLocalClasses) {
                final PsiDirectory psiDirectory = psiJavaFile.getContainingDirectory();
                if (psiDirectory != null) {
                    return new JavaAnalysisScope(JavaDirectoryService.getInstance().getPackage(psiDirectory), null);
                }
            }
        }
    } else if (myType == PACKAGE) {
        final PsiDirectory[] directories = ((PsiPackage) myElement).getDirectories();
        final HashSet<Module> modules = new HashSet<>();
        for (PsiDirectory directory : directories) {
            modules.addAll(getAllInterestingModules(fileIndex, directory.getVirtualFile()));
        }
        return collectScopes(defaultProject, modules);
    }
    return super.getNarrowedComplementaryScope(defaultProject);
}
Also used : ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 100 with ProjectFileIndex

use of com.intellij.openapi.roots.ProjectFileIndex in project intellij-community by JetBrains.

the class MoveClassesOrPackagesUtil method chooseDestinationPackage.

@Nullable
public static PsiDirectory chooseDestinationPackage(Project project, String packageName, @Nullable PsiDirectory baseDir) {
    final PsiManager psiManager = PsiManager.getInstance(project);
    final PackageWrapper packageWrapper = new PackageWrapper(psiManager, packageName);
    final PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage(packageName);
    PsiDirectory directory;
    PsiDirectory[] directories = aPackage != null ? aPackage.getDirectories() : null;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final VirtualFile baseDirVirtualFile = baseDir != null ? baseDir.getVirtualFile() : null;
    final boolean isBaseDirInTestSources = baseDirVirtualFile != null && fileIndex.isInTestSourceContent(baseDirVirtualFile);
    if (directories != null && directories.length == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(directories[0].getVirtualFile()) == isBaseDirInTestSources)) {
        directory = directories[0];
    } else {
        final List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
        if (contentSourceRoots.size() == 1 && (baseDirVirtualFile == null || fileIndex.isInTestSourceContent(contentSourceRoots.get(0)) == isBaseDirInTestSources)) {
            directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {

                @Override
                public PsiDirectory compute() {
                    return RefactoringUtil.createPackageDirectoryInSourceRoot(packageWrapper, contentSourceRoots.get(0));
                }
            });
        } else {
            final VirtualFile sourceRootForFile = chooseSourceRoot(packageWrapper, contentSourceRoots, baseDir);
            if (sourceRootForFile == null)
                return null;
            directory = ApplicationManager.getApplication().runWriteAction(new Computable<PsiDirectory>() {

                @Override
                public PsiDirectory compute() {
                    return new AutocreatingSingleSourceRootMoveDestination(packageWrapper, sourceRootForFile).getTargetDirectory((PsiDirectory) null);
                }
            });
        }
    }
    return directory;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PackageWrapper(com.intellij.refactoring.PackageWrapper) Computable(com.intellij.openapi.util.Computable) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)157 VirtualFile (com.intellij.openapi.vfs.VirtualFile)119 Module (com.intellij.openapi.module.Module)52 Project (com.intellij.openapi.project.Project)49 Nullable (org.jetbrains.annotations.Nullable)37 NotNull (org.jetbrains.annotations.NotNull)24 PsiDirectory (com.intellij.psi.PsiDirectory)23 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)16 PsiFile (com.intellij.psi.PsiFile)14 ArrayList (java.util.ArrayList)13 IdeView (com.intellij.ide.IdeView)10 OrderEntry (com.intellij.openapi.roots.OrderEntry)9 THashSet (gnu.trove.THashSet)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)5 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)5 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)4 File (java.io.File)4 List (java.util.List)4 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)3 Presentation (com.intellij.openapi.actionSystem.Presentation)3