TextRange (Editor editor, @NotNull PsiFile psiFile) { if (editor == null) return null; String selectedText = editor.getSelectionModel().getSelectedText(); if (selectedText != null) { return new TextRange(editor.getSelectionModel().getSelectionStart(), editor.getSelectionModel().getSelectionEnd()); } PsiElement psiElement = psiFile.findElementAt(editor.getCaretModel().getOffset()); if (psiElement == null || psiElement instanceof PsiWhiteSpace) return null; return psiElement.getTextRange(); }