public static int getLineEndOffset(int offset, @NotNull Document document) {
    if (offset < 0 || offset > document.getTextLength()) {
      return offset;
    }
    int lineNumber = document.getLineNumber(offset);
    return document.getLineEndOffset(lineNumber);
  }
public static int getLineEndOffset(int offset, @NotNull Document document)