void (@NotNull PsiElement element, @NotNull ActionContext context, @NotNull ModPsiUpdater updater) { PsiElement parent = element.getParent(); if (!"if".equals(element.getText()) || !(parent instanceof GrIfStatement parentIf)) { throw new IncorrectOperationException("Not invoked on an if"); } GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(context.project()); GrExpression condition = parentIf.getCondition(); if (condition == null) { throw new IncorrectOperationException("Invoked on an if with empty condition"); } GrExpression negatedCondition = null; if (condition instanceof GrUnaryExpression unaryCondition) { if ("!".equals(unaryCondition.getOperationToken().getText())) { negatedCondition = stripParenthesis(unaryCondition.getOperand()); } } if (negatedCondition == null) { // Now check whether this is a simple expression condition = stripParenthesis(condition); String negatedExpressionText; if (condition instanceof GrCallExpression || condition instanceof GrReferenceExpression) { negatedExpressionText = "!" + condition.getText(); } else { negatedExpressionText = "!(" + condition.getText() + ")"; } negatedCondition = groovyPsiElementFactory.createExpressionFromText(negatedExpressionText, parentIf); } GrStatement thenBranch = parentIf.getThenBranch(); final boolean thenIsNotEmpty = isNotEmpty(thenBranch); String newIfText = "if (" + negatedCondition.getText() + ") {}"; if (thenIsNotEmpty) { newIfText += " else {}"; } GrIfStatement newIf = (GrIfStatement)groovyPsiElementFactory.createStatementFromText(newIfText, parentIf.getContext()); generateElseBranchTextAndRemoveTailStatements(parentIf, newIf); if (thenIsNotEmpty) { final GrStatement elseBranch = newIf.getElseBranch(); assert elseBranch != null; elseBranch.replaceWithStatement(thenBranch); } parentIf.replace(newIf); }