Search in sources :

Example 51 with FileTemplate

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

the class AppEngineSupportProvider method createFileFromTemplate.

@Nullable
public static VirtualFile createFileFromTemplate(final String templateName, final VirtualFile parent, final String fileName) {
    parent.refresh(false, false);
    final FileTemplate template = FileTemplateManager.getDefaultInstance().getJ2eeTemplate(templateName);
    try {
        final String text = template.getText(FileTemplateManager.getDefaultInstance().getDefaultProperties());
        VirtualFile file = parent.findChild(fileName);
        if (file == null) {
            file = parent.createChildData(AppEngineSupportProvider.class, fileName);
        }
        VfsUtil.saveText(file, text);
        return file;
    } catch (IOException e) {
        LOG.error(e);
        return null;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with FileTemplate

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

the class LightFileTemplatesTest method testSchemas.

public void testSchemas() throws Exception {
    assertEquals(FileTemplatesScheme.DEFAULT, myTemplateManager.getCurrentScheme());
    FileTemplate template = myTemplateManager.getTemplate(TEST_TEMPLATE_TXT);
    assertEquals(HI_THERE, template.getText());
    String newText = "good bye";
    template.setText(newText);
    assertEquals(newText, myTemplateManager.getTemplate(TEST_TEMPLATE_TXT).getText());
    myTemplateManager.setCurrentScheme(myTemplateManager.getProjectScheme());
    assertEquals(HI_THERE, myTemplateManager.getTemplate(TEST_TEMPLATE_TXT).getText());
    myTemplateManager.setCurrentScheme(FileTemplatesScheme.DEFAULT);
    assertEquals(newText, myTemplateManager.getTemplate(TEST_TEMPLATE_TXT).getText());
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 53 with FileTemplate

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

the class LightFileTemplatesTest method testSaveFileAsTemplate.

public void testSaveFileAsTemplate() throws Exception {
    AllFileTemplatesConfigurable configurable = new AllFileTemplatesConfigurable(getProject());
    try {
        configurable.createComponent();
        configurable.reset();
        FileTemplate template = configurable.createNewTemplate("foo", "bar", "hey");
        assertTrue(configurable.isModified());
        FileTemplate[] templates = configurable.getTabs()[0].getTemplates();
        assertTrue(ArrayUtil.contains(template, templates));
        configurable.changeScheme(myTemplateManager.getProjectScheme());
        assertTrue(configurable.isModified());
    //      assertEquals(templates.length, configurable.getTabs()[0].getTemplates().length);
    //      assertTrue(ArrayUtil.contains(template, configurable.getTabs()[0].getTemplates()));
    } finally {
        configurable.disposeUIResources();
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 54 with FileTemplate

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

the class LightFileTemplatesTest method testSurviveOnProjectReopen.

public void testSurviveOnProjectReopen() throws Exception {
    File foo = PlatformTestCase.createTempDir("foo");
    Project reloaded = null;
    final Project project = ProjectManager.getInstance().createProject("foo", foo.getPath());
    try {
        assertNotNull(project);
        FileTemplateManager manager = FileTemplateManager.getInstance(project);
        manager.setCurrentScheme(manager.getProjectScheme());
        FileTemplate template = manager.getTemplate(TEST_TEMPLATE_TXT);
        assertEquals(HI_THERE, template.getText());
        String newText = "good bye";
        template.setText(newText);
        assertEquals(newText, manager.getTemplate(TEST_TEMPLATE_TXT).getText());
        manager.saveAllTemplates();
        PlatformTestUtil.saveProject(project);
        closeProject(project);
        reloaded = ProjectManager.getInstance().loadAndOpenProject(foo.getPath());
        assertNotNull(reloaded);
        manager = FileTemplateManager.getInstance(reloaded);
        assertEquals(manager.getProjectScheme(), manager.getCurrentScheme());
        //manager.setCurrentScheme(FileTemplatesScheme.DEFAULT);
        //manager.setCurrentScheme(manager.getProjectScheme()); // enforce reloading
        assertEquals(newText, manager.getTemplate(TEST_TEMPLATE_TXT).getText());
    } finally {
        closeProject(project);
        closeProject(reloaded);
    }
}
Also used : Project(com.intellij.openapi.project.Project) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) File(java.io.File)

Example 55 with FileTemplate

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

the class LightFileTemplatesTest method testRemoveTemplate.

public void testRemoveTemplate() throws Exception {
    FileTemplate[] before = myTemplateManager.getAllTemplates();
    try {
        FileTemplate template = myTemplateManager.getTemplate(TEST_TEMPLATE_TXT);
        myTemplateManager.removeTemplate(template);
        assertNull(myTemplateManager.getTemplate(TEST_TEMPLATE_TXT));
        myTemplateManager.setCurrentScheme(myTemplateManager.getProjectScheme());
        assertNotNull(myTemplateManager.getTemplate(TEST_TEMPLATE_TXT));
        myTemplateManager.setCurrentScheme(FileTemplatesScheme.DEFAULT);
        assertNull(myTemplateManager.getTemplate(TEST_TEMPLATE_TXT));
    } finally {
        myTemplateManager.setTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY, Arrays.asList(before));
        myTemplateManager.setCurrentScheme(myTemplateManager.getProjectScheme());
        myTemplateManager.setTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY, Arrays.asList(before));
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

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