Search in sources :

Example 16 with TemplateBuilderImpl

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

the class DefineParamsDefaultValueAction method startTemplate.

public static void startTemplate(@NotNull Project project, Editor editor, PsiExpression[] argsToBeDelegated, PsiMethod delegateMethod) {
    TemplateBuilderImpl builder = new TemplateBuilderImpl(delegateMethod);
    RangeMarker rangeMarker = editor.getDocument().createRangeMarker(delegateMethod.getTextRange());
    for (final PsiExpression exprToBeDefault : argsToBeDelegated) {
        builder.replaceElement(exprToBeDefault, new TextExpression(""));
    }
    Template template = builder.buildTemplate();
    editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset());
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    editor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset());
    rangeMarker.dispose();
    CreateFromUsageBaseFix.startTemplate(editor, template, project);
}
Also used : TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) RangeMarker(com.intellij.openapi.editor.RangeMarker) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) Template(com.intellij.codeInsight.template.Template)

Example 17 with TemplateBuilderImpl

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

the class HighlightUtil method showRenameTemplate.

public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element) {
    context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
    final Query<PsiReference> query = ReferencesSearch.search(element, element.getUseScope());
    final Collection<PsiReference> references = query.findAll();
    final Project project = context.getProject();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    final Editor editor = fileEditorManager.getSelectedTextEditor();
    if (editor == null) {
        return;
    }
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(context);
    final Expression macroCallNode = new MacroCallNode(new SuggestVariableNameMacro());
    final PsiElement identifier = element.getNameIdentifier();
    builder.replaceElement(identifier, "PATTERN", macroCallNode, true);
    for (PsiReference reference : references) {
        builder.replaceElement(reference, "PATTERN", "PATTERN", false);
    }
    final Template template = builder.buildInlineTemplate();
    final TextRange textRange = context.getTextRange();
    final int startOffset = textRange.getStartOffset();
    editor.getCaretModel().moveToOffset(startOffset);
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    templateManager.startTemplate(editor, template);
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Template(com.intellij.codeInsight.template.Template) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Expression(com.intellij.codeInsight.template.Expression) TemplateManager(com.intellij.codeInsight.template.TemplateManager) SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 18 with TemplateBuilderImpl

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

the class AddWithParamFix method invoke.

public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) throws IncorrectOperationException {
    final RunResult<SmartPsiElementPointer<XmlTag>> result = new WriteAction<SmartPsiElementPointer<XmlTag>>() {

        protected void run(@NotNull Result<SmartPsiElementPointer<XmlTag>> result) throws Throwable {
            final XmlTag withParamTag = RefactoringUtil.addWithParam(myTag);
            withParamTag.setAttribute("name", myName != null ? myName : "dummy");
            withParamTag.setAttribute("select", "dummy");
            result.setResult(SmartPointerManager.getInstance(project).createSmartPsiElementPointer(withParamTag));
        }
    }.execute();
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    final Document doc = psiDocumentManager.getDocument(file);
    assert doc != null;
    psiDocumentManager.doPostponedOperationsAndUnblockDocument(doc);
    final XmlTag withParamTag = result.getResultObject().getElement();
    assert withParamTag != null;
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(withParamTag);
    final XmlAttribute selectAttr = withParamTag.getAttribute("select", null);
    assert selectAttr != null;
    PsiElement dummy = XsltSupport.getAttValueToken(selectAttr);
    builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    if (myName == null) {
        final XmlAttribute nameAttr = withParamTag.getAttribute("name", null);
        assert nameAttr != null;
        dummy = XsltSupport.getAttValueToken(nameAttr);
        builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    }
    moveTo(editor, withParamTag);
    new WriteAction() {

        @SuppressWarnings({ "RawUseOfParameterizedType" })
        protected void run(@NotNull Result result) throws Throwable {
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            final TemplateManager mgr = TemplateManager.getInstance(myTag.getProject());
            mgr.startTemplate(editor, builder.buildInlineTemplate());
        }
    }.execute();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) WriteAction(com.intellij.openapi.application.WriteAction) Document(com.intellij.openapi.editor.Document) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) Result(com.intellij.openapi.application.Result) RunResult(com.intellij.openapi.application.RunResult) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TemplateManager(com.intellij.codeInsight.template.TemplateManager) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlTag(com.intellij.psi.xml.XmlTag)

Example 19 with TemplateBuilderImpl

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

the class CreateVariableFix method invoke.

