Search in sources :

Example 1 with TemplateEditingAdapter

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

the class TestIntegrationUtils method runTestMethodTemplate.

public static void runTestMethodTemplate(final Editor editor, final PsiClass targetClass, final PsiMethod method, boolean automatic, final Template template) {
    final int startOffset = method.getModifierList().getTextRange().getStartOffset();
    final TextRange range = new TextRange(startOffset, method.getTextRange().getEndOffset());
    editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), "");
    editor.getCaretModel().moveToOffset(range.getStartOffset());
    final Project project = targetClass.getProject();
    TemplateEditingAdapter adapter = null;
    if (!automatic) {
        adapter = new TemplateEditingAdapter() {

            @Override
            public void templateFinished(Template template, boolean brokenOff) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
                    PsiFile psi = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
                    PsiElement el = PsiTreeUtil.findElementOfClassAtOffset(psi, editor.getCaretModel().getOffset() - 1, PsiMethod.class, false);
                    if (el != null) {
                        PsiMethod method1 = PsiTreeUtil.getParentOfType(el, PsiMethod.class, false);
                        if (method1 != null) {
                            if (method1.findDeepestSuperMethods().length > 0) {
                                GenerateMembersUtil.setupGeneratedMethod(method1);
                            }
                            CreateFromUsageUtils.setupEditor(method1, editor);
                        }
                    }
                });
            }
        };
    }
    TemplateManager.getInstance(project).startTemplate(editor, template, adapter);
}
Also used : Project(com.intellij.openapi.project.Project) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Template(com.intellij.codeInsight.template.Template)

Example 2 with TemplateEditingAdapter

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

the class CreateFieldFromUsageFix method createFieldFromUsageTemplate.

public static void createFieldFromUsageTemplate(final PsiClass targetClass, final Project project, final ExpectedTypeInfo[] expectedTypes, final PsiField field, final boolean createConstantField, final PsiElement context) {
    final PsiFile targetFile = targetClass.getContainingFile();
    final Editor newEditor = positionCursor(project, targetFile, field);
    if (newEditor == null)
        return;
    Template template = CreateFieldFromUsageHelper.setupTemplate(field, expectedTypes, targetClass, newEditor, context, createConstantField);
    startTemplate(newEditor, template, project, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
            final int offset = newEditor.getCaretModel().getOffset();
            final PsiField psiField = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset, PsiField.class, false);
            if (psiField != null) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    CodeStyleManager.getInstance(project).reformat(psiField);
                });
                newEditor.getCaretModel().moveToOffset(psiField.getTextRange().getEndOffset() - 1);
            }
        }
    });
}
Also used : TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) Editor(com.intellij.openapi.editor.Editor) Template(com.intellij.codeInsight.template.Template)

Example 3 with TemplateEditingAdapter

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

the class CreateMethodFromUsageFix method doCreate.

public static void doCreate(PsiClass targetClass, PsiMethod method, boolean shouldBeAbstract, List<Pair<PsiExpression, PsiType>> arguments, PsiSubstitutor substitutor, ExpectedTypeInfo[] expectedTypes, @Nullable final PsiElement context) {
    method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
    if (method == null) {
        return;
    }
    final Project project = targetClass.getProject();
    final PsiFile targetFile = targetClass.getContainingFile();
    Document document = PsiDocumentManager.getInstance(project).getDocument(targetFile);
    if (document == null)
        return;
    TemplateBuilderImpl builder = new TemplateBuilderImpl(method);
    CreateFromUsageUtils.setupMethodParameters(method, builder, context, substitutor, arguments);
    final PsiTypeElement returnTypeElement = method.getReturnTypeElement();
    if (returnTypeElement != null) {
        new GuessTypeParameters(JavaPsiFacade.getInstance(project).getElementFactory()).setupTypeElement(returnTypeElement, expectedTypes, substitutor, builder, context, targetClass);
    }
    PsiCodeBlock body = method.getBody();
    builder.setEndVariableAfter(shouldBeAbstract || body == null ? method : body.getLBrace());
    method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
    if (method == null)
        return;
    RangeMarker rangeMarker = document.createRangeMarker(method.getTextRange());
    final Editor newEditor = positionCursor(project, targetFile, method);
    if (newEditor == null)
        return;
    Template template = builder.buildTemplate();
    newEditor.getCaretModel().moveToOffset(rangeMarker.getStartOffset());
    newEditor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
    rangeMarker.dispose();
    if (!shouldBeAbstract) {
        startTemplate(newEditor, template, project, new TemplateEditingAdapter() {

            @Override
            public void templateFinished(Template template, boolean brokenOff) {
                if (brokenOff)
                    return;
                WriteCommandAction.runWriteCommandAction(project, () -> {
                    PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
                    final int offset = newEditor.getCaretModel().getOffset();
                    PsiMethod method1 = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false);
                    if (method1 != null) {
                        try {
                            CreateFromUsageUtils.setupMethodBody(method1);
                        } catch (IncorrectOperationException e) {
                            LOG.error(e);
                        }
                        CreateFromUsageUtils.setupEditor(method1, newEditor);
                    }
                });
            }
        });
    } else {
        startTemplate(newEditor, template, project);
    }
}
Also used : TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) Template(com.intellij.codeInsight.template.Template) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

