Search in sources :

Example 11 with TemplateManager

use of com.intellij.codeInsight.template.TemplateManager in project intellij-plugins by JetBrains.

the class CreateToStringFix method processElements.

@Override
protected void processElements(@NotNull final Project project, @NotNull final Editor editor, @NotNull final Set<DartComponent> elementsToProcess) {
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    anchor = doAddMethodsForOne(editor, templateManager, buildFunctionsText(templateManager, elementsToProcess), anchor);
}
Also used : TemplateManager(com.intellij.codeInsight.template.TemplateManager)

Example 12 with TemplateManager

use of com.intellij.codeInsight.template.TemplateManager in project intellij-plugins by JetBrains.

the class CreateConstructorFix method processElements.

@Override
protected void processElements(@NotNull final Project project, @NotNull final Editor editor, @NotNull final Set<DartComponent> elementsToProcess) {
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    anchor = doAddMethodsForOne(editor, templateManager, buildFunctionsText(templateManager, elementsToProcess), anchor);
}
Also used : TemplateManager(com.intellij.codeInsight.template.TemplateManager)

Example 13 with TemplateManager

use of com.intellij.codeInsight.template.TemplateManager 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 14 with TemplateManager

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

the class TryWithResourcesPostfixTemplate method expand.

@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
    PsiExpression expression = JavaPostfixTemplatesUtils.getTopmostExpression(context);
    assert expression != null;
    Project project = context.getProject();
    editor.getDocument().deleteString(expression.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    Template template = manager.createTemplate("", "");
    template.setToReformat(true);
    template.addTextSegment("try (");
    MacroCallNode name = new MacroCallNode(new SuggestVariableNameMacro());
    template.addVariable("type", new TypeExpression(project, new PsiType[] { expression.getType() }), false);
    template.addTextSegment(" ");
    template.addVariable("name", name, name, true);
    template.addTextSegment(" = ");
    template.addVariable("variable", new TextExpression(expression.getText()), false);
    template.addTextSegment(") {\n");
    template.addEndVariable();
    template.addTextSegment("\n}");
    Collection<PsiClassType> unhandled = getUnhandled(expression);
    for (PsiClassType exception : unhandled) {
        MacroCallNode variable = new MacroCallNode(new SuggestVariableNameMacro());
        template.addTextSegment("catch(");
        template.addVariable("type " + exception.getClassName(), new TypeExpression(project, new PsiType[] { exception }), false);
        template.addTextSegment(" ");
        template.addVariable("name " + exception.getClassName(), variable, variable, false);
        template.addTextSegment(") {}");
    }
    manager.startTemplate(editor, template);
}
Also used : Project(com.intellij.openapi.project.Project) TypeExpression(com.intellij.codeInsight.intention.impl.TypeExpression) TemplateManager(com.intellij.codeInsight.template.TemplateManager) SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) Template(com.intellij.codeInsight.template.Template)

Example 15 with TemplateManager

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

the class CreateLocalVariableFromUsageFix method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiFile file = element.getContainingFile();
    PsiClassType type = JavaPsiFacade.getInstance(project).getElementFactory().createTypeByFQClassName("Object", GlobalSearchScope.allScope(project));
    GrVariableDeclaration decl = GroovyPsiElementFactory.getInstance(project).createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, "", type, myRefExpression.getReferenceName());
    int offset = myRefExpression.getTextRange().getStartOffset();
    GrStatement anchor = findAnchor(file, offset);
    TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints(myRefExpression);
    if (myRefExpression.equals(anchor)) {
        decl = myRefExpression.replaceWithStatement(decl);
    } else {
        decl = myOwner.addVariableDeclarationBefore(decl, anchor);
    }
    GrTypeElement typeElement = decl.getTypeElementGroovy();
    assert typeElement != null;
    ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), typeElement.getResolveScope());
    TemplateBuilderImpl builder = new TemplateBuilderImpl(decl);
    builder.replaceElement(typeElement, expr);
    decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
    Template template = builder.buildTemplate();
    Editor newEditor = positionCursor(project, myOwner.getContainingFile(), decl);
    TextRange range = decl.getTextRange();
    newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    manager.startTemplate(newEditor, template);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) TextRange(com.intellij.openapi.util.TextRange) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Template(com.intellij.codeInsight.template.Template) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor)

Aggregations

TemplateManager (com.intellij.codeInsight.template.TemplateManager)20 Template (com.intellij.codeInsight.template.Template)15 Editor (com.intellij.openapi.editor.Editor)9 Project (com.intellij.openapi.project.Project)7 PsiElement (com.intellij.psi.PsiElement)7 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)6 TextRange (com.intellij.openapi.util.TextRange)6 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)4 Document (com.intellij.openapi.editor.Document)4 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)3 SuggestVariableNameMacro (com.intellij.codeInsight.template.macro.SuggestVariableNameMacro)3 RangeMarker (com.intellij.openapi.editor.RangeMarker)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 PsiFile (com.intellij.psi.PsiFile)3 XmlTag (com.intellij.psi.xml.XmlTag)3 Expression (com.intellij.codeInsight.template.Expression)2 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 PsiReference (com.intellij.psi.PsiReference)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2