PsiClass (@NotNull PsiFile file, PsiElement leaf) { PsiElement element = leaf; while (element != null && !(element instanceof PsiFile)) { if (element instanceof PsiClass psiClass && !(element instanceof PsiTypeParameter)) { if (psiClass.isEnum()) { PsiElement lastChild = null; for (PsiElement child = psiClass.getFirstChild(); child != null; child = child.getNextSibling()) { if (child instanceof PsiJavaToken && ";".equals(child.getText())) { lastChild = child; break; } else if (child instanceof PsiJavaToken && ",".equals(child.getText()) || child instanceof PsiEnumConstant) { lastChild = child; } } if (lastChild != null) { int adjustedOffset = lastChild.getTextRange().getEndOffset(); if (leaf.getTextRange().getEndOffset() <= adjustedOffset) return findClassAtOffset(file, file.findElementAt(adjustedOffset)); } } return psiClass; } element = element.getParent(); } return null; }