Search in sources :

Example 21 with FileTemplateManager

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

the class CreateClassUtil method createClassFromTemplate.

@Nullable
private static PsiClass createClassFromTemplate(@NotNull final Properties attributes, @Nullable final String templateName, @NotNull final PsiDirectory directoryRoot, @NotNull final String className) throws IncorrectOperationException {
    if (templateName == null)
        return null;
    if (templateName.equals(DO_NOT_CREATE_CLASS_TEMPLATE))
        return null;
    final Project project = directoryRoot.getProject();
    try {
        final PsiDirectory directory = createParentDirectories(directoryRoot, className);
        final PsiFile psiFile = directory.findFile(className + "." + StdFileTypes.JAVA.getDefaultExtension());
        if (psiFile != null) {
            psiFile.delete();
        }
        final String rawClassName = extractClassName(className);
        final PsiFile existing = directory.findFile(rawClassName + ".java");
        if (existing instanceof PsiJavaFile) {
            final PsiClass[] classes = ((PsiJavaFile) existing).getClasses();
            if (classes.length > 0) {
                return classes[0];
            }
            return null;
        }
        final PsiClass aClass;
        if (templateName.equals(DEFAULT_CLASS_TEMPLATE)) {
            aClass = JavaDirectoryService.getInstance().createClass(directory, rawClassName);
        } else {
            final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project);
            FileTemplate fileTemplate = fileTemplateManager.getJ2eeTemplate(templateName);
            LOG.assertTrue(fileTemplate != null, templateName + " not found");
            final String text = fileTemplate.getText(attributes);
            aClass = JavaCreateFromTemplateHandler.createClassOrInterface(project, directory, text, true, fileTemplate.getExtension());
        }
        return (PsiClass) JavaCodeStyleManager.getInstance(project).shortenClassReferences(aClass);
    } catch (IOException e) {
        throw new IncorrectOperationException(e);
    }
}
Also used : Project(com.intellij.openapi.project.Project) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with FileTemplateManager

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

the class CodeInsightTestCase method fixTemplates.

public static void fixTemplates() {
    FileTemplateManager manager = FileTemplateManager.getDefaultInstance();
    for (String tname : Arrays.asList("Class", "AnnotationType", "Enum", "Interface")) {
        for (FileTemplate template : manager.getInternalTemplates()) {
            if (tname.equals(template.getName())) {
                String text = "package $PACKAGE_NAME$;\npublic " + manager.internalTemplateToSubject(tname) + " $NAME$ { }";
                template.setText(FileTemplateManagerImpl.normalizeText(text));
                break;
            }
        }
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager)

Example 23 with FileTemplateManager

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

the class SaveFileAsTemplateAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = assertNotNull(e.getData(CommonDataKeys.PROJECT));
    String fileText = assertNotNull(e.getData(PlatformDataKeys.FILE_TEXT));
    VirtualFile file = assertNotNull(e.getData(CommonDataKeys.VIRTUAL_FILE));
    String extension = StringUtil.notNullize(file.getExtension());
    String nameWithoutExtension = file.getNameWithoutExtension();
    PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
    for (SaveFileAsTemplateHandler handler : Extensions.getExtensions(SaveFileAsTemplateHandler.EP_NAME)) {
        String textFromHandler = handler.getTemplateText(psiFile, fileText, nameWithoutExtension);
        if (textFromHandler != null) {
            fileText = textFromHandler;
            break;
        }
    }
    FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
    FileTemplate[] templates = templateManager.getAllTemplates();
    FileTemplate template = FileTemplateUtil.createTemplate(nameWithoutExtension, extension, fileText, templates);
    FileTemplateConfigurable configurable = new FileTemplateConfigurable(project);
    configurable.setProportion(0.6f);
    configurable.setTemplate(template, FileTemplateManagerImpl.getInstanceImpl(project).getDefaultTemplateDescription());
    SaveFileAsTemplateDialog dialog = new SaveFileAsTemplateDialog(project, configurable);
    if (dialog.showAndGet()) {
        templateManager.setTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY, Arrays.asList(ArrayUtil.append(templates, template)));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) FileTemplateConfigurable(com.intellij.ide.fileTemplates.impl.FileTemplateConfigurable) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) PsiFile(com.intellij.psi.PsiFile)

Example 24 with FileTemplateManager

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

the class PyUtil method getOrCreateFile.

@NotNull
public static PyFile getOrCreateFile(String path, Project project) {
    final VirtualFile vfile = LocalFileSystem.getInstance().findFileByIoFile(new File(path));
    final PsiFile psi;
    if (vfile == null) {
        final File file = new File(path);
        try {
            final VirtualFile baseDir = project.getBaseDir();
            final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project);
            final FileTemplate template = fileTemplateManager.getInternalTemplate("Python Script");
            final Properties properties = fileTemplateManager.getDefaultProperties();
            properties.setProperty("NAME", FileUtil.getNameWithoutExtension(file.getName()));
            final String content = (template != null) ? template.getText(properties) : null;
            psi = PyExtractSuperclassHelper.placeFile(project, StringUtil.notNullize(file.getParent(), baseDir != null ? baseDir.getPath() : "."), file.getName(), content);
        } catch (IOException e) {
            throw new IncorrectOperationException(String.format("Cannot create file '%s'", path), (Throwable) e);
        }
    } else {
        psi = PsiManager.getInstance(project).findFile(vfile);
    }
    if (!(psi instanceof PyFile)) {
        throw new IncorrectOperationException(PyBundle.message("refactoring.move.module.members.error.cannot.place.elements.into.nonpython.file"));
    }
    return (PyFile) psi;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 25 with FileTemplateManager

use of com.intellij.ide.fileTemplates.FileTemplateManager in project intellij-elixir by KronicDeth.

the class CreateElixirModuleAction method createModuleFromTemplate.

/**
     * @link com.intellij.ide.acitons.CreateTemplateInPackageAction#doCreate
     * @link com.intellij.ide.actions.CreateClassAction
     * @link com.intellij.psi.impl.file.JavaDirectoryServiceImpl.createClassFromTemplate
     */
@Nullable
private static ElixirFile createModuleFromTemplate(@NotNull PsiDirectory directory, @NotNull String basename, @NotNull String moduleName, @NotNull String templateName) {
    FileTemplateManager fileTemplateManager = FileTemplateManager.getDefaultInstance();
    FileTemplate template = fileTemplateManager.getInternalTemplate(templateName);
    Properties defaultProperties = fileTemplateManager.getDefaultProperties();
    Properties properties = new Properties(defaultProperties);
    properties.setProperty(FileTemplate.ATTRIBUTE_NAME, moduleName);
    PsiElement element;
    try {
        element = FileTemplateUtil.createFromTemplate(template, basename, properties, directory);
    } catch (Exception exception) {
        LOG.error(exception);
        return null;
    }
    if (element == null) {
        return null;
    }
    return (ElixirFile) element;
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) Properties(java.util.Properties) ElixirFile(org.elixir_lang.psi.ElixirFile) PsiElement(com.intellij.psi.PsiElement) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable)

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