void (PsiTryStatement tryStatement, PsiCodeBlock tryBlock, Context context) { Optional<PsiExpression> lastInTryVariable = StreamEx.of(context.myResourceVariables) .map(v -> v.myInitializer) .filter(e -> e != null && PsiTreeUtil.isAncestor(tryBlock, e, false)) .max(PsiElementOrderComparator.getInstance()); List<PsiStatement> elementsToRestore = new ArrayList<>(); if (lastInTryVariable.isPresent()) { PsiStatement last = PsiTreeUtil.getParentOfType(lastInTryVariable.get(), PsiStatement.class); PsiStatement[] statements = tryBlock.getStatements(); for (int i = 0; i < statements.length && statements[i] != last; i++) { PsiStatement current = statements[i]; if (context.myStatementsToDelete.contains(current)) { continue; } elementsToRestore.add(current); } } PsiElement tryStatementParent = tryStatement.getParent(); for (PsiStatement statement : elementsToRestore) { tryStatementParent.addBefore(statement, tryStatement); statement.delete(); } }