public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    editor = editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
    XmlTag tag = PsiTreeUtil.getContextOfType(myReference, XmlTag.class, true);
    if (tag == null)
        return;
    XmlTag xmlTag = tag.createChildTag("variable", XsltSupport.XSLT_NS, null, false);
    xmlTag.setAttribute("name", myReference.getReferencedName());
    xmlTag.setAttribute("select", "dummy");
    final XmlAttribute select = xmlTag.getAttribute("select", null);
    assert select != null;
    final PsiElement dummy = XsltSupport.getAttValueToken(select);
    assert dummy != null;
    final TemplateBuilderImpl builder = createTemplateBuilder(xmlTag);
    builder.replaceElement(dummy, new MacroCallNode(new CompleteMacro()));
    builder.setEndVariableAfter(select);
    final Template template = builder.buildTemplate();
    template.addTextSegment("\n");
    template.setToIndent(true);
    final XmlTag insertionPoint = findVariableInsertionPoint(tag);
    moveTo(editor, insertionPoint);
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) XmlAttribute(com.intellij.psi.xml.XmlAttribute) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) CompleteMacro(com.intellij.codeInsight.template.macro.CompleteMacro) PsiElement(com.intellij.psi.PsiElement) EditorWindow(com.intellij.injected.editor.EditorWindow) XmlTag(com.intellij.psi.xml.XmlTag) Template(com.intellij.codeInsight.template.Template)

Example 20 with TemplateBuilderImpl

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

the class GrCreateSubclassAction method startTemplate.

private static void startTemplate(GrTypeParameterList oldTypeParameterList, final Project project, final GrTypeDefinition psiClass, final GrTypeDefinition targetClass, boolean includeClassName) {
    PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(project);
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    GrCodeReferenceElement stubRef = elementFactory.createCodeReferenceElementFromClass(psiClass);
    try {
        GrReferenceList clause = psiClass.isInterface() ? targetClass.getImplementsClause() : targetClass.getExtendsClause();
        if (clause == null) {
            GrReferenceList stubRefList = psiClass.isInterface() ? elementFactory.createImplementsClause() : elementFactory.createExtendsClause();
            clause = (GrExtendsClause) targetClass.addAfter(stubRefList, targetClass.getNameIdentifierGroovy());
        }
        GrCodeReferenceElement ref = (GrCodeReferenceElement) clause.add(stubRef);
        if (psiClass.hasTypeParameters() || includeClassName) {
            final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.getContainingFile(), targetClass);
            final TemplateBuilderImpl templateBuilder = editor == null || ApplicationManager.getApplication().isUnitTestMode() ? null : (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass);
            if (includeClassName && templateBuilder != null) {
                templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName());
            }
            if (oldTypeParameterList != null && oldTypeParameterList.getTypeParameters().length > 0) {
                GrTypeArgumentList existingList = ref.getTypeArgumentList();
                final GrTypeParameterList typeParameterList = (GrTypeParameterList) targetClass.addAfter(elementFactory.createTypeParameterList(), targetClass.getNameIdentifierGroovy());
                GrTypeArgumentList argList;
                if (existingList == null) {
                    GrCodeReferenceElement codeRef = elementFactory.createCodeReferenceElementFromText("A<T>");
                    argList = ((GrTypeArgumentList) ref.add(codeRef.getTypeArgumentList()));
                    argList.getTypeArgumentElements()[0].delete();
                } else {
                    argList = existingList;
                }
                for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) {
                    final PsiElement param = argList.add(elementFactory.createTypeElement(jfactory.createType(parameter)));
                    if (templateBuilder != null) {
                        templateBuilder.replaceElement(param, param.getText());
                    }
                    typeParameterList.add(elementFactory.createTypeParameter(parameter.getName(), parameter.getExtendsListTypes()));
                }
            }
            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) {
                        chooseAndImplement(psiClass, project, PsiTreeUtil.getParentOfType(containingFile.findElementAt(startClassOffset.getStartOffset()), GrTypeDefinition.class), editor);
                    }
                }, getTitle(psiClass));
            }
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) TextRange(com.intellij.openapi.util.TextRange) RangeMarker(com.intellij.openapi.editor.RangeMarker) Template(com.intellij.codeInsight.template.Template) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrReferenceList(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)

Aggregations

TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)24 Template (com.intellij.codeInsight.template.Template)19 Editor (com.intellij.openapi.editor.Editor)15 TextRange (com.intellij.openapi.util.TextRange)13 Project (com.intellij.openapi.project.Project)11 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)8 RangeMarker (com.intellij.openapi.editor.RangeMarker)8 TemplateManager (com.intellij.codeInsight.template.TemplateManager)7 PsiElement (com.intellij.psi.PsiElement)7 Document (com.intellij.openapi.editor.Document)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)4 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)3 TypeConstraint (org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint)3 ChooseTypeExpression (org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression)3 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 Expression (com.intellij.codeInsight.template.Expression)2 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)2 CompleteMacro (com.intellij.codeInsight.template.macro.CompleteMacro)2