Search in sources :

Example 1 with FileTemplateManager

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

the class PyMoveTest method testTemplateAttributesExpansionInCreatedDestinationModule.

// PY-14979
public void testTemplateAttributesExpansionInCreatedDestinationModule() {
    final FileTemplateManager instance = FileTemplateManager.getInstance(myFixture.getProject());
    final FileTemplate template = instance.getInternalTemplate("Python Script");
    assertNotNull(template);
    final String oldTemplateContent = template.getText();
    try {
        template.setText("NAME = '${NAME}'");
        doMoveSymbolTest("C", "b.py");
    } finally {
        template.setText(oldTemplateContent);
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager)

Example 2 with FileTemplateManager

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

the class CreatePackageAction method createInitPy.

private static void createInitPy(PsiDirectory directory) {
    final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(directory.getProject());
    final FileTemplate template = fileTemplateManager.getInternalTemplate("Python Script");
    if (directory.findFile(PyNames.INIT_DOT_PY) != null) {
        return;
    }
    if (template != null) {
        try {
            FileTemplateUtil.createFromTemplate(template, PyNames.INIT_DOT_PY, fileTemplateManager.getDefaultProperties(), directory);
        } catch (Exception e) {
            LOG.error(e);
        }
    } else {
        final PsiFile file = PsiFileFactory.getInstance(directory.getProject()).createFileFromText(PyNames.INIT_DOT_PY, "");
        directory.add(file);
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) PsiFile(com.intellij.psi.PsiFile)

Example 3 with FileTemplateManager

use of com.intellij.ide.fileTemplates.FileTemplateManager 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 4 with FileTemplateManager

use of com.intellij.ide.fileTemplates.FileTemplateManager 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 5 with FileTemplateManager

use of com.intellij.ide.fileTemplates.FileTemplateManager in project android by JetBrains.

the class AndroidResourceUtil method createFileResource.

@NotNull
public static XmlFile createFileResource(@NotNull String fileName, @NotNull PsiDirectory resSubdir, @NotNull String rootTagName, @NotNull String resourceType, boolean valuesResourceFile) throws Exception {
    FileTemplateManager manager = FileTemplateManager.getInstance(resSubdir.getProject());
    String templateName = getTemplateName(resourceType, valuesResourceFile, rootTagName);
    FileTemplate template = manager.getJ2eeTemplate(templateName);
    Properties properties = new Properties();
    if (!valuesResourceFile) {
        properties.setProperty(ROOT_TAG_PROPERTY, rootTagName);
    }
    if (ResourceType.LAYOUT.getName().equals(resourceType)) {
        final Module module = ModuleUtilCore.findModuleForPsiElement(resSubdir);
        final AndroidPlatform platform = module != null ? AndroidPlatform.getInstance(module) : null;
        final int apiLevel = platform != null ? platform.getApiLevel() : -1;
        final String value = apiLevel == -1 || apiLevel >= 8 ? "match_parent" : "fill_parent";
        properties.setProperty(LAYOUT_WIDTH_PROPERTY, value);
        properties.setProperty(LAYOUT_HEIGHT_PROPERTY, value);
    }
    PsiElement createdElement = FileTemplateUtil.createFromTemplate(template, fileName, properties, resSubdir);
    assert createdElement instanceof XmlFile;
    return (XmlFile) createdElement;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) AndroidPlatform(org.jetbrains.android.sdk.AndroidPlatform) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)25 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)23 PsiFile (com.intellij.psi.PsiFile)7 Project (com.intellij.openapi.project.Project)6 Properties (java.util.Properties)6 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)5 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)5 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 IOException (java.io.IOException)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 File (java.io.File)3 NotNull (org.jetbrains.annotations.NotNull)3 Editor (com.intellij.openapi.editor.Editor)2 Module (com.intellij.openapi.module.Module)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 CreateClassAction (com.android.tools.idea.actions.CreateClassAction)1