TextRange (@NotNull CharSequence text, int cursor, @NotNull TextRange range) { int subsequenceOffset = range.getStartOffset(); CharSequence rangeText = new CharSequenceSubSequence(text, subsequenceOffset, range.getEndOffset()); //limit to current line int start = CharArrayUtil.shiftBackwardUntil(rangeText, cursor - subsequenceOffset, "\n\r") + 1; int end = CharArrayUtil.shiftForwardUntil(rangeText, cursor - subsequenceOffset, "\n\r"); //strip whitespace start = CharArrayUtil.shiftForward(rangeText, start, cursor - subsequenceOffset, " \t"); end = CharArrayUtil.shiftBackward(rangeText, cursor - subsequenceOffset, end - 1, " \t") + 1; return new TextRange( subsequenceOffset + start, subsequenceOffset + end ); }