Search in sources :

Example 11 with TemplateEditingAdapter

use of com.intellij.codeInsight.template.TemplateEditingAdapter 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)

Example 12 with TemplateEditingAdapter

use of com.intellij.codeInsight.template.TemplateEditingAdapter in project intellij-community by JetBrains.

the class XmlTagInsertHandler method insertIncompleteTag.

public static void insertIncompleteTag(char completionChar, final Editor editor, XmlTag tag) {
    XmlElementDescriptor descriptor = tag.getDescriptor();
    final Project project = editor.getProject();
    TemplateManager templateManager = TemplateManager.getInstance(project);
    Template template = templateManager.createTemplate("", "");
    template.setToIndent(true);
    // temp code
    PsiFile containingFile = tag.getContainingFile();
    boolean htmlCode = HtmlUtil.hasHtml(containingFile) || HtmlUtil.supportsXmlTypedHandlers(containingFile);
    template.setToReformat(!htmlCode);
    StringBuilder indirectRequiredAttrs = addRequiredAttributes(descriptor, tag, template, containingFile);
    final boolean chooseAttributeName = addTail(completionChar, descriptor, htmlCode, tag, template, indirectRequiredAttrs);
    templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {

        private RangeMarker myAttrValueMarker;

        @Override
        public void waitingForInput(Template template) {
            int offset = editor.getCaretModel().getOffset();
            myAttrValueMarker = editor.getDocument().createRangeMarker(offset + 1, offset + 4);
        }

        @Override
        public void templateFinished(final Template template, boolean brokenOff) {
            final int offset = editor.getCaretModel().getOffset();
            if (chooseAttributeName && offset > 0) {
                char c = editor.getDocument().getCharsSequence().charAt(offset - 1);
                if (c == '/' || (c == ' ' && brokenOff)) {
                    new WriteCommandAction.Simple(project) {

                        @Override
                        protected void run() throws Throwable {
                            editor.getDocument().replaceString(offset, offset + 3, ">");
                        }
                    }.execute();
                }
            }
        }

        @Override
        public void templateCancelled(final Template template) {
            if (myAttrValueMarker == null) {
                return;
            }
            final UndoManager manager = UndoManager.getInstance(project);
            if (manager.isUndoInProgress() || manager.isRedoInProgress()) {
                return;
            }
            if (chooseAttributeName && myAttrValueMarker.isValid()) {
                final int startOffset = myAttrValueMarker.getStartOffset();
                final int endOffset = myAttrValueMarker.getEndOffset();
                new WriteCommandAction.Simple(project) {

                    @Override
                    protected void run() throws Throwable {
                        editor.getDocument().replaceString(startOffset, endOffset, ">");
                    }
                }.execute();
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) TemplateManager(com.intellij.codeInsight.template.TemplateManager) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) PsiFile(com.intellij.psi.PsiFile) RangeMarker(com.intellij.openapi.editor.RangeMarker) Template(com.intellij.codeInsight.template.Template)

Aggregations

Template (com.intellij.codeInsight.template.Template)12 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)12 TextRange (com.intellij.openapi.util.TextRange)9 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)8 Editor (com.intellij.openapi.editor.Editor)8 RangeMarker (com.intellij.openapi.editor.RangeMarker)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)5 TemplateManager (com.intellij.codeInsight.template.TemplateManager)3 Document (com.intellij.openapi.editor.Document)3 TypeExpression (com.intellij.codeInsight.intention.impl.TypeExpression)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 UndoManager (com.intellij.openapi.command.undo.UndoManager)1 Computable (com.intellij.openapi.util.Computable)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiParameter (com.intellij.psi.PsiParameter)1 PsiType (com.intellij.psi.PsiType)1