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