How to create a plugin with new file templates?

Hi all,

Can someone help me with the following:

1. I want to create a plugin that contains file templates that I can then share with others via the plugin.
2. I want to be able to create some kind of special template that will let the user generate multiple templates at the same time (e.g., an interface and a class, generated from the same dialog)
3. I want to be able to create a file template that will also inject some code into already existing files

Thanks,

Geertjan

8 comments
Comment actions Permalink

When would I need to implement FileTemplateGroupDescriptorFactory?

0
Comment actions Permalink

Right now I have a class like this:

public class FileModuleComponent implements ModuleComponent {

Module module;

public FileModuleComponent(Module module) {
this.module = module;
}

public void createFileFromTemplate() throws Exception {
ModuleRootManager rootModel = ModuleRootManager.getInstance(module);
JOptionPane.showMessageDialog(null,rootModel.getModule().getName());
final Module module = rootModel.getModule();
final Project project = module.getProject();
Properties properties = FileTemplateManager.getInstance().getDefaultProperties();
final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(rootModel.getSourceRoots()[0]);
FileTemplate template = FileTemplateManager.getInstance().getJ2eeTemplate("test.java");
FileTemplateUtil.createFromTemplate(template, "test.java", properties, directory);
}

...
...
...


And I am calling this from an AnAction class:

public void actionPerformed(AnActionEvent e) {
Application application = ApplicationManager.getApplication();
FileModuleComponent appModuleComponent = application.getComponent(FileModuleComponent.class);
try {
appModuleComponent.createFileFromTemplate();
} catch (Exception e1) {
e1.printStackTrace();
}
}


But I am sure all of this is the wrong approach. Also because it doesn't work. Please help.

0
Comment actions Permalink

-- Registering file template code under
Templates tab in File Templates Settings -
private static void addFileTemplate(final @NonNls String name,@NonNls
String ext) {
FileTemplate template = FileTemplateManager.getInstance().getTemplate(name);
if (template == null) {
try {
template =
FileTemplateManager.getInstance().addTemplate(name, ext);
template.setText(FileUtil.loadTextAndClose(
new
InputStreamReader(JSSupportLoader.class.getResourceAsStream("/fileTemplates/"
+ name + "." + ext + ".ft")))
);
} catch (IOException ex) { ... }
}
}

e.g. addFileTemplate("ActionScript Class", "as");
-


creating file (@see
com.intellij.ide.actions.CreateElementActionBase) -



final FileTemplate template =
FileTemplateManager.getInstance().getInternalTemplate(myTemplateName);

PsiElement element;
try {
element = FileTemplateUtil
.createFromTemplate(template, getName(newName),
FileTemplateManager.getInstance().getDefaultProperties(), directory);
final PsiFile psiFile = element.getContainingFile();

final VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null) {

FileEditorManager.getInstance(directory.getProject()).openFile(virtualFile,
true);
return new PsiElement[]{psiFile};
}
}
catch (IncorrectOperationException e) {
throw e;
}
catch (Exception e) {
LOG.error(e);
}

Geertjan wrote:

Right now I have a class like this:

public class FileModuleComponent implements ModuleComponent {

Module module;

public FileModuleComponent(Module module) {
this.module = module;
}

public void createFileFromTemplate() throws Exception {
ModuleRootManager rootModel = ModuleRootManager.getInstance(module);
JOptionPane.showMessageDialog(null,rootModel.getModule().getName());
final Module module = rootModel.getModule();
final Project project = module.getProject();
Properties properties = FileTemplateManager.getInstance().getDefaultProperties();
final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(rootModel.getSourceRoots()[0]);
FileTemplate template = FileTemplateManager.getInstance().getJ2eeTemplate("test.java");
FileTemplateUtil.createFromTemplate(template, "test.java", properties, directory);
}

...
...
...


And I am calling this from an AnAction class:

public void actionPerformed(AnActionEvent e) {
Application application = ApplicationManager.getApplication();
FileModuleComponent appModuleComponent = application.getComponent(FileModuleComponent.class);
try {
appModuleComponent.createFileFromTemplate();
} catch (Exception e1) {
e1.printStackTrace();
}
}


But I am sure all of this is the wrong approach. Also because it doesn't work. Please help.



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

I'm sorry. This is not enough information. Do I need to register something in the plugin.xml file, under group-id="NewGroup"? Or not? I'd like step by step instructions, please.

0
Comment actions Permalink

Hi Geertjan,

The file template system does not allow to create multiple files from a single template, or to modify the code in existing files, in a declarative way. Maxim showed you samples of code for registering individual templates, and any additional functionality needs to be coordinated from your code. You can use the PSI API to modify Java code in existing classes, and the file template API (FileTemplateUtil.createFromTemplate()) to create individual files from file templates.

0
Comment actions Permalink

If you want provide an action to generate some code then you'd need to
insert appropriate declaration in plugin.xml (New Action does that
automatically), 'creating code' snippet should go to action class
implementation, besides you need application component that will
register templates (and it also should be registered in plugin.xml and
can be created by 'New Application component' action), code registereing
snippet will go in that place.

Geertjan wrote:

I'm sorry. This is not enough information. Do I need to register something in the plugin.xml file, under group-id="NewGroup"? Or not? I'd like step by step instructions, please.


--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Please sign in to leave a comment.