PsiElement (@NotNull final PsiFile psiFile, int caretOffset, @NotNull Class<? extends PsiElement> @NotNull ... toSkip) { PsiElement element = psiFile.findElementAt(caretOffset); if (element == null) { return null; } final Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile); int lineEndOffset = 0; if (document != null) { int lineNumber = document.getLineNumber(caretOffset); lineEndOffset = document.getLineEndOffset(lineNumber); } while (caretOffset < lineEndOffset && PsiTreeUtil.instanceOf(element, toSkip)) { caretOffset++; element = psiFile.findElementAt(caretOffset); } return PsiTreeUtil.instanceOf(element, toSkip) ? null : element; }