@Override
  public int getLeadSelectionOffset() {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    int caretOffset = getOffset();
    if (hasSelection()) {
      RangeMarker marker = mySelectionMarker;
      if (marker != null && marker.isValid()) {
        int startOffset = marker.getStartOffset();
        int endOffset = marker.getEndOffset();
        if (caretOffset != startOffset && caretOffset != endOffset) {
          // Try to check if current selection is tweaked by fold region.
          FoldingModelEx foldingModel = myEditor.getFoldingModel();
          FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(caretOffset);
          if (foldRegion != null) {
            if (foldRegion.getStartOffset() == startOffset) {
              return endOffset;
            }
            else if (foldRegion.getEndOffset() == endOffset) {
              return startOffset;
            }
          }
          return isRangeMarkerEndPositionIsLead() ? endOffset : startOffset;
        }

        return caretOffset == endOffset ? startOffset : endOffset;
      }
    }
    return caretOffset;
  }
@Override
  public int getLeadSelectionOffset()