Search in sources :

Example 1 with Template

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

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

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

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

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

the class CreateEnumConstantFromUsageFix method invokeImpl.

@Override
protected void invokeImpl(final PsiClass targetClass) {
    LOG.assertTrue(targetClass.isEnum());
    final String name = myReferenceExpression.getReferenceName();
    LOG.assertTrue(name != null);
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(myReferenceExpression.getProject()).getElementFactory();
    PsiEnumConstant enumConstant = elementFactory.createEnumConstantFromText(name, null);
    enumConstant = (PsiEnumConstant) targetClass.add(enumConstant);
    final PsiMethod[] constructors = targetClass.getConstructors();
    if (constructors.length > 0) {
        final PsiMethod constructor = constructors[0];
        final PsiParameter[] parameters = constructor.getParameterList().getParameters();
        if (parameters.length > 0) {
            final String params = StringUtil.join(parameters, psiParameter -> psiParameter.getName(), ",");
            enumConstant = (PsiEnumConstant) enumConstant.replace(elementFactory.createEnumConstantFromText(name + "(" + params + ")", null));
            final TemplateBuilderImpl builder = new TemplateBuilderImpl(enumConstant);
            final PsiExpressionList argumentList = enumConstant.getArgumentList();
            LOG.assertTrue(argumentList != null);
            for (PsiExpression expression : argumentList.getExpressions()) {
                builder.replaceElement(expression, new EmptyExpression());
            }
            enumConstant = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(enumConstant);
            final Template template = builder.buildTemplate();
            final Project project = targetClass.getProject();
            final Editor newEditor = positionCursor(project, targetClass.getContainingFile(), enumConstant);
            if (newEditor != null) {
                final TextRange range = enumConstant.getTextRange();
                newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
                startTemplate(newEditor, template, project);
            }
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Template(com.intellij.codeInsight.template.Template) Project(com.intellij.openapi.project.Project) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Editor(com.intellij.openapi.editor.Editor)

Aggregations

Template (com.intellij.codeInsight.template.Template)57 Editor (com.intellij.openapi.editor.Editor)23 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)19 TemplateManager (com.intellij.codeInsight.template.TemplateManager)18 Project (com.intellij.openapi.project.Project)17 TextRange (com.intellij.openapi.util.TextRange)15 PsiElement (com.intellij.psi.PsiElement)13 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)12 RangeMarker (com.intellij.openapi.editor.RangeMarker)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 Document (com.intellij.openapi.editor.Document)5 NotNull (org.jetbrains.annotations.NotNull)5 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)4 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)4 DartComponent (com.jetbrains.lang.dart.psi.DartComponent)4 Expression (com.intellij.codeInsight.template.Expression)3 SuggestVariableNameMacro (com.intellij.codeInsight.template.macro.SuggestVariableNameMacro)3 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 XmlTag (com.intellij.psi.xml.XmlTag)3