void (PsiForStatement forStatement) { if (!(forStatement.getParent() instanceof PsiCodeBlock)) { forStatement = BlockUtils.expandSingleStatementToBlockStatement(forStatement); } final PsiStatement initialization = forStatement.getInitialization(); if (!(initialization instanceof PsiDeclarationStatement declarationStatement)) { return; } final List<PsiLocalVariable> variables = ContainerUtil.filterIsInstance(declarationStatement.getDeclaredElements(), PsiLocalVariable.class); final int min, max; final boolean dependentVariables = containsDependentVariables(variables); if (dependentVariables) { min = 0; max = variables.size() - 1; } else { min = 1; max = variables.size(); } final PsiElementFactory factory = JavaPsiFacade.getElementFactory(forStatement.getProject()); final CommentTracker ct = new CommentTracker(); for (int i = min; i < max; i++) { final PsiVariable variable = variables.get(i); final String name = variable.getName(); assert name != null; final PsiDeclarationStatement newStatement = factory.createVariableDeclarationStatement(name, variable.getType(), ct.markUnchanged(variable.getInitializer()), declarationStatement); forStatement.getParent().addBefore(newStatement, forStatement); } final PsiVariable remainingVariable = variables.get(dependentVariables ? variables.size() - 1 : 0); final String name = remainingVariable.getName(); assert name != null; final PsiStatement replacementStatement = factory.createVariableDeclarationStatement(name, remainingVariable.getType(), ct.markUnchanged(remainingVariable.getInitializer()), declarationStatement); ct.replaceAndRestoreComments(declarationStatement, replacementStatement); }