Search in sources :

Example 56 with FileTemplate

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

the class CreateResourceBundleDialogComponent method createPropertiesFiles.

private List<PsiFile> createPropertiesFiles() {
    final Set<String> fileNames = getFileNamesToCreate();
    final List<PsiFile> createdFiles = WriteCommandAction.runWriteCommandAction(myProject, new Computable<List<PsiFile>>() {

        @Override
        public List<PsiFile> compute() {
            return ReadAction.compute(() -> ContainerUtil.map(fileNames, n -> {
                final boolean isXml = myResourceBundle == null ? myUseXMLBasedPropertiesCheckBox.isSelected() : myResourceBundle.getDefaultPropertiesFile() instanceof XmlPropertiesFile;
                if (isXml) {
                    FileTemplate template = FileTemplateManager.getInstance(myProject).getInternalTemplate("XML Properties File.xml");
                    LOG.assertTrue(template != null);
                    try {
                        return (PsiFile) FileTemplateUtil.createFromTemplate(template, n, null, myDirectory);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    return myDirectory.createFile(n);
                }
            }));
        }
    });
    combineToResourceBundleIfNeed(createdFiles);
    return createdFiles;
}
Also used : XmlPropertiesFile(com.intellij.lang.properties.xml.XmlPropertiesFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) JBList(com.intellij.ui.components.JBList) PsiFile(com.intellij.psi.PsiFile)

Example 57 with FileTemplate

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

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

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

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

the class OverrideImplementUtil method setupMethodBody.

public static void setupMethodBody(PsiMethod result, PsiMethod originalMethod, PsiClass targetClass) throws IncorrectOperationException {
    boolean isAbstract = originalMethod.hasModifierProperty(PsiModifier.ABSTRACT) || originalMethod.hasModifierProperty(PsiModifier.DEFAULT);
    String templateName = isAbstract ? JavaTemplateUtil.TEMPLATE_IMPLEMENTED_METHOD_BODY : JavaTemplateUtil.TEMPLATE_OVERRIDDEN_METHOD_BODY;
    FileTemplate template = FileTemplateManager.getInstance(originalMethod.getProject()).getCodeTemplate(templateName);
    setupMethodBody(result, originalMethod, targetClass, template);
}
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