Search in sources :

Example 1 with MyLookupExpression

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();
                }
            });
        }
    });
}
Also used : MyLookupExpression(com.intellij.refactoring.rename.inplace.MyLookupExpression) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) Template(com.intellij.codeInsight.template.Template) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor) Computable(com.intellij.openapi.util.Computable)

Aggregations

Template (com.intellij.codeInsight.template.Template)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 Computable (com.intellij.openapi.util.Computable)1 TextRange (com.intellij.openapi.util.TextRange)1 MyLookupExpression (com.intellij.refactoring.rename.inplace.MyLookupExpression)1 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)1