Search in sources :

Example 61 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class JavaPsiImplementationHelperImpl method setupCatchBlock.

@Override
public void setupCatchBlock(@NotNull String exceptionName, @NotNull PsiType exceptionType, PsiElement context, @NotNull PsiCatchSection catchSection) {
    final FileTemplate catchBodyTemplate = FileTemplateManager.getInstance(catchSection.getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY);
    LOG.assertTrue(catchBodyTemplate != null);
    Properties props = FileTemplateManager.getInstance(myProject).getDefaultProperties();
    props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName);
    props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION_TYPE, exceptionType.getCanonicalText());
    if (context != null && context.isPhysical()) {
        final PsiDirectory directory = context.getContainingFile().getContainingDirectory();
        if (directory != null) {
            JavaTemplateUtil.setPackageNameAttribute(props, directory);
        }
    }
    final PsiCodeBlock codeBlockFromText;
    try {
        codeBlockFromText = PsiElementFactory.SERVICE.getInstance(myProject).createCodeBlockFromText("{\n" + catchBodyTemplate.getText(props) + "\n}", null);
    } catch (ProcessCanceledException ce) {
        throw ce;
    } catch (Exception e) {
        throw new IncorrectOperationException("Incorrect file template", (Throwable) e);
    }
    catchSection.getCatchBlock().replace(codeBlockFromText);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Properties(java.util.Properties) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 62 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class JavaDirectoryServiceImpl method createClassFromTemplate.

private static PsiClass createClassFromTemplate(@NotNull PsiDirectory dir, String name, String templateName, boolean askToDefineVariables, @NotNull Map<String, String> additionalProperties) throws IncorrectOperationException {
    if (askToDefineVariables) {
        LOG.assertTrue(!ApplicationManager.getApplication().isWriteAccessAllowed());
    }
    //checkCreateClassOrInterface(dir, name);
    Project project = dir.getProject();
    FileTemplate template = FileTemplateManager.getInstance(project).getInternalTemplate(templateName);
    Properties defaultProperties = FileTemplateManager.getInstance(project).getDefaultProperties();
    Properties properties = new Properties(defaultProperties);
    properties.setProperty(FileTemplate.ATTRIBUTE_NAME, name);
    for (Map.Entry<String, String> entry : additionalProperties.entrySet()) {
        properties.setProperty(entry.getKey(), entry.getValue());
    }
    String ext = StdFileTypes.JAVA.getDefaultExtension();
    String fileName = name + "." + ext;
    PsiElement element;
    try {
        element = askToDefineVariables ? new CreateFromTemplateDialog(project, 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, project));
    }
    return classes[0];
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Properties(java.util.Properties) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CreateFromTemplateDialog(com.intellij.ide.fileTemplates.ui.CreateFromTemplateDialog) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Map(java.util.Map)

Example 63 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class PyUtil method getOrCreateFile.

@NotNull
public static PyFile getOrCreateFile(String path, Project project) {
    final VirtualFile vfile = LocalFileSystem.getInstance().findFileByIoFile(new File(path));
    final PsiFile psi;
    if (vfile == null) {
        final File file = new File(path);
        try {
            final VirtualFile baseDir = project.getBaseDir();
            final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project);
            final FileTemplate template = fileTemplateManager.getInternalTemplate("Python Script");
            final Properties properties = fileTemplateManager.getDefaultProperties();
            properties.setProperty("NAME", FileUtil.getNameWithoutExtension(file.getName()));
            final String content = (template != null) ? template.getText(properties) : null;
            psi = PyExtractSuperclassHelper.placeFile(project, StringUtil.notNullize(file.getParent(), baseDir != null ? baseDir.getPath() : "."), file.getName(), content);
        } catch (IOException e) {
            throw new IncorrectOperationException(String.format("Cannot create file '%s'", path), (Throwable) e);
        }
    } else {
        psi = PsiManager.getInstance(project).findFile(vfile);
    }
    if (!(psi instanceof PyFile)) {
        throw new IncorrectOperationException(PyBundle.message("refactoring.move.module.members.error.cannot.place.elements.into.nonpython.file"));
    }
    return (PyFile) psi;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 64 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.

the class PyCCLanguageManager method createTestsForNewSubtask.

@Override
public void createTestsForNewSubtask(@NotNull Project project, @NotNull Task task) {
    VirtualFile taskDir = task.getTaskDir(project);
    if (taskDir == null) {
        return;
    }
    int nextSubtaskIndex = task.getLastSubtaskIndex() + 1;
    String nextSubtaskTestsFileName = getSubtaskTestsFileName(nextSubtaskIndex);
    ApplicationManager.getApplication().runWriteAction(() -> {
        try {
            PsiDirectory taskPsiDir = PsiManager.getInstance(project).findDirectory(taskDir);
            FileTemplate testsTemplate = getTestsTemplate(project);
            if (taskPsiDir == null || testsTemplate == null) {
                return;
            }
            FileTemplateUtil.createFromTemplate(testsTemplate, nextSubtaskTestsFileName, null, taskPsiDir);
        } catch (Exception e) {
            LOG.error(e);
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate)

Example 65 with FileTemplate

use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-elixir by KronicDeth.

the class CreateElixirModuleAction method createModuleFromTemplate.

/**
     * @link com.intellij.ide.acitons.CreateTemplateInPackageAction#doCreate
     * @link com.intellij.ide.actions.CreateClassAction
     * @link com.intellij.psi.impl.file.JavaDirectoryServiceImpl.createClassFromTemplate
     */
@Nullable
private static ElixirFile createModuleFromTemplate(@NotNull PsiDirectory directory, @NotNull String basename, @NotNull String moduleName, @NotNull String templateName) {
    FileTemplateManager fileTemplateManager = FileTemplateManager.getDefaultInstance();
    FileTemplate template = fileTemplateManager.getInternalTemplate(templateName);
    Properties defaultProperties = fileTemplateManager.getDefaultProperties();
    Properties properties = new Properties(defaultProperties);
    properties.setProperty(FileTemplate.ATTRIBUTE_NAME, moduleName);
    PsiElement element;
    try {
        element = FileTemplateUtil.createFromTemplate(template, basename, properties, directory);
    } catch (Exception exception) {
        LOG.error(exception);
        return null;
    }
    if (element == null) {
        return null;
    }
    return (ElixirFile) element;
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) Properties(java.util.Properties) ElixirFile(org.elixir_lang.psi.ElixirFile) PsiElement(com.intellij.psi.PsiElement) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)66 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)20 Project (com.intellij.openapi.project.Project)13 IOException (java.io.IOException)12 PsiFile (com.intellij.psi.PsiFile)10 Properties (java.util.Properties)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 PsiDirectory (com.intellij.psi.PsiDirectory)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiElement (com.intellij.psi.PsiElement)6 Module (com.intellij.openapi.module.Module)5 File (java.io.File)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 Nullable (org.jetbrains.annotations.Nullable)4 Editor (com.intellij.openapi.editor.Editor)3 ConfigurationException (com.intellij.openapi.options.ConfigurationException)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)2