int (@Nullable PsiElement element1, @Nullable PsiElement element2) { if (element1 == null && element2 == null) return 0; if (element1 == null) return -1; if (element2 == null) return 1; if (element1.equals(element2)) return 0; final PsiFile psiFile1 = element1.getContainingFile(); final PsiFile psiFile2 = element2.getContainingFile(); if (psiFile1 == null && psiFile2 == null) return 0; if (psiFile1 == null) return -1; if (psiFile2 == null) return 1; if (Comparing.equal(psiFile1, psiFile2)) { final TextRange textRange1 = element1.getTextRange(); final TextRange textRange2 = element2.getTextRange(); if (textRange1 == null && textRange2 == null) return 0; if (textRange1 == null) return -1; if (textRange2 == null) return 1; return textRange1.getStartOffset() - textRange2.getStartOffset(); } final String name1 = psiFile1.getName(); final String name2 = psiFile2.getName(); return name1.compareToIgnoreCase(name2); }