void (@NotNull PsiElement element) { final PsiWhileStatement whileStatement = (PsiWhileStatement)element.getParent(); if (whileStatement == null) { return; } final PsiStatement body = whileStatement.getBody(); final PsiExpression condition = whileStatement.getCondition(); final boolean infiniteLoop = BoolUtils.isTrue(condition); @NonNls final StringBuilder doWhileStatementText = new StringBuilder(); CommentTracker tracker = new CommentTracker(); if (!infiniteLoop) { doWhileStatementText.append("if("); if (condition != null) { doWhileStatementText.append(tracker.text(condition)); } doWhileStatementText.append(") {\n"); } if (body instanceof PsiBlockStatement blockStatement) { doWhileStatementText.append("do {"); final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); final PsiElement[] children = codeBlock.getChildren(); if (children.length > 2) { for (int i = 1; i < children.length - 1; i++) { final PsiElement child = children[i]; doWhileStatementText.append(tracker.text(child)); } } doWhileStatementText.append('}'); } else if (body != null) { doWhileStatementText.append("do ").append(tracker.text(body)).append('\n'); } doWhileStatementText.append("while("); if (condition != null) { doWhileStatementText.append(tracker.text(condition)); } doWhileStatementText.append(");"); if (!infiniteLoop) { doWhileStatementText.append("\n}"); } PsiReplacementUtil.replaceStatement(whileStatement, doWhileStatementText.toString(), tracker); }