void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiElement parent = element.getParent(); if (!(parent instanceof PsiTryStatement tryStatement)) { return; } PsiCodeBlock tryBlock = tryStatement.getTryBlock(); if (tryBlock == null) return; Context context = Context.from(tryStatement); if (context == null) return; Pair<List<ResourceVariable>, List<ResourceVariable>> partition = partition(context.myResourceVariables, variable -> variable.getInitializedElement().getTextOffset() < tryStatement.getTextOffset()); List<ResourceVariable> before = partition.first; List<ResourceVariable> after = partition.second; String resourceListBefore = joinToString(before); String resourceListAfter = joinToString(after); @NonNls StringBuilder sb = new StringBuilder("try("); PsiResourceList resourceListElement = tryStatement.getResourceList(); boolean hasSemicolon = false; if (!before.isEmpty()) { sb.append(resourceListBefore); if (resourceListElement != null || !after.isEmpty()) { sb.append(";"); hasSemicolon = true; } } if (resourceListElement != null) { PsiElement[] children = resourceListElement.getChildren(); if (children.length > 2 && resourceListElement.getResourceVariablesCount() > 0) { for (int i = 1; i < children.length - 1; i++) { sb.append(children[i].getText()); } } } if (!after.isEmpty()) { if ((!before.isEmpty() || resourceListElement != null) && !hasSemicolon) { sb.append(";"); } sb.append(resourceListAfter); } sb.append(")"); List<PsiLocalVariable> locals = StreamEx.of(context.myResourceVariables) .map(resourceVariable -> resourceVariable.myVariable) .select(PsiLocalVariable.class) .sorted(PsiElementOrderComparator.getInstance()) .toList(); if (locals.size() == 1) { PsiLocalVariable variable = locals.get(0); PsiStatement declaration = PsiTreeUtil.getParentOfType(variable, PsiStatement.class); if (declaration != null) { if (declaration.getParent() == tryStatement.getParent()) { List<PsiStatement> statements = collectStatementsBetween(declaration, tryStatement); PsiJavaToken lBrace = tryBlock.getLBrace(); if (lBrace != null) { for (int i = statements.size() - 1; i >= 0; i--) { PsiStatement statement = statements.get(i); tryBlock.addAfter(statement, lBrace); if (statement.isValid()) { statement.delete(); } } } } } } restoreStatementsBeforeLastVariableInTryResource(tryStatement, tryBlock, context); for (PsiStatement statement : context.myStatementsToDelete) { if (statement.isValid()) { new CommentTracker().deleteAndRestoreComments(statement); } } for (ResourceVariable variable : context.myResourceVariables) { if (!variable.myUsedOutsideTry) { if (variable.myVariable.isValid()) { new CommentTracker().deleteAndRestoreComments(variable.myVariable); } } } sb.append(tryBlock.getText()); for (PsiCatchSection section : tryStatement.getCatchSections()) { sb.append(section.getText()); } PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); if (finallyBlock == null) return; if (!ControlFlowUtils.isEmptyCodeBlock(finallyBlock)) { sb.append("finally").append(finallyBlock.getText()); } else { PsiElement[] finallyBlockChildren = finallyBlock.getChildren(); if (!StreamEx.of(finallyBlockChildren).skip(1).limit(finallyBlockChildren.length - 2).allMatch(el -> el instanceof PsiWhiteSpace)) { PsiElement tryParent = tryStatement.getParent(); tryParent.addRangeAfter(finallyBlockChildren[1], finallyBlockChildren[finallyBlockChildren.length - 2], tryStatement); } } tryStatement.replace(JavaPsiFacade.getElementFactory(project).createStatementFromText(sb.toString(), tryStatement)); }