Search in sources :

Example 16 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class CreateFromTemplateActionBase method actionPerformed.

@Override
public final void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
    if (view == null)
        return;
    PsiDirectory dir = getTargetDirectory(dataContext, view);
    if (dir == null)
        return;
    Project project = dir.getProject();
    FileTemplate selectedTemplate = getTemplate(project, dir);
    if (selectedTemplate != null) {
        AnAction action = getReplacedAction(selectedTemplate);
        if (action != null) {
            action.actionPerformed(e);
        } else {
            FileTemplateManager.getInstance(project).addRecentName(selectedTemplate.getName());
            AttributesDefaults defaults = getAttributesDefaults(dataContext);
            Properties properties = defaults != null ? defaults.getDefaultProperties() : null;
            CreateFromTemplateDialog dialog = new CreateFromTemplateDialog(project, dir, selectedTemplate, defaults, properties);
            PsiElement createdElement = dialog.create();
            if (createdElement != null) {
                elementCreated(dialog, createdElement);
                view.selectElement(createdElement);
                if (selectedTemplate.isLiveTemplateEnabled() && createdElement instanceof PsiFile) {
                    Map<String, String> defaultValues = getLiveTemplateDefaults(dataContext, ((PsiFile) createdElement));
                    startLiveTemplate((PsiFile) createdElement, notNull(defaultValues, Collections.emptyMap()));
                }
            }
        }
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IdeView(com.intellij.ide.IdeView) AnAction(com.intellij.openapi.actionSystem.AnAction) CreateFromTemplateDialog(com.intellij.ide.fileTemplates.ui.CreateFromTemplateDialog) Project(com.intellij.openapi.project.Project) DataContext(com.intellij.openapi.actionSystem.DataContext) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 17 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class JavaTestFramework method createSetUpPatternMethod.

public PsiMethod createSetUpPatternMethod(JVMElementFactory factory) {
    final FileTemplate template = FileTemplateManager.getDefaultInstance().getCodeTemplate(getSetUpMethodFileTemplateDescriptor().getFileName());
    final String templateText = StringUtil.replace(StringUtil.replace(template.getText(), "${BODY}\n", ""), "${NAME}", "setUp");
    return factory.createMethodFromText(templateText, null);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 18 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class ConfigFileFactoryImpl method getText.

private static String getText(final String templateName, @Nullable Project project) throws IOException {
    final FileTemplateManager templateManager = project == null ? FileTemplateManager.getDefaultInstance() : FileTemplateManager.getInstance(project);
    final FileTemplate template = templateManager.getJ2eeTemplate(templateName);
    if (template == null) {
        return "";
    }
    return template.getText(templateManager.getDefaultProperties());
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager)

Example 19 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class CreateFromUsageUtils method setupMethodBody.

public static void setupMethodBody(final PsiMethod method, final PsiClass aClass) throws IncorrectOperationException {
    FileTemplate template = FileTemplateManager.getInstance(method.getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_FROM_USAGE_METHOD_BODY);
    setupMethodBody(method, aClass, template);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 20 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class LightFileTemplatesTest method testSaveReformatCode.

public void testSaveReformatCode() throws Exception {
    FileTemplate template = myTemplateManager.getTemplate(TEST_TEMPLATE_TXT);
    assertTrue(template.isReformatCode());
    template.setReformatCode(false);
    FileTemplateSettings settings = ServiceManager.getService(ExportableFileTemplateSettings.class);
    Element state = settings.getState();
    assertNotNull(state);
    Element element = state.getChildren().get(0).getChildren().get(0);
    assertEquals("<template name=\"testTemplate.txt\" reformat=\"false\" live-template-enabled=\"false\" enabled=\"true\" />", JDOMUtil.writeElement(element));
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Element(org.jdom.Element)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)66 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)20 Project (com.intellij.openapi.project.Project)13 IOException (java.io.IOException)12 PsiFile (com.intellij.psi.PsiFile)10 Properties (java.util.Properties)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 PsiDirectory (com.intellij.psi.PsiDirectory)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiElement (com.intellij.psi.PsiElement)6 Module (com.intellij.openapi.module.Module)5 File (java.io.File)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 Nullable (org.jetbrains.annotations.Nullable)4 Editor (com.intellij.openapi.editor.Editor)3 ConfigurationException (com.intellij.openapi.options.ConfigurationException)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)2