public List<TextRange> select(
      PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = new ArrayList<TextRange>();

    PsiElement parent = e.getParent();

    if (!(parent instanceof PsiCodeBlock) && !(parent instanceof PsiBlockStatement)
        || parent instanceof JspCodeBlock) {
      return result;
    }

    PsiElement startElement = e;
    PsiElement endElement = e;

    while (startElement.getPrevSibling() != null) {
      PsiElement sibling = startElement.getPrevSibling();

      if (sibling instanceof PsiJavaToken) {
        PsiJavaToken token = (PsiJavaToken) sibling;
        if (token.getTokenType() == JavaTokenType.LBRACE) {
          break;
        }
      }

      if (sibling instanceof PsiWhiteSpace) {
        PsiWhiteSpace whiteSpace = (PsiWhiteSpace) sibling;

        String[] strings = LineTokenizer.tokenize(whiteSpace.getText().toCharArray(), false);
        if (strings.length > 2) {
          break;
        }
      }

      startElement = sibling;
    }

    while (startElement instanceof PsiWhiteSpace) {
      startElement = startElement.getNextSibling();
    }

    while (endElement.getNextSibling() != null) {
      PsiElement sibling = endElement.getNextSibling();

      if (sibling instanceof PsiJavaToken) {
        PsiJavaToken token = (PsiJavaToken) sibling;
        if (token.getTokenType() == JavaTokenType.RBRACE) {
          break;
        }
      }

      if (sibling instanceof PsiWhiteSpace) {
        PsiWhiteSpace whiteSpace = (PsiWhiteSpace) sibling;

        String[] strings = LineTokenizer.tokenize(whiteSpace.getText().toCharArray(), false);
        if (strings.length > 2) {
          break;
        }
      }

      endElement = sibling;
    }

    while (endElement instanceof PsiWhiteSpace) {
      endElement = endElement.getPrevSibling();
    }

    result.addAll(
        expandToWholeLine(
            editorText,
            new TextRange(
                startElement.getTextRange().getStartOffset(),
                endElement.getTextRange().getEndOffset())));

    return result;
  }
  /**
   * There is a possible case that target code block already starts with the empty line:
   *
   * <pre>
   *   void test(int i) {
   *     if (i > 1[caret]) {
   *
   *     }
   *   }
   * </pre>
   *
   * We want just move caret to correct position at that empty line without creating additional
   * empty line then.
   *
   * @param editor target editor
   * @param codeBlock target code block to which new empty line is going to be inserted
   * @param element target element under caret
   * @return <code>true</code> if it was found out that the given code block starts with the empty
   *     line and caret is pointed to correct position there, i.e. no additional processing is
   *     required; <code>false</code> otherwise
   */
  private static boolean processExistingBlankLine(
      @NotNull Editor editor, @Nullable PsiCodeBlock codeBlock, @Nullable PsiElement element) {
    PsiWhiteSpace whiteSpace = null;
    if (codeBlock == null) {
      if (element != null) {
        final PsiElement next = PsiTreeUtil.nextLeaf(element);
        if (next instanceof PsiWhiteSpace) {
          whiteSpace = (PsiWhiteSpace) next;
        }
      }
    } else {
      whiteSpace = PsiTreeUtil.findChildOfType(codeBlock, PsiWhiteSpace.class);
      if (whiteSpace == null) {
        return false;
      }

      PsiElement lbraceCandidate = whiteSpace.getPrevSibling();
      if (lbraceCandidate == null) {
        return false;
      }

      ASTNode node = lbraceCandidate.getNode();
      if (node == null || node.getElementType() != JavaTokenType.LBRACE) {
        return false;
      }
    }

    if (whiteSpace == null) {
      return false;
    }

    final TextRange textRange = whiteSpace.getTextRange();
    final Document document = editor.getDocument();
    final CharSequence whiteSpaceText =
        document
            .getCharsSequence()
            .subSequence(textRange.getStartOffset(), textRange.getEndOffset());
    if (StringUtil.countNewLines(whiteSpaceText) < 2) {
      return false;
    }

    int i = CharArrayUtil.shiftForward(whiteSpaceText, 0, " \t");
    if (i >= whiteSpaceText.length() - 1) {
      assert false
          : String.format(
              "code block: %s, white space: %s",
              codeBlock == null ? "undefined" : codeBlock.getTextRange(),
              whiteSpace.getTextRange());
      return false;
    }

    editor.getCaretModel().moveToOffset(i + 1 + textRange.getStartOffset());
    EditorActionManager actionManager = EditorActionManager.getInstance();
    EditorActionHandler actionHandler =
        actionManager.getActionHandler(IdeActions.ACTION_EDITOR_MOVE_LINE_END);
    final DataContext dataContext = DataManager.getInstance().getDataContext(editor.getComponent());
    if (dataContext == null) {
      i = CharArrayUtil.shiftForwardUntil(whiteSpaceText, i, "\n");
      if (i >= whiteSpaceText.length()) {
        i = whiteSpaceText.length();
      }
      editor.getCaretModel().moveToOffset(i + textRange.getStartOffset());
    } else {
      actionHandler.execute(editor, dataContext);
    }
    return true;
  }