private int getCaretOffset() {
    int caretOffset = myCaretModel.getOffset();
    int upperBound = Math.max(myDocument.getTextLength() - 1, 0);
    caretOffset = MathUtil.clamp(caretOffset, 0, upperBound);
    return caretOffset;
  }
private int getCaretOffset()