use of com.intellij.ide.fileTemplates.FileTemplateManager 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.FileTemplateManager 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.FileTemplateManager 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.FileTemplateManager 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.FileTemplateManager 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