void (@NotNull Project project, @NotNull PsiElement variableIdentifier, @NotNull ModPsiUpdater updater) { if (!(variableIdentifier instanceof PsiIdentifier)) { return; } final PsiLocalVariable variable = (PsiLocalVariable)variableIdentifier.getParent(); assert variable != null; final List<PsiReferenceExpression> references = findReferences(variable); PsiElement commonParent = ScopeUtils.getCommonParent(references); if (commonParent == null) { return; } final PsiExpression initializer = variable.getInitializer(); if (initializer != null) { final PsiElement variableScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, PsiForStatement.class, PsiTryStatement.class); if (variableScope == null) { return; } commonParent = ScopeUtils.moveOutOfLoopsAndClasses(commonParent, variableScope); if (commonParent == null) { return; } } final PsiElement referenceElement = references.get(0); final PsiElement firstReferenceScope = PsiTreeUtil.getParentOfType(referenceElement, PsiCodeBlock.class, PsiForStatement.class, PsiTryStatement.class); if (firstReferenceScope == null) { return; } PsiElement newDeclaration; final CommentTracker tracker = new CommentTracker(); if (commonParent instanceof PsiTryStatement) { final PsiElement resourceReference = referenceElement.getParent(); final PsiResourceVariable resourceVariable = JavaPsiFacade.getElementFactory(project).createResourceVariable( variable.getName(), variable.getType(), tracker.markUnchanged(initializer), variable); newDeclaration = resourceReference.getParent().addBefore(resourceVariable, resourceReference); resourceReference.delete(); } else if (commonParent instanceof PsiForStatement forStatement) { final PsiStatement initialization = forStatement.getInitialization(); if (initialization == null) { return; } if (initialization instanceof PsiExpressionStatement expressionStatement) { final PsiExpression expression = expressionStatement.getExpression(); if (!(expression instanceof PsiAssignmentExpression assignmentExpression)) { return; } final PsiExpression rhs = assignmentExpression.getRExpression(); newDeclaration = createNewDeclaration(variable, rhs, tracker); } else { newDeclaration = createNewDeclaration(variable, initializer, tracker); } newDeclaration = initialization.replace(newDeclaration); } else if (firstReferenceScope.equals(commonParent)) { newDeclaration = moveDeclarationToLocation(variable, referenceElement, tracker); } else { final PsiElement commonParentChild = ScopeUtils.getChildWhichContainsElement(commonParent, referenceElement); if (commonParentChild == null) { return; } final PsiElement location = commonParentChild.getPrevSibling(); newDeclaration = createNewDeclaration(variable, initializer, tracker); newDeclaration = commonParent.addAfter(newDeclaration, location); } final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); newDeclaration = codeStyleManager.reformat(newDeclaration); removeOldVariable(variable, tracker); tracker.insertCommentsBefore(newDeclaration); updater.highlight(newDeclaration); }