@SuppressWarnings("AssignmentToForLoopParameter")
  public static @NotNull Pair<LogicalPosition, LogicalPosition> calcSurroundingRange(@NotNull Editor editor,
                                                                                     @NotNull VisualPosition start,
                                                                                     @NotNull VisualPosition end) {
    return ReadAction.compute(() -> {
      final Document document = editor.getDocument();
      final FoldingModel foldingModel = editor.getFoldingModel();

      LogicalPosition first = editor.visualToLogicalPosition(new VisualPosition(start.line, 0));
      for (
        int line = first.line, offset = document.getLineStartOffset(line);
        offset >= 0;
        offset = document.getLineStartOffset(line)) {
        final FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(offset);
        if (foldRegion == null) {
          first = new LogicalPosition(line, 0);
          break;
        }
        final int foldEndLine = document.getLineNumber(foldRegion.getStartOffset());
        if (foldEndLine <= line) {
          first = new LogicalPosition(line, 0);
          break;
        }
        line = foldEndLine;
      }


      LogicalPosition second = editor.visualToLogicalPosition(new VisualPosition(end.line, 0));
      for (
        int line = second.line, offset = document.getLineEndOffset(line);
        offset <= document.getTextLength();
        offset = document.getLineEndOffset(line)) {
        final FoldRegion foldRegion = foldingModel.getCollapsedRegionAtOffset(offset);
        if (foldRegion == null) {
          second = new LogicalPosition(line + 1, 0);
          break;
        }
        final int foldEndLine = document.getLineNumber(foldRegion.getEndOffset());
        if (foldEndLine <= line) {
          second = new LogicalPosition(line + 1, 0);
          break;
        }
        line = foldEndLine;
      }

      if (second.line >= document.getLineCount()) {
        second = editor.offsetToLogicalPosition(document.getTextLength());
      }
      return Pair.create(first, second);
    });
  }
@SuppressWarnings("AssignmentToForLoopParameter")
  public static @NotNull Pair<LogicalPosition, LogicalPosition> calcSurroundingRange(@NotNull Editor editor,
                                                                                     @NotNull VisualPosition start,
                                                                                     @NotNull VisualPosition end)