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)));
}
}
Aggregations