}
@Override
protected void processIntention(@NotNull PsiElement element, Editor editor)
throws IncorrectOperationException {
GoIfStatement stmt = findParentOfType(element, GoIfStatement.class);
if (stmt == null) {
throw new IncorrectOperationException(GoIntentionsBundle.message("error.if.statement.not.found"));
}
PsiFile file = stmt.getContainingFile();
if (file == null) {
return;
}
final Document document = editor.getDocument();
RangeMarker stmtRange = document.createRangeMarker(stmt.getTextRange());
GoExpr condition = stmt.getExpression();
boolean parentIsFunctionDeclaration = isFunctionBlock(stmt.getParent());
GoBlockStatement thenBlock = stmt.getThenBlock();
final int rightCurlyPosition = blockRightCurlyPosition(thenBlock);
final int leftCurlyPosition = blockLeftCurlyPosition(thenBlock);
if (rightCurlyPosition < 0 || leftCurlyPosition < 0) {
return;
}
GoBlockStatement elseBlock = stmt.getElseBlock();
boolean hasElseBlock = elseBlock != null;
List<PsiElement> siblings = getSiblings(stmt);
if (hasElseBlock) {
swapThenAndElseBlock(document, condition, thenBlock, elseBlock);
} else if (parentIsFunctionDeclaration &&
!containsNonSpaceCommentOrReturnElements(siblings)) {
String returnDeclaration = "return";
if (onlyOneReturnStatement(siblings)) {
returnDeclaration = concatenateElements(siblings);
}
final String finalReturnDeclaration = returnDeclaration;
WriteCommandAction writeCommandAction = new WriteCommandAction(element.getContainingFile().getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
document.deleteString(rightCurlyPosition, rightCurlyPosition + 1);
document.insertString(leftCurlyPosition + 1, "\n" + finalReturnDeclaration + "\n}");
}
};
writeCommandAction.execute();
flipCondition(document, condition);
GoSimpleStatement simpleStatement = stmt.getSimpleStatement();
if (simpleStatement != null) {
extractSimpleStatement(stmt, document, condition, simpleStatement);
}
} else {
WriteCommandAction writeCommandAction = new WriteCommandAction(element.getContainingFile().getProject()) {