void (@NotNull ActionContext context, @NotNull PsiKeyword elseKeyword, @NotNull ModPsiUpdater updater) { PsiElement parent = elseKeyword.getParent(); if (parent instanceof PsiIfStatement ifStatement) { PsiStatement elseBranch = ifStatement.getElseBranch(); PsiElement grandParent = ifStatement.getParent(); if (elseBranch != null && grandParent != null) { if (!(grandParent instanceof PsiCodeBlock)) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.project()); PsiBlockStatement blockStatement = (PsiBlockStatement)factory.createStatementFromText("{" + ifStatement.getText() + "}", ifStatement); blockStatement = (PsiBlockStatement)ifStatement.replace(blockStatement); ifStatement = (PsiIfStatement)blockStatement.getCodeBlock().getStatements()[0]; elseBranch = ifStatement.getElseBranch(); LOG.assertTrue(elseBranch != null); } CommentTracker ct = new CommentTracker(); InvertIfConditionAction.addAfter(ifStatement, elseBranch, ct); ct.deleteAndRestoreComments(elseBranch); } } }