PsiElement (Editor editor, PsiFile file) { int offset = editor.getCaretModel().getOffset(); PsiElement endElement = file.findElementAt(offset); SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) { PsiElement startElement = file.findElementAt(selectionModel.getSelectionStart()); if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) { return startElement; } } return endElement; }