@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; }
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; }
@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; }
@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); } }
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; }
@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; }
@Override public Module getModuleByFile(VirtualFile file) { final Module module = myProjectFileIndex.getModuleForFile(file); if (module != null) { LOG.assertTrue(!module.isDisposed()); return module; } for (final VirtualFile root : myRootToModuleMap.keySet()) { if (VfsUtil.isAncestor(root, file, false)) { final Module mod = myRootToModuleMap.get(root); if (mod != null) { LOG.assertTrue(!mod.isDisposed()); } return mod; } } return null; }
@Nullable public BundleManifest getManifest(@NotNull PsiFileSystemItem item) { VirtualFile file = item.getVirtualFile(); if (file != null) { List<OrderEntry> entries = myIndex.getOrderEntriesForFile(file); if (entries.size() == 1 && entries.get(0) instanceof JdkOrderEntry) { return new JdkBundleManifest(); } Module module = myIndex.getModuleForFile(file); if (module != null) { return getManifest(module); } VirtualFile libRoot = myIndex.getClassRootForFile(file); if (libRoot != null) { return getManifest(libRoot); } } return null; }
public static void customizeElementLabel(final PsiElement element, final JLabel label) { if (element != null) { PsiFile file = element.getContainingFile(); VirtualFile vfile = file == null ? null : file.getVirtualFile(); if (vfile == null) { label.setText(""); label.setIcon(null); return; } final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex(); final Module module = fileIndex.getModuleForFile(vfile); if (module != null) { label.setText(module.getName()); label.setIcon(AllIcons.Nodes.Module); } else { final List<OrderEntry> entries = fileIndex.getOrderEntriesForFile(vfile); OrderEntry entry = null; for (OrderEntry order : entries) { if (order instanceof LibraryOrderEntry || order instanceof SdkOrderEntry) { entry = order; break; } } if (entry != null) { label.setText(entry.getPresentableName()); label.setIcon(AllIcons.Nodes.PpLibFolder); } } } }
private boolean isInScopeOf(DebugProcessImpl debugProcess, String className) { final SourcePosition position = getSourcePosition(); if (position != null) { final VirtualFile breakpointFile = position.getFile().getVirtualFile(); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex(); if (breakpointFile != null && fileIndex.isUnderSourceRootOfType(breakpointFile, JavaModuleSourceRootTypes.SOURCES)) { if (debugProcess.getSearchScope().contains(breakpointFile)) { return true; } // apply filtering to breakpoints from content sources only, not for sources attached to // libraries final Collection<VirtualFile> candidates = findClassCandidatesInSourceContent(className, debugProcess.getSearchScope(), fileIndex); if (LOG.isDebugEnabled()) { LOG.debug( "Found " + (candidates == null ? "null" : candidates.size()) + " candidate containing files for class " + className); } if (candidates == null) { // If no candidates are found in scope then assume that class is loaded dynamically and // allow breakpoint return true; } // breakpointFile is not in scope here and there are some candidates in scope // for (VirtualFile classFile : candidates) { // if (LOG.isDebugEnabled()) { // LOG.debug("Breakpoint file: " + breakpointFile.getPath()+ "; candidate file: " + // classFile.getPath()); // } // if (breakpointFile.equals(classFile)) { // return true; // } // } if (LOG.isDebugEnabled()) { final GlobalSearchScope scope = debugProcess.getSearchScope(); final boolean contains = scope.contains(breakpointFile); final Project project = getProject(); final List<VirtualFile> files = ContainerUtil.map( JavaFullClassNameIndex.getInstance().get(className.hashCode(), project, scope), new Function<PsiClass, VirtualFile>() { @Override public VirtualFile fun(PsiClass aClass) { return aClass.getContainingFile().getVirtualFile(); } }); final List<VirtualFile> allFiles = ContainerUtil.map( JavaFullClassNameIndex.getInstance() .get(className.hashCode(), project, new EverythingGlobalScope(project)), new Function<PsiClass, VirtualFile>() { @Override public VirtualFile fun(PsiClass aClass) { return aClass.getContainingFile().getVirtualFile(); } }); final VirtualFile contentRoot = fileIndex.getContentRootForFile(breakpointFile); final Module module = fileIndex.getModuleForFile(breakpointFile); LOG.debug( "Did not find '" + className + "' in " + scope + "; contains=" + contains + "; contentRoot=" + contentRoot + "; module = " + module + "; all files in index are: " + files + "; all possible files are: " + allFiles); } return false; } } return true; }
@Nullable private static Module getModule(PsiElement element) { ProjectFileIndex index = ProjectRootManager.getInstance(element.getProject()).getFileIndex(); return index.getModuleForFile(element.getContainingFile().getVirtualFile()); }
@Nullable public static List<LocalQuickFix> registerFixes( @NotNull final QuickFixActionRegistrar registrar, @NotNull final PsiReference reference) { final PsiElement psiElement = reference.getElement(); @NonNls final String shortReferenceName = reference.getRangeInElement().substring(psiElement.getText()); Project project = psiElement.getProject(); PsiFile containingFile = psiElement.getContainingFile(); if (containingFile == null) return null; final VirtualFile classVFile = containingFile.getVirtualFile(); if (classVFile == null) return null; final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); final Module currentModule = fileIndex.getModuleForFile(classVFile); if (currentModule == null) return null; final List<LocalQuickFix> providedFixes = findFixes( new Function<MissingDependencyFixProvider, List<LocalQuickFix>>() { @Override public List<LocalQuickFix> fun(MissingDependencyFixProvider provider) { return provider.registerFixes(registrar, reference); } }); if (providedFixes != null) { return providedFixes; } List<LocalQuickFix> result = new ArrayList<LocalQuickFix>(); JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); String fullReferenceText = reference.getCanonicalText(); for (ExternalLibraryResolver resolver : ExternalLibraryResolver.EP_NAME.getExtensions()) { final ExternalClassResolveResult resolveResult = resolver.resolveClass(shortReferenceName, isReferenceToAnnotation(psiElement)); OrderEntryFix fix = null; if (resolveResult != null && psiFacade.findClass( resolveResult.getQualifiedClassName(), currentModule.getModuleWithDependenciesAndLibrariesScope(true)) == null) { fix = new AddExternalLibraryToDependenciesQuickFix( currentModule, resolveResult.getLibrary(), reference, resolveResult.getQualifiedClassName()); } else if (!fullReferenceText.equals(shortReferenceName)) { ExternalLibraryDescriptor descriptor = resolver.resolvePackage(fullReferenceText); if (descriptor != null) { fix = new AddExternalLibraryToDependenciesQuickFix( currentModule, descriptor, reference, null); } } if (fix != null) { registrar.register(fix); result.add(fix); } } if (!result.isEmpty()) { return result; } Set<Object> librariesToAdd = new THashSet<Object>(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(psiElement.getProject()); PsiClass[] classes = PsiShortNamesCache.getInstance(project) .getClassesByName(shortReferenceName, GlobalSearchScope.allScope(project)); List<PsiClass> allowedDependencies = filterAllowedDependencies(psiElement, classes); if (allowedDependencies.isEmpty()) { return result; } classes = allowedDependencies.toArray(new PsiClass[allowedDependencies.size()]); OrderEntryFix moduleDependencyFix = new AddModuleDependencyFix(currentModule, classVFile, classes, reference); final PsiClass[] finalClasses = classes; final OrderEntryFix finalModuleDependencyFix = moduleDependencyFix; final OrderEntryFix providedModuleDependencyFix = provideFix( new Function<MissingDependencyFixProvider, OrderEntryFix>() { @Override public OrderEntryFix fun(MissingDependencyFixProvider provider) { return provider.getAddModuleDependencyFix( reference, finalModuleDependencyFix, currentModule, classVFile, finalClasses); } }); moduleDependencyFix = ObjectUtils.notNull(providedModuleDependencyFix, moduleDependencyFix); registrar.register(moduleDependencyFix); result.add(moduleDependencyFix); for (final 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; ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(currentModule).getFileIndex(); 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 && !ModuleRootManager.getInstance(currentModule) .getFileIndex() .isInTestSourceContent(classVFile))) { continue; } final OrderEntryFix platformFix = new OrderEntryFix() { @Override @NotNull public String getText() { return QuickFixBundle.message( "orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName()); } @Override @NotNull public String getFamilyName() { return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath"); } @Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { return !project.isDisposed() && !currentModule.isDisposed() && libraryEntry.isValid(); } @Override public void invoke( @NotNull final Project project, @Nullable final Editor editor, PsiFile file) { OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule); if (editor != null) { DumbService.getInstance(project) .withAlternativeResolveEnabled( new Runnable() { @Override public void run() { new AddImportAction(project, reference, editor, aClass).execute(); } }); } } }; final OrderEntryFix providedFix = provideFix( new Function<MissingDependencyFixProvider, OrderEntryFix>() { @Override public OrderEntryFix fun(MissingDependencyFixProvider provider) { return provider.getAddLibraryToClasspathFix( reference, platformFix, currentModule, libraryEntry, aClass); } }); final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix); registrar.register(fix); result.add(fix); } } } return result; }