use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class CreateResourceBundleDialogComponent method createPropertiesFiles.
private List<PsiFile> createPropertiesFiles() {
final Set<String> fileNames = getFileNamesToCreate();
final List<PsiFile> createdFiles = WriteCommandAction.runWriteCommandAction(myProject, new Computable<List<PsiFile>>() {
@Override
public List<PsiFile> compute() {
return ReadAction.compute(() -> ContainerUtil.map(fileNames, n -> {
final boolean isXml = myResourceBundle == null ? myUseXMLBasedPropertiesCheckBox.isSelected() : myResourceBundle.getDefaultPropertiesFile() instanceof XmlPropertiesFile;
if (isXml) {
FileTemplate template = FileTemplateManager.getInstance(myProject).getInternalTemplate("XML Properties File.xml");
LOG.assertTrue(template != null);
try {
return (PsiFile) FileTemplateUtil.createFromTemplate(template, n, null, myDirectory);
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
return myDirectory.createFile(n);
}
}));
}
});
combineToResourceBundleIfNeed(createdFiles);
return createdFiles;
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class CreateClassUtil method createClassFromTemplate.
@Nullable
private static PsiClass createClassFromTemplate(@NotNull final Properties attributes, @Nullable final String templateName, @NotNull final PsiDirectory directoryRoot, @NotNull final String className) throws IncorrectOperationException {
if (templateName == null)
return null;
if (templateName.equals(DO_NOT_CREATE_CLASS_TEMPLATE))
return null;
final Project project = directoryRoot.getProject();
try {
final PsiDirectory directory = createParentDirectories(directoryRoot, className);
final PsiFile psiFile = directory.findFile(className + "." + StdFileTypes.JAVA.getDefaultExtension());
if (psiFile != null) {
psiFile.delete();
}
final String rawClassName = extractClassName(className);
final PsiFile existing = directory.findFile(rawClassName + ".java");
if (existing instanceof PsiJavaFile) {
final PsiClass[] classes = ((PsiJavaFile) existing).getClasses();
if (classes.length > 0) {
return classes[0];
}
return null;
}
final PsiClass aClass;
if (templateName.equals(DEFAULT_CLASS_TEMPLATE)) {
aClass = JavaDirectoryService.getInstance().createClass(directory, rawClassName);
} else {
final FileTemplateManager fileTemplateManager = FileTemplateManager.getInstance(project);
FileTemplate fileTemplate = fileTemplateManager.getJ2eeTemplate(templateName);
LOG.assertTrue(fileTemplate != null, templateName + " not found");
final String text = fileTemplate.getText(attributes);
aClass = JavaCreateFromTemplateHandler.createClassOrInterface(project, directory, text, true, fileTemplate.getExtension());
}
return (PsiClass) JavaCodeStyleManager.getInstance(project).shortenClassReferences(aClass);
} catch (IOException e) {
throw new IncorrectOperationException(e);
}
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class CodeInsightTestCase method fixTemplates.
public static void fixTemplates() {
FileTemplateManager manager = FileTemplateManager.getDefaultInstance();
for (String tname : Arrays.asList("Class", "AnnotationType", "Enum", "Interface")) {
for (FileTemplate template : manager.getInternalTemplates()) {
if (tname.equals(template.getName())) {
String text = "package $PACKAGE_NAME$;\npublic " + manager.internalTemplateToSubject(tname) + " $NAME$ { }";
template.setText(FileTemplateManagerImpl.normalizeText(text));
break;
}
}
}
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class SaveFileAsTemplateAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
Project project = assertNotNull(e.getData(CommonDataKeys.PROJECT));
String fileText = assertNotNull(e.getData(PlatformDataKeys.FILE_TEXT));
VirtualFile file = assertNotNull(e.getData(CommonDataKeys.VIRTUAL_FILE));
String extension = StringUtil.notNullize(file.getExtension());
String nameWithoutExtension = file.getNameWithoutExtension();
PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
for (SaveFileAsTemplateHandler handler : Extensions.getExtensions(SaveFileAsTemplateHandler.EP_NAME)) {
String textFromHandler = handler.getTemplateText(psiFile, fileText, nameWithoutExtension);
if (textFromHandler != null) {
fileText = textFromHandler;
break;
}
}
FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
FileTemplate[] templates = templateManager.getAllTemplates();
FileTemplate template = FileTemplateUtil.createTemplate(nameWithoutExtension, extension, fileText, templates);
FileTemplateConfigurable configurable = new FileTemplateConfigurable(project);
configurable.setProportion(0.6f);
configurable.setTemplate(template, FileTemplateManagerImpl.getInstanceImpl(project).getDefaultTemplateDescription());
SaveFileAsTemplateDialog dialog = new SaveFileAsTemplateDialog(project, configurable);
if (dialog.showAndGet()) {
templateManager.setTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY, Arrays.asList(ArrayUtil.append(templates, template)));
}
}
use of com.intellij.ide.fileTemplates.FileTemplate in project intellij-community by JetBrains.
the class OverrideImplementUtil method setupMethodBody.
public static void setupMethodBody(PsiMethod result, PsiMethod originalMethod, PsiClass targetClass) throws IncorrectOperationException {
boolean isAbstract = originalMethod.hasModifierProperty(PsiModifier.ABSTRACT) || originalMethod.hasModifierProperty(PsiModifier.DEFAULT);
String templateName = isAbstract ? JavaTemplateUtil.TEMPLATE_IMPLEMENTED_METHOD_BODY : JavaTemplateUtil.TEMPLATE_OVERRIDDEN_METHOD_BODY;
FileTemplate template = FileTemplateManager.getInstance(originalMethod.getProject()).getCodeTemplate(templateName);
setupMethodBody(result, originalMethod, targetClass, template);
}
Aggregations