Search in sources :

Example 1 with TemplateManager

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

the class CreateFieldFix method doFix.

protected void doFix(@NotNull Project project, @NotNull @GrModifier.ModifierConstant String[] modifiers, @NotNull @NonNls String fieldName, @NotNull TypeConstraint[] typeConstraints, @NotNull PsiElement context) throws IncorrectOperationException {
    JVMElementFactory factory = JVMElementFactories.getFactory(myTargetClass.getLanguage(), project);
    if (factory == null)
        return;
    PsiField field = factory.createField(fieldName, PsiType.INT);
    if (myTargetClass instanceof GroovyScriptClass) {
        field.getModifierList().addAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD);
    }
    for (@GrModifier.ModifierConstant String modifier : modifiers) {
        PsiUtil.setModifierProperty(field, modifier, true);
    }
    field = CreateFieldFromUsageHelper.insertField(myTargetClass, field, context);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(field.getParent());
    Editor newEditor = IntentionUtils.positionCursor(project, myTargetClass.getContainingFile(), field);
    Template template = CreateFieldFromUsageHelper.setupTemplate(field, typeConstraints, myTargetClass, newEditor, context, false);
    TemplateManager manager = TemplateManager.getInstance(project);
    manager.startTemplate(newEditor, template);
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor) Template(com.intellij.codeInsight.template.Template)

Example 2 with TemplateManager

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

the class AddDtdDeclarationFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    final PsiFile containingFile = element.getContainingFile();
    @NonNls String prefixToInsert = "";
    @NonNls String suffixToInsert = "";
    final int UNDEFINED_OFFSET = -1;
    int anchorOffset = UNDEFINED_OFFSET;
    PsiElement anchor = PsiTreeUtil.getParentOfType(element, XmlElementDecl.class, XmlAttlistDecl.class, XmlEntityDecl.class, XmlConditionalSection.class);
    if (anchor != null)
        anchorOffset = anchor.getTextRange().getStartOffset();
    if (anchorOffset == UNDEFINED_OFFSET && containingFile.getLanguage() == XMLLanguage.INSTANCE) {
        XmlFile file = (XmlFile) containingFile;
        final XmlProlog prolog = file.getDocument().getProlog();
        assert prolog != null;
        final XmlDoctype doctype = prolog.getDoctype();
        final XmlMarkupDecl markupDecl;
        if (doctype != null) {
            markupDecl = doctype.getMarkupDecl();
        } else {
            markupDecl = null;
        }
        if (doctype == null) {
            final XmlTag rootTag = file.getDocument().getRootTag();
            prefixToInsert = "<!DOCTYPE " + ((rootTag != null) ? rootTag.getName() : "null");
            suffixToInsert = ">\n";
        }
        if (markupDecl == null) {
            prefixToInsert += " [\n";
            suffixToInsert = "]" + suffixToInsert;
            if (doctype != null) {
                // just before last '>'
                anchorOffset = doctype.getTextRange().getEndOffset() - 1;
            } else {
                anchorOffset = prolog.getTextRange().getEndOffset();
            }
        }
    }
    if (anchorOffset == UNDEFINED_OFFSET)
        anchorOffset = element.getTextRange().getStartOffset();
    OpenFileDescriptor openDescriptor = new OpenFileDescriptor(project, containingFile.getVirtualFile(), anchorOffset);
    final Editor editor = FileEditorManager.getInstance(project).openTextEditor(openDescriptor, true);
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    final Template t = templateManager.createTemplate("", "");
    if (!prefixToInsert.isEmpty())
        t.addTextSegment(prefixToInsert);
    t.addTextSegment("<!" + myElementDeclarationName + " " + myReference + " ");
    t.addEndVariable();
    t.addTextSegment(">\n");
    if (!suffixToInsert.isEmpty())
        t.addTextSegment(suffixToInsert);
    templateManager.startTemplate(editor, t);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) Template(com.intellij.codeInsight.template.Template) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with TemplateManager

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

the class StringBasedPostfixTemplate method expandForChooseExpression.

@Override
public final void expandForChooseExpression(@NotNull PsiElement expr, @NotNull Editor editor) {
    Project project = expr.getProject();
    Document document = editor.getDocument();
    PsiElement elementForRemoving = getElementToRemove(expr);
    document.deleteString(elementForRemoving.getTextRange().getStartOffset(), elementForRemoving.getTextRange().getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    String templateString = getTemplateString(expr);
    if (templateString == null) {
        PostfixTemplatesUtils.showErrorHint(expr.getProject(), editor);
        return;
    }
    Template template = createTemplate(manager, templateString);
    if (shouldAddExpressionToContext()) {
        template.addVariable("expr", new TextExpression(expr.getText()), false);
    }
    setVariables(template, expr);
    manager.startTemplate(editor, template);
}
Also used : Project(com.intellij.openapi.project.Project) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) Template(com.intellij.codeInsight.template.Template)

Example 4 with TemplateManager

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

the class JsonLiveTemplateTest method createJsonTemplate.

@NotNull
private Template createJsonTemplate(@NotNull String name, @NotNull String group, @NotNull String text) {
    final TemplateManager templateManager = TemplateManager.getInstance(getProject());
    final Template template = templateManager.createTemplate(name, group, text);
    final TemplateContextType context = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JsonContextType.class);
    assertNotNull(context);
    ((TemplateImpl) template).getTemplateContext().setEnabled(context, true);
    CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable());
    return template;
}
Also used : TemplateManager(com.intellij.codeInsight.template.TemplateManager) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType) Template(com.intellij.codeInsight.template.Template) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TemplateManager

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

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