Search in sources :

Example 1 with ModuleFileIndex

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

the class GroovyAntCustomCompilerProvider method hasCustomCompile.

/**
   * {@inheritDoc}
   */
@Override
public boolean hasCustomCompile(ModuleChunk chunk) {
    for (Module m : chunk.getModules()) {
        if (LibrariesUtil.hasGroovySdk(m)) {
            final Set<String> scriptExtensions = GroovyFileTypeLoader.getCustomGroovyScriptExtensions();
            final ContentIterator groovyFileSearcher = new ContentIterator() {

                @Override
                public boolean processFile(VirtualFile fileOrDir) {
                    ProgressManager.checkCanceled();
                    if (isCompilableGroovyFile(fileOrDir, scriptExtensions)) {
                        return false;
                    }
                    return true;
                }
            };
            final ModuleRootManager rootManager = ModuleRootManager.getInstance(m);
            final ModuleFileIndex fileIndex = rootManager.getFileIndex();
            for (VirtualFile file : rootManager.getSourceRoots(JavaModuleSourceRootTypes.SOURCES)) {
                if (!fileIndex.iterateContentUnderDirectory(file, groovyFileSearcher)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentIterator(com.intellij.openapi.roots.ContentIterator) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ModuleFileIndex(com.intellij.openapi.roots.ModuleFileIndex) Module(com.intellij.openapi.module.Module)

Example 2 with ModuleFileIndex

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

the class HighlightClassUtil method checkDuplicateTopLevelClass.

@Nullable
static HighlightInfo checkDuplicateTopLevelClass(PsiClass aClass) {
    if (!(aClass.getParent() instanceof PsiFile))
        return null;
    String qualifiedName = aClass.getQualifiedName();
    if (qualifiedName == null)
        return null;
    int numOfClassesToFind = 2;
    if (qualifiedName.contains("$")) {
        qualifiedName = qualifiedName.replaceAll("\\$", ".");
        numOfClassesToFind = 1;
    }
    PsiManager manager = aClass.getManager();
    Module module = ModuleUtilCore.findModuleForPsiElement(aClass);
    if (module == null)
        return null;
    PsiClass[] classes = JavaPsiFacade.getInstance(aClass.getProject()).findClasses(qualifiedName, GlobalSearchScope.moduleScope(module).intersectWith(aClass.getResolveScope()));
    if (classes.length < numOfClassesToFind)
        return null;
    final ModuleFileIndex fileIndex = ModuleRootManager.getInstance(module).getFileIndex();
    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(aClass);
    if (virtualFile == null)
        return null;
    final boolean isTestSourceRoot = fileIndex.isInTestSourceContent(virtualFile);
    String dupFileName = null;
    for (PsiClass dupClass : classes) {
        // do not use equals
        if (dupClass != aClass) {
            VirtualFile file = dupClass.getContainingFile().getVirtualFile();
            if (file != null && manager.isInProject(dupClass) && fileIndex.isInTestSourceContent(file) == isTestSourceRoot) {
                dupFileName = FileUtil.toSystemDependentName(file.getPath());
                break;
            }
        }
    }
    if (dupFileName == null)
        return null;
    String message = JavaErrorMessages.message("duplicate.class.in.other.file", dupFileName);
    TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
    return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(message).create();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextRange(com.intellij.openapi.util.TextRange) ModuleFileIndex(com.intellij.openapi.roots.ModuleFileIndex) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ModuleFileIndex

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

the class AbstractFileProcessor method findFiles.

protected static void findFiles(final Module module, final List<PsiFile> files) {
    final ModuleFileIndex idx = ModuleRootManager.getInstance(module).getFileIndex();
    final VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
    for (final VirtualFile root : roots) {
        ApplicationManager.getApplication().runReadAction(() -> {
            idx.iterateContentUnderDirectory(root, new ContentIterator() {

                @Override
                public boolean processFile(final VirtualFile dir) {
                    if (dir.isDirectory()) {
                        final PsiDirectory psiDir = PsiManager.getInstance(module.getProject()).findDirectory(dir);
                        if (psiDir != null) {
                            findFiles(files, psiDir, false);
                        }
                    }
                    return true;
                }
            });
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ContentIterator(com.intellij.openapi.roots.ContentIterator) PsiDirectory(com.intellij.psi.PsiDirectory) ModuleFileIndex(com.intellij.openapi.roots.ModuleFileIndex)

Example 4 with ModuleFileIndex

use of com.intellij.openapi.roots.ModuleFileIndex in project intellij-plugins by JetBrains.

the class InjectionUtil method resolveResult.

@Nullable
private static PsiFileSystemItem resolveResult(PsiElement element, ResolveResult[] resolveResults) {
    InjectedLanguageManager manager = InjectedLanguageManager.getInstance(element.getProject());
    final PsiFile currentTopLevelFile = manager.getTopLevelFile(element);
    // find equal files
    for (ResolveResult resolveResult : resolveResults) {
        PsiElement resolvedElement = resolveResult.getElement();
        assert resolvedElement != null;
        if (manager.getTopLevelFile(resolvedElement).equals(currentTopLevelFile)) {
            return (PsiFileSystemItem) resolvedElement;
        }
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module != null) {
        final ModuleFileIndex fileIndex = ModuleRootManager.getInstance(module).getFileIndex();
        // return if is local file
        for (ResolveResult resolveResult : resolveResults) {
            PsiFileSystemItem resolvedElement = (PsiFileSystemItem) resolveResult.getElement();
            assert resolvedElement != null;
            VirtualFile virtualFile = resolvedElement.getVirtualFile();
            if (virtualFile != null && fileIndex.isInSourceContent(virtualFile)) {
                return resolvedElement;
            }
        }
    }
    return (PsiFileSystemItem) resolveResults[0].getElement();
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleFileIndex(com.intellij.openapi.roots.ModuleFileIndex) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ModuleFileIndex (com.intellij.openapi.roots.ModuleFileIndex)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Module (com.intellij.openapi.module.Module)3 ContentIterator (com.intellij.openapi.roots.ContentIterator)2 Nullable (org.jetbrains.annotations.Nullable)2 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiDirectory (com.intellij.psi.PsiDirectory)1