Java Code Examples for com.intellij.ide.fileTemplates.FileTemplate#getText()

The following examples show how to use com.intellij.ide.fileTemplates.FileTemplate#getText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeexTemplateFactory.java    From weex-language-support with MIT License 6 votes vote down vote up
public static PsiFile createFromTemplate(final PsiDirectory directory, final String name,
                                         String fileName, String templateName,
                                         @NonNls String... parameters) throws IncorrectOperationException {

    final FileTemplate template = FileTemplateManager.getInstance(directory.getProject()).getInternalTemplate(templateName);
    String text;

    try {
        text = template.getText();
    } catch (Exception e) {
        throw new RuntimeException("Unable to load template for " +
                FileTemplateManager.getInstance().internalTemplateToSubject(templateName), e);
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(directory.getProject());

    final PsiFile file = factory.createFileFromText(fileName, WeexFileType.INSTANCE, text);
    CodeStyleManager.getInstance(directory.getProject()).reformat(file);
    return (PsiFile) directory.add(file);
}
 
Example 2
Source File: BashTemplatesFactory.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@NotNull
static PsiFile createFromTemplate(final PsiDirectory directory, String fileName, String templateName) throws IncorrectOperationException {
    Project project = directory.getProject();
    FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
    FileTemplate template = templateManager.getInternalTemplate(templateName);

    Properties properties = new Properties(templateManager.getDefaultProperties());

    String templateText;
    try {
        templateText = template.getText(properties);
    } catch (IOException e) {
        throw new RuntimeException("Unable to load template for " + templateManager.internalTemplateToSubject(templateName), e);
    }

    PsiFile file = PsiFileFactory.getInstance(project).createFileFromText(fileName, BashFileType.BASH_FILE_TYPE, templateText);
    return (PsiFile) directory.add(file);
}
 
Example 3
Source File: FileReferenceQuickFixProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected String getFileText() {
  if (!isDirectory && myNewFileTemplateName != null) {
    Project project = getStartElement().getProject();
    FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project);
    FileTemplate template = findTemplate(fileTemplateManager);

    if (template != null) {
      try {
        return template.getText(fileTemplateManager.getDefaultProperties());
      } catch (IOException ex) {
        throw new RuntimeException(ex);
      }
    }
  }
  return super.getFileText();
}
 
Example 4
Source File: AsposeMavenUtil.java    From Aspose.OCR-for-Java with MIT License 5 votes vote down vote up
private static void runOrApplyFileTemplate(Project project,
                                           VirtualFile file,
                                           String templateName,
                                           Properties properties) throws IOException {
    FileTemplateManager manager = FileTemplateManager.getInstance();
    FileTemplate fileTemplate = manager.getJ2eeTemplate(templateName);
    Properties allProperties = manager.getDefaultProperties(project);
    allProperties.putAll(properties);
    String text = fileTemplate.getText(allProperties);
    Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
    Matcher matcher = pattern.matcher(text);
    StringBuffer builder = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(builder, "\\$" + matcher.group(1).toUpperCase() + "\\$");
    }
    matcher.appendTail(builder);
    text = builder.toString();

    TemplateImpl template = (TemplateImpl) TemplateManager.getInstance(project).createTemplate("", "", text);
    for (int i = 0; i < template.getSegmentsCount(); i++) {
        if (i == template.getEndSegmentNumber()) continue;
        String name = template.getSegmentName(i);
        String value = "\"" + properties.getProperty(name, "") + "\"";
        template.addVariable(name, value, value, true);
    }

    VfsUtil.saveText(file, template.getTemplateText());

    PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
    if (psiFile != null) {
        new ReformatCodeProcessor(project, psiFile, null, false).run();
    }

}
 
Example 5
Source File: AbstractCreateElementActionBase.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
protected static PsiFile createFromTemplateInternal( @NotNull PsiDirectory directory,
                                                     @NotNull String name,
                                                     @NotNull String fileName,
                                                     @NotNull String templateName,
                                                     @NonNls String... parameters )
    throws IncorrectOperationException
{
    // Load template
    FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance();
    FileTemplate template = fileTemplateManager.getJ2eeTemplate( templateName );

    // Process template properties
    Properties properties = new Properties( fileTemplateManager.getDefaultProperties() );
    JavaTemplateUtil.setPackageNameAttribute( properties, directory );
    properties.setProperty( NAME_TEMPLATE_PROPERTY, name );

    // Add parameters
    for( int i = 0; i < parameters.length; i += 2 )
    {
        properties.setProperty( parameters[ i ], parameters[ i + 1 ] );
    }

    // Create text from template with specified properties
    String text;
    try
    {
        text = template.getText( properties );
    }
    catch( Exception e )
    {
        String message = "Unable to load template for " +
                         fileTemplateManager.internalTemplateToSubject( templateName );
        throw new RuntimeException( message, e );
    }

    // Serialized text to file
    PsiManager psiManager = PsiManager.getInstance( directory.getProject() );
    PsiFileFactory fileFactory = PsiFileFactory.getInstance( directory.getProject() );
    PsiFile file = fileFactory.createFileFromText( fileName, text );

    // Reformat the file according to project/default style
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance( psiManager );
    codeStyleManager.reformat( file );

    // Add newly created file to directory
    return (PsiFile) directory.add( file );
}
 
Example 6
Source File: VueTemplatesFactory.java    From vue-for-idea with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
public static PsiFile createFromTemplate(final PsiDirectory directory, final String name,
                                         String fileName, String templateName,
                                         @NonNls String... parameters) throws IncorrectOperationException {

    final FileTemplate template = FileTemplateManager.getDefaultInstance().getTemplate(templateName);

    Properties properties = new Properties(FileTemplateManager.getDefaultInstance().getDefaultProperties());

    Project project = directory.getProject();
    properties.setProperty("PROJECT_NAME", project.getName());
    properties.setProperty("NAME", fileName);


    String text;

    try {
        text = template.getText(properties);
    } catch (Exception e) {
        throw new RuntimeException("Unable to load template for " +
                FileTemplateManager.getInstance().internalTemplateToSubject(templateName), e);
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(directory.getProject());

    final PsiFile file = factory.createFileFromText(fileName, VueFileType.INSTANCE, text);

    return (PsiFile) directory.add(file);
}