@Nullable private static VirtualFile getVcsRootForLibraryFile( @NotNull Project project, @NotNull VirtualFile file) { ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project); // for a file inside .jar/.zip consider the .jar/.zip file itself VirtualFile root = vcsManager.getVcsRootFor(VfsUtilCore.getVirtualFileForJar(file)); if (root != null) { LOGGER.debug("Found root for zip/jar file: " + root); return root; } // for other libs which don't have jars inside the project dir (such as JDK) take the owner // module of the lib List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(file); Set<VirtualFile> libraryRoots = new HashSet<VirtualFile>(); for (OrderEntry entry : entries) { if (entry instanceof LibraryOrderEntry || entry instanceof JdkOrderEntry) { VirtualFile moduleRoot = vcsManager.getVcsRootFor(entry.getOwnerModule().getModuleFile()); if (moduleRoot != null) { libraryRoots.add(moduleRoot); } } } if (libraryRoots.size() == 0) { LOGGER.debug("No library roots"); return null; } // if the lib is used in several modules, take the top module // (for modules of the same level we can't guess anything => take the first one) Iterator<VirtualFile> libIterator = libraryRoots.iterator(); VirtualFile topLibraryRoot = libIterator.next(); while (libIterator.hasNext()) { VirtualFile libRoot = libIterator.next(); if (VfsUtilCore.isAncestor(libRoot, topLibraryRoot, true)) { topLibraryRoot = libRoot; } } LOGGER.debug("Several library roots, returning " + topLibraryRoot); return topLibraryRoot; }