Search in sources :

Example 1 with FileTemplateConfigurable

use of com.intellij.ide.fileTemplates.impl.FileTemplateConfigurable 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)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 FileTemplateConfigurable (com.intellij.ide.fileTemplates.impl.FileTemplateConfigurable)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1