private static PsiClass createClassFromTemplate( @NotNull PsiDirectory dir, String name, String templateName, boolean askToDefineVariables) throws IncorrectOperationException { // checkCreateClassOrInterface(dir, name); FileTemplate template = FileTemplateManager.getInstance().getInternalTemplate(templateName); Properties defaultProperties = FileTemplateManager.getInstance().getDefaultProperties(); Properties properties = new Properties(defaultProperties); properties.setProperty(FileTemplate.ATTRIBUTE_NAME, name); String ext = StdFileTypes.JAVA.getDefaultExtension(); String fileName = name + "." + ext; PsiElement element; try { element = askToDefineVariables ? new CreateFromTemplateDialog(dir.getProject(), dir, template, null, properties) .create() : FileTemplateUtil.createFromTemplate(template, fileName, properties, dir); } catch (IncorrectOperationException e) { throw e; } catch (Exception e) { LOG.error(e); return null; } if (element == null) return null; final PsiJavaFile file = (PsiJavaFile) element.getContainingFile(); PsiClass[] classes = file.getClasses(); if (classes.length < 1) { throw new IncorrectOperationException(getIncorrectTemplateMessage(templateName)); } return classes[0]; }
@SuppressWarnings("DialogTitleCapitalization") @Nullable public static PsiFile createFileFromTemplate( @SuppressWarnings("NullableProblems") @NotNull String name, @NotNull FileTemplate template, @NotNull PsiDirectory dir, @Nullable String defaultTemplateProperty) { // TODO: Do we *have* to hack the IntelliJ source? // This is a roughly a copy/paste then slight adaptation from the IntelliJ definition of this // method. // We can't override it directly, and more importantly we can't override its call to // FileTemplateUtil.createFromTemplate() List<String> pathItems = FileUtil.getPathFromSourceRoot(dir.getProject(), dir.getVirtualFile()); // modulePrefix is the empty string if the module is either in the top // level directory or one of the subdirectories start with a lower-case // letter. final String modulePrefix = pathItems == null || invalidPathItems(pathItems) ? "" : StringUtil.join(pathItems, "."); // Adapted from super definition. CreateFileAction.MkDirs mkdirs = new CreateFileAction.MkDirs(name, dir); name = mkdirs.newName; dir = mkdirs.directory; PsiElement element; Project project = dir.getProject(); try { // Patch props with custom property. Properties props = FileTemplateManager.getInstance().getDefaultProperties(project); props.setProperty( "HASKELL_MODULE_NAME", modulePrefix.isEmpty() ? name : modulePrefix + '.' + name); element = FileTemplateUtil.createFromTemplate(template, name, props, dir); final PsiFile psiFile = element.getContainingFile(); final VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile != null) { FileEditorManager.getInstance(project).openFile(virtualFile, true); if (defaultTemplateProperty != null) { PropertiesComponent.getInstance(project) .setValue(defaultTemplateProperty, template.getName()); } return psiFile; } } catch (ParseException e) { Messages.showErrorDialog( project, "Error parsing Velocity template: " + e.getMessage(), "Create File from Template"); return null; } catch (IncorrectOperationException e) { throw e; } catch (Exception e) { LOG.error(e); } return null; }
private static void createTestHelper(@NotNull Project project, PsiDirectory projectDir) { final FileTemplate template = FileTemplateManager.getInstance(project) .getInternalTemplate(FileUtil.getNameWithoutExtension(EduNames.TEST_HELPER)); try { FileTemplateUtil.createFromTemplate(template, EduNames.TEST_HELPER, null, projectDir); } catch (Exception ignored) { } }
static boolean canCreateFromTemplate(AnActionEvent e, FileTemplate template) { if (e == null) return false; DataContext dataContext = e.getDataContext(); IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext); if (view == null) return false; PsiDirectory[] dirs = view.getDirectories(); if (dirs.length == 0) return false; return FileTemplateUtil.canCreateFromTemplate(dirs, template); }
public static PsiElement createFromTemplate( final Project project, String templateName, String fileName, @NotNull PsiDirectory directory, Properties properties) throws Exception { FileTemplateManager manager = FileTemplateManager.getInstance(project); FileTemplate template = manager.getInternalTemplate(templateName); return FileTemplateUtil.createFromTemplate(template, fileName, properties, directory); }