Search in sources :

Example 21 with FileTemplate

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

the class LightFileTemplatesTest method testAddRemoveShared.

public void testAddRemoveShared() throws Exception {
    File foo = PlatformTestCase.createTempDir("foo");
    final Project project = ProjectManager.getInstance().createProject("foo", foo.getPath());
    try {
        assertNotNull(project);
        FileTemplateManager manager = FileTemplateManager.getInstance(project);
        manager.setCurrentScheme(manager.getProjectScheme());
        manager.saveAllTemplates();
        FileTemplateSettings settings = ServiceManager.getService(project, FileTemplateSettings.class);
        FTManager ftManager = settings.getDefaultTemplatesManager();
        File root = ftManager.getConfigRoot(false);
        assertTrue(root.exists());
        File file = new File(root, "Foo.java");
        assertTrue(file.createNewFile());
        manager.saveAllTemplates();
        assertTrue(file.exists());
        /*
      FileTemplate template = manager.addTemplate("Foo", "java");
      // now remove it via "remove template" call
      manager.removeTemplate(template);
      manager.saveAllTemplates();
      assertFalse(file.exists());
      */
        // check "setTemplates" call
        FileTemplateBase templateBase = (FileTemplateBase) manager.addTemplate("Foo", "java");
        List<FileTemplate> templates = new ArrayList<>(ftManager.getAllTemplates(true));
        assertTrue(templates.contains(templateBase));
        ftManager.saveTemplates();
        assertTrue(file.exists());
        templates.remove(templateBase);
        manager.setTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY, templates);
        assertFalse(file.exists());
    } finally {
        closeProject(project);
    }
}
Also used : Project(com.intellij.openapi.project.Project) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) ArrayList(java.util.ArrayList) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) File(java.io.File)

Example 22 with FileTemplate

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

the class LightFileTemplatesTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    myTemplateManager = FileTemplateManagerImpl.getInstanceImpl(getProject());
    FileTemplate template = myTemplateManager.getTemplate(TEST_TEMPLATE_TXT);
    ((BundledFileTemplate) template).revertToDefaults();
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 23 with FileTemplate

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

the class GradleModuleBuilder method saveFile.

private static void saveFile(@NotNull VirtualFile file, @NotNull String templateName, @Nullable Map templateAttributes) throws ConfigurationException {
    FileTemplateManager manager = FileTemplateManager.getDefaultInstance();
    FileTemplate template = manager.getInternalTemplate(templateName);
    try {
        appendToFile(file, templateAttributes != null ? template.getText(templateAttributes) : template.getText());
    } catch (IOException e) {
        LOG.warn(String.format("Unexpected exception on applying template %s config", GradleConstants.SYSTEM_ID.getReadableName()), e);
        throw new ConfigurationException(e.getMessage(), String.format("Can't apply %s template config text", GradleConstants.SYSTEM_ID.getReadableName()));
    }
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) IOException(java.io.IOException)

Example 24 with FileTemplate

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

the class GradleModuleBuilder method appendToFile.

private static void appendToFile(@NotNull VirtualFile file, @NotNull String templateName, @Nullable Map templateAttributes) throws ConfigurationException {
    FileTemplateManager manager = FileTemplateManager.getDefaultInstance();
    FileTemplate template = manager.getInternalTemplate(templateName);
    try {
        appendToFile(file, templateAttributes != null ? template.getText(templateAttributes) : template.getText());
    } catch (IOException e) {
        LOG.warn(String.format("Unexpected exception on appending template %s config", GradleConstants.SYSTEM_ID.getReadableName()), e);
        throw new ConfigurationException(e.getMessage(), String.format("Can't append %s template config text", GradleConstants.SYSTEM_ID.getReadableName()));
    }
}
Also used : ConfigurationException(com.intellij.openapi.options.ConfigurationException) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) IOException(java.io.IOException)

Example 25 with FileTemplate

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

the class NewGroovyClassAction method buildDialog.

@Override
protected void buildDialog(Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder) {
    builder.setTitle(GroovyBundle.message("newclass.dlg.title")).addKind("Class", JetgroovyIcons.Groovy.Class, GroovyTemplates.GROOVY_CLASS).addKind("Interface", JetgroovyIcons.Groovy.Interface, GroovyTemplates.GROOVY_INTERFACE);
    if (GroovyConfigUtils.getInstance().isVersionAtLeast(directory, GroovyConfigUtils.GROOVY2_3, true)) {
        builder.addKind("Trait", JetgroovyIcons.Groovy.Trait, GroovyTemplates.GROOVY_TRAIT);
    }
    builder.addKind("Enum", JetgroovyIcons.Groovy.Enum, GroovyTemplates.GROOVY_ENUM).addKind("Annotation", JetgroovyIcons.Groovy.AnnotationType, GroovyTemplates.GROOVY_ANNOTATION);
    for (FileTemplate template : FileTemplateManager.getInstance(project).getAllTemplates()) {
        FileType fileType = FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(template.getExtension());
        if (fileType.equals(GroovyFileType.GROOVY_FILE_TYPE) && JavaDirectoryService.getInstance().getPackage(directory) != null) {
            builder.addKind(template.getName(), JetgroovyIcons.Groovy.Class, template.getName());
        }
    }
}
Also used : GroovyFileType(org.jetbrains.plugins.groovy.GroovyFileType) FileType(com.intellij.openapi.fileTypes.FileType) 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