TextRange (PsiElement e, CharSequence editorText, int selStart, int selEnd) { TextRange range = e.getTextRange(); int shift = range.getStartOffset(); if (selStart <= shift || selEnd >= range.getEndOffset()) { return null; } String elementText = editorText.subSequence(shift, range.getEndOffset()).toString(); int start = selStart - shift; int end = selEnd - shift; TextRange best = findSentenceRange(elementText, start, end); if (best == null) return null; best = narrowRange(best, findCustomRange(elementText, start, end, '\"', '\"')); best = narrowRange(best, findCustomRange(elementText, start, end, '(', ')')); best = narrowRange(best, findCustomRange(elementText, start, end, '<', '>')); best = narrowRange(best, findCustomRange(elementText, start, end, '[', ']')); TextRange natural = findNaturalRange(elementText, start, end); if (!natural.contains(best)) { return null; } TextRange paragraph = findParagraphRange(elementText, start, end); if (best.getStartOffset() == start && best.getEndOffset() == end || !paragraph.contains(best)) { return paragraph.shiftRight(shift); } return best.shiftRight(shift); }