TextRange (CharSequence editorText, int cursorOffset, CharCondition isWordPartCondition) { if (cursorOffset < 0 || cursorOffset >= editorText.length()) { return null; } if (cursorOffset > 0 && !isWordPartCondition.value(editorText.charAt(cursorOffset)) && isWordPartCondition.value(editorText.charAt(cursorOffset - 1))) { cursorOffset--; } if (isWordPartCondition.value(editorText.charAt(cursorOffset))) { int start = cursorOffset; int end = cursorOffset + 1; final int textLen = editorText.length(); while (start > 0 && isWordPartCondition.value(editorText.charAt(start - 1)) && !EditorActionUtil.isHumpBound(editorText, start, true)) { start--; } while (end < textLen && isWordPartCondition.value(editorText.charAt(end)) && !EditorActionUtil.isHumpBound(editorText, end, false)) { end++; } if (start + 1 < end) { return new TextRange(start, end); } } return null; }