Search in sources :

Example 1 with PsiJavaModuleReference

use of com.intellij.psi.impl.source.PsiJavaModuleReference in project intellij-community by JetBrains.

the class OrderEntryFix method registerFixes.

@Nullable
public static List<LocalQuickFix> registerFixes(@NotNull QuickFixActionRegistrar registrar, @NotNull PsiReference reference) {
    PsiElement psiElement = reference.getElement();
    String shortReferenceName = reference.getRangeInElement().substring(psiElement.getText());
    Project project = psiElement.getProject();
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null)
        return null;
    VirtualFile refVFile = containingFile.getVirtualFile();
    if (refVFile == null)
        return null;
    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    Module currentModule = fileIndex.getModuleForFile(refVFile);
    if (currentModule == null)
        return null;
    if (reference instanceof PsiJavaModuleReference) {
        List<LocalQuickFix> result = ContainerUtil.newSmartList();
        createModuleFixes((PsiJavaModuleReference) reference, currentModule, refVFile, result);
        result.forEach(fix -> registrar.register((IntentionAction) fix));
        return result;
    }
    List<LocalQuickFix> result = ContainerUtil.newSmartList();
    JavaPsiFacade facade = JavaPsiFacade.getInstance(psiElement.getProject());
    registerExternalFixes(registrar, reference, psiElement, shortReferenceName, facade, currentModule, result);
    if (!result.isEmpty()) {
        return result;
    }
    PsiClass[] classes = PsiShortNamesCache.getInstance(project).getClassesByName(shortReferenceName, GlobalSearchScope.allScope(project));
    List<PsiClass> allowedDependencies = filterAllowedDependencies(psiElement, classes);
    if (allowedDependencies.isEmpty()) {
        return result;
    }
    OrderEntryFix moduleDependencyFix = new AddModuleDependencyFix(currentModule, refVFile, allowedDependencies, reference);
    registrar.register(moduleDependencyFix);
    result.add(moduleDependencyFix);
    Set<Object> librariesToAdd = new THashSet<>();
    ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(currentModule).getFileIndex();
    for (PsiClass aClass : allowedDependencies) {
        if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass))
            continue;
        PsiFile psiFile = aClass.getContainingFile();
        if (psiFile == null)
            continue;
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile == null)
            continue;
        for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) {
            if (orderEntry instanceof LibraryOrderEntry) {
                final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) orderEntry;
                final Library library = libraryEntry.getLibrary();
                if (library == null)
                    continue;
                VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
                if (files.length == 0)
                    continue;
                final VirtualFile jar = files[0];
                if (jar == null || libraryEntry.isModuleLevel() && !librariesToAdd.add(jar) || !librariesToAdd.add(library))
                    continue;
                OrderEntry entryForFile = moduleFileIndex.getOrderEntryForFile(virtualFile);
                if (entryForFile != null && !(entryForFile instanceof ExportableOrderEntry && ((ExportableOrderEntry) entryForFile).getScope() == DependencyScope.TEST && !moduleFileIndex.isInTestSourceContent(refVFile))) {
                    continue;
                }
                OrderEntryFix platformFix = new AddLibraryToDependenciesFix(currentModule, library, reference, aClass.getQualifiedName());
                registrar.register(platformFix);
                result.add(platformFix);
            }
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) PsiJavaModuleReference(com.intellij.psi.impl.source.PsiJavaModuleReference) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 Library (com.intellij.openapi.roots.libraries.Library)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiJavaModuleReference (com.intellij.psi.impl.source.PsiJavaModuleReference)1 THashSet (gnu.trove.THashSet)1 Nullable (org.jetbrains.annotations.Nullable)1