@Override
protected void processIntention(@NotNull PsiElement element, Editor editor)
throws IntentionExecutionException {
final Document document = editor.getDocument();
GoIfStatement outer = findParentOfType(element, GoIfStatement.class);
RangeMarker reformatRange = document.createRangeMarker(outer.getTextRange());
GoIfStatement inner = (GoIfStatement) outer.getThenBlock().getStatements()[0];
if (outer.getSimpleStatement() != null && inner.getSimpleStatement() != null) {
SmartPsiElementPointer<GoIfStatement> outerPointer = createSmartElementPointer(outer);
MoveSimpleStatementOutIntention.moveSimpleStatementOut(editor, outer);
outer = outerPointer.getElement();
if (outer == null) {
return;
}
inner = (GoIfStatement) outer.getThenBlock().getStatements()[0];
}
// outer if range is text range from "if" to "{", inclusive, and surrounding whitespaces are also included.
final RangeMarker outerIfRange = getOuterIfRange(document, outer);
final RangeMarker rightCurlyRange = getRightCurlyRange(document, outer);
if (outerIfRange == null || rightCurlyRange == null) {
return;
}
final int innerEnd = inner.getThenBlock().getTextOffset();
final String simpleStatementAndExpression = getSimpleStatementAndExpression(outer, inner);
final GoIfStatement finalInner = inner;
WriteCommandAction writeCommandAction = new WriteCommandAction(element.getContainingFile().getProject()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
document.replaceString(finalInner.getTextOffset(), innerEnd, simpleStatementAndExpression);
document.deleteString(outerIfRange.getStartOffset(), outerIfRange.getEndOffset());
document.deleteString(rightCurlyRange.getStartOffset(), rightCurlyRange.getEndOffset());
}
};
writeCommandAction.execute();
PsiFile file = outer.getContainingFile();