embed a custom live template in a plugin Follow
Hallo,
in my custom language plugin I want to provide live templates. I tried it the following way: I created my template via the settings-editor. The created xml looks like this:
<?xml version="1.0" encoding="UTF-8"?> <templateSet group="user"> <template name="create property" value="$PROPERTY_TYPE$ $PROPERTY_NAME$ { $END$ }" description="creates the stub for a new property" toReformat="false" toShortenFQNames="true"> <variable name="PROPERTY_TYPE" expression="enum("BOOLEAN NOT_COMPARABLE","BOOLEAN AVAILABILITY","INTEGER MORE_IS_BETTER", "INTEGER LESS_IS_BETTER","DOUBLE MORE_IS_BETTER", "DOUBLE LESS_IS_BETTER","STRING NOT_COMPARABLE","STRING_ENUM AVIALABILITY", "STRING_ENUM RICHNESS")" defaultValue=""STRINGENUM NOT_COMPARABLE"" alwaysStopAt="true" /> <variable name="PROPERTY_NAME" expression="" defaultValue=""PROPERTY_NAME"" alwaysStopAt="true" /> <context> <option name="sol" value="true" /> </context> </template> </templateSet>
I stored the generated xml in my plugin project under %project%/resources/liveTemplate/user.xml
Then I implemented a DefaultLiveTemplateProvider:
public class SOLTemplateProvider implements DefaultLiveTemplatesProvider{ @Override public String[] getDefaultLiveTemplateFiles() { return new String[] {"resources/liveTemplates/user"}; } @Nullable @Override public String[] getHiddenLiveTemplateFiles() { return new String[] {}; } }
I used this extension point to register the provider:
<defaultLiveTemplatesProvider implementation="com.sol.SOLTemplateProvider"/>
In my plugin project I then created the template context with this implementation:
public class SOLTemplateContextProvider extends TemplateContextType{ protected SOLTemplateContextProvider() { super("SOLTemplates", "sol"); } @Override public boolean isInContext(@NotNull PsiFile file, int offset) { return true; } }
And I used this extension point to register my TemplateContext:
<liveTemplateContext implementation="com.sol.SOLTemplateContextProvider"/>
I hoped that's all I needed to embed the live template in my plugin. However when I started the plugin the template can not be used and is not listed in the settings editor.
Have I forgot something or is this the wrong way to accomplish this?
Thanks for any help, Sebastian
Please sign in to leave a comment.
I assume "resources" is a source/resource folder root, so its output is copied to plugin output. Please try removing "/resources/" prefix in your SOLTemplateProvider.
See XmlDefaultLiveTemplatesProvider/XmlContextType as reference.
Hallo Yann,
thank you for the quick answer, it works perfect. I thought I tried all path-possibilities
Hi Yann,
Is there any way to embedd predefined project templates(.zip file) into a plugin so that when the plugin is installed those project templates would display in the "New Project Wizard"
Yes, see com.intellij.platform.ProjectTemplateEP, you can find example in JavaFX plugin
Thank you very much for the quick reply. I'll look inside the JavaFX plugin
I've copied my project template zip file as resources/projectTemplates/Java/MyProjectTemplate.zip and the added
<projectTemplate projectType="Java" templatePath="resources/projectTemplates/Java/MyProjectTemplate.zip"></projectTemplate> line into extension section in plugin.xml. But it is shown in red and there is a message saying "Element projectTemplates is not allowed here". What did I do wrong?
What version of IntelliJ IDEA are you using?
Please post your full plugin.xml.
Nevermind. I've able to solve that problem by using Idea13 as SDK. Thanks for the help :)
Hi Yann,
Sorry to bother you again. As it turns out by placing my .zip files of projectTemplates inside resources/projectTemplates/Java Idea will automaticall pick them up and shows in 'new project wizard'. I don't have to specify anything in plugin.xml. But the problem is there are 3 .zip files and the name of those projectTemplates are repeated 3 times (altogether 9 times) in 'new project wizard. if I put 4 .zip files in that folder, their name will be repeated 4 times (altoger 16 times). Is this a bug in Idea? If not how can I overcome it?
Hi Yann,
I don't understand how can I see JavaFX plugin in "com.intellij.platform.ProjectTemplateEP".
I want to figure out how can I embed predefined project templates(.zip file) into a plugin so that when the plugin is installed those project templates would display in the "New Project Wizard"
Can you please just tell me how can I see the JavaFX plugin?
Zahra Rahmaniez it's part of IJ Community sources: https://github.com/JetBrains/intellij-community/tree/master/plugins/javaFX