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;
}
Aggregations