PsiClass (@NotNull DataContext dataContext) { Project project = CommonDataKeys.PROJECT.getData(dataContext); if (project == null) return null; Editor editor = CommonDataKeys.EDITOR.getData(dataContext); if (LOG.isDebugEnabled()) { LOG.debug("editor " + editor); } if (editor != null) { PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null) return null; PsiElement targetElement = TargetElementUtil.findTargetElement(editor, TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil.LOOKUP_ITEM_ACCEPTED); if (LOG.isDebugEnabled()) { LOG.debug("target element " + targetElement); } if (targetElement instanceof PsiClass) { return (PsiClass)targetElement; } int offset = editor.getCaretModel().getOffset(); PsiElement element = file.findElementAt(offset); while (element != null) { if (LOG.isDebugEnabled()) { LOG.debug("context element " + element); } if (element instanceof PsiFile) { if (!(element instanceof PsiClassOwner)) return null; PsiClass[] classes = ((PsiClassOwner)element).getClasses(); return classes.length == 1 ? classes[0] : null; } if (element instanceof PsiClass && !(element instanceof PsiAnonymousClass) && !(element instanceof PsiSyntheticClass)) { return (PsiClass)element; } element = element.getParent(); } return null; } else { PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext); return element instanceof PsiClass ? (PsiClass)element : null; } }