Search in sources :

Example 51 with Template

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

the class CreateToStringFix method buildFunctionsText.

protected Template buildFunctionsText(TemplateManager templateManager, Set<DartComponent> elementsToProcess) {
    final Template template = templateManager.createTemplate(getClass().getName(), DART_TEMPLATE_GROUP);
    template.setToReformat(true);
    if (CodeStyleSettingsManager.getSettings(myDartClass.getProject()).INSERT_OVERRIDE_ANNOTATION) {
        template.addTextSegment("@override\n");
    }
    template.addTextSegment("String toString() {");
    template.addTextSegment(DartTokenTypes.RETURN.toString());
    template.addTextSegment(" ");
    template.addTextSegment("'");
    //noinspection ConstantConditions
    template.addTextSegment(myDartClass.getName());
    template.addTextSegment("{");
    for (Iterator<DartComponent> iterator = elementsToProcess.iterator(); iterator.hasNext(); ) {
        DartComponent component = iterator.next();
        //noinspection ConstantConditions
        template.addTextSegment(component.getName());
        template.addTextSegment(": $");
        //noinspection ConstantConditions
        template.addTextSegment(component.getName());
        if (iterator.hasNext()) {
            template.addTextSegment(", ");
        }
    }
    template.addTextSegment("}';\n");
    template.addTextSegment("}");
    template.addEndVariable();
    // trailing space is removed when auto-reformatting, but it helps to enter line break if needed
    template.addTextSegment(" ");
    return template;
}
Also used : DartComponent(com.jetbrains.lang.dart.psi.DartComponent) Template(com.intellij.codeInsight.template.Template)

Example 52 with Template

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

the class AbstractStepDefinitionCreator method closeActiveTemplateBuilders.

protected void closeActiveTemplateBuilders(PsiFile file) {
    final Project project = file.getProject();
    final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
    final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
    FileEditorManager.getInstance(project).getAllEditors(vFile);
    FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    assert editor != null;
    final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
    final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    final Template template = templateManager.getActiveTemplate(editor);
    if (templateState != null && template != null) {
        templateState.gotoEnd(false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) TemplateManager(com.intellij.codeInsight.template.TemplateManager) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Template(com.intellij.codeInsight.template.Template)

Example 53 with Template

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

the class CreateEventMetadataByMxmlAttributeFix method applyFix.

@Override
protected void applyFix(final Project project, final PsiElement psiElement, PsiFile file, Editor editor) {
    assert psiElement instanceof XmlAttribute;
    final XmlTag tag = (XmlTag) psiElement.getParent();
    final XmlElementDescriptor descriptor = tag.getDescriptor();
    PsiElement type = descriptor == null ? null : descriptor.getDeclaration();
    if (type == null) {
        // can not resolve
        return;
    }
    if (!FileModificationService.getInstance().preparePsiElementForWrite(type)) {
        return;
    }
    file = type.getContainingFile();
    editor = getEditor(project, file);
    if (editor == null) {
        return;
    }
    final boolean addingToMxml = type instanceof XmlFile;
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    final Template template = templateManager.createTemplate("", "");
    template.setToReformat(true);
    if (addingToMxml) {
        template.addTextSegment("\n");
    }
    template.addTextSegment("[Event(name=\"" + myEventName + "\", type=\"");
    template.addVariable(new MyExpression(FlexCommonTypeNames.FLASH_EVENT_FQN), true);
    template.addTextSegment("\")]");
    if (!addingToMxml) {
        template.addTextSegment("\n");
    }
    final int offset;
    if (addingToMxml) {
        XmlTag metadataTag = WriteAction.compute(() -> createOrGetMetadataTag((XmlFile) type));
        offset = metadataTag.getValue().getTextRange().getStartOffset();
    } else {
        offset = type.getTextRange().getStartOffset();
    }
    navigate(project, editor, offset, file.getVirtualFile());
    templateManager.startTemplate(editor, template);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) TemplateManager(com.intellij.codeInsight.template.TemplateManager) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Template(com.intellij.codeInsight.template.Template)

Example 54 with Template

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

the class GenerateFlexUnitMethodActionBase method getGenerateHandler.

protected BaseJSGenerateHandler getGenerateHandler() {
    return new BaseJSGenerateHandler() {

        protected String getTitleKey() {
            return null;
        }

        protected BaseCreateMethodsFix createFix(JSClass clazz) {
            return new BaseCreateMethodsFix(clazz) {

                public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
                    evalAnchor(editor, file);
                    final PsiElement addedElement = doAddOneMethod(project, "public function fake():void{}", anchor);
                    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
                    final TemplateManager manager = TemplateManager.getInstance(project);
                    final Template template = manager.createTemplate("", "");
                    template.setToReformat(true);
                    buildTemplate(template, myJsClass);
                    final TextRange range = addedElement.getTextRange();
                    editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), "");
                    editor.getCaretModel().moveToOffset(range.getStartOffset());
                    manager.startTemplate(editor, template);
                }
            };
        }

        protected boolean canHaveEmptySelectedElements() {
            return true;
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) TemplateManager(com.intellij.codeInsight.template.TemplateManager) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) BaseCreateMethodsFix(com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix) Template(com.intellij.codeInsight.template.Template)

Aggregations

Template (com.intellij.codeInsight.template.Template)54 Editor (com.intellij.openapi.editor.Editor)20 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)19 Project (com.intellij.openapi.project.Project)17 TemplateManager (com.intellij.codeInsight.template.TemplateManager)15 TextRange (com.intellij.openapi.util.TextRange)15 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)12 PsiElement (com.intellij.psi.PsiElement)11 RangeMarker (com.intellij.openapi.editor.RangeMarker)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 NotNull (org.jetbrains.annotations.NotNull)5 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)4 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)4 Document (com.intellij.openapi.editor.Document)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