Example 4 with TemplateEditingAdapter

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

the class GenerateMembersHandlerBase method runTemplates.

private static void runTemplates(final Project myProject, final Editor editor, final List<TemplateGenerationInfo> templates, final int index) {
    TemplateGenerationInfo info = templates.get(index);
    final Template template = info.getTemplate();
    final PsiElement element = info.getPsiMember();
    final TextRange range = element.getTextRange();
    WriteAction.run(() -> editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset()));
    int offset = range.getStartOffset();
    editor.getCaretModel().moveToOffset(offset);
    editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
    TemplateManager.getInstance(myProject).startTemplate(editor, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (index + 1 < templates.size()) {
                ApplicationManager.getApplication().invokeLater(() -> new WriteCommandAction(myProject) {

                    @Override
                    protected void run(@NotNull Result result) throws Throwable {
                        runTemplates(myProject, editor, templates, index + 1);
                    }
                }.execute());
            }
        }
    });
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) Template(com.intellij.codeInsight.template.Template) Result(com.intellij.openapi.application.Result)

Example 5 with TemplateEditingAdapter

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

the class CreateSubclassAction method startTemplate.

private static void startTemplate(PsiTypeParameterList oldTypeParameterList, final Project project, final PsiClass psiClass, final PsiClass targetClass, final boolean includeClassName) {
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
    PsiJavaCodeReferenceElement ref = elementFactory.createClassReferenceElement(psiClass);
    try {
        if (psiClass.isInterface()) {
            ref = (PsiJavaCodeReferenceElement) targetClass.getImplementsList().add(ref);
        } else {
            ref = (PsiJavaCodeReferenceElement) targetClass.getExtendsList().add(ref);
        }
        if (psiClass.hasTypeParameters() || includeClassName) {
            final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.getContainingFile(), targetClass);
            final TemplateBuilderImpl templateBuilder = editor != null ? (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass) : null;
            if (includeClassName && templateBuilder != null) {
                templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName());
            }
            if (oldTypeParameterList != null) {
                for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) {
                    final PsiElement param = ref.getParameterList().add(elementFactory.createTypeElement(elementFactory.createType(parameter)));
                    if (templateBuilder != null) {
                        templateBuilder.replaceElement(param, param.getText());
                    }
                }
            }
            replaceTypeParamsList(targetClass, oldTypeParameterList);
            if (templateBuilder != null) {
                templateBuilder.setEndVariableBefore(ref);
                final Template template = templateBuilder.buildTemplate();
                template.addEndVariable();
                final PsiFile containingFile = targetClass.getContainingFile();
                PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                final TextRange textRange = targetClass.getTextRange();
                final RangeMarker startClassOffset = editor.getDocument().createRangeMarker(textRange.getStartOffset(), textRange.getEndOffset());
                startClassOffset.setGreedyToLeft(true);
                startClassOffset.setGreedyToRight(true);
                editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
                CreateFromUsageBaseFix.startTemplate(editor, template, project, new TemplateEditingAdapter() {

                    @Override
                    public void templateFinished(Template template, boolean brokenOff) {
                        try {
                            LOG.assertTrue(startClassOffset.isValid(), startClassOffset);
                            final PsiElement psiElement = containingFile.findElementAt(startClassOffset.getStartOffset());
                            final PsiClass aTargetClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class);
                            LOG.assertTrue(aTargetClass != null, psiElement);
                            if (!brokenOff) {
                                TransactionGuard.getInstance().submitTransactionAndWait(() -> chooseAndImplement(psiClass, project, aTargetClass, editor));
                            }
                        } finally {
                            startClassOffset.dispose();
                        }
                    }
                }, getTitle(psiClass));
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Template(com.intellij.codeInsight.template.Template) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

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