Search in sources :

Example 1 with ParseException

use of org.apache.velocity.runtime.parser.ParseException in project intellij-community by JetBrains.

the class CreateFileFromTemplateAction method createFileFromTemplate.

@Nullable
public static PsiFile createFileFromTemplate(@Nullable String name, @NotNull FileTemplate template, @NotNull PsiDirectory dir, @Nullable String defaultTemplateProperty, boolean openFile, @NotNull Map<String, String> liveTemplateDefaultValues) {
    if (name != null) {
        CreateFileAction.MkDirs mkdirs = new CreateFileAction.MkDirs(name, dir);
        name = mkdirs.newName;
        dir = mkdirs.directory;
    }
    PsiElement element;
    Project project = dir.getProject();
    try {
        element = FileTemplateUtil.createFromTemplate(template, name, FileTemplateManager.getInstance(dir.getProject()).getDefaultProperties(), dir);
        final PsiFile psiFile = element.getContainingFile();
        final VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile != null) {
            if (openFile) {
                if (template.isLiveTemplateEnabled()) {
                    CreateFromTemplateActionBase.startLiveTemplate(psiFile, liveTemplateDefaultValues);
                } else {
                    FileEditorManager.getInstance(project).openFile(virtualFile, true);
                }
            }
            if (defaultTemplateProperty != null) {
                PropertiesComponent.getInstance(project).setValue(defaultTemplateProperty, template.getName());
            }
            return psiFile;
        }
    } catch (ParseException e) {
        throw new IncorrectOperationException("Error parsing Velocity template: " + e.getMessage(), (Throwable) e);
    } catch (IncorrectOperationException e) {
        throw e;
    } catch (Exception e) {
        LOG.error(e);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ParseException(org.apache.velocity.runtime.parser.ParseException) PsiElement(com.intellij.psi.PsiElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ParseException(org.apache.velocity.runtime.parser.ParseException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ParseException

use of org.apache.velocity.runtime.parser.ParseException in project intellij-plugins by JetBrains.

the class FlexMainStep method commit.

@Override
public void commit(final CommitType commitType) throws CommitStepException {
    super.commit(commitType);
    // let's replace parent component only if template contains 'Superclass' macro
    final FileTemplate template;
    try {
        template = ClassLoaderUtil.runWithClassLoader(ActionScriptCreateClassOrInterfaceFix.class.getClassLoader(), new ThrowableComputable<FileTemplate, IOException>() {

            @Override
            public FileTemplate compute() throws IOException {
                return FileTemplateManager.getDefaultInstance().getInternalTemplate(myModel.getTemplateName());
            }
        });
        String[] attributes = FileTemplateUtil.calculateAttributes(template.getText(), new Properties(), true, myProject);
        if (ArrayUtil.contains(ActionScriptCreateClassOrInterfaceFix.SUPERCLASS, attributes)) {
            myModel.setSuperclassFqn(getSuperclassFqn());
        }
    } catch (IOException e) {
    // ignore as the action will not succeed
    } catch (ParseException e) {
    // ignore as the action will not succeed
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) ThrowableComputable(com.intellij.openapi.util.ThrowableComputable) IOException(java.io.IOException) ParseException(org.apache.velocity.runtime.parser.ParseException) Properties(java.util.Properties)

Aggregations

ParseException (org.apache.velocity.runtime.parser.ParseException)2 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 Project (com.intellij.openapi.project.Project)1 ThrowableComputable (com.intellij.openapi.util.ThrowableComputable)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Nullable (org.jetbrains.annotations.Nullable)1