String (@NotNull Project project, @NotNull VirtualFile virtualFile) { Module module = ProjectFileIndex.getInstance(project).getModuleForFile(virtualFile, false); if (module == null || !ModuleType.is(module, JavaModuleType.getModuleType())) { return null; } ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex(); VirtualFile sourceRoot = index.getSourceRootForFile(virtualFile); if (sourceRoot != null && !sourceRoot.equals(virtualFile)) { return Objects.requireNonNull(VfsUtilCore.getRelativePath(virtualFile, sourceRoot, '/')); } VirtualFile outerMostRoot = null; VirtualFile each = virtualFile; while (each != null && (each = index.getContentRootForFile(each, false)) != null) { outerMostRoot = each; each = each.getParent(); } if (outerMostRoot != null && !outerMostRoot.equals(virtualFile)) { String relative = VfsUtilCore.getRelativePath(virtualFile, outerMostRoot, '/'); if (relative != null) { return relative; } } return virtualFile.getPath(); }