Module (PsiDirectory dir) { Project project = dir.getProject(); ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); VirtualFile vFile = dir.getVirtualFile(); if (fileIndex.isInLibrary(vFile)) { List<OrderEntry> orderEntries = fileIndex.getOrderEntriesForFile(vFile); if (orderEntries.isEmpty()) { return null; } Set<Module> modules = new HashSet<>(); for (OrderEntry orderEntry : orderEntries) { modules.add(orderEntry.getOwnerModule()); } Module[] candidates = modules.toArray(Module.EMPTY_ARRAY); Arrays.sort(candidates, ModuleManager.getInstance(project).moduleDependencyComparator()); return candidates[0]; } return fileIndex.getModuleForFile(vFile); }