Set<PsiFile> (final MultiMap<Module, PsiFile> files, final MultiMap<VirtualFile, PsiFile> jars, DefaultMutableTreeNode root) { final HashSet<PsiFile> psiFiles = new HashSet<>(); final List<Module> modules = new ArrayList<>(files.keySet()); modules.sort(ModulesAlphaComparator.INSTANCE); for (Module module : modules) { DefaultMutableTreeNode moduleNode = createFileNode(module); root.add(moduleNode); if (files.containsKey(module)) { List<PsiFile> moduleFiles = new ArrayList<>(files.get(module)); MultiMap<FileType, PsiFile> filesByType = new MultiMap<>(); for (PsiFile file : moduleFiles) { filesByType.putValue(file.getFileType(), file); } if (hasNonEmptyGroups(filesByType)) { for (Map.Entry<FileType, Collection<PsiFile>> entry : filesByType.entrySet()) { DefaultMutableTreeNode fileTypeNode = createFileNode(entry.getKey()); moduleNode.add(fileTypeNode); addChildrenFiles(psiFiles, fileTypeNode, new ArrayList<>(entry.getValue())); } } else { addChildrenFiles(psiFiles, moduleNode, moduleFiles); } } } List<VirtualFile> sortedJars = new ArrayList<>(jars.keySet()); sortedJars.sort((o1, o2) -> StringUtil.naturalCompare(o1.getName(), o2.getName())); for (VirtualFile file : sortedJars) { if (!file.isValid()) continue; final List<PsiFile> list = new ArrayList<>(jars.get(file)); final PsiFile jar = list.get(0).getManager().findFile(file); if (jar != null) { final DefaultMutableTreeNode jarNode = createFileNode(jar); root.add(jarNode); list.sort(FILE_COMPARATOR); for (PsiFile psiFile : list) { jarNode.add(createFileNode(psiFile)); psiFiles.add(psiFile); } } } return psiFiles; }