void (@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiType type) { Project project = file.getProject(); if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) return; int offset = editor.getCaretModel().getOffset(); PsiElement at = file.findElementAt(offset); PsiElement anchorStatement = CommonJavaRefactoringUtil.getParentStatement(at, false); if (anchorStatement == null) { CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.getCannotRefactorMessage( JavaRefactoringBundle.message("invalid.expression.context")), RefactoringBundle.message("introduce.variable.title"), HelpID.INTRODUCE_VARIABLE); return; } SuggestedNameInfo suggestedNameInfo = getSuggestedVariableNames(project, type, at); assert suggestedNameInfo.names.length > 0; ApplicationManager.getApplication().runWriteAction(() -> { Document document = editor.getDocument(); String initialVariableName = insertArgument(project, document, offset, suggestedNameInfo); PsiDocumentManager.getInstance(project).commitDocument(document); PsiLocalVariable localVariable = addLocalVariable(project, anchorStatement, type, initialVariableName); PsiElement context = PsiTreeUtil.getParentOfType(localVariable, PsiCodeBlock.class); if (context == null) return; PsiTypeElement typeElement = localVariable.getTypeElement(); PsiReference reference = ReferencesSearch.search(localVariable, new LocalSearchScope(context)).findFirst(); assert reference != null; PsiElement variableReference = reference.getElement(); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document); TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(context); templateBuilder.replaceElement(typeElement, TYPE_NAME, new ConstantNode(typeElement.getText()), true, true); templateBuilder.replaceElement(localVariable.getNameIdentifier(), VARIABLE_NAME, new ConstantNode(suggestedNameInfo.names[0]).withLookupStrings(suggestedNameInfo.names), true); templateBuilder.replaceElement(variableReference, VARIABLE_NAME, (String)null, false); templateBuilder.replaceElement(Objects.requireNonNull(localVariable.getInitializer()), ""); templateBuilder.setEndVariableAfter(variableReference); Template template = templateBuilder.buildInlineTemplate(); editor.getCaretModel().moveToOffset(context.getTextOffset()); TemplateManager.getInstance(project).startTemplate(editor, template); }); }