Search in sources :

Example 1 with SuggestVariableNameMacro

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

the class ForeachPostfixTemplate method setVariables.

@Override
public void setVariables(@NotNull Template template, @NotNull PsiElement element) {
    MacroCallNode type = new MacroCallNode(new IterableComponentTypeMacro());
    MacroCallNode name = new MacroCallNode(new SuggestVariableNameMacro());
    type.addParameter(new VariableNode("expr", null));
    template.addVariable("type", type, type, false);
    template.addVariable("name", name, name, true);
}
Also used : VariableNode(com.intellij.codeInsight.template.impl.VariableNode) IterableComponentTypeMacro(com.intellij.codeInsight.template.macro.IterableComponentTypeMacro) SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode)

Example 2 with SuggestVariableNameMacro

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

the class HighlightUtils method showRenameTemplate.

public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element, PsiReference... references) {
    context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
    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 3 with SuggestVariableNameMacro

use of com.intellij.codeInsight.template.macro.SuggestVariableNameMacro 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 4 with SuggestVariableNameMacro

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

the class ForIndexedPostfixTemplate method setVariables.

@Override
public void setVariables(@NotNull Template template, @NotNull PsiElement element) {
    MacroCallNode index = new MacroCallNode(new SuggestVariableNameMacro());
    template.addVariable("index", index, index, true);
}
Also used : SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode)

Example 5 with SuggestVariableNameMacro

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

Aggregations

MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)5 SuggestVariableNameMacro (com.intellij.codeInsight.template.macro.SuggestVariableNameMacro)5 Template (com.intellij.codeInsight.template.Template)3 TemplateManager (com.intellij.codeInsight.template.TemplateManager)3 Project (com.intellij.openapi.project.Project)3 Expression (com.intellij.codeInsight.template.Expression)2 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2 Editor (com.intellij.openapi.editor.Editor)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)2 TypeExpression (com.intellij.codeInsight.intention.impl.TypeExpression)1 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)1 VariableNode (com.intellij.codeInsight.template.impl.VariableNode)1 IterableComponentTypeMacro (com.intellij.codeInsight.template.macro.IterableComponentTypeMacro)1