SmartPsiElementPointer<PsiVariable> () { if (!IntentionPreviewUtils.isPreviewElement(myExpression)) { ApplicationManager.getApplication().assertWriteAccessAllowed(); } final PsiExpression newExpr = myFieldConflictsResolver.fixInitializer(myExpression); if (myAnchor == myExpression) { myAnchor = newExpr; } PsiExpression initializer = RefactoringUtil.unparenthesizeExpression(newExpr); final SmartTypePointer selectedType = SmartTypePointerManager.getInstance(myProject).createSmartTypePointer( mySettings.getSelectedType()); initializer = IntroduceVariableBase.simplifyVariableInitializer(initializer, selectedType.getType()); CommentTracker commentTracker = new CommentTracker(); commentTracker.markUnchanged(initializer); initializer = (PsiExpression)initializer.copy(); PsiType type = stripNullabilityAnnotationsFromTargetType(selectedType, newExpr); PsiElement declaration = createDeclaration(type, mySettings.getEnteredName(), initializer); replaceOccurrences(newExpr); ensureCodeBlock(type); PsiVariable var = addVariable(declaration, initializer); if (myAnchor instanceof PsiExpressionStatement && ExpressionUtils.isReferenceTo(((PsiExpressionStatement)myAnchor).getExpression(), var)) { commentTracker.deleteAndRestoreComments(myAnchor); if (myEditor != null) { assert myPosition != null; myEditor.getCaretModel().moveToLogicalPosition(myPosition); myEditor.getCaretModel().moveToOffset(var.getTextRange().getEndOffset()); myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); myEditor.getSelectionModel().removeSelection(); } } highlight(var); PsiUtil.setModifierProperty(var, PsiModifier.FINAL, mySettings.isDeclareFinal()); if (!(var instanceof PsiPatternVariable)) { if (mySettings.isDeclareVarType()) { PsiTypeElement typeElement = var.getTypeElement(); LOG.assertTrue(typeElement != null); IntroduceVariableUtil.expandDiamondsAndReplaceExplicitTypeWithVar(typeElement, var); } } myFieldConflictsResolver.fix(); return SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(var); }