Simplify and refactor caret position to text range mapping code.