void (Project project, final GrImportStatement context, PsiMember resolved, final GroovyFileBase file, final List<UsageInfo> usages, GrImportStatement templateImport) { PostprocessReformattingAspect.getInstance(project).doPostponedFormatting(); TemplateBuilderImpl templateBuilder = new TemplateBuilderImpl(templateImport); LinkedHashSet<String> names = getSuggestedNames(resolved, context); final GrImportAlias alias = templateImport.getAlias(); assert alias != null; final PsiElement aliasNameElement = alias.getNameElement(); assert aliasNameElement != null; MyLookupExpression lookupExpression = new MyLookupExpression(resolved.getName(), names, (PsiNamedElement)resolved, resolved, true, null); lookupExpression.setLookupFocusDegree(LookupFocusDegree.UNFOCUSED); templateBuilder.replaceElement(aliasNameElement, lookupExpression); Template built = templateBuilder.buildTemplate(); final Editor newEditor = IntentionUtils.positionCursor(project, file, templateImport); final Document document = newEditor.getDocument(); final RangeMarker contextImportPointer = document.createRangeMarker(context.getTextRange()); final TextRange range = templateImport.getTextRange(); document.deleteString(range.getStartOffset(), range.getEndOffset()); final String name = resolved.getName(); TemplateManager manager = TemplateManager.getInstance(project); manager.startTemplate(newEditor, built, new TemplateEditingAdapter() { @Override public void templateFinished(@NotNull Template template, boolean brokenOff) { final GrImportStatement importStatement = ReadAction .compute(() -> PsiTreeUtil.findElementOfClassAtOffset(file, range.getStartOffset(), GrImportStatement.class, true)); if (brokenOff) { if (importStatement != null) { ApplicationManager.getApplication().runWriteAction(() -> importStatement.delete()); } return; } updateRefs(usages, name, importStatement); ApplicationManager.getApplication().runWriteAction(() -> { final GrImportStatement context1 = PsiTreeUtil.findElementOfClassAtRange(file, contextImportPointer.getStartOffset(), contextImportPointer.getEndOffset(), GrImportStatement.class); if (context1 != null) { context1.delete(); } }); } }); }