use of com.intellij.refactoring.rename.inplace.MyLookupExpression in project intellij-community by JetBrains.
the class GrAliasImportIntention method runTemplate.
private static void runTemplate(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 PsiElement aliasNameElement = templateImport.getAliasNameElement();
assert aliasNameElement != null;
templateBuilder.replaceElement(aliasNameElement, new MyLookupExpression(resolved.getName(), names, (PsiNamedElement) resolved, resolved, true, null));
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(Template template, boolean brokenOff) {
final GrImportStatement importStatement = ApplicationManager.getApplication().runReadAction(new Computable<GrImportStatement>() {
@Nullable
@Override
public GrImportStatement compute() {
return 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();
}
});
}
});
}
Aggregations