Location (@NotNull final Location location) { PsiElement element = location.getPsiElement(); TextRange elementTextRange = element.getTextRange(); if (!(element instanceof PsiClassOwner)) { if (PsiTreeUtil.getParentOfType(element, PsiClass.class) != null) return location; element = PsiTreeUtil.getParentOfType(element, PsiClassOwner.class); if (element == null) return location; } final PsiClassOwner psiFile = (PsiClassOwner)element; final PsiClass[] classes = psiFile.getClasses(); if (classes.length != 1) return location; TextRange textRange = classes[0].getTextRange(); if (textRange == null) return location; if (elementTextRange != null && textRange.contains(elementTextRange)) return location; return PsiLocation.fromPsiElement(classes[0]); }