int (Editor editor, PsiFile file, PsiClass targetClass) { int result = editor.getCaretModel().getOffset(); PsiClass classAtCursor = PsiTreeUtil.getParentOfType(file.findElementAt(result), PsiClass.class, false); if (classAtCursor == targetClass) { return result; } while (classAtCursor != null && !(classAtCursor.getParent() instanceof PsiFile)) { result = classAtCursor.getTextRange().getEndOffset(); classAtCursor = PsiTreeUtil.getParentOfType(classAtCursor, PsiClass.class); } return result; }