void (@NotNull final GrStatement anchor, @NotNull final Ref<GrIntroduceContext> contextRef) { CommandProcessor.getInstance().executeCommand(contextRef.get().getProject(), () -> ApplicationManager.getApplication().runWriteAction(() -> { GrIntroduceContext context = contextRef.get(); SmartPointerManager pointManager = SmartPointerManager.getInstance(context.getProject()); SmartPsiElementPointer<GrExpression> expressionRef = context.getExpression() != null ? pointManager.createSmartPsiElementPointer(context.getExpression()) : null; SmartPsiElementPointer<GrVariable> varRef = context.getVar() != null ? pointManager.createSmartPsiElementPointer(context.getVar()) : null; SmartPsiElementPointer[] occurrencesRefs = new SmartPsiElementPointer[context.getOccurrences().length]; PsiElement[] occurrences = context.getOccurrences(); for (int i = 0; i < occurrences.length; i++) { occurrencesRefs[i] = pointManager.createSmartPsiElementPointer(occurrences[i]); } PsiFile file = anchor.getContainingFile(); SmartPsiFileRange anchorPointer = pointManager.createSmartPsiFileRangePointer(file, anchor.getTextRange()); Document document = context.getEditor().getDocument(); CharSequence sequence = document.getCharsSequence(); TextRange range = anchor.getTextRange(); int end = range.getEndOffset(); document.insertString(end, "\n}"); int start = range.getStartOffset(); while (start > 0 && Character.isWhitespace(sequence.charAt(start - 1))) { start--; } document.insertString(start, "{"); PsiDocumentManager.getInstance(context.getProject()).commitDocument(document); Segment anchorSegment = anchorPointer.getRange(); PsiElement restoredAnchor = PsiImplUtil .findElementInRange(file, anchorSegment.getStartOffset(), anchorSegment.getEndOffset(), PsiElement.class); GrCodeBlock block = (GrCodeBlock)restoredAnchor.getParent(); CodeStyleManager.getInstance(context.getProject()).reformat(block.getRBrace()); CodeStyleManager.getInstance(context.getProject()).reformat(block.getLBrace()); for (int i = 0; i < occurrencesRefs.length; i++) { occurrences[i] = occurrencesRefs[i].getElement(); } contextRef.set(new GrIntroduceContextImpl(context.getProject(), context.getEditor(), expressionRef != null ? expressionRef.getElement() : null, varRef != null ? varRef.getElement() : null, null, occurrences, context.getScope())); }), getRefactoringName(), getRefactoringName()